Hi,
I am trying to create a dynamic stacked column series that binds to the data in view model.
Here is the sample of data I have
var ChartData = new ObservableCollection<CityData>();
public class CityData
{
string CityName;
ObservableCollection<CityInfo> Info;
}
public class CityInfo
string BuildingType
int BuildingCount
I am trying to create a stacked column series with City names on X axis and columns with multiple stacked fragmented series by Building Type and Count
Hello Priyanka,
I have been investigating into the behavior you are looking to achieve in this case, and I would like you to take a look at the sample project I am attaching.
The sample project’s XamDataChart is using the “UpdatedViewModel,” as the “ViewModel” was used for an attempt to use your data structure for this, but this will not work. Your data structure appears hierarchical in nature, and there is no easy way for the StackedColumnSeries to work with this.
Instead, I would suggest that you format your data structure into a DataTable. Your columns in the DataTable in this case would be CityName and then as many BuildingTypes that you need. The values of the rows would then be the name of the city and the value of BuildingCount for the BuildingType. This would allow you to dynamically add as many BuildingTypes that you would need as well as many cities that you would need to plot. The sample project I am attaching does this and allows you to add new cities and building types dynamically.
I hope this helps you. Please let me know if you have any other questions or concerns on this matter.
XamDataChartDynamicStackedSeries.zip
Hi Andrew,
How can I modify the code to auto-generate StackedFragmentSeries and use DataTable with hierarchial data
I would like you to take a look at the modified sample project I am attaching in this case.
In order to utilize the “auto-generate” functionality of the StackedColumnSeries, you will need to utilize a different type of data source in this case, called a GroupBy. This is required in order to use the StackedColumnSeries with auto-generate.
The StackedColumnSeries will also not work with auto-generate if you are using hierarchical data or a DataTable, and being that you are looking to have backend updates happening easily, I have made some modifications to the data structure in this case as well. The sample project is back to utilizing an ObservableCollection in a group-by structure that is feeding the GroupBy data source, which will make updates on the back-end simpler as long as you are implementing the INotifyPropertyChanged interface on the underlying data item.
I am attaching the sample project here. Please let me know if you have any other questions or concerns on this matter.
7802.XamDataChartDynamicStackedSeries.zip
Currently the data structure I like to use looks like this -
DataStructure - ObservableCollection<DataType1>
DataType1 = {DataType2, ObservableCollection<DataType3>}
DataType2 = {string21, string22}
DataType3 = {string31, string32, int33}
The data I am trying to plot on my stacked column series is (string21) on X-axis
For each stacked column, I am trying to plot string31 and int33.
Using the above data structure, how can I set GroupBy MemberPaths to properties that are nested inside my data structure so I can plot them as desired.
I apologize, but this data structure will not work with a GroupBy collection, and it likely would not work well for the StackedColumnSeries as that series type does not like hierarchical data. As such, the best thing I can recommend for you in this case is to flatten that data structure out such that it is not hierarchical.
The GroupBy data source expects a data source that makes sense that it would be grouped. For example, if you were to print out the DataSource ObservableCollection in the most recent sample project I sent you in a JSON sort of format, you would see the following:
City: Lansing, Type: BuildingType1, Count: 19 City: Lansing, Type: BuildingType2, Count: 16 City: Lansing, Type: BuildingType3, Count: 10 City: Seattle, Type: BuildingType1, Count: 18 City: Seattle, Type: BuildingType2, Count: 11 City: Seattle, Type: BuildingType3, Count: 10 City: Los Angeles, Type: BuildingType1, Count: 15 City: Los Angeles, Type: BuildingType2, Count: 11 City: Los Angeles, Type: BuildingType3, Count: 10 City: Las Vegas, Type: BuildingType1, Count: 17 City: Las Vegas, Type: BuildingType2, Count: 12 City: Las Vegas, Type: BuildingType3, Count: 16
Note that there are three data items per each city, each with a different building type. This is what the GroupBy data source reads. Essentially, it takes this data and groups by “City.” It then creates stacks for each of the "BuildingTypes" of the size denoted by "Count".
With the data structure you have provided, the property paths are impossible, as you would not be able to point at a property on DataType3 in this case when the group by would need information about DataType2 as well as using your “DataStructure” collection as its ItemsSource. You will need to flatten out your data structure for use with the StackedColumnSeries like is done in the most recent sample project I sent you.
Please let me know if you have any other questions or concerns on this matter.