how to enable auto scaling property in chart control using c# - c#

In my application im having a chart control that is working fine. For every 1 second updating the x-Axis values and y-axis values in the chart. I want to enable auto scaling for the chart control programmatically for y axis. Please refer my code below,
chart1.Series.Clear();
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
series1.Name = "Series1";
series1.MarkerStyle = MarkerStyle.Circle;
series1.MarkerColor = Color.BlueViolet;
series1.MarkerSize = 10;
series1.Color = System.Drawing.Color.Green;
series1.IsXValueIndexed = true;
series1.YAxisType = AxisType.Primary;
series1.ChartType = SeriesChartType.Line;
this.chart1.Series.Add(series1);
How to enable auto scaling programmatically.

chart1.Series.Clear();
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].RecalculateAxesScale();
series1.Name = "Series1";
series1.MarkerStyle = MarkerStyle.Circle;
series1.MarkerColor = Color.BlueViolet;
series1.MarkerSize = 10;
series1.Color = System.Drawing.Color.Green;
//series1.IsVisibleInLegend = false;
series1.IsXValueIndexed = true;
//series1.XValueType = ChartValueType.Time;
series1.YAxisType = AxisType.Primary;
series1.ChartType = SeriesChartType.Line;
this.chart1.Series.Add(series1);
Working fine

chart.ChartAreas[0].AxisY.Maximum = Double.NaN;
But some people have trouble with the above one,
so try this if that doesn't work
chart.ChartAreas[0].RecalculateAxesScale();

Related

C# Windows Forms Chart is weird

I made a C# windows forms and my chart is very strange. My chart has a lot of white borders and the text for Master and Slave is very tiny and pixelated.
Why the Axis is so far away from the borders? How can I fix this?
// chart1
//
chartArea2.AxisX.MajorGrid.Enabled = false;
chartArea2.AxisX.MajorTickMark.Enabled = false;
chartArea2.AxisY.MajorGrid.Enabled = false;
chartArea2.AxisY.MajorTickMark.Enabled = false;
chartArea2.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea2);
legend2.Name = "Legend1";
this.chart1.Legends.Add(legend2);
this.chart1.Location = new System.Drawing.Point(543, 49);
this.chart1.Name = "chart1";
series3.ChartArea = "ChartArea1";
series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
series3.Legend = "Legend1";
series3.MarkerBorderColor = System.Drawing.Color.Black;
series3.MarkerImage = "C:\\Users\\Tiago\\Desktop\\CODIGO_TESE_FINAL_BACKUP1408_BOM\\C# - AR.Drone SDK\\AR.Dron" +
"e\\icone_drone_2_50x50.png";
series3.MarkerImageTransparentColor = System.Drawing.Color.Red;
series3.Name = "Master";
series4.ChartArea = "ChartArea1";
series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
series4.Legend = "Legend1";
series4.MarkerImage = "C:\\Users\\Tiago\\Desktop\\CODIGO_TESE_FINAL_BACKUP1408_BOM\\C# - AR.Drone SDK\\AR.Dron" +
"e\\icone_drone_2_50x50.png";
series4.Name = "Slave";
this.chart1.Series.Add(series3);
this.chart1.Series.Add(series4);
this.chart1.Size = new System.Drawing.Size(1159, 359);
this.chart1.TabIndex = 7;
this.chart1.Text = "chart1";
this.chart1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseDown);
this.chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseMove);
this.chart1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseUp);
You can set the border spaces and the Fonts all in the designer.
Pick the ChartArea and modify the Position. Note that it is not in pixels but in percentages of the Chart.ClientSize!! So for your largish chart the 3% defualt is a little too much..
The Legend's Font is changed here:

Why I can't see the Scrollbar in ChartArea of MSChart?

