开发者

Microsoft Chart stacked columns grouped with non-stacked

开发者 https://www.devze.com 2023-03-26 08:28 出处:网络
I was hoping to display data for 4 groups.Each group has 1 stacked column, and 1 non-stacked.Her开发者_如何学编程e\'s a mockup of what I\'m after:

I was hoping to display data for 4 groups. Each group has 1 stacked column, and 1 non-stacked. Her开发者_如何学编程e's a mockup of what I'm after:

Microsoft Chart stacked columns grouped with non-stacked

Getting four groups is easy, this gives me what I want:

Series series = chart.Series.Add("Budget");
series.ChartType = SeriesChartType.Column;
series.Name = "Budget";
series.Points.Add(55);
series.Points.Add(10);
series.Points.Add(50);
series.Points.Add(50);

series  = chart.Series.Add("Actual");
series.ChartType = SeriesChartType.Column;
series.Name = "Actual";
series.Points.Add(80);
series.Points.Add(90);
series.Points.Add(10);
series.Points.Add(10);

Microsoft Chart stacked columns grouped with non-stacked

Now I want to make the yellow bars stacked bars. I've experimened with adding 3 series; ChartType = Column, StackedColumn, StackedColumn. But the stacked columns appear infront of the non-stacked one.

Microsoft Chart stacked columns grouped with non-stacked

Is what I'm attempting possible?


Under the Series Invoke the CustomPropertiesMethod and assign the groups different StackedGroupNames for example if you want Series 1 and 2 to stack but want series 3 to be beside

Series1.CustomProperties = "StackedGroupName=Group1"; Series2.CustomProperties = "StackedGroupName=Group1"; Series3.CustomProperties = "StackedGroupName=Group2";


I may be wrong but the series are shown in the same order as they're added.


The way I approached it was to phony up the X-axis data so it was offset on either side of the 'real' points, then use custom labels. I don't have easily pasted. tested code, but I think it goes something like this:

double pointWidth = 0.375;
double pointOffset = pointWidth * 0.5;
// Add first series - column
_Chart.Add("Series1");
_Chart.Series["Series1"].ChartType = SeriesChartType.Column;
_Chart.Series["Series1"]["PointWidth"] = pointWidth.ToString();
for (int ii = 0; ii < 10; ii++)
{
  _Chart.Series["Series1"].Points.AddXY(ii - pointOffset, YourYValueHere);
}
// Add second series - stacked column
_Chart.Add("Series2");
_Chart.Series["Series2"].ChartType = SeriesChartType.Column;
_Chart.Series["Series2"]["PointWidth"] = pointWidth.ToString();
for (int ii = 0; ii < 10; ii++)
{
  _Chart.Series["Series2"].Points.AddXY(ii + pointOffset, YourYValueHere);
}
// Add thrid series - stacked column
_Chart.Add("Series3");
_Chart.Series["Series3"].ChartType = SeriesChartType.Column;
_Chart.Series["Series3"]["PointWidth"] = pointWidth.ToString();
for (int ii = 0; ii < 10; ii++)
{
  _Chart.Series["Series3"].Points.AddXY(ii + pointOffset, YourYValueHere);
    _Chart.ChartAreas["Area1"].AxisX.CustomLabels.Add(
     ii - 0.5, ii + 0.5, ii);
}
_Chart.ChartAreas["Area1"].AxisX.Minimum = -0.5;
_Chart.ChartAreas["Area1"].AxisX.Maximum =
              _Chart.Series["Series3"].Points.Count - 0.5;
_Chart.ChartAreas["Area1"].AxisX.LabelStyle.IsEndLabelVisible = true;
_Chart.ChartAreas["Area1"].AxisX.IsMarginVisible = false;

If that StackedGroupName thing works, it would be much simpler, though. I'm going to try it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号