WPF graph column graph - c#

I am trying to set up a column graph with the time. Why is my graph not showing up on the main window?
I initialized the graph object like this :
private Chart graph = new Chart();
my chart functions are:
private void DrawTimeChart(string testName,double time)
{
Series sinSeries = new Series();
sinSeries.ChartType = SeriesChartType.Column;
sinSeries.Color = System.Drawing.Color.Blue;
sinSeries.BorderWidth = 2;
sinSeries.Points.AddXY(testName,time);
}
private void cnvChart_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
graph.Width = 250;
graph.Height = 250;
host.Child = graph;
// Add the chart to the canvas so it can be displayed.
this.cnvChart.Children.Add(host);
}
private void SetUpChart(Chart funcChart, int marginX, int marginY)
{
graph.Series.Clear();
graph.ChartAreas.Clear();
graph.ChartAreas.Add("Default" + new Random());
// Add a series with some data points.
funcChart.Width = 349;
funcChart.Height = 177;
funcChart.Location = new System.Drawing.Point(marginX, marginY);
funcChart.ChartAreas[0].AxisX.Title = "X";
funcChart.ChartAreas[0].AxisX.Interval = 1.0;
funcChart.ChartAreas[0].AxisX.Crossing = 0;
funcChart.ChartAreas[0].AxisX.LabelStyle.Format = "{0.0}";
funcChart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
funcChart.ChartAreas[0].AxisY.Title = "Y";
funcChart.ChartAreas[0].AxisY.Interval = 1.0;
funcChart.ChartAreas[0].AxisY.Crossing = 0;
funcChart.ChartAreas[0].AxisY.LabelStyle.Format = "{0.0}";
funcChart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
funcChart.ChartAreas[0].AxisX.Maximum = 6;
funcChart.ChartAreas[0].AxisX.Minimum = -6;
funcChart.ChartAreas[0].AxisY.Maximum = 7;
funcChart.ChartAreas[0].AxisY.Minimum = -7;
}
I'm calling the functions like this:
SetUpChart(graph,5,5);
DrawTimeChart("test1",time1);
in regards to XAML, that's how my canvas is written:
<Canvas x:Name="cnvChart" HorizontalAlignment="Right" Height="177"
Margin="0,242,392.333,0" VerticalAlignment="Top" Width="349"
Loaded="cnvChart_Loaded" RenderTransformOrigin="0.5,0.5"/>

Related

Adding pictureBox to panel with codes

I have a panel in Visual Studio/windows form app.But I coulnd't add pictureBox on it with code.It is working if I work without panel however I need it.MyCodes:
PictureBox[] pipe = new PictureBox[3];
private void Form1_Load(object sender, EventArgs e)
{
CreatePipes(1);}
private void CreatetopPipes(int Number)
{
for (int i = 0; i <= Number; i++)
{
PictureBox temp = new PictureBox();
this.Controls.Add(temp);
temp.Width = 50;
temp.Height = 350;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = 30;
temp.Left = 300;
topPipe[i] = temp;
topPipe[i].Visible = true;
}
}
You can use panelName.Controls.Add(temp), and i advise you to change a top of PictureBox in for loop to view all PictureBox, like this :
private void CreatetopPipes(int Number)
{
for (int i = 0; i <= Number; i++)
{
PictureBox temp = new PictureBox();
panelName.Controls.Add(temp);
temp.Width = 50;
temp.Height = 350;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = temp.Height * panelName.Controls.Count;
temp.Left = 300;
topPipe[i] = temp;
topPipe[i].Visible = true;
}
}

How do I limit the data collection on a chart?

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);

Zoom in downsampling chart

