This is a migrated thread and some comments may be shown as answers.

Color for CategoricalDataPoint

7 Answers 270 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 15 Dec 2014, 04:27 AM
I have RadCartesianChart with 10 CategoricalDataPoints. How can i set color for each CategoricalDataPoint programmatically?

7 Answers, 1 is accepted

Sort by
1
Martin Ivanov
Telerik team
answered on 15 Dec 2014, 01:06 PM
Hi Igor,

Let me start with a short explanation on that how the chart's data points are visualized. Most chart series (except the line series) have a default visual elements that represents its data points. This element can be replaced through the PointTemplate of the series, which means that there is no guarantee that the point will be always visualized with the series' default visual. 

The color of the data points cannot be set through the (Categorical)DataPoint model. One of the reasons is that if there is a custom PointTemplate the model won't know what property this elements use for setting its color and if there is such property. 

To customize the color of the data points you can define a DefaultVisualStyle (or PointTemplate) and bind a Brush property from your view model to the color property of the visual element. 

<telerik:BarSeries.DefaultVisualStyle>
    <Style TargetType="Border">
        <Setter Property="Background" Value="{Binding DataItem.PointBrush}" />
    </Style>
</telerik:BarSeries.DefaultVisualStyle>
Where the DataItem is a property of the DataPoint object that holds a reference to the view model behind the point. You can take a look at the Customizing CatesianChart Series help article to see how to define DefaultVisualStyle or PointTemplate.

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Igor
Top achievements
Rank 1
answered on 16 Dec 2014, 06:43 AM
Thanks for information. I am afraid, this method does not suit me.
Can you help me in my case?
I have following chart (custom control)
<telerik:RadCartesianChart Name="cartesianChart">
      <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:CategoricalAxis/>
      </telerik:RadCartesianChart.HorizontalAxis>
      <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis />
      </telerik:RadCartesianChart.VerticalAxis>
      <telerik:RadCartesianChart.Series>
                <telerik:BarSeries x:Name="barchart" >
                    <telerik:BarSeries.DataPoints>
            <telerik:CategoricalDataPoint  x:Name="CategoricalDataPoint1" />
            <telerik:CategoricalDataPoint  x:Name="CategoricalDataPoint2" />
            <telerik:CategoricalDataPoint  x:Name="CategoricalDataPoint3" />
            <telerik:CategoricalDataPoint  x:Name="CategoricalDataPoint4" />
             </telerik:BarSeries.DataPoints>
        </telerik:BarSeries>
      </telerik:RadCartesianChart.Series>
    </telerik:RadCartesianChart>

I make binding in xaml for each datapoint - i'm not using collection for binding
<Chart    
Prop0="{Binding Path=SelectedItem[column0], ElementName=GridView,  UpdateSourceTrigger=PropertyChanged}"
Prop1="{Binding Path=SelectedItem[column1], ElementName=GridView,  UpdateSourceTrigger=PropertyChanged}"
Prop2="{Binding Path=SelectedItem[column2], ElementName=GridView,  UpdateSourceTrigger=PropertyChanged}"
Prop3="{Binding Path=SelectedItem[column3], ElementName=GridView,  UpdateSourceTrigger=PropertyChanged}"
/>

CodeBehind:
public static void OnProp0PropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    (dependencyObject as Chart).CategoricalDataPoint0.Value = (double)e.NewValue;
}
0
Martin Ivanov
Telerik team
answered on 18 Dec 2014, 01:18 PM
Hello Igor,

Another approach for setting a different color for each point is to use a binding with an IValueConverter in the DefaultVisualStyle. For example, inside the converter you can check the value of the data point and if it falls in a specific range set the corresponding color.

Here is an example in code for such implementation:
<telerik:BarSeries.DefaultVisualStyle>
    <Style TargetType="Border">
        <Setter Property="Background" Value="{Binding Converter={StaticResource dataPointToBrushConverter}}" />    
    </Style>
</telerik:BarSeries.DefaultVisualStyle>
.
public class DataPointToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var point = value as CategoricalDataPoint;
        if(point.Value >= 10 && point.Value < 20)
        {
            return Brushes.Green;
        }
        else
        {
            return Brushes.Red;
        }
    }
    .....
}

Please let me know if this approach works for you.

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
brian
Top achievements
Rank 1
answered on 20 Dec 2016, 06:15 PM

I know this is an old post, but could someone assist me in converting this to vb.net? I can' seem to make it work.

 

Thanks

 

0
Martin Ivanov
Telerik team
answered on 22 Dec 2016, 10:16 AM
Hi Brian,

You can try this:
<Grid>
    <Grid.Resources>
        <local:DataPointToBrushConverter x:Key="dataPointToBrushConverter" />
    </Grid.Resources>
    <telerik:RadCartesianChart>
        <telerik:RadCartesianChart.VerticalAxis>
            <telerik:LinearAxis />
        </telerik:RadCartesianChart.VerticalAxis>
        <telerik:RadCartesianChart.HorizontalAxis>
            <telerik:CategoricalAxis />
        </telerik:RadCartesianChart.HorizontalAxis>
        <telerik:RadCartesianChart.Series>
            <telerik:BarSeries CombineMode="None">
                <telerik:BarSeries.DefaultVisualStyle>
                    <Style TargetType="Border">
                        <Setter Property="Background" Value="{Binding Converter={StaticResource dataPointToBrushConverter}}" />
                    </Style>
                </telerik:BarSeries.DefaultVisualStyle>
                <telerik:BarSeries.DataPoints>
                    <telerik:CategoricalDataPoint Category="1" Value="33" />
                    <telerik:CategoricalDataPoint Category="2" Value="15" />
                    <telerik:CategoricalDataPoint Category="3" Value="6" />
                </telerik:BarSeries.DataPoints>
            </telerik:BarSeries>              
        </telerik:RadCartesianChart.Series>           
    </telerik:RadCartesianChart>
</Grid>

Public Class DataPointToBrushConverter
    Implements IValueConverter
 
    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim point = TryCast(value, CategoricalDataPoint)
        If point.Value >= 10 AndAlso point.Value < 20 Then
            Return Brushes.Green
        Else
            Return Brushes.Red
        End If
    End Function
 
    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

Regards,
Martin
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Trump
Top achievements
Rank 1
answered on 16 Nov 2017, 07:14 PM

Hello,

I use the same method and converter. The different thing is that I added ShowLabel=True in the BarSeries. I want to show the values on the bars. However it doesn't show the numbers. It shows the object name.

Telerik.Charting.CategoricalDataPoint

So what is wrong?

0
Martin Ivanov
Telerik team
answered on 21 Nov 2017, 03:36 PM
Hello Trump,

Without your setup I cannot be sure what is going on, but my guess here is that there is a custom LabelDefinition added in the series LabelDefinitions collection. If this is your case you may need to set the Binding or the Template property of the ChartSeriesLabelDefinition. If this doesn't help I would ask you to post some runnable code snippets demonstrating the described behavior and I will check what could be reason behind this.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
ChartView
Asked by
Igor
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Igor
Top achievements
Rank 1
brian
Top achievements
Rank 1
Trump
Top achievements
Rank 1
Share this question
or