Change the color of the legend text

6 posts, 0 answers
  1. Eugenio
    Eugenio avatar
    6 posts
    Member since:
    Jun 2009

    Posted 06 Oct 2014 Link to this post

    Hi,

    I'd like to change the color of the legend text.

    I've tried the following code:

        for (int index=0; index<self.chart.legend.container.itemCount; index++) {
            TKChartLegendItem *legendItem = [self.chart.legend.container itemAtIndex:index];
            legendItem.label.textColor = [UIColor lightGrayColor];
        }

    This code doesn't work.

    Any ideas?

    Thanks in advance,

    Eugenio
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 07 Oct 2014 Link to this post

    Hi Eugenio,

    This code works properly on my side. Here is the code that I am using:

    @interface TestLegendColor ()
     
    @property (nonatomic, strong) TKChart *chart;
     
    @end
     
    @implementation TestLegendColor
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
         
        _chart = [[TKChart alloc] initWithFrame:[self exampleBounds]];
        _chart.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_chart];
         
        NSMutableArray *items = [NSMutableArray new];
        for (int i = 0; i < 200; i++) {
            [items addObject:[[TKChartDataPoint alloc] initWithX:@(arc4random()%200) Y:@(arc4random()%1000)]];
        }
        TKChartScatterSeries *series = [[TKChartScatterSeries alloc] initWithItems:items];
        [_chart addSeries:series];
         
        self.chart.legend.hidden = NO;
         
        for (int index=0; index<self.chart.legend.container.itemCount; index++) {
            TKChartLegendItem *legendItem = [self.chart.legend.container itemAtIndex:index];
            legendItem.label.textColor = [UIColor lightGrayColor];
        }
    }
     
    @end

    Maybe there is something specific in your project. Please, could you send me your code and I will try to locate the issue and provide a proper solution.

    Please note that if you want to change the axis label color, you should call the reloadData method as demonstrated below:

    self.chart.xAxis.style.labelStyle.textColor = [UIColor redColor];
    [self.chart reloadData];

    I am looking forward to your reply.

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

     
  3. DevCraft banner
  4. Eugenio
    Eugenio avatar
    6 posts
    Member since:
    Jun 2009

    Posted 07 Oct 2014 Link to this post

    Hi Jack,

    Changing the axis label color is working, the problem happens just with the legend.

    I'm gonna be on vacation for two weeks but as soon as I come back I will send you the code.

    Thank you for your help.

    Regards,

    Eugenio


  5. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 08 Oct 2014 Link to this post

    Hi Eugenio,

    Thank you for this update. Have a nice vacation.

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

     
  6. Oleg
    Oleg avatar
    8 posts
    Member since:
    Nov 2014

    Posted 20 Nov 2014 in reply to Jack Link to this post

    I am also trying to customise the legend's appearance. Not working :(
    I want to make the legend items much bigger and change the shape of the indicator icon:

    TKChartLegendContainer *legendContainerScrollView = chart.legend.container;
        legendContainerScrollView.stack.itemSpacing = 50;
        TKChartLegendItem *item = [legendContainerScrollView itemAtIndex:0];
        item.style.iconSize = CGSizeMake(50, 50); // Not working

    Not working...

    I also want to change the shape of the legend's indicator:

    I see in the debugger that :

    TKChartLegendItem *item = [legendContainerScrollView itemAtIndex:0];TKChartLegendItem *item = [legendContainerScrollView itemAtIndex:0];
    // item.icon is a TKChartShapeView - would like to change the shape to "Square"

    Also I see that TKChartLegendItem.h declares a protocol "TKChartLegendItemDelegate"

    But no way to become the delegate without sub classing TKChartSeries:

     @discussion The legend asks all series first about the count of legend items they will provide and then gets this count of items.




    But I don't see any implementation or declaration in TKSeries of this protocol.

    It also seems like a lot of work for something very trivial

    Here is the appearance I want to achieve in the attached file:


  7. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 21 Nov 2014 Link to this post

    Hello Avner,

    You can change the legend item shape by using a custom UIView for the legend icon. Here is a sample code for this (assuming that you are using a pie chart):
    for (int i = 0; i<_pieChart.legend.container.itemCount; i++) {
        TKChartLegendItem *item = [_pieChart.legend.container itemAtIndex:i];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        TKChartPaletteItem *paletteItem = [_pieChart paletteItemForSeries:_pieChart.series[0] atIndex:i];
        TKSolidFill *fill = (TKSolidFill*)paletteItem.fill;
        view.backgroundColor = fill.color;
        item.icon = view;
    }

    However, currently it is not possible to achieve the same layout like in the attached file. We know about this limitation and we are working on improving how legends can be customized in TKChart.

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

     
Back to Top
DevCraft banner