簡體   English   中英

Microsoft圖表控件和X軸時間刻度格式

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

我的winforms應用程序中有一個Microsoft Chart Controls。

我目前在循環中播放X和y值。 我也將X軸格式設置為

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

這作為一種時間格式工作正常,但是我注意到一旦我的時間值超過60秒(即00:00:60),而不是比例移動到1分鍾(即00:01:00)它變為61 (即00:00:61)一直到99分(00:00:99)然后(00:01:00)

有沒有辦法解決這個問題?

我懷疑LabelStyle.Format屬性的使用方式與string.Format(mySringFormat,objToFormat)類似。
因此,假設你的底層X對象類型是double ,它只會打印一個冒號分隔的 double(例如4321將是00:43:21 )。

AFAIK,沒有一種簡單的方法可以使用字符串格式打印像時間值這樣的double值。

如果您可以更改填充圖表的代碼,我建議您為X值傳遞DateTime ,然后您將能夠使用自定義DateTime格式,例如

"HH:mm:ss"其他人

編輯:

根據你的評論:

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

編輯2:

這是一個完整的示例,應該很容易將此邏輯應用於您的代碼:

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