I make a c#
Bnnn
chart program.
And, I use the MSChart.
I setup the Scrollbar information on ChartArea Collection.
When execute my program, the scrollbar is not visible... what's wrong with my ChartArea Collection.
And, I try to coding for setting scrollbar like this.
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 10;
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 10;
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chart1.ChartAreas[0].AxisX.ScrollBar.BackColor = Color.LightGray;
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.Gray;
chart1.ChartAreas[0].AxisX.ScrollBar.LineColor = Color.Black;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 100;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.Series["Series1"].Points.AddXY(0, 0);
chart1.Series["Series2"].Points.AddXY(0, 0);
chart1.Series["Series3"].Points.AddXY(0, 0);
Is it any wrong?.
You are setting some properties on the AxisX scroll bar twice (such as Size and Enabled), but you are not setting them for the AxisY.
I suspect that you did a cut and past and forgot to change some X to Y ....

Chart messing zoom on ResetAutoValues

I´m using a chart to plot some infos from a Sensor. Everything is going right, until I reach the interval of 3660 points.
So, what I want to do is when my points reach that value, my chart will remove the first point and add the new one, take a look at the code:
if (graficoKVm.Series[0].Points.Count > 3660)
{
graficoKVm.Series[0].Points.RemoveAt(0);
graficoKVm.Series[0].Points.AddXY(DateTime.Now.ToOADate(), _sensorCampoEletroMagnetico.kVm);
graficoKVm.ResetAutoValues();
}
The problem is that, if the chart is zoomed, when it execute the graficoKVm.ResetAutoValues(); my zoom got messed up.
So far I´ve tried to store the zoom values and then apply the zoom after the reset, but I´ve failed implementing it. Here is the chart declaration:
this.graficoKVm.BackColor = System.Drawing.Color.Black;
this.graficoKVm.BorderSkin.BorderColor = System.Drawing.Color.White;
chartArea1.AxisX.Interval = 5D;
chartArea1.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.LabelStyle.Angle = 45;
chartArea1.AxisX.LabelStyle.ForeColor = System.Drawing.Color.White;
chartArea1.AxisX.LabelStyle.Format = "HH:mm";
chartArea1.AxisX.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisX.MajorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MinorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.MinorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.ScaleBreakStyle.LineColor = System.Drawing.Color.White;
chartArea1.AxisX.ScaleView.SizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.ScaleView.SmallScrollMinSize = 5D;
chartArea1.AxisX.ScaleView.SmallScrollMinSizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.ScaleView.SmallScrollSizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.AxisX.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisX.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.AxisX2.LineColor = System.Drawing.Color.White;
chartArea1.AxisX2.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisX2.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisX2.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisX2.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.AxisY.Interval = 2D;
chartArea1.AxisY.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
chartArea1.AxisY.LabelStyle.ForeColor = System.Drawing.Color.White;
chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisY.MajorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.Maximum = 18D;
chartArea1.AxisY.Minimum = -18D;
chartArea1.AxisY.MinorGrid.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.MinorTickMark.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.ScaleBreakStyle.LineColor = System.Drawing.Color.White;
chartArea1.AxisY.ScaleView.SmallScrollMinSize = 0.5D;
chartArea1.AxisY.ScaleView.SmallScrollSize = 0.5D;
chartArea1.AxisY.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisY.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisY.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.AxisY2.LineColor = System.Drawing.Color.White;
chartArea1.AxisY2.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
chartArea1.AxisY2.ScrollBar.BackColor = System.Drawing.Color.LightGray;
chartArea1.AxisY2.ScrollBar.ButtonColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
chartArea1.AxisY2.ScrollBar.LineColor = System.Drawing.Color.Black;
chartArea1.BackColor = System.Drawing.Color.Black;
chartArea1.BorderColor = System.Drawing.Color.White;
chartArea1.CursorX.Interval = 0.01D;
chartArea1.CursorX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chartArea1.CursorX.IsUserEnabled = true;
chartArea1.CursorX.IsUserSelectionEnabled = true;
chartArea1.CursorX.SelectionColor = System.Drawing.Color.White;
chartArea1.CursorY.IsUserEnabled = true;
chartArea1.CursorY.IsUserSelectionEnabled = true;
chartArea1.CursorY.SelectionColor = System.Drawing.Color.White;
chartArea1.InnerPlotPosition.Auto = false;
chartArea1.InnerPlotPosition.Height = 91.06797F;
chartArea1.InnerPlotPosition.Width = 95F;
chartArea1.InnerPlotPosition.X = 5F;
chartArea1.InnerPlotPosition.Y = 2.16495F;
chartArea1.Name = "ChartArea1";
chartArea1.Position.Auto = false;
chartArea1.Position.Height = 97F;
chartArea1.Position.Width = 97F;
this.graficoKVm.ChartAreas.Add(chartArea1);
this.graficoKVm.Location = new System.Drawing.Point(3, 270);
this.graficoKVm.Margin = new System.Windows.Forms.Padding(0);
this.graficoKVm.Name = "graficoKVm";
series1.BorderWidth = 2;
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
series1.IsVisibleInLegend = false;
series1.Name = "Valor";
series1.ShadowColor = System.Drawing.Color.Black;
series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
this.graficoKVm.Series.Add(series1);
this.graficoKVm.Size = new System.Drawing.Size(1201, 295);
this.graficoKVm.TabIndex = 27;
this.graficoKVm.Text = "chart1";
Have you tried in your code to use Refresh or Update? When the index reaches 3660, the chart will be replotted with the Point saved in your series, no matter how much zoom you applied.
To make it work in the right way, I've done this implementation:
if (graficoKVm.Series[0].Points.Count > 3659)
{
graficoKVm.Series[0].Points.AddXY(DateTime.Now.ToOADate(), _sensorCampoEletroMagnetico.kVm);
if (!graficoKVm.ChartAreas[0].AxisX.ScaleView.IsZoomed &&
!graficoKVm.ChartAreas[0].AxisY.ScaleView.IsZoomed &&
!graficoKVm.ChartAreas[0].AxisY2.ScaleView.IsZoomed &&
!graficoKVm.ChartAreas[0].AxisX2.ScaleView.IsZoomed)
{
while (graficoKVm.Series[0].Points.Count > 3660)
{
graficoKVm.Series[0].Points.RemoveAt(0);
}
graficoKVm.ResetAutoValues();
}
}
So, if the chart is zoomed on any Axis, it will not reset the min and max value from each Axis. Eliminating the zoom problem.

How to change my chart control background color

This is my control:
seriesTraffic = new Series();
seriesTraffic.Color = Color.Black;
seriesTraffic.ChartType = SeriesChartType.Spline;
seriesTraffic.BorderWidth = 2;
chart1.Series.Add(seriesTraffic);
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
chart1.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
chart1.ChartAreas[0].AxisY.Minimum = 10;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisY.Minimum = 10;
chart1.ChartAreas[0].AxisY.Maximum = 10;
chart1.ChartAreas[0].AxisY.Interval = 5;
chart1.ChartAreas[0].AxisY.Interval = 1;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number;
var chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
chart1.BackColor = System.Drawing.Color.Black;
chartArea1.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea1.AxisY.MinorGrid.Enabled = true;
chartArea1.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea1.AxisX2.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea1.AxisX2.MinorGrid.Enabled = true;
chartArea1.AxisX.LineColor = System.Drawing.Color.Green;
chartArea1.AxisX2.LineColor = System.Drawing.Color.Green;
chartArea1.AxisX2.LineWidth = 1;
chartArea1.AxisX2.MinorGrid.Interval = 0.25D;
chartArea1.AxisX2.MinorGrid.IntervalOffset = -0.01D;
chartArea1.AxisX2.MinorGrid.LineColor = System.Drawing.Color.Green;
chartArea1.AxisY.LineColor = System.Drawing.Color.Green;
chartArea1.AxisY.LineWidth = 1;
chartArea1.AxisY.Maximum = 2D;
chartArea1.AxisY.MaximumAutoSize = 100F;
chartArea1.AxisY.Minimum = -2D;
chartArea1.AxisY.MinorGrid.Interval = 0.25D;
chartArea1.AxisY.MinorGrid.IntervalOffset = -0.01D;
chartArea1.AxisY.MinorGrid.LineColor = System.Drawing.Color.Green;
chart1.ChartAreas.Add(chartArea1);
i want to change my control style to similar to this:
i try to change BackSecondaryColor, BorderColor but it has no effect.
i try to change BackSecondaryColor, BorderColor but it has no effect.
i try to change BackSecondaryColor, BorderColor but it has no effect.
i try to change BackSecondaryColor, BorderColor but it has no effect.
To change the background color of the chart, you will need to change the BackColor property of the ChartArea.
chart.ChartAreas["ChartArea1"].BackColor = Color.Black;
chart.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.LawnGreen;
chart.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.LawnGreen;
You could try to use something like this
var chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
var chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
chart1.BackColor = System.Drawing.Color.Black;
chartArea1.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea1.AxisY.MinorGrid.Enabled = true;
chartArea1.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea1.AxisX2.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea1.AxisX2.MinorGrid.Enabled = true;
chartArea1.AxisX.LineColor = System.Drawing.Color.Green;
chartArea1.AxisX2.LineColor = System.Drawing.Color.Green;
chartArea1.AxisX2.LineWidth = 1;
chartArea1.AxisX2.MinorGrid.Interval = 0.25D;
chartArea1.AxisX2.MinorGrid.IntervalOffset = -0.01D;
chartArea1.AxisX2.MinorGrid.LineColor = System.Drawing.Color.Green;
chartArea1.AxisY.LineColor = System.Drawing.Color.Green;
chartArea1.AxisY.LineWidth = 1;
chartArea1.AxisY.Maximum = 2D;
chartArea1.AxisY.MaximumAutoSize = 100F;
chartArea1.AxisY.Minimum = -2D;
chartArea1.AxisY.MinorGrid.Interval = 0.25D;
chartArea1.AxisY.MinorGrid.IntervalOffset = -0.01D;
chartArea1.AxisY.MinorGrid.LineColor = System.Drawing.Color.Green;
chart1.ChartAreas.Add(chartArea1);
If the above does not work for you, I think that you should try to modify your control only in designer (using properties).
The background color is set by BackColor on your chart, you should also change the ChartArea BackColor to transparent.
Here a quick example, hope it will help:)

