Ios 具有重复图例的核心图条形图

Ios 具有重复图例的核心图条形图,ios,core-plot,bar-chart,Ios,Core Plot,Bar Chart,我正在使用core plot框架创建带有图例的条形图。除了图例复制条形图各部分中的列外,所有功能都正常。请参见所附的屏幕截图 我已经查看了核心plot类引用,但没有找到任何可以解决此问题的方法。我曾尝试将CPTLegendProperties numberOfColumns设置为1,将numberOfRows设置为3,这使得图例在图例中显示了适当数量的项目,但显示的数据不正确 下面是我用来构建条形图的代码。你们对我如何解决这个问题有什么建议吗?我假设这不是一个核心情节的bug,而是传说的局限性

我正在使用core plot框架创建带有图例的条形图。除了图例复制条形图各部分中的列外,所有功能都正常。请参见所附的屏幕截图

我已经查看了核心plot类引用,但没有找到任何可以解决此问题的方法。我曾尝试将CPTLegendProperties numberOfColumns设置为1,将numberOfRows设置为3,这使得图例在图例中显示了适当数量的项目,但显示的数据不正确

下面是我用来构建条形图的代码。你们对我如何解决这个问题有什么建议吗?我假设这不是一个核心情节的bug,而是传说的局限性,我希望有一个解决办法

// Create barChart from theme
barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[barChart applyTheme:theme];
chartView.hostedGraph = barChart;
barChart.plotAreaFrame.masksToBorder = NO;

barChart.paddingLeft = 60.0;
barChart.paddingTop = 10.0;
barChart.paddingRight = 0.0;
barChart.paddingBottom = 30.0;

//find max y
int maxY = 0;
NSMutableArray *maxDrillDownData = [chartData.drillDownData objectForKey:DRILLDOWN_EQUIPMENT_ALL_TYPE];
for (NSMutableArray *dataArray in maxDrillDownData) {
    maxY = dataArray.count>maxY?dataArray.count:maxY;
}

//add buffer
maxY = maxY+100;

// Add plot space for horizontal bar charts
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromInt(maxY)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(3.25)];

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPTDecimalFromString(@"1");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x.titleLocation = CPTDecimalFromFloat(7.5f);
x.titleOffset = 55.0f;
x.labelOffset = 2.0;

// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithFloat:0.5], [NSDecimalNumber numberWithFloat:1.5], [NSDecimalNumber numberWithFloat:2.5], nil];
NSArray *xAxisLabels = (NSMutableArray *)[chartData.labels objectForKey:LABELS_EQUIPMENT_TYPE];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = x.labelOffset;
    [customLabels addObject:newLabel];
    //[newLabel release];
}

x.axisLabels =  [NSSet setWithArray:customLabels];

CPTXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPTDecimalFromString(@"50");
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
y.title = (NSString *)[chartData.labels objectForKey:LABELS_Y_AXIS];
y.titleOffset = 45.0f;
y.titleLocation = CPTDecimalFromFloat(150.0f);

//identifiers
NSMutableArray *identifiers = (NSMutableArray *)[chartData.identifiers objectForKey:IDENTIFIER_EQUIPMENT_TYPE];

// First bar plot
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
barPlot.baseValue = CPTDecimalFromString(@"0");
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.6f);
barPlot.barWidth = CPTDecimalFromFloat(.5);
barPlot.delegate = self;
barPlot.identifier = (NSString *)[identifiers objectAtIndex:0];
[barChart addPlot:barPlot toPlotSpace:plotSpace];

NSLog(@"barPlot.identifier: %@", barPlot.identifier);

// Second bar plot
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.4f);
barPlot.barWidth = CPTDecimalFromFloat(.5);
barPlot.baseValue = CPTDecimalFromString(@"0");
barPlot.identifier = (NSString *)[identifiers objectAtIndex:1];
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];

NSLog(@"barPlot.identifier: %@", barPlot.identifier);

//third bar plot
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.barOffset = CPTDecimalFromFloat(-0.2f);
barPlot.barWidth = CPTDecimalFromFloat(.5);
barPlot.baseValue = CPTDecimalFromString(@"0");
barPlot.identifier = (NSString *)[identifiers objectAtIndex:2];
barPlot.delegate = self;
[barChart addPlot:barPlot toPlotSpace:plotSpace];

NSLog(@"barPlot.identifier: %@", barPlot.identifier);

// Add legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
theLegend.numberOfColumns = 3;
theLegend.numberOfRows = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;

barChart.legend = theLegend;
barChart.legendAnchor = CPTRectAnchorTopRight;
barChart.legendDisplacement = CGPointMake(-10.0f, -20.0f);

谢谢你的帮助

确保在绘图数据源中未实现以下两种方法:

-legendTitleForBarPlot:recordIndex:
-barFillForBarPlot:recordIndex:

如果存在这两种方法中的任何一种,绘图将为每个条创建图例条目,而不是为整个绘图使用单个图例条目。

请确保在绘图数据源中未实现以下两种方法:

-legendTitleForBarPlot:recordIndex:
-barFillForBarPlot:recordIndex:

如果存在这两种方法中的任何一种,绘图将为每个条创建一个图例条目,而不是为整个绘图使用单个图例条目。

我知道这是一个非常古老的问题,但提供的答案对我没有任何帮助。所以我调查了一下,想弄明白。在我看来,最好的解决方案是只在图例中添加一个绘图,而不是添加图形。添加图形会将与图形关联的所有绘图添加到图例中

CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
取代

NSMutableArray *legendPlots =[[NSMutableArray alloc] init];
[legendPlots addObject:barPlot];
CPTLegend *theLegend = [CPTLegend legendWithPlots:legendPlots];

我在一个图形中使用多个饼图时遇到了同样的问题。这为我解决了问题。

我知道这是一个非常古老的问题,但提供的答案对我没有任何帮助。所以我调查了一下,想弄明白。在我看来,最好的解决方案是只在图例中添加一个绘图,而不是添加图形。添加图形会将与图形关联的所有绘图添加到图例中

CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
取代

NSMutableArray *legendPlots =[[NSMutableArray alloc] init];
[legendPlots addObject:barPlot];
CPTLegend *theLegend = [CPTLegend legendWithPlots:legendPlots];

我在一个图形中使用多个饼图时遇到了同样的问题。这为我解决了问题。

Awesome…删除-barFillForBarPlot:recordIndex:修复了我的问题。谢谢你的帮助@EricSkroch我必须使用barFillForBarPlot:recordIndex方法,我还需要单个图例,而不是多个图例。如果我将numberOfColumns设置为1,它将重叠。有什么解决方案吗?太棒了…删除-barFillForBarPlot:recordIndex:修复了我的问题。谢谢你的帮助@EricSkroch我必须使用barFillForBarPlot:recordIndex方法,我还需要单个图例,而不是多个图例。如果我将numberOfColumns设置为1,它将重叠。有什么解决办法吗?很酷,是的,考虑到这篇文章已经三年了,很有可能你使用的CorePlot版本比我第一次遇到这个问题并发布答案时更新了很多。谢谢你更新这个帖子,让它保持相关性。很酷,是的,考虑到这篇文章已经三年了,很有可能你使用的CorePlot版本比我第一次发布这个问题并给出答案时更新了很多。感谢您更新此线程,使其保持相关性。