8

I have a Microsoft Chart Controls within my winforms app.

I currently play the X and y values within 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 once my time values went above 60 seconds, (i.e. 00:00:60), rather than the scale moving up to 1 minute (i.e. 00:01:00) it goes to 61 (i.e. 00:00:61) right up to 99 before it goes to one minute (00:00:99) then (00:01:00)

Is there a way to fix this please?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
PleaseHelpMe
  • 81
  • 1
  • 1
  • 2

1 Answers1

12

I suspect that LabelStyle.Format property is used in a similar way as in string.Format(mySringFormat,objToFormat).
Hence, given that your underlying X objects type is double, it will just print a colon-separated double (e.g. 4321 will be 00:43:21).

AFAIK, there isn't an easy way to print a double value like a time value using just a string format.

If you can change the code filling the chart, I suggest you to pass DateTime's for the X values, and then you will be able to use custom DateTime formatting, e.g.

"HH:mm:ss", or others

EDIT:

As per 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";
}
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • ok but may I ask how I would go about doing the DateTime's for the X values? please – PleaseHelpMe Apr 09 '11 at 16:13
  • @PleaseHelpMe: Are you binding chart to a datasource or adding elements using series.Points.AddXY() ? – digEmAll Apr 09 '11 at 16:16
  • adding my elements using the "....Points.addXY((double)value1,(double)value2)" method – PleaseHelpMe Apr 09 '11 at 16:18
  • I have to cast the values as doubles, its a bit of a long story :S – PleaseHelpMe Apr 09 '11 at 16:18
  • I keep getting DD/MM/YYY values on the xAxis instead of time HH:MM:SS :( – PleaseHelpMe Apr 09 '11 at 16:46
  • @PleaseHelpMe: strange, have you set the format to `HH:mm:ss` right ? You can also try to force the type of axis X doing `series.XValueType = ChartValueType.DateTime;` – digEmAll Apr 09 '11 at 16:52
  • tried both, first solution causes my data not to be displayed, second one does not show time scale correcly, keeps repeating same time value :S – PleaseHelpMe Apr 09 '11 at 17:05
  • thank you for the help anyway digEmAll but I really cant figure out why when casting as a DateTime object that my data does not display :( here is my actual X value -------------------------------------------------------------------------------------------------- series.Points.addXY((double)i / 170, (double)(data - 44) / 170); Maybe because of calculations the cast is messing up? – PleaseHelpMe Apr 09 '11 at 17:14
  • @PleaseHelpMe: Please, post the code you use to fill the chart in the question... :( – digEmAll Apr 09 '11 at 17:23
  • You have to use DateTime.FromOADate() and DateTime.Now.ToOADate() – ykatchou Oct 04 '11 at 09:01