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

Implementing TKChartDataSource Protocol

1 Answer 39 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
John
Top achievements
Rank 1
John asked on 19 Feb 2014, 02:20 AM
If I derive a class from TKChartViewController and implement these methods, I get a line chart as expected:

- (NSUInteger)numberOfSeriesForChart:(TKChart *)chart
{
    return 1;
}

- (TKChartSeries *)seriesForChart:(TKChart *)chart atIndex:(NSUInteger)index
{
    TKChartSeries *s = [[TKChartLineSeries alloc] init];
    s.title = @"Test";
    return s;
}

- (NSUInteger)chart:(TKChart *)chart numberOfDataPointsForSeriesAtIndex:(NSUInteger)seriesIndex
{
    return 4;
}
 
- (id<TKChartData>)chart:(TKChart *)chart dataPointAtIndex:(NSUInteger)dataIndex forSeriesAtIndex:(NSUInteger)seriesIndex
{
    TKChartDataPoint *point = [[TKChartDataPoint alloc] init];
    point.dataXValue = @(dataIndex);
    point.dataYValue = @(arc4random() % 100);
    return point;
}

But if I change TKChartLineSeries to TKChartColumnSeries, then I get an empty chart with just axes, no columns.

What am I missing?


1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 20 Feb 2014, 09:38 AM
Hi John,

Thank you contacting us.

I can confirm that this is an issue in TKChart. It appears when using TKChartViewController. The good news is that we already addressed the issue and the fix will be included in our upcoming release planned for the next week.

In the mean time, you have two options to solve the issue:

1. You can specify the bar fill explicitly by implementing the chart:paletteItemForSeries:atIndex: method:

- (TKChartPaletteItem *)chart:(TKChart *)chart paletteItemForSeries:(TKChartSeries *)series atIndex:(NSUInteger)index
{
    TKSolidFill *blueFill = [[TKSolidFill alloc] initWithColor:[UIColor blueColor]];
    TKChartPaletteItem *paletteItem = [[TKChartPaletteItem alloc] initWithFill:blueFill];
    return paletteItem;
}

2. The second option is to use the default UIViewController and implement TKChartDelegate:

@interface ChartViewController () <TKChartDataSource>
@end
 
@implementation ChartViewController
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
     
    TKChart *chart = [[TKChart alloc] initWithFrame:CGRectInset(self.view.bounds, 15, 15)];
    chart.autoresizingMask = ~UIViewAutoresizingNone;
    chart.dataSource = self;
    [self.view addSubview:chart];
}
 
- (NSUInteger)numberOfSeriesForChart:(TKChart *)chart
{
    return 1;
}
 
- (TKChartSeries *)seriesForChart:(TKChart *)chart atIndex:(NSUInteger)index
{
    TKChartColumnSeries *s = [[TKChartColumnSeries alloc] init];
    s.title = @"Test";
    return s;
}
 
- (NSUInteger)chart:(TKChart *)chart numberOfDataPointsForSeriesAtIndex:(NSUInteger)seriesIndex
{
    return 4;
}
 
- (id<TKChartData>)chart:(TKChart *)chart dataPointAtIndex:(NSUInteger)dataIndex forSeriesAtIndex:(NSUInteger)seriesIndex
{
    TKChartDataPoint *point = [[TKChartDataPoint alloc] init];
    point.dataXValue = @(dataIndex);
    point.dataYValue = @(arc4random() % 100);
    return point;
}
 
@end

We recommend the second option as it preserves the default bar style.

I updated your Telerik points for reporting this issue.

Do not hesitate to contact us if you have further questions.

Regards,
Julian Benkov
Telerik
Tags
General Discussion
Asked by
John
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or