TrackBallTemplate. Get Stroke and Fill from corresponding line

1 Answer 22 Views
ChartView
Aleksej
Top achievements
Rank 1
Aleksej asked on 15 Dec 2021, 12:56 PM

I am changing TrackBallTemplate and I want to get the following result:

I changed the palette to a custom palette using PaletteEntryCollection SeriesFamily = "Line" and ChartPalette.GlobalEntries

I am using ChartSeriesProvider and style TargetType = "telerik: LineSeries"

But from the DataContext of the TrackBallTemplate, I cannot find the brushes...

StrokeShapeStyle returns not my color: #FF5AA4D4
LegendItem is not available

 

How do I get the line brushes I need?
Thank you

 

------------------
WPF v.2021.1.325

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 16 Dec 2021, 10:03 AM

Hello Aleksej,

The series and their data points usually don't hold a reference to the used color unless the associated Stroke, Fill or StrokeShapeStyle property is set. When using a palette, you can get the series color (the line brush in your case) using the GetEntry() method of the ChartPalette object.

For your scenario, you can use an IValueConverter in the TrackBallTemplate. The data context passed to the template is an object of type DataPointInfo which you can use to get the chart and the series which can be used in the converter to get the color. For example:

<!-- this is placed on an element in the TrackBallTemplate -->
Background="{Binding DataPoint, Converter={StaticResource MyDataPointToPaletteBrushConverter}}" 

<!-- this is placed in the IValueConverter -->
var dataPoint = (DataPoint)value;
var series = (CartesianSeries)dataPoint.Presenter;
var palette = series.Chart.Palette;
int index = chart.Series.IndexOf(series); 
PaletteEntry? paletteEntry = this.chart.Palette.GetEntry(series, index); 
return paletteEntry.Stroke; // here you may need to return a different property of the PaletteEntry object, based on the used chart series. For example, you can use paletteEntry.Fill

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Aleksej
Top achievements
Rank 1
commented on 16 Dec 2021, 01:09 PM

thanks, what is chart?

<telerik: RadCartesianChart x:Name = "chart">


I can't get the Series collection in any way using only the value from the converter?

Unfortunately the ChartAreaModel class is internal. I haven’t found other ways yet...

Aleksej
Top achievements
Rank 1
commented on 16 Dec 2021, 01:36 PM

I did it!

<Path Stroke="{Binding Converter={StaticResource DataPointGetBrushConverter}}" ... />

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DataPointInfo dataPointInf)
            {
                var series = (CategoricalStrokedSeries)dataPointInf.DataPoint.Presenter;
                if (series.Chart is RadCartesianChart chart)
                {
                    var palette = chart.Palette;
                    var index = chart.Series.IndexOf(series);
                    PaletteEntry? paletteEntry = palette.GetEntry(dataPointInf.Series, index);
                    if (paletteEntry != null)
                        return paletteEntry?.Stroke;
                }
            }
            return (SolidColorBrush)new BrushConverter().ConvertFrom("#ADADAD");
        }

Is this the correct way?
Martin Ivanov
Telerik team
commented on 16 Dec 2021, 02:34 PM

My mistake. I forgot to add series.Chart and instead typed only chart in the following part of the code:

int index = chart.Series.IndexOf(series); 

It should be:

int index = series.Chart.Series.IndexOf(series); 

Anyway, I can see that you already found the solution. Great job.

Tags
ChartView
Asked by
Aleksej
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or