This is a migrated thread and some comments may be shown as answers.

Change the color of the legend text

5 Answers 187 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Eugenio
Top achievements
Rank 1
Eugenio asked on 06 Oct 2014, 07:54 PM
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

5 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 07 Oct 2014, 03:54 PM
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.

 
0
Eugenio
Top achievements
Rank 1
answered on 07 Oct 2014, 09:07 PM
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


0
Jack
Telerik team
answered on 08 Oct 2014, 06:55 AM
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.

 
0
Oleg
Top achievements
Rank 1
answered on 20 Nov 2014, 12:24 PM
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:


0
Jack
Telerik team
answered on 21 Nov 2014, 01:09 PM
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.

 
Tags
Chart
Asked by
Eugenio
Top achievements
Rank 1
Answers by
Jack
Telerik team
Eugenio
Top achievements
Rank 1
Oleg
Top achievements
Rank 1
Share this question
or