|
1 | | -# How-to-change-the-individual-appearance-of-the-WPF-Bar-Chart-SfChart- |
2 | | -How to change the individual appearance of the WPF Bar Chart (SfChart)) |
| 1 | +# How to change the individual appearance of the WPF Bar Chart (SfChart)? |
| 2 | + |
| 3 | +This article describes how to change the individual appearance(color) of bar segment based on the corresponding YValues in WPF SfChart by following these steps: |
| 4 | + |
| 5 | +**Step 1:** To change color for specific data point in chart you can refer this [KB article](https://www.syncfusion.com/kb/10928/how-to-change-colors-of-specific-data-points-in-the-chart). |
| 6 | + |
| 7 | +**Step 2:** Also you can customize the interior color of the particular segment based on the respective Y value by using the converter in the [CustomTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ColumnSeries.html#Syncfusion_UI_Xaml_Charts_ColumnSeries_CustomTemplate) property of [ColumnSeries](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Charts.ColumnSeries.html) as demonstrated in the following code example. The detailed explanation of this appearance customization can be found in this user documentation [page](https://help.syncfusion.com/wpf/charts/seriestypes/custom). |
| 8 | + |
| 9 | +**[XAML]** |
| 10 | +``` |
| 11 | +<Grid> |
| 12 | + <Grid.DataContext> |
| 13 | + <local:ViewModel/> |
| 14 | + </Grid.DataContext> |
| 15 | +
|
| 16 | + <Grid.Resources> |
| 17 | + <local:InteriorConverter x:Key="interiorConverter"/> |
| 18 | + </Grid.Resources> |
| 19 | +
|
| 20 | + <chart:SfChart Margin="10"> |
| 21 | +
|
| 22 | + <chart:SfChart.PrimaryAxis> |
| 23 | + <chart:CategoryAxis LabelFormat="yyyy"></chart:CategoryAxis> |
| 24 | + </chart:SfChart.PrimaryAxis> |
| 25 | +
|
| 26 | + <chart:SfChart.SecondaryAxis> |
| 27 | + <chart:NumericalAxis></chart:NumericalAxis> |
| 28 | + </chart:SfChart.SecondaryAxis> |
| 29 | +
|
| 30 | + <chart:ColumnSeries XBindingPath="Year" YBindingPath="India" ItemsSource="{Binding DataPoints}"> |
| 31 | + <chart:ColumnSeries.CustomTemplate> |
| 32 | + <DataTemplate> |
| 33 | + <Canvas> |
| 34 | + <Rectangle Fill="{Binding Converter={StaticResource interiorConverter}}" Height="{Binding Height}" Width="{Binding Width}" Canvas.Left="{Binding RectX}" Canvas.Top="{Binding RectY}"></Rectangle> |
| 35 | + </Canvas> |
| 36 | + </DataTemplate> |
| 37 | + </chart:ColumnSeries.CustomTemplate> |
| 38 | + <chart:ColumnSeries.AdornmentsInfo> |
| 39 | + <chart:ChartAdornmentInfo AdornmentsPosition="TopAndBottom" LabelPosition="Center" SegmentLabelContent="LabelContentPath" ShowLabel="True" > |
| 40 | + <chart:ChartAdornmentInfo.LabelTemplate> |
| 41 | + <DataTemplate> |
| 42 | + <StackPanel Orientation="Horizontal"> |
| 43 | + <Label Content="{Binding Item.India}" Foreground="White" FontSize="11"></Label> |
| 44 | + </StackPanel> |
| 45 | + </DataTemplate> |
| 46 | + </chart:ChartAdornmentInfo.LabelTemplate> |
| 47 | + </chart:ChartAdornmentInfo> |
| 48 | + </chart:ColumnSeries.AdornmentsInfo> |
| 49 | + </chart:ColumnSeries> |
| 50 | +
|
| 51 | + </chart:SfChart> |
| 52 | +</Grid> |
| 53 | +``` |
| 54 | + |
| 55 | +**[C#]** |
| 56 | +``` |
| 57 | +public class InteriorConverter : IValueConverter |
| 58 | +{ |
| 59 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 60 | + { |
| 61 | + var ydata = (value as ColumnSegment).YData; |
| 62 | + Brush interior; |
| 63 | +
|
| 64 | + interior = ydata > 0 ? |
| 65 | + new SolidColorBrush(Colors.Green) : |
| 66 | + new SolidColorBrush(Colors.Red); |
| 67 | +
|
| 68 | + return interior; |
| 69 | + } |
| 70 | +
|
| 71 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 72 | + { |
| 73 | + throw new NotImplementedException(); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +**View Model** |
| 79 | +``` |
| 80 | +public class ViewModel |
| 81 | +{ |
| 82 | + public ViewModel() |
| 83 | + { |
| 84 | + this.DataPoints = new ObservableCollection<Model>(); |
| 85 | + DateTime year = new DateTime(2005, 5, 1); |
| 86 | +
|
| 87 | + DataPoints.Add(new Model() { Year = year.AddYears(1), India = 28, Germany = 31, England = 36, France = 39 }); |
| 88 | + DataPoints.Add(new Model() { Year = year.AddYears(2), India = 25, Germany = 28, England = 32, France = 36 }); |
| 89 | + DataPoints.Add(new Model() { Year = year.AddYears(3), India = 26, Germany = 30, England = 34, France = 40 }); |
| 90 | + DataPoints.Add(new Model() { Year = year.AddYears(4), India = -27, Germany = 36, England = 41, France = 44 }); |
| 91 | + DataPoints.Add(new Model() { Year = year.AddYears(5), India = -32, Germany = 36, England = 42, France = 45 }); |
| 92 | + DataPoints.Add(new Model() { Year = year.AddYears(6), India = 35, Germany = 39, England = 42, France = 48 }); |
| 93 | + DataPoints.Add(new Model() { Year = year.AddYears(7), India = -30, Germany = 38, England = 43, France = 46 }); |
| 94 | + } |
| 95 | +
|
| 96 | + public ObservableCollection<Model> DataPoints { get; set; } |
| 97 | +} |
| 98 | +
|
| 99 | +public class Model |
| 100 | +{ |
| 101 | + public DateTime Year { get; set; } |
| 102 | + public double India { get; set; } |
| 103 | + public double Germany { get; set; } |
| 104 | + public double England { get; set; } |
| 105 | + public double France { get; set; } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Output: |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +## See also: |
| 114 | + |
| 115 | +[Series customization in WPF SfChart](https://help.syncfusion.com/wpf/charts/seriescustomization) |
| 116 | + |
| 117 | +[How-to-customize-the-chart-series-in-WPF-SfChart?](https://github.com/SyncfusionExamples/how-to-customize-the-chart-series-in-wpf-sfchart) |
0 commit comments