Styling datapoints in a ScatterLineSeries at runtime

2 Answers 141 Views
Chart
Deepak
Top achievements
Rank 1
Deepak asked on 26 May 2021, 05:03 PM | edited on 02 Jun 2021, 04:05 AM

I am trying to display the data points in a ScatterLineSeries when the user clicks a button using the following code:

private void ShowDataPointsButton_Click(object sender, RoutedEventArgs e)
{
    foreach (var series in CartesianChart.Series)
    {
        if (series is ScatterLineSeries)
        {
            series.DefaultVisualStyle = Application.Current.Resources["DataPointStyle"] as Style;
        }
    }
}

(where `CartesianChart` is a `RadCartesianChart` control)

The style definition is as follows:

<Style x:Key="DataPointStyle" TargetType="{x:Type Path}">
    <Setter Property="Height" Value="5" />
    <Setter Property="Width" Value="5" />
</Style>

(I am using a palette to style the chart; hence the missing definition for `Fill`)

But for some reason, I have to follow these steps in order to see the data points styled:

  1. Press the `ShowDataPointsButton` button
  2. Zoom into the chart
  3. Press the `ShowDataPointsButton` button
  4. Press the `ShowDataPointsButton` button again

I have tried the following options to fix this issue:

  1. Remove the series from the chart -> Apply the style -> Add the series back into the chart
  2. Apply the style -> Force a redraw manipulating the zoom level on the chart
  3. Using the dispatcher associated with the series to force a redraw
  4. Using Application.Current.Dispatcher to apply the style with Render priority
  5. Binding a property to `DefaultVisualStyleProperty` and setting its value when user clicks the button

But in all these instances (except option 5 where the trial with binding did not work at all), I have to follow the steps mentioned up above to see the data points in the chart.

Could you please guide me on what is the right way to apply `DefaultVisualStyle` for a ScatterLineSeries in runtime from C# code-behind?

2 Answers, 1 is accepted

Sort by
1
Accepted
Vladimir Stoyanov
Telerik team
answered on 02 Jun 2021, 11:56 AM

Hello Deepak,

Thank you for the updated project. 

What I can suggest in the described scenario is to utilize the PointTemplate of the series. I am attaching the sample project back updated to demonstrate this approach. 

Alternatively, you can also consider statically declaring the series in xaml without any data points. Then you can set their ItemsSource when the series need to be displayed.

I hope you find this helpful. Let me know, if I can be of any further assistance. 

Regards,
Vladimir Stoyanov
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.

Deepak
Top achievements
Rank 1
commented on 02 Jun 2021, 04:54 PM

Thank you for sharing the new code Vladimir. Your suggestion with `PointTemplate` worked.
0
Vladimir Stoyanov
Telerik team
answered on 31 May 2021, 10:34 AM

Hello Deepak ,

Thank you for the shared code snippets. 

Generally speaking, removing and readding the series should be sufficient in order to redraw the visuals after applying a DefaultVisualStyle. I tested this in a small sample project on my end and I am attaching it here for your reference. 

Please, check it out and let me know, if it helps.

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Deepak
Top achievements
Rank 1
commented on 02 Jun 2021, 04:41 AM

Hi Vladimir,

Thanks for sharing the sample code. Using the sample you shared, I was able to recreate the issue I faced by introducing a small change (Please refer my original post - I have attached a modified version of your sample code for reference).

I noticed that you had already declared a `ScatterLineSeries` element named `series` in XAML file and its `ItemsSource` had been bound to `Data` property from the view-model.

This is what is different in my original code. In my code, the `ScatterLineSeries` elements are added into the chart through the constructor in C# code-behind file:

```csharp
public partial class GraphContainer : UserControl
{
public GraphContainer(IEnumerable<CartesianSeries> seriesList) : this()
{
foreach (var series in seriesList)
{
CartesianChart.Series.Add(series);
}
}
}
```
where `GraphContainer` is a user control that defines layout containing the `RadCartesianChart`:

```xaml
<UserControl x:Class="GraphContainer" ... >
<Grid>
<telerik:RadCartesianChart>
....
</telerik:RadCartesianChart>
</Grid>
</UserControl>
```

I have to pass in a list of series elements through constructor into this user control because the user can select one or more items to be plotted in the same chart through a different UI.

So from what I understand, the root cause boils down to two fundamental changes in my version of the code:
1. The `CartesianSeries` elements are being added to the chart through the constructor after all XAML elements are initialized (instead of them being a part of the XAML decleration for `RadCartesianChart`)
2. The `ItemsSource` for these `CartesianSeries` elements are not using binding; Instead an `IEnumerable<T>` is being directly set on this property

I tried binding the data to `ItemsSource` property of the `CartesianSeries`, but that did not solve the issue. (Please refer the modified version of your sample code I have attached along with my original post). So that leads me to think that I cannot solve the issue unless I statically add `CartesianSeries` elements in the XAML code.

Could you please guide me on how I can solve this issue?
Tags
Chart
Asked by
Deepak
Top achievements
Rank 1
Answers by
Vladimir Stoyanov
Telerik team
Share this question
or