How do I limit the data collection on a chart? - c#

I have a C# program that reads in data from an Arduino Mega four times a second and displays the data in two charts, each chart has two sets of data. After a couple of hours of running the data starts to lag. I can watch the program grow in size over time. I believe the problem is the charts are showing snapshot of 1 minute on each chart but the program just keeps collecting data and creating the memory leak issue. Is there a way to limit the data collection to a minute or two and dump the old data?
This is my first C# program I have ever written and I'm an old fart (62) and suffer from CRS "Can't Remember Sh_t".
Any help would be greatly appreciated.
Bryan
Program data collection
// load the main form and sets up the charts
private void TelemetryForm_Load(object sender, EventArgs e)
{
intensityChart.ChartAreas.Add("area");
intensityChart.Legends.Add("INTENSITY");
intensityChart.Legends.Add("I SENSE");
intensityChart.Series.Add("INTENSITY");
intensityChart.Series.Add("I SENSE");
intensityChart.Legends["INTENSITY"].Position.Auto = false;
intensityChart.Legends["INTENSITY"].Position.Height = 10;
intensityChart.Legends["INTENSITY"].Position.Width = 50;
intensityChart.Legends["INTENSITY"].Position.X = 20;
intensityChart.Legends["INTENSITY"].Position.Y = 0;
intensityChart.Legends["I SENSE"].Position.Auto = false;
intensityChart.Legends["I SENSE"].Position.Height = 10;
intensityChart.Legends["I SENSE"].Position.Width = 50;
intensityChart.Legends["I SENSE"].Position.X = 20;
intensityChart.Legends["I SENSE"].Position.Y = 0;
toolCapChart.ChartAreas.Add("area2");
toolCapChart.Series.Add("CAPACITOR VOLTAGE");
toolCapChart.Series.Add("TOOL VOLTAGE");
toolCapChart.Legends.Add("CAPACITOR VOLTAGE");
toolCapChart.Legends.Add("TOOL VOLTAGE");
toolCapChart.Legends["TOOL VOLTAGE"].Position.Auto = false;
toolCapChart.Legends["TOOL VOLTAGE"].Position.Height = 10;
toolCapChart.Legends["TOOL VOLTAGE"].Position.Width = 50;
toolCapChart.Legends["TOOL VOLTAGE"].Position.X = 20;
toolCapChart.Legends["TOOL VOLTAGE"].Position.Y = 0;
toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Auto = false;
toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Height = 10;
toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Width = 50;
toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.X = 20;
toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Y = 0;
timer1.Enabled = true;
timer1.Start();
}
// adds the data to the charts
public void chartRead(Double timeofday)
{
Double x = timeofday;
Double z = 1 * timeofday;
Double y = 6 * timeofday;
int charttime = 240;
FontHeight = -1;
intensityChart.ChartAreas["area"].Position.Auto = false;
intensityChart.ChartAreas["area"].Position.Y = 8;
intensityChart.Series["INTENSITY"].Points.AddXY(DateTime.Now.ToLongTimeString(), mic_out);
intensityChart.Series["I SENSE"].Points.AddXY(DateTime.Now.ToLongTimeString(), i_sense);
intensityChart.Series["INTENSITY"].LegendText = "INTENSITY";
intensityChart.Series["I SENSE"].LegendText = "I SENSE";
intensityChart.Series["INTENSITY"].Color = Color.Blue;
intensityChart.Series["I SENSE"].Color = Color.Orange;
intensityChart.Series["INTENSITY"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
intensityChart.Series["I SENSE"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
intensityChart.Series["I SENSE"].YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
intensityChart.ChartAreas["area"].AxisX.Minimum = intensityChart.ChartAreas["area"].AxisX.Maximum - charttime;
intensityChart.ChartAreas["area"].AxisX.Interval =charttime/6;
intensityChart.ChartAreas["area"].AxisY.Minimum = 0;
intensityChart.ChartAreas["area"].AxisY.Maximum = 100;
intensityChart.ChartAreas["area"].AxisY.Interval = 10;
intensityChart.ChartAreas["area"].AxisY.MajorTickMark.Enabled = false;
intensityChart.ChartAreas["area"].AxisY.MajorTickMark.Interval = 5;
intensityChart.ChartAreas["area"].AxisY2.MajorTickMark.Enabled = false;
intensityChart.ChartAreas["area"].AxisY2.MajorTickMark.Interval = 5;
intensityChart.ChartAreas["area"].AxisX.MinorTickMark.Interval = 5;
intensityChart.ChartAreas["area"].AxisX.MinorTickMark.Enabled = true;
intensityChart.ChartAreas["area"].AxisY.MinorTickMark.Interval = 5;
intensityChart.ChartAreas["area"].AxisY.MinorTickMark.Enabled = true;
intensityChart.ChartAreas["area"].AxisY2.Minimum = 0;
intensityChart.ChartAreas["area"].AxisY2.Maximum = isenseYscale;
intensityChart.ChartAreas["area"].AxisY2.Interval = isenseYscale/10;
intensityChart.ChartAreas["area"].AxisX2.MinorTickMark.Interval = 5;
intensityChart.ChartAreas["area"].AxisX2.MinorTickMark.Enabled = true;
intensityChart.ChartAreas["area"].AxisY2.MinorTickMark.Interval = 1;
intensityChart.ChartAreas["area"].AxisY2.MinorTickMark.Enabled = true;
intensityChart.ChartAreas["area"].AxisY.Title = "INTENSITY";
intensityChart.ChartAreas["area"].AxisY2.Title = "I SENSE";
toolCapChart.ChartAreas["area2"].Position.Auto = false;
toolCapChart.ChartAreas["area2"].Position.Y = 8;
toolCapChart.Series["TOOL VOLTAGE"].Color = Color.Red;
toolCapChart.Series["CAPACITOR VOLTAGE"].Color = Color.Green;
toolCapChart.Series["CAPACITOR VOLTAGE"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
toolCapChart.Series["TOOL VOLTAGE"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
toolCapChart.Series["TOOL VOLTAGE"].YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
toolCapChart.Series["TOOL VOLTAGE"].LegendText = "TOOL VOLTAGE";
toolCapChart.Series["CAPACITOR VOLTAGE"].LegendText = "CAPACITOR VOLTAGE";
toolCapChart.Series["CAPACITOR VOLTAGE"].Points.AddXY(DateTime.Now.ToLongTimeString(), hv_sense);
toolCapChart.Series["TOOL VOLTAGE"].Points.AddXY(DateTime.Now.ToLongTimeString(), tool_vin);
toolCapChart.ChartAreas["area2"].AxisX.Minimum = toolCapChart.ChartAreas["area2"].AxisX.Maximum - charttime;
toolCapChart.ChartAreas["area2"].AxisY2.Minimum = 0;
toolCapChart.ChartAreas["area2"].AxisY2.Maximum = toolVoltageChart;
toolCapChart.ChartAreas["area2"].AxisY.Minimum = 0;
toolCapChart.ChartAreas["area2"].AxisY.Maximum =hvSenseChartYScale;
toolCapChart.ChartAreas["area2"].AxisY2.MinorTickMark.Interval = toolVoltageChart/25;
toolCapChart.ChartAreas["area2"].AxisY2.MinorTickMark.Enabled = true;
toolCapChart.ChartAreas["area2"].AxisY2.MajorTickMark.Interval = 100;
toolCapChart.ChartAreas["area2"].AxisY2.MajorTickMark.Enabled = false;
toolCapChart.ChartAreas["area2"].AxisX.Interval = charttime/6;
toolCapChart.ChartAreas["area2"].AxisX.MinorTickMark.Interval = 5;
toolCapChart.ChartAreas["area2"].AxisX.MinorTickMark.Enabled = true;
toolCapChart.ChartAreas["area2"].AxisY.MinorTickMark.Interval = hvSenseChartYScale/25;
toolCapChart.ChartAreas["area2"].AxisY.MinorTickMark.Enabled = true;
toolCapChart.ChartAreas["area2"].AxisY.MajorTickMark.Interval = 1000;
toolCapChart.ChartAreas["area2"].AxisY.MajorTickMark.Enabled = false;
toolCapChart.ChartAreas["area2"].AxisY2.Title = "TOOL VOLTAGE";
toolCapChart.ChartAreas["area2"].AxisY.Title = "CAPACITOR VOLTAGE";
capVoltageAngularGauge.Value = hv_sense;
pulseIntensityLinearGauge.Value = mic_ph_out;
pulseIntensityLinearGauge.Max = mic_ph_scaling;

After calling series.Points.AddXY(...) try calling series.Points.RemoveAt(0) if the series has more points than can be displayed. This effectively pops the oldest each time a new one comes in.
if(toolCapChart.Series["TOOL VOLTAGE"].Points.Count > maxSize)
toolCapChart.Series["TOOL VOLTAGE"].Points.RemoveAt(0);

Related

MS Gantt Chart 2 Axis

I had Gantt chart like this. I'd like to show AxisX2 with value is percentage that I prepared formula for it. I had challenges to show AxisX2 and set series for it.
Here is the Gantt chart I captured click here.
I expect to one more axis like this.
Please help, Thank you .
Here are some basic function to render that chart
public void setUpGantt(Chart chart)
{
chart.Series.Clear();
Series s = chart.Series.Add("sszs");
s.ChartType = SeriesChartType.RangeBar;
s.YValueType = ChartValueType.DateTime;
s.ResetIsVisibleInLegend () ;
s.IsVisibleInLegend = true;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ax.MajorGrid.Enabled = false;
ay.IntervalType = DateTimeIntervalType.Minutes;
ay.Interval = 60;
ay.LabelStyle.Format = "HH:mm";
ay.Minimum = 0;
ay.Maximum = 0.2;
limitGantt(chart, "0:00", "24:00");
s.ToolTip = "#VALY1{HH:mm}~#VALY2{HH:mm}";
}
public void limitGantt(Chart chart, string start, string end)
{
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ay.Minimum = fromTimeString(start).ToOADate();
ay.Maximum = fromTimeString(end).ToOADate();
}
DateTime fromTimeString(string time)
{
var p = time.Split(':');
int sec = p.Length == 3 ? Convert.ToInt16(p[2]) : 0;
TimeSpan t = new TimeSpan(Convert.ToInt16(p[0]), Convert.ToInt16(p[1]), sec);
return DateTime.Today.Add(t);
}
public void addGanttTask(Series s, string start, string end, Color c, int slot, string [] array)
{
DateTime start_ = fromTimeString(start);
DateTime end_ = fromTimeString(end);
int pt = s.Points.AddXY(slot, start_, end_);
s.Points[pt].Color = c;
s.IsVisibleInLegend = true;
if (array != null)
{
for (int i = 0; i < array.Length; i++)
{
if (slot == i + 1)
{ s.Points[pt].AxisLabel = array[i];
}
}
}
}
Taw's comment
public void setUpGantt(Chart chart)
{
chart.Series.Clear();
Series s = chart.Series.Add("sszs");
s.ChartType = SeriesChartType.RangeBar;
s.YValueType = ChartValueType.DateTime;
s.ResetIsVisibleInLegend () ;
s.IsVisibleInLegend = true;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ax.MajorGrid.Enabled = false;
Axis ax2 = chart.ChartAreas[0].AxisX2;
ax2.Enabled = AxisEnabled.True;
ax2.Maximum = 100;
ax2.MajorGrid.Enabled = false;
ay.IntervalType = DateTimeIntervalType.Minutes;
ay.Interval = 60;
ay.LabelStyle.Format = "HH:mm";
ay.Minimum = 0;
ay.Maximum = 0.2;
limitGantt(chart, "0:00", "24:00");
s.ToolTip = "#VALY1{HH:mm}~#VALY2{HH:mm}";
}
I'd like to have percentage = Green Time/Total time (from 00:
00 to Current time) example
Thank you for TaW's suggestion. It worked for me.
I post here to share who want to know
public void setUpGantt(Chart chart)
{
chart.Series.Clear();
Series s = chart.Series.Add("sszs");
s.ChartType = SeriesChartType.RangeBar;
s.YValueType = ChartValueType.DateTime;
s.ResetIsVisibleInLegend () ;
s.IsVisibleInLegend = true;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
ax.MajorGrid.Enabled = false;
Axis ax2 = chart.ChartAreas[0].AxisX2;
ax2.Enabled = AxisEnabled.True;
ax2.MajorGrid.Enabled = true;
ax2.CustomLabels.Clear();// clear previous value when switch another data
for (int i = 0; i < 12; i++)
{
CustomLabel cl = new CustomLabel();
cl.FromPosition = i+0.5;
cl.ToPosition = i+1.5;
cl.Text = i+" %"; // example value to show on CustomLabel
ax2.CustomLabels.Add(cl);
}
ay.IntervalType = DateTimeIntervalType.Minutes;
ay.Interval = 60;
ay.LabelStyle.Format = "HH:mm";
ay.Minimum = 0;
ay.Maximum = 0.2;
limitGantt(chart, "0:00", "24:00");
s.ToolTip = "#VALY1{HH:mm}~#VALY2{HH:mm}";
}
The main script to show one more Axis with CustomLabel
Axis ax2 = chart.ChartAreas[0].AxisX2;
ax2.Enabled = AxisEnabled.True;
ax2.MajorGrid.Enabled = true;
ax2.CustomLabels.Clear();// clear previous value when switch another data
for (int i = 0; i < 12; i++)
{
CustomLabel cl = new CustomLabel();
cl.FromPosition = i+0.5;
cl.ToPosition = i+1.5;
cl.Text = i+" %"; // example value to show on CustomLabel
ax2.CustomLabels.Add(cl);
}

Chart Area with scrollbar

I would like to have on the six chartareas on the image 6 scrollbars i have genrate it but on the srollbar apears a button if i pressed the button the scrollbare hide but i do not want this button can anyone help me with this Problem please.
the unintentional Button1
private void makeChartsScrollable(Chart chart) {
chart.ChartAreas[3].AxisX.ScrollBar.Enabled = true;
// chart.ChartAreas[3].CursorX.IsUserEnabled = false;
// chart.ChartAreas[3].CursorX.IsUserSelectionEnabled = false;
// chart.ChartAreas[2].AxisX.ScaleView.Zoomable = false;
// chart.ChartAreas[2].AxisX.IsLabelAutoFit = true;
// chart.ChartAreas[2].AxisX.ScaleView.Position = 0;
// chart.ChartAreas[2].AxisX.ScaleView.Size = 23;
//chart.ChartAreas[3].AxisX.
chart.ChartAreas[3].AxisX.ScaleView.Zoom(0, 8);
chart.ChartAreas[3].AxisX.ScaleView.MinSize = 0;
chart.ChartAreas[3].AxisX.ScrollBar.IsPositionedInside = true;
chart.ChartAreas[3].AxisX.ScrollBar.Size = 23;
chart.ChartAreas[3].AxisX.ScrollBar.ButtonColor = Color.Silver;
chart.ChartAreas[3].AxisX.ScrollBar.LineColor = Color.Black;
/*
chart.ChartAreas[3].AxisX2.ScaleView.Zoomable = false;
chart.ChartAreas[3].AxisX2.ScrollBar.Enabled = true;
chart.ChartAreas[3].AxisX2.ScaleView.Position = 0;
chart.ChartAreas[3].AxisX2.IsLabelAutoFit = false;
chart.ChartAreas[3].AxisX2.ScaleView.Size = 23;
chart.ChartAreas[3].AxisX2.ScrollBar.ButtonColor = Color.Silver;
chart.ChartAreas[3].AxisX2.ScrollBar.LineColor = Color.Black;*/
}

setting axis minumum as negative number, but have bar extend down to origin instead of zero

I have 2 bar series in a tee chart. One is a percent value between 0 and 100 and uses the left axis. The other is a temperature, uses the right axis, and the range of possible values is between -40F and 160F.
I would like both bars to start at the bottom axis. I thought that the UseOrigin and Origin properties of the series would do this but apparently it doesn't work.
Below is my code:
chartTank = new TChart();
chartTank.Axes.Left.Grid.Visible = false;
chartTank.Axes.Right.Grid.Visible = false;
chartTank.Axes.Right.Maximum = 160.0;
chartTank.Axes.Right.Minimum = -40;
chartTank.Axes.Right.Increment = 40;
chartTank.Axes.Right.Automatic = false;
chartTank.Axes.Right.AutomaticMinimum = false;
chartTank.Axes.Right.AutomaticMaximum = false;
chartTank.Aspect.View3D = false;
chartTank.Panel.Bevel.Inner = Steema.TeeChart.Drawing.BevelStyles.None;
chartTank.Panel.Bevel.Outer = Steema.TeeChart.Drawing.BevelStyles.None;
chartTank.Axes.Left.Grid.Visible = false;
chartTank.Axes.Bottom.GridCentered = false;
chartTank.Axes.Bottom.Ticks.Visible = false;
chartTank.Axes.Left.Automatic = false;
chartTank.Axes.Left.Minimum = 0;
chartTank.Axes.Left.Maximum = 100;
chartTank.Axes.Right.Visible = true;
var barProduct = new Steema.TeeChart.Styles.Bar();
barProduct.MultiBar = MultiBars.Stacked;
barProduct.Color = Color.Green;
barProduct.Marks.Visible = false;
barProduct.Title = "% Vol";
barProduct.ShowInLegend = true;
chartTank.Series.Add(barProduct);
var barTemperature = new Steema.TeeChart.Styles.Bar();
barTemperature.MultiBar = MultiBars.None;
barTemperature.Color = Color.FromArgb(153, 74, 11);
barTemperature.Marks.Visible = false;
barTemperature.VertAxis = VerticalAxis.Right;
barTemperature.UseOrigin = true;
barTemperature.Origin = -40;
barTemperature.Title = "Temperature";
barTemperature.ShowInLegend = true;
chartTank.Series.Add(barTemperature);
Controls.Add(chartTank);
Here is the result:
I am using TeeChart 2014 4.1 for .NET running on windows CE 6.0
The following code:
private void InitializeChart()
{
tChart1.Axes.Right.Grid.Visible = false;
tChart1.Axes.Right.Maximum = 160.0;
tChart1.Axes.Right.Minimum = -40;
tChart1.Axes.Right.Increment = 40;
tChart1.Axes.Right.Automatic = false;
tChart1.Axes.Right.AutomaticMinimum = false;
tChart1.Axes.Right.AutomaticMaximum = false;
tChart1.Aspect.View3D = false;
tChart1.Panel.Bevel.Inner = Steema.TeeChart.Drawing.BevelStyles.None;
tChart1.Panel.Bevel.Outer = Steema.TeeChart.Drawing.BevelStyles.None;
tChart1.Axes.Left.Grid.Visible = false;
tChart1.Axes.Bottom.Grid.Centered = false;
tChart1.Axes.Bottom.Ticks.Visible = false;
tChart1.Axes.Left.Automatic = false;
tChart1.Axes.Left.Minimum = 0;
tChart1.Axes.Left.Maximum = 100;
tChart1.Axes.Right.Visible = true;
var barProduct = new Steema.TeeChart.Styles.Bar();
barProduct.MultiBar = MultiBars.Side;
barProduct.Color = Color.Green;
barProduct.Marks.Visible = false;
barProduct.Title = "% Vol";
barProduct.ShowInLegend = true;
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
barProduct.Add(rnd.Next(0, 100));
}
tChart1.Series.Add(barProduct);
var barTemperature = new Steema.TeeChart.Styles.Bar();
barTemperature.MultiBar = MultiBars.Side;
barTemperature.Color = Color.FromArgb(153, 74, 11);
barTemperature.Marks.Visible = false;
barTemperature.VertAxis = VerticalAxis.Right;
barTemperature.UseOrigin = true;
barTemperature.Origin = -40;
barTemperature.Title = "Temperature";
barTemperature.ShowInLegend = true;
for (int i = 0; i < 10; i++)
{
barTemperature.Add(rnd.Next(-40, 160));
}
tChart1.Series.Add(barTemperature);
tChart1.Panel.Gradient.Visible = false;
tChart1.Walls.Back.Gradient.Visible = false;
tChart1.Panel.Color = Color.White;
tChart1.Walls.Back.Color = Color.White;
}
gives me the following chart:
Do you get the same results at your end?

3D graph in ASP.NET MVC

I want to see the series more clearly (side by side). How can I achieve it?
Chart1.Series["Legend"].Points.DataBindXY(xAxis, yAxis);
Chart1.Series["Legend1"].Points.DataBindXY(xAxis, yAxis);
Chart1.Series["Legend"].ChartType = SeriesChartType.Column;
Chart1.Series["Legend1"].ChartType = SeriesChartType.Column;
Chart1.Series["Legend"].IsValueShownAsLabel = false;
Chart1.Series["Legend"].Color = Color.FromArgb(198, 99, 99);
// Chart1.Series["Legend"].ChartType = ;
Chart1.Series["Legend"].BorderWidth = 2;
Chart1.ChartAreas["studentChartArea"].Area3DStyle.Enable3D = true;
Chart1.ChartAreas["studentChartArea"].Area3DStyle.LightStyle = System.Web.UI.DataVisualization.Charting.LightStyle.Realistic;
Chart1.ChartAreas["studentChartArea"].Area3DStyle.PointGapDepth = 50;
Chart1.ChartAreas["studentChartArea"].Area3DStyle.WallWidth = 10;

Remove Grid Lines From Chart Control C#

I have an application with Chart control that dynamic updated with data via timer:
Series series;
MyObject obj...
series = new Series();
chart1.Series.Add(series);
//chart1.Legends.Add(new Legend("DifferentLegend"));
//chart1.Legends["DifferentLegend"].DockedToChartArea = "Default";
//chart1.Series["Series1"].Legend = "DifferentLegend";
//chart1.Series["Series1"].IsVisibleInLegend = true;
series.Color = Color.Blue;
series.ChartType = SeriesChartType.Line;
series.BorderWidth = 3;
chart1.Series.Add(series);
//chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
//chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisX.Maximum = 4;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number;
Timer tick:
private void chartTimer_Tick(object sender, EventArgs e)
{
series.Points.Add(obj.BitsPerSecond * 0.000001);
}
I want to change it style and remove the squares from the control:
How can I do that?
Try This:
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisX.MinorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MinorGrid.LineWidth = 0;
To disable X-Axis Labels from the Chart :
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
This then?
mainChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
mainChart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
mainChart.ChartAreas[0].AxisX.MinorGrid.Enabled = false;
mainChart.ChartAreas[0].AxisY.MinorGrid.Enabled = false;

Categories

Resources