Unplanned
Last Updated: 02 Jul 2021 08:26 by ADMIN
Nicholas
Created on: 20 Feb 2020 13:21
Category: Chart
Type: Bug Report
1
Chart: [iOS] Any customizations made in the custom renderer are reset when Chart series are updated.
The Chart loses all the UI customizations made in the iOS platform renderer when the Series are updated, for example, if you show/hide certain Series or updated their ItemsSource.
1 comment
ADMIN
Didi
Posted on: 02 Jul 2021 08:26

Hello,

In order to workaround this behavior you will need to do the following: 

1. create a custom chart class that inherits from RadCartesianChart for example MyCustomChart. Then implement an event inside the MyCustomChart class.

 

public class MyCustomChart : RadCartesianChart
{
    public event ValueChangedEventHandler SeriesUpdated;

    public delegate void ValueChangedEventHandler(object sender, EventArgs e);

    public void CallSeriesUpdate()
    {
        this.SeriesUpdated(this, EventArgs.Empty);
    }
}

 

2. Raise the event when there is a data update.
For example in the method where adding new data points to the chart series.

Device.BeginInvokeOnMainThread(() =>
                    {
                        viewModel.AddPoint();

                        // call series update
                        chart.CallSeriesUpdate();
                    });

 

3. In the renderer, subscribe to that event in order to be notified of the data change and update the customization accordingly.

using Foundation;
using System;
using Telerik.XamarinForms.Chart;
using Telerik.XamarinForms.ChartRenderer.iOS;
using TelerikUI;
using TestXamarin.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Telerik.XamarinForms.Chart.RadCartesianChart), typeof(CustomChartRenderer))]

namespace TestXamarin.iOS
{
    public class CustomChartRenderer : CartesianChartRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<RadCartesianChart> e)
        {
            base.OnElementChanged(e);
            this.DoAnnotationCustomizations();
            (this.Element as MyCustomChart).SeriesUpdated += CustomChartRenderer_SeriesUpdated;

        }

        // add annotation on series update
        private void CustomChartRenderer_SeriesUpdated(object sender, EventArgs e)
        {
            this.DoAnnotationCustomizations();
        }

        // add annotation
        private void DoAnnotationCustomizations()
        {
            // customization to the chart control
            // example with band annotation
            Foundation.NSDate nsDate = NSDate.Now;
            if (this.Control != null)
            {
                this.Control.AddAnnotation(new TKChartBandAnnotation(new TKRange(nsDate, nsDate.AddSeconds(200000)), this.Control.XAxis, new TKSolidFill(UIColor.Red), null));
            }
        }
    }
}

 

Regards,
Didi
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.