Why do my X-Axes labels disappear when I zoom using MS Chart Controls?

I'm using MS Chart Controls. The line chart is a normal time based chart. The problem is when I click the chart and select some time it zooms in, the scrollbar appears, and the x-axes labels disappears. How can I prevent this from happening? If I cannot fix it automatically, is there code I can add to a button that will fix the labels?
private void Chart0Configuration()
{
chart1.ChartAreas[0].Visible = false;
chart1.ChartAreas[0].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
chart1.ChartAreas[0].AlignmentStyle = AreaAlignmentStyles.All;
chart1.ChartAreas[0].Position.Auto = false;
chart1.ChartAreas[0].Position.X = 2;
chart1.ChartAreas[0].Position.Y = 10;
chart1.ChartAreas[0].Position.Width = 98;
//chart1.ChartAreas[0].Position.Height = *****variable
//chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
//chart1.ChartAreas[0].AxisY.MinorTickMark.Enabled = false;
//chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
//chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
//chart1.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;
//chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
//chart1.ChartAreas[0].AxisX.MajorTickMark.Interval = 10;
chart1.ChartAreas[0].AxisX.Interval = 0;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = true;
//chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss";
chart1.ChartAreas[0].AxisY.LabelStyle.IsEndLabelVisible = true;
chart1.ChartAreas[0].InnerPlotPosition.Auto = false;
chart1.ChartAreas[0].InnerPlotPosition.X = 3;
chart1.ChartAreas[0].InnerPlotPosition.Y = 10;
chart1.ChartAreas[0].InnerPlotPosition.Width = 88;
chart1.ChartAreas[0].InnerPlotPosition.Height = 80;
chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
//chart1.ChartAreas[0].CursorX.AutoScroll = true;
chart1.ChartAreas[0].CursorX.Position = 0;
chart1.ChartAreas[0].CursorX.Interval = 0;
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 5;
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisX.ScrollBar.BackColor = Color.LightGray;
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.Gray;
chart1.ChartAreas[0].AxisX.ScrollBar.LineColor = Color.Black;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
}
I inadvertently found the answer when looking at the intervals. They stay visible now that I've add the following line of code.
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;

Categories