MS图表C#折线图对带间隙的数据不准确

时间:2014-06-12 17:51:50

标签: c# mschart

我有一些如下所示的代码,它循环遍历数据网格视图并绘制数据图形。我遇到的问题有时数据在有间隙时没有正确排列。我有多个设备将数据吐出到我的datagridview中,有时该单元不会报告2个报告,因此数据存在差距,但我仍然希望我的数据正确排列。我该如何实现这一目标?下面的图表将更好地描述问题。我正在使用MSCharts,Winforms,C#和.NET 4。

//for each column in my datagridview
for (int i = 0; i < currentDGV.Rows.Count; i++)
{
     //for each column in each row in my datagridview
     for (int j = 2; j < currentDGV.Columns.Count - 5; j++)
     {
         //make sure the column is visible and isnt a outlier
         if (currentDGV.Columns[j].Visible == true &&      (bool)currentDGV.Rows[i].Cells[OUTLIER_VALUE].Value != true)
         {
            string deviceName = currentDGV.Rows[i].Cells[DEVICE_NAME_VALUE].Value.ToString();
            string currentSize = currentDGV.Columns[j].HeaderText;
            string currentTime = currentDGV.Rows[i].Cells[DATETIME_VALUE].Value.ToString();
            string currentSizeValue = currentDGV.Rows[i].Cells[j].Value.ToString();

            createNewSeries(deviceName + " - " + currentSize);
            plotValueOnSeries(deviceName + " - " + currentSize, currentTime, currentSizeValue);
          }
      }
 }

private void createNewSeries(String SeriesName)
    {
        if (tempChart != null)
        {
            if (tempChart.Series.IsUniqueName(SeriesName))
            {
                tempChart.Series.Add(SeriesName);
                //tempChart.Series[SeriesName].IsXValueIndexed = true;
                //tempChart.Series[SeriesName].XValueType = ChartValueType.DateTime;
            }
        }
    }

    private void plotValueOnSeries(String SeriesName, string XValue, string YValue)
    {
        if (tempChart != null)
        {
            if (radioButtonStep.Checked == true)
                tempChart.Series[SeriesName].ChartType = SeriesChartType.StepLine;
            else if (radioButtonSpline.Checked == true)
                tempChart.Series[SeriesName].ChartType = SeriesChartType.Spline;
            else if (radioButtonPoint.Checked == true)
                tempChart.Series[SeriesName].ChartType = SeriesChartType.Point;
            else
                tempChart.Series[SeriesName].ChartType = SeriesChartType.Line;

            tempChart.Series[SeriesName].Points.AddXY(XValue, YValue);
        }
    }

MSChart

1 个答案:

答案 0 :(得分:0)

您必须执行日期字符串值的正确转换,否则图表控件会将它们用作简单标签。设置XValueType = = ChartValueType.DateTime是不够的。您应该使用DateTime.ToOADate()转换将时间戳记传递为双倍,如下例所示:

Gaps are not an issue

        chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm";

        Series s1 = new Series("s1");
        s1.XValueType = ChartValueType.DateTime;
        s1.Points.AddXY(Convert.ToDateTime("10:01").ToOADate(), 1);
        s1.Points.AddXY(Convert.ToDateTime("10:02").ToOADate(), 2);
        s1.Points.AddXY(Convert.ToDateTime("10:03").ToOADate(), 3);
        s1.Points.AddXY(Convert.ToDateTime("10:04").ToOADate(), 4);
        s1.Points.AddXY(Convert.ToDateTime("10:07").ToOADate(), 5);            
        chart1.Series.Add(s1);

        Series s2 = new Series("s2");
        s2.XValueType = ChartValueType.DateTime;
        s2.Points.AddXY(Convert.ToDateTime("10:01").ToOADate(), 10);
        s2.Points.AddXY(Convert.ToDateTime("10:02").ToOADate(), 9);
        s2.Points.AddXY(Convert.ToDateTime("10:05").ToOADate(), 8);
        s2.Points.AddXY(Convert.ToDateTime("10:06").ToOADate(), 7);
        s2.Points.AddXY(Convert.ToDateTime("10:09").ToOADate(), 6);
        chart1.Series.Add(s2);