简体   繁体   English

Microsoft图表控件和X轴时间刻度格式

[英]Microsoft Chart Controls and X-Axis time scale format

I have a Microsoft Chart Controls within my winforms app. 我的winforms应用程序中有一个Microsoft Chart Controls。

I currently play the X and y values within a loop. 我目前在循环中播放X和y值。 I also had the X-axis format set as 我也将X轴格式设置为

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, (ie 00:00:60), rather than the scale moving up to 1 minute (ie 00:01:00) it goes to 61 (ie 00:00:61) right up to 99 before it goes to one minute (00:00:99) then (00:01:00) 这作为一种时间格式工作正常,但是我注意到一旦我的时间值超过60秒(即00:00:60),而不是比例移动到1分钟(即00:01:00)它变为61 (即00:00:61)一直到99分(00:00:99)然后(00:01:00)

Is there a way to fix this please? 有没有办法解决这个问题?

I suspect that LabelStyle.Format property is used in a similar way as in string.Format(mySringFormat,objToFormat) . 我怀疑LabelStyle.Format属性的使用方式与string.Format(mySringFormat,objToFormat)类似。
Hence, given that your underlying X objects type is double , it will just print a colon-separated double (eg 4321 will be 00:43:21 ). 因此,假设你的底层X对象类型是double ,它只会打印一个冒号分隔的 double(例如4321将是00:43:21 )。

AFAIK, there isn't an easy way to print a double value like a time value using just a string format. AFAIK,没有一种简单的方法可以使用字符串格式打印像时间值这样的double值。

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, eg 如果您可以更改填充图表的代码,我建议您为X值传递DateTime ,然后您将能够使用自定义DateTime格式,例如

"HH:mm:ss" , or others "HH:mm:ss"其他人

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: 编辑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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM