创建堆积条形图

时间:2011-07-10 23:30:54

标签: c# .net graphics charts bar-chart

我正在尝试创建堆积条形图, 但是我在“.Series”(如何定义系列?)

的行中出现错误
    SeriesChartType chart1 = new SeriesChartType();

    // Populate series data
    Random random = new Random();

    for (int pointIndex = 0; pointIndex < 10; pointIndex++)
    {
        chart1.Series["LightBlue"].Points.AddY(random.Next(45, 95));
    }

    // Set chart type
    chart1.Series["LightBlue"].ChartType = SeriesChartType.StackedArea100;

    // Show point labels
    chart1.Series["LightBlue"].IsValueShownAsLabel = true;

    // Disable X axis margin
    chart1.ChartAreas["Default"].AxisX.IsMarginVisible = false;

    // Set the first two series to be grouped into Group1
    chart1.Series["LightBlue"]["StackedGroupName"] = "Group1";
    chart1.Series["Gold"]["StackedGroupName"] = "Group1";

    // Set the last two series to be grouped into Group2
    chart1.Series["Red"]["StackedGroupName"] = "Group2";
    chart1.Series["DarkBlue"]["StackedGroupName"] = "Group2";

1 个答案:

答案 0 :(得分:2)

上面的源代码似乎来自MS Chart Samples应用程序。查看屏幕上的MS示例堆积条形图和上面的源代码,很明显示例代码不合适,并没有告诉我们如何进行堆积条形图。

您可以以编程方式创建和附加系列:

Series s1 = new Series("LightBlue");
s1.ChartType = SeriesChartType.StackedBar100;
chart1.Series.Add(s1);

或者,您可以在ASPX文件中定义系列,并在后面的代码中为每个系列添加简单的Y值:

Random  random = new Random();
for(int pointIndex = 0; pointIndex < 10; pointIndex++)
{
    Chart1.Series["Series1"].Points.AddY(Math.Round((double)random.Next(45, 95),0));
    Chart1.Series["Series2"].Points.AddY(Math.Round((double)random.Next(5, 75),0));
    Chart1.Series["Series3"].Points.AddY(Math.Round((double)random.Next(5, 95),0));
    Chart1.Series["Series4"].Points.AddY(Math.Round((double)random.Next(35, 95),0));
}

在MS Chart Samples网络解决方案中,请查看

/ChartTypes/BarColumnCharts/Stacked/stackedchart.aspx

它应该拥有你所需要的一切。