问题
I am using CorePlot 1.0 and would like to add a legend to my bar chart (which is a subclassed XYGraph). I have the datasource and legend title methods in the viewController, which instantiates the bar chart and sets itself as its delegate.
I have spend quite a few hours with different delegate and legend setups but can't find my error. Can you give my any hints on where I may have made an error?
I have subclassed XYGraph to generate bar charts as follows:
.h
@interface BarLineChart : CPTXYGraph
@property (strong, nonatomic) id <CPTPlotDataSource, CPTBarPlotDataSource, CPTBarPlotDelegate, CPTPlotSpaceDelegate> delegate;
@end
In my implementation .m, I set up the legend as follows:
// Add legend
CPTLegend *legend = [CPTLegend legendWithGraph:self];
legend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
legend.borderLineStyle = axisLineStyle;
legend.cornerRadius = 10.0;
legend.swatchSize = CGSizeMake(16.0, 16.0);
CPTMutableTextStyle *legendTextStyle = [CPTMutableTextStyle textStyle];
legendTextStyle.color = [CPTColor whiteColor];
legendTextStyle.fontSize = 10.0;
legend.textStyle = legendTextStyle;
legend.rowMargin = 1.0;
legend.numberOfRows = 3;
legend.paddingLeft = 10.0;
legend.paddingTop = 10.0;
legend.paddingRight = 10.0;
legend.paddingBottom = 10.0;
legend.delegate = self.delegate;
self.legend = legend;
self.legendAnchor = CPTRectAnchorTop;
self.defaultPlotSpace.delegate = self.delegate;
I keep the datasource methods in a separate viewController, for which I have set the delegate (stored in self.delegate). This is my viewController's interface:
`@interface FinstatViewController : CompanyViewController <UITableViewDelegate, UITableViewDataSource, UISplitViewControllerDelegate, UIPopoverControllerDelegate, CompanyPresenter, ChartSelectionDelegate, CPTPlotDataSource, CPTBarPlotDataSource, CPTBarPlotDelegate, CPTPlotSpaceDelegate, CPTLegendDelegate, CPTScatterPlotDataSource, CPTScatterPlotDelegate>`
My data source methods to get the plot data works fine, but the legend title method
-(NSString *)legendTitleForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index
is never called.
回答1:
The legend title method is part of the datasource, not the delegate. If your datasource and delegate are in different classes, make sure the title method is implemented in the correct one.
来源:https://stackoverflow.com/questions/9720863/core-plot-1-0-provide-custom-labels-for-legends