iosobjective-ccore-plot

CorePlot piechart disappears when there's only 1 plot data


I've got a coreplot piechart running perfectly for years, but recently found out a pretty edge case that if there's only one plot data value that's greater than 0, the pie chart won't show. It displays fine as long as there are more than 1 data greater than 0. It'd be great if it could just show a single color ring.

Has anyone come across this issue? Thanks!

- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return 3;
}

- (double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx {
    if (CPTPieChartFieldSliceWidth == fieldEnum) {
        return [[self plotData][idx][@"double"] doubleValue];
    }
    return 0;
}


Solution

  • A pie chart displays data values as a fraction of the whole. If all of the data values sum to zero (0), the slice widths are undefined (divide by zero). We could add a way to display an empty chart. In the meantime, you can just provide a non-zero dummy data value when you don't have valid data to plot. Change the fill, border, and labels as desired to indicate the lack of data.