This is a migrated thread and some comments may be shown as answers.
Set color of series labels
3 Answers 59 Views
This is a migrated thread and some comments may be shown as answers.
Bruno
Top achievements
Rank 1
Bruno asked on 01 Jun 2017, 04:35 PM

Hi, I´m trying to set the color of the labels of a serie, due to the fact that with iOS the color of the labels is always black and it doesn´t take into account any of the colors set in the pallet. Moreover, it doesn´t establish the background color as it does in Android. I already read all the posts related and i found nothing that can help me with this issue.

 

The images attached are an example of yours with the labels on for the fourth serie in android and iOS.

Regards,

Bruno Torterolo

3 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 05 Jun 2017, 08:38 AM
Hi Bruno,

In order to modify the labels in the iOS Platform, you need to create a custom renderer and work with the native control. Eventually, I suggest you have a look at the iOS Chart Control documentation for more information on what customizations can be achieved. More specifically, you can have a detailed look at the following page - Point Labels: Customizations. I am here providing a possible implementation of a custom renderer that changes the color of the label:

//argument needed to register the renderer
[assembly: Xamarin.Forms.ExportRenderer(typeof(Telerik.XamarinForms.Chart.RadPieChart), typeof(MyChartRenderer))]
  
namespace TelerikXamarin.iOS.CustomRenderer
{
    public class MyChartRenderer : Telerik.XamarinForms.ChartRenderer.iOS.PieChartRenderer
    {
        protected override TKChartDelegate CreateChartDelegate(RadPieChart chart)
        {
            return new MyChartDelegate(chart);
        }
    }
  
    public class MyChartDelegate : Telerik.XamarinForms.ChartRenderer.iOS.PieChartDelegate
    {
        public MyChartDelegate(RadPieChart chart) : base(chart)
        {
        }
        public override TKChartPointLabel LabelForDataPoint(TKChart chart, TKChartData dataPoint, string propertyName, TKChartSeries series, nuint dataIndex)
        {
            TKChartDataPoint chartDataPoint = (TKChartDataPoint)dataPoint;
            return new MyChartPointLabel(chartDataPoint, series, chartDataPoint.DataXValue.ToString());
        }
    }
  
    public class MyChartPointLabel : TKChartPointLabel
    {
        public MyChartPointLabel(TKChartDataPoint point, TKChartSeries series, string text) : base(point, series, text)
        {
            this.Style.TextColor = UIColor.White;
        }
    }
}

I have also attached a sample application for your reference. I hope it will be helpful.

Regards,
Stefan Nenchev
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ivailo
Top achievements
Rank 1
answered on 01 Aug 2018, 11:20 AM
Hi Stefan, 

Can you provide an Android example for the same thing if possible ?
0
Lance | Manager Technical Support
Telerik team
answered on 01 Aug 2018, 06:17 PM
Hello Ivailo,

You can find a demo of setting the label properties in this GitHub repo: Custom Series Labels

There are three platform projects, each contain a Xamarin Effect class that sets the native chart's properties for customizing labels.

For Android, this is the relevant code:

// Get a reference to the native chart control
if (Control is RadCartesianChartView nativeChart)
{
     // Iterate over the series
     for (int i = 0; i < nativeChart.Series.Size(); i++)
     {
         // Get a reference to the series you need, in this case it's a spline series
         if (nativeChart.Series.Get(i) is SplineAreaSeries series)
         {
             // Set the Label properties you want
             series.LabelFillColor = Color.Red;
             series.LabelTextColor = Color.White;
        }
    }
}

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Chart
Asked by
Bruno
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Ivailo
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
Share this question
or