Microsoft Chart Controls and X-Axis Timeline Format

I have a Microsoft chart control in my winforms application.

I am currently reproducing the values ​​of X and y in a loop. I also had the X-axis format set as

ChartAreas[0].AxisX.LabelStyle.Format={"00:00:00"} 

This worked fine as a time format, however, I noticed that when my time values ​​exceeded 60 seconds (i.e. 00:00:60), and not a scale moving up to 1 minute (i.e. 00:01:00) , it reaches 61 (i.e. 00:00:61) up to 99, before switching to one minute (00:00:99), then (00:01:00)

Is there a way to fix this, please?

+7
source share
1 answer

I suspect that the LabelStyle.Format property is used the same way as in string.Format(mySringFormat,objToFormat) .
Therefore, given that your main type of X objects is double , it just prints a double colon (for example, 4321 will be 00:43:21 ).

AFAIK, there is no easy way to print a double value as a time value using only the string format.

If you can change the code that populates the chart, I suggest you pass a DateTime for the X values, and then you can use your own DateTime formatting, for example.

"HH:mm:ss" or others

EDIT:

According to your comment:

 // create a base date at the beginning of the method that fills the chart. // Today is just an example, you can use whatever you want // as the date part is hidden using the format = "HH:mm:ss" DateTime baseDate = DateTime.Today; var x = baseDate.AddSeconds((double)value1); var y = (double)value2; series.Points.addXY(x, y); 

EDIT 2:

Here's a complete example, it should be easy to apply this logic to your code:

 private void PopulateChart() { int elements = 100; // creates 100 random X points Random r = new Random(); List<double> xValues = new List<double>(); double currentX = 0; for (int i = 0; i < elements; i++) { xValues.Add(currentX); currentX = currentX + r.Next(1, 100); } // creates 100 random Y values List<double> yValues = new List<double>(); for (int i = 0; i < elements; i++) { yValues.Add(r.Next(0, 20)); } // remove all previous series chart1.Series.Clear(); var series = chart1.Series.Add("MySeries"); series.ChartType = SeriesChartType.Line; series.XValueType = ChartValueType.Auto; DateTime baseDate = DateTime.Today; for (int i = 0; i < xValues.Count; i++) { var xDate = baseDate.AddSeconds(xValues[i]); var yValue = yValues[i]; series.Points.AddXY(xDate, yValue); } // show an X label every 3 Minute chart1.ChartAreas[0].AxisX.Interval = 3.0; chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes; chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; } 
+11
source

All Articles