I use a .NET Winform version teechart 4.1.2012.1032.
I modified the sample that you supplied. "Extended\Reducing number of points\DownSampling Additions"
But When I zoom in chart, fastline's marks count is not 100 , downSampling.DisplayedPointCount.
How can I resolve it?
private void InitializeChart()
{
this.cursorTool1 = new Steema.TeeChart.Tools.CursorTool();//
this.tChart1.Tools.Add(this.cursorTool1);//
this.cursorTool1.FollowMouse = true;//
this.cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;//
this.cursorTool1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool1_Change);//
CreateArrays();
tChart1.Aspect.View3D = false;
tChart1.Zoom.Direction = ZoomDirections.Both;//.Horizontal;//
tChart1.Series.Add(points = new Steema.TeeChart.Styles.Points());
tChart1.Series.Add(fastLine = new Steema.TeeChart.Styles.FastLine());
downSampling = new Steema.TeeChart.Functions.DownSampling(tChart1.Chart);
points.Add(xValues, yValues);
points.Active = false;
downSampling.DisplayedPointCount = 100;
downSampling.Method = Steema.TeeChart.Functions.DownSamplingMethod.MinMaxFirstLast;// Null;
fastLine.TreatNulls = Steema.TeeChart.Styles.TreatNullsStyle.DoNotPaint;
fastLine.DataSource = points;
fastLine.Function = downSampling;
this.tChart1.Axes.Custom.Add(new Steema.TeeChart.Axis(this.tChart1.Chart));//
this.tChart1[1].CustomVertAxis = this.tChart1.Axes.Custom[0];//
this.tChart1[0].CustomVertAxis = this.tChart1.Axes.Custom[0];//
this.fastLine.Marks.Visible = true;//
}
private void CreateArrays()
{
int length = 2600000;
xValues = new Nullable<double>[length];
yValues = new Nullable<double>[length];
Random rnd = new Random();
for (int i = 0; i < length; i++)
{
xValues[i] = i;
yValues[i] = i;
}
}
private void tChart1_Zoomed(object sender, EventArgs e)
{
tChart1[1].CheckDataSource(); //series 1 is the function series
}
The DisplayedPointCount specifies how many points the DownSampling function should paint and displays this number as a maximum number of points. As I explained here, you may need to combine it with Tolerance property to get the results you expect. So, you could do something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
//tChart1.Dock = DockStyle.Fill;
tChart1.Aspect.View3D = false;
tChart1.Zoomed += tChart1_Zoomed;
Steema.TeeChart.Styles.Points points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
Random y = new Random();
for (int i = 0; i < 10000; i++)
{
points1.Add(DateTime.Now.AddHours(i), y.Next());
}
points1.XValues.DateTime = true;
points1.Pointer.HorizSize = 1;
points1.Pointer.VertSize = 1;
Steema.TeeChart.Functions.DownSampling downSampling1 = new Steema.TeeChart.Functions.DownSampling(tChart1.Chart);
downSampling1.Method = Steema.TeeChart.Functions.DownSamplingMethod.Average;
downSampling1.Tolerance = 100;
downSampling1.DisplayedPointCount = Convert.ToInt32(downSampling1.Tolerance * 4);
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
line1.Function = downSampling1;
line1.DataSource = points1;
line1.Marks.Visible = true;
line1.Marks.Style = MarksStyles.PointIndex;
UpdateTitle();
}
void tChart1_Zoomed(object sender, EventArgs e)
{
tChart1[1].CheckDataSource();
UpdateTitle();
}
private void UpdateTitle()
{
tChart1.Header.Text = (tChart1[1].Function as Steema.TeeChart.Functions.DownSampling).DisplayedPointCount.ToString();
}
}

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;

How to change the default y axis for self stack bar

I want to plot a Bar series with MultiBars style as SelfStack. By default the self stack bar y axis value is 0 , Is there any way to change the default y axis for self stack bar
public class Bar_SelfStack
{
private Steema.TeeChart.Styles.Bar bar1;
private System.ComponentModel.IContainer components = null;
private Steema.TeeChart.TChart tChart1;
private Steema.TeeChart.Tools.GridBand gridBand1;
public Bar_SelfStack()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
bar1.Add(100, "Cars");
bar1.Add(300, "Phones");
bar1.Add(200, "Lamps");
// Set "Self-Stacked":
bar1.MultiBar = Steema.TeeChart.Styles.MultiBars.SelfStack;
// cosmetic
bar1.Marks.Visible = false;
bar1.Marks.Style = Steema.TeeChart.Styles.MarksStyles.Value;
bar1.ColorEach = true;
}
}
You can change the default Y axis to custom vertical axis as do in next simple code:
Steema.TeeChart.TChart tChart1;
public Form1()
{
InitializeComponent();
tChart1 = new TChart();
this.Controls.Add(tChart1);
tChart1.Top = 150;
tChart1.Left = 100;
tChart1.Height = 400;
tChart1.Width = 550;
InitializeChart();
}
Steema.TeeChart.Styles.Bar series1, series2, series3,series4;
Steema.TeeChart.Axis axis1;
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
series1 = new Bar(tChart1.Chart);
series2 = new Bar(tChart1.Chart);
series3 = new Bar(tChart1.Chart);
series4 = new Bar(tChart1.Chart);
axis1 = new Axis(tChart1.Chart);
tChart1.Axes.Custom.Add(axis1);
series1.FillSampleValues();
series2.FillSampleValues();
series3.FillSampleValues();
series4.FillSampleValues();
series1.Marks.Visible = false;
series2.Marks.Visible = false;
series3.Marks.Visible = false;
series4.Marks.Visible = false;
axis1.RelativePosition = tChart1.Panel.MarginLeft -12;
tChart1.Panel.MarginLeft = 10;
axis1.Horizontal = false;
series1.CustomVertAxis = axis1;
series1.MultiBar = MultiBars.SelfStack;
button1.Click +=button1_Click;
}
Could you tell us if previous code help you to achieve as you want?
I hope will helps.
Thanks,

Categories

Resources