【问题标题】:Create a stacked bar chart创建堆积条形图
【发布时间】:2011-07-10 23:30:53
【问题描述】:

我正在尝试创建堆积条形图, 但是我在具有“.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";

【问题讨论】:

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


    【解决方案1】:

    上面的源代码似乎来自 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 web 解决方案中,查看

    /ChartTypes/BarColumnCharts/Stacked/stackedchart.aspx
    

    它应该有你需要的一切。

    【讨论】: