Q: I have data in 8 categories and 3 distinct values for each. I want to show these categories in a stacked chart with the name of each category as a label but don’t know how to do it.
A: What you want to show can be done by adding 3 BarSeries to the bar chart control. Then you can bind the XDataPath or YDataPath properties of each series to the appropriate field in your data source. Set the type of the bar to stacked.
Here is how to build a stacked column chart:
//the first series is predefined BarSeries series1 = barChart1.Series[0] as BarSeries; series1.YDataPath = "EuropeSales"; series1.XData = indexes; series1.BarType = BarType.VerticalStack; BarSeries series2 = new BarSeries(); series2.YDataPath = "AsiaSales"; series2.XData = indexes; barChart1.Series.Add(series2); series2.BarType = BarType.VerticalStack; BarSeries series3 = new BarSeries(); series3.YDataPath = "USASales"; series3.XData = indexes; barChart1.Series.Add(series3); series3.BarType = BarType.VerticalStack;
Here is a sample list with the data used for binding the chart:
var salesList = new List() { new Sales(){Category="apples", EuropeSales=34, AsiaSales=12, USASales=24}, new Sales(){Category="oranges", EuropeSales=23, AsiaSales=17, USASales=10}, new Sales(){Category="bananas", EuropeSales=4, AsiaSales=31, USASales=27}, new Sales(){Category="cherries", EuropeSales=8, AsiaSales=9, USASales=30} };
We assign the data source and set the type of labels for the X-axis as custom text. The XLabelPath property sets the name of the field that provides the custom labels:
barChart1.DataSource = salesList; barChart1.XAxisSettings.LabelType = LabelType.CustomText; barChart1.XLabelPath = "Category";