Hello,
is it possible to change the color of the bars and the title of the legend when the chart is already created and displays chart data?
Currently I use the "SeriesCreated" event to do this, but need to change these properties when the user sets new data.
private void StackedColumnSeries_SeriesCreated(object sender, StackedSeriesCreatedEventArgs e) { if (_classInfos == null) return; StackedFragmentSeries series = sender as StackedFragmentSeries; Brush columnBrush = Brushes.Transparent; Brush outlineBrush = Brushes.Gray; string title = lang.Default_Label_NotAvailable; //Info: The series.ValueMemberPath contains the Group Number (ClassId) + _ as prefix. e.g. "0_Value" string[] vals = series.ValueMemberPath.Split('_'); if (vals.Length > 1) { Guid classId = Guid.Empty; ObjectClassificationConfig classInfo; Guid.TryParse(vals[0], out classId); _classInfos.ContainsKey(classId); if (_classInfos.TryGetValue(classId, out classInfo)) { columnBrush = new SolidColorBrush(classInfo.Color); outlineBrush = new SolidColorBrush(ImageUtils.MakeDarker(classInfo.Color, 60)); title = classInfo.Label; } } e.Brush = columnBrush; e.Outline = outlineBrush; e.Title = title; }
When the user sets new data you will need to iterate through the StackedBarSeries.Series collection and set the properties.
How to access the StackedFragmentSeries object within the StackedColumnSeries, in order I can set the required properties?
Hello Markus,
The chart's series, which is of type StackedBarSeries or StackedColumnSeries will contain it's its own Series collection.
eg.
StackedFragmentSeries seriesFragment = new StackedFragmentSeries();seriesFragment.ValueMemberPath = "Coal";seriesFragment.Title = "Coal";...StackedBarSeries series = new StackedBarSeries();series.ItemsSource = dataSample;series.XAxis = xAxis;series.YAxis = yAxis;// add all stack fragments to the seriesseries.Series.Add(seriesFragment); //==> iterate this series collection...this.DataChart.Series.Add(series);
Thank! I use AutoGenerateSeries="True". When iterating through these series.Series, it is empty. Can I access that collection when it is auto generated?
If the chart is rendered, the series should be fulfilled. Perhaps you are doing this before load? When would the user change the data?
It is triggered by an event. The user can select from a combobox which data he wants to display. When the data has changed, an event is fired to signal the chart, it should reload different colors and labels for the columns and legend.
I just tried to create the stacked fragmenst in code, but that doesn't work either. The fragments are created, but the chart remains emtpy.
The legend is show properly, but the chart remains empty. Do I miss something?
private void ReloadSeriesProperties() { var series = dataChart.Series[0] as StackedColumnSeries; series.Series.Clear(); foreach (var classInfo in _classInfos.Values) { StackedFragmentSeries fragment = new StackedFragmentSeries(); fragment.ValueMemberPath = nameof(CategoryValueGroupItem<Guid>.Value); fragment.Brush = new SolidColorBrush(classInfo.Color); fragment.Outline = new SolidColorBrush(ImageUtils.MakeDarker(classInfo.Color, 60)); fragment.Title = classInfo.Label; series.Series.Add(fragment); } }
<ig:XamDataChart.Series> <ig:StackedColumnSeries ItemsSource="{Binding ChartItemsGroup}" XAxis="{Binding ElementName=xAxis}" YAxis="{Binding ElementName=yAxis}" Legend="{Binding ElementName=Legend}"> <ig:StackedColumnSeries.ToolTip> <Grid> <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource tooltipTemplate}"/> </Grid> </ig:StackedColumnSeries.ToolTip> </ig:StackedColumnSeries> </ig:XamDataChart.Series>
Sorry for any confusion. I meant that you can use the series.Series collection to obtain your fragments and set their brushes. You shouldn't need to create manually everytime the data change, especially when AutoGenerateSeries is true.
Hello Michael,
unfortunattely, the fragment series is always empty, so I cannot reuse. Nevertheless, re-creating the fragments is now working for me.
The ValueMameberPath in your example was wrong. For stacked series that path must be combined from the KeyValue and the property name of the Value, with an underscore. In my example: 00000-00000-00000-00000000000000_Value