基于单列值的两个系列的折线图

时间:2016-11-05 18:40:44

标签: c# .net winforms mschart

我想基于此表构建折线图: table
每个不同的KPIName在图表上都有自己的行 我该怎么做? 我试过这个:

foreach (KPI_PG kpi in _PG.KPI_PG)
{
    Series currentPG = new Series(kpi.KPIName);
    chrt_PGByYear.Series.Add(currentPG);
    currentPG.ChartType = SeriesChartType.Line;
    currentPG.BorderWidth = 3;
    currentPG.XValueType = ChartValueType.String;
    currentPG.XValueMember = "Year";
    currentPG.YValueType = ChartValueType.Int32;
    currentPG.YValueMembers = "Value";
}

但由于图表名称相同而无效。

2 个答案:

答案 0 :(得分:0)

系列名称必须是唯一的。您应该检查系列是否已经存在,如果已经存在,则不要添加它。否则添加系列。

private void createNewSeries(String SeriesName)
{
    if(chrt_PGByYear.Series.IsUniqueName(SeriesName))
    {
        Series currentPG = chrt_PGByYear.Series.Add(SeriesName);
        currentPG.ChartType = SeriesChartType.Line;
        currentPG.BorderWidth = 3;
        currentPG.XValueType = ChartValueType.String;
        currentPG.XValueMember = "Year";
        currentPG.YValueType = ChartValueType.Int32;
        currentPG.YValueMembers = "Value";
    }
}

然后你就可以使用它:

foreach (KPI_PG kpi in _PG.KPI_PG)
{
    createNewSeries(kpi.KPIName);
}

答案 1 :(得分:0)

似乎不可能。我只是使用chrt_PGByYear.Series[serieName].Points.AddXY()方法手动填充此图表。