如何组合图表数据点并为每个数据点的分组的SUM添加系列?

时间:2015-05-07 07:45:57

标签: c# visual-studio-2010 visual-studio visual-studio-2012 charts

我试图通过多个系列的数据点以15分钟的间隔对下图中的数据进行分组。

如图所示,有两个系列,我的程序有可能添加无限系列的不同名称。如何将不同系列的y值分组到相同的点以添加到总成本系列中?我已经尝试过了:

chartDailyCost.DataManipulator.Group(
"SUM", 15, System.Windows.Forms.DataVisualization.Charting.IntervalType.Minutes, "*");

其中“*”显然分组多个输入系列 这也是我用来将线图添加到图表的循环:

chartDailyCost.Series.Add(textBoxApplianceName.Text);
chartDailyCost.Legends.Add(textBoxApplianceName.Text); 
chartDailyCost.Series[textBoxApplianceName.Text].ChartType = System.Windows.Forms.
    DataVisualization.Charting.SeriesChartType.Line;

while (starttime <= endtime)
{
   if ((starttime <= (Convert.ToDateTime("06:00"))) 
   && (starttime > (Convert.ToDateTime("00:00"))))
   {
     TotalCostLowPeak = TotalCost * (8.0 / 12.2);
     chartDailyCost.Series[textBoxApplianceName.Text].
                    Points.AddXY( starttime, TotalCostLowPeak);
   }
   if ((starttime <= (Convert.ToDateTime("10:00"))) 
   && (starttime > (Convert.ToDateTime("06:00"))))
   {
     TotalCostHighPeak = TotalCost * (16.1 / 12.2);
     chartDailyCost.Series[textBoxApplianceName.Text]
                   .Points.AddXY(starttime, TotalCostHighPeak);
   }
   if ((starttime <= (Convert.ToDateTime("16:00"))) 
   && (starttime > (Convert.ToDateTime("10:00"))))
   {
     chartDailyCost.Series[textBoxApplianceName.Text]
                   .Points.AddXY(starttime, TotalCost);
   }
  if ((starttime <= (Convert.ToDateTime("20:00"))) 
  && (starttime > (Convert.ToDateTime("16:00"))))
 {
   TotalCostHighPeak = TotalCost * (16.1 / 12.2);
   chartDailyCost.Series[textBoxApplianceName.Text]
                 .Points.AddXY(starttime, TotalCostHighPeak);
 }
 if ((starttime <= (Convert.ToDateTime("23:45"))) 
 && (starttime > (Convert.ToDateTime("20:00"))))
 {
     TotalCostLowPeak = TotalCost * (8.0 / 12.2);
     chartDailyCost.Series[textBoxApplianceName.Text]
                   .Points.AddXY(starttime, TotalCostLowPeak);
 }

 starttime = starttime.AddMinutes(15);
}
sumSeries(chartDailyCost);

这里我尝试了TaW推荐的方法,但是我收到了错误:

http://postimg.org/image/7pco8vrst/

using System.Windows.Forms.DataVisualization.Charting;
private void sumSeries(Chart chartDailyCost)
    {
        chartDailyCost.Series.Remove(chartDailyCost.Series["Total Cost"]);

        List<Series> dataSeries = chartDailyCost.Series.ToList();
//this line shows an error when multiple series are added
        Series sumSeries = chartDailyCost.Series.Add("Total Cost");
        chartDailyCost.Series["Total Cost"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
        //I have initialised many of the chart formats in the form_load method

        for (int i = 1; i < dataSeries[0].Points.Count; i++)
        {
            double sum = 0d;

            //for every series in the chart collection sum the yvalues at the specified 
            foreach (Series s in dataSeries)
            {
                sum += s.Points[i].YValues[0];
            }
            DataPoint dp = new DataPoint();
            //Add a new yvalue to the datapoint for the summed series's
            dp.XValue = dataSeries[0].Points[i].XValue;
            dp.YValues[0] = sum;
            sumSeries.Points.Add(dp);
        }
        //Add all of the data points to the "Total Cost" Series
        //chartDailyCost.Series.Add(sumSeries);//Found in above coding
    }

1 个答案:

答案 0 :(得分:1)

代码示例假定您要将所有系列的所有值加总到新系列的点中。此外,所有系列都具有相同的点数和相同的XValues

using System.Windows.Forms.DataVisualization.Charting;
...
...
void sumSeries(Chart chart)
{
    List<Series> dataSeries = chart.Series.ToList();
    Series sumSeries = chart.Series.Add("Total");
    // insert charttype and styles here!
    for (int i = 0; i < dataSeries[0].Points.Count; i++)
    {
        double sum = 0d;

        foreach (Series s in dataSeries)
        {
            sum += s.Points[i].YValues[0];
        }
        DataPoint dp = new DataPoint();
        dp.XValue = dataSeries[0].Points[i].XValue;
        dp.YValues[0] = sum;
        sumSeries.Add(dp);
    }
}

这会添加一个新的Series sumSeries。在您再次调用该函数以反映更新的数据之前,请务必删除