图表错误:数据点插入错误。此数据系列只能设置1个Y值

时间:2017-09-03 05:07:10

标签: c# winforms charts

我的图表中有4个系列。 下面的方法效果很好,我看到了结果。

private void ChartSettings(AxisEnabled axisY2Enabled,
                                      bool enabledS1, bool enabledS2, bool enabledS3, bool enabledS4,
                                      bool isVisibleInLegendS1, bool isVisibleInLegendS2, bool isVisibleInLegendS3, bool isVisibleInLegendS4,
                                      string legendTextS1, string legendTextS2, string legendTextS3, string legendTextS4,
                                      string yValueMembersS1, string yValueMembersS2, string yValueMembersS3, string yValueMembersS4)
    {
        chartShow.ChartAreas[0].AxisY2.Enabled = axisY2Enabled;

        chartShow.Series["S1"].Enabled = enabledS1;
        chartShow.Series["S2"].Enabled = enabledS2;
        chartShow.Series["S3"].Enabled = enabledS3;
        chartShow.Series["S4"].Enabled = enabledS4;
        chartShow.Series["S1"].IsVisibleInLegend = isVisibleInLegendS1;
        chartShow.Series["S2"].IsVisibleInLegend = isVisibleInLegendS2;
        chartShow.Series["S3"].IsVisibleInLegend = isVisibleInLegendS3;
        chartShow.Series["S4"].IsVisibleInLegend = isVisibleInLegendS4;

        chartShow.Series["S1"].LegendText = legendTextS1;
        chartShow.Series["S2"].LegendText = legendTextS2;
        chartShow.Series["S3"].LegendText = legendTextS3;
        chartShow.Series["S4"].LegendText = legendTextS4;
        chartShow.Series["S1"].YValueMembers = yValueMembersS1;
        chartShow.Series["S2"].YValueMembers = yValueMembersS2;
        chartShow.Series["S3"].YValueMembers = yValueMembersS3;
        chartShow.Series["S4"].YValueMembers = yValueMembersS4;

        chartShow.Series["S1"].YAxisType = AxisType.Primary;
        chartShow.Series["S2"].YAxisType = AxisType.Primary;
        chartShow.Series["S3"].YAxisType = AxisType.Primary;
        chartShow.Series["S4"].YAxisType = AxisType.Primary;

        chartShow.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
        chartShow.Series["S4"].XValueMember = "Date";
    }

但是,当我将上面的代码重构为:

private void ChartSettings(AxisEnabled axisY2Enabled,
                                      bool[] enabledAndIsVisibleInLegend, string[] legendText, string[] yValueMembers,
                                      AxisType[] axisType)
    {
        chartShow.ChartAreas[0].AxisY2.Enabled = axisY2Enabled;

        for (int i = 1; i <= 4; i++ )
        {
            chartShow.Series["S" + i].Enabled = enabledAndIsVisibleInLegend[i - 1];
            chartShow.Series["S" + i].IsVisibleInLegend = enabledAndIsVisibleInLegend[i - 1];
            chartShow.Series["S" + i].LegendText = legendText[i - 1];
            chartShow.Series["S" + i].YAxisType = axisType[i - 1];
            chartShow.Series["S" + i].XValueMember = "Date";
            chartShow.Series["S" + i].YValueMembers = yValueMembers[i - 1];
        }

        chartShow.ChartAreas[0].AxisX.LabelStyle.Interval = interval;
    }

我收到此错误:数据点插入错误。此数据系列只能设置1个Y值。

任何帮助/说明? 谢谢。

修改

调用方法的一个例子:

ChartSettingsForShow(AxisEnabled.False,
                                     false, false, false, true,
                                     false, false, false, true,
                                     "", "", "", legendText,
                                     "", "", "", yValueMembers);

ChartSettingsForShow(AxisEnabled.False,
                                 new bool[] { false, false, false, true },
                                 new string[] { "", "", "", legendText },
                                 new string[] { "", "", "", yValueMembers },
                                 new AxisType[] { AxisType.Primary, AxisType.Primary, AxisType.Primary, AxisType.Primary });

yValueMembers是一个字符串,对应于DataTable中的列名。

0 个答案:

没有答案