'SeriesCollection'中已存在名为'0'的图表元素

时间:2014-03-12 14:58:29

标签: c# .net charts mschart

我有Chart control收到实时数据。

// MBit/Sec
Series seriesBps = new Series("bps");
seriesBps.Color = Color.Gray;
seriesBps.ChartType = SeriesChartType.Spline;
seriesBps.BorderWidth = 2;
seriesBps.Name = "MBit/Sec";
chart1.Series.Add(seriesBps);

//// Packets/Sec
Series seriesPps = new Series("pps");
seriesPps.Color = Color.SteelBlue;
seriesPps.ChartType = SeriesChartType.Spline;
seriesPps.BorderWidth = 2;
seriesPps.Name = "Packets/Sec";
chart1.Series.Add(seriesPps);

这是我需要添加此数据的计时器刻度事件:

private void chartTimer_Tick(object sender, EventArgs e)
{
      if (seriesBps.Points.Count() > 300)
          seriesBps.Points.RemoveAt(0);
      seriesBps.Points.Add(wf.BitsPerSecond * 0.000001);

      DataPoint _point1 = default(DataPoint);
      foreach (DataPoint item in chart1.Series[1].Points)
      {
          item.Label = "";
          item.MarkerStyle = MarkerStyle.None;
      }

      chart1.Series[1].LegendText = chart1.Series[1].Name = (wf.BitsPerSecond * 0.000001).ToString("#,##0");
      DataPoint Point1 = chart1.Series[1].Points[chart1.Series[1].Points.Count - 1];
      Point1.Label = chart1.Series[1].Name;

            chart1.Series[1].SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
            chart1.Series[1].SmartLabelStyle.IsMarkerOverlappingAllowed = false;
            chart1.Series[1].SmartLabelStyle.MovingDirection = LabelAlignmentStyles.BottomRight;

                // --------------------------------------------------------------------------------------------------- //

      if (seriesPps.Points.Count() > 300)
          seriesPps.Points.RemoveAt(0);
      seriesPps.Points.Add(wf.PacketsPerSecond);

      DataPoint _point = default(DataPoint);
      foreach (DataPoint item in chart1.Series[2].Points)
      {
           item.Label = "";
           item.MarkerStyle = MarkerStyle.None;
       }

      chart1.Series[2].LegendText = chart1.Series[2].Name = wf.PacketsPerSecond.ToString("#,##0");
      DataPoint Point = chart1.Series[2].Points[chart1.Series[2].Points.Count - 1];
       Point.Label = chart1.Series[2].Name;

      chart1.Series[2].SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
      chart1.Series[2].SmartLabelStyle.IsMarkerOverlappingAllowed = false;
      chart1.Series[2].SmartLabelStyle.MovingDirection = LabelAlignmentStyles.BottomRight;

      chart1.ResetAutoValues();
}

Chart与第一个Series一起工作正常但在添加第二个收到此错误后,我尝试将系列名称更改为唯一但我不会帮助

2 个答案:

答案 0 :(得分:1)

索引从0开始,而不是1,就像任何其他for i循环一样。将每个系列向后移动一个。

private void chartTimer_Tick(object sender, EventArgs e)
{
      if (seriesBps.Points.Count() > 300)
          seriesBps.Points.RemoveAt(0);
      seriesBps.Points.Add(wf.BitsPerSecond * 0.000001);

      DataPoint _point1 = default(DataPoint);
      foreach (DataPoint item in chart1.Series[0].Points)
      {
          item.Label = "";
          item.MarkerStyle = MarkerStyle.None;
      }

      chart1.Series[0].LegendText =  (wf.BitsPerSecond *     0.000001).ToString("#,##0");
      DataPoint Point1 = chart1.Series[0].Points[chart1.Series[0].Points.Count - 1];
      Point1.Label = chart1.Series[0].Name;

            chart1.Series[0].SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
            chart1.Series[0].SmartLabelStyle.IsMarkerOverlappingAllowed = false;
            chart1.Series[0].SmartLabelStyle.MovingDirection = LabelAlignmentStyles.BottomRight;

            // --------------------------------------------------------------------------------------------------- //

      if (seriesPps.Points.Count() > 300)
          seriesPps.Points.RemoveAt(0);
      seriesPps.Points.Add(wf.PacketsPerSecond);

      DataPoint _point = default(DataPoint);
      foreach (DataPoint item in chart1.Series[2].Points)
      {
           item.Label = "";
           item.MarkerStyle = MarkerStyle.None;
       }

      chart1.Series[1].LegendText = wf.PacketsPerSecond.ToString("#,##0");
      DataPoint Point = chart1.Series[1].Points[chart1.Series[1].Points.Count - 1];
      Point.Label = chart1.Series[1].Name;

      chart1.Series[1].SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
      chart1.Series[1].SmartLabelStyle.IsMarkerOverlappingAllowed = false;
      chart1.Series[1].SmartLabelStyle.MovingDirection = LabelAlignmentStyles.BottomRight;

      chart1.ResetAutoValues();
}

答案 1 :(得分:0)

我遇到了同样的问题,对我来说,答案是先清除这个系列:

_chart.Series.Clear()

如果您动态构建图表,这一点非常重要