Implementing TKChartDataSource Protocol

2 posts, 0 answers
  1. John
    John avatar
    1 posts
    Member since:
    Feb 2014

    Posted 18 Feb 2014 Link to this post

    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?


  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 20 Feb 2014 Link to this post

    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
  3. DevCraft banner
Back to Top