MS Charts C# Line Chart not accurate on data with gaps

你说的曾经没有我的故事 提交于 2020-01-03 06:15:39

问题


I have some code shown below that loops through a datagridview and graphs the data. The problem I'm having is sometimes the data doesn't line up correctly when there are gaps. I have multiple devices spitting data into my datagridview and sometimes the unit will not report for 2 reports, so there were be a gap in data but I still want my data to line up properly. How do I achieve this? The chart picture below will describe the issue better. I'm using MSCharts, Winforms, C#, and .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);
        }
    }


回答1:


You must perform the correct conversion of your date string values otherwise the charting control will use them as simple labels. Setting XValueType = = ChartValueType.DateTime is not enough. You should pass your time stamps as double using the DateTime.ToOADate() conversion as illustrated in the example below:

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


来源:https://stackoverflow.com/questions/24190843/ms-charts-c-sharp-line-chart-not-accurate-on-data-with-gaps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!