在Coreplot iOS中触摸LEGENDS上的事件

我希望如果用户触摸特定的图例,那么我可以触发一些动作 ,比如显示该切片/条/图的详细信息。 除此之外是否有任何委托方法:

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index 

谢谢!

到目前为止,这在coreplot中不可用。 您必须子类化CPTLegend类才能添加此function。 这里已经有了这方面的要求。

只是为了指向正确的方向。 为了实现这一目标,您需要执行以下操作,

  1. 修改方法renderAsVectorInContext以存储绘制了图例标题和样例的CGRect。 在与图例标题对应的框架之间应该存在连接。
  2. 修改方法-(BOOL)pointingDeviceDownEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint并检查点击是否在上面存储的任何这些CGRect上。 如果该点在该帧内,则需要调用委托方法并告知哪个Legend被点击。 检查其他coreplot类中此方法的类似实现。 在这种情况下,应该几乎相似,以识别抽头点是否位于该帧内。

我在我的coreplot子类中实现了它。 这是我做的事情(可能不是最好的方法,但我在这里工作):

1 – 为CPlot创建一个类别,并添加一个名为CGRect legendRect的属性;

2 – 在初始化图时,为每个图设置此属性为CGRectZero;

3 – 添加协议CPTLegendDelegate并实现以下方法:

 -(BOOL)legend:(CPTLegend *)legend shouldDrawSwatchAtIndex:(NSUInteger)idx forPlot:(CPTPlot *)plot inRect:(CGRect)rect inContext:(CGContextRef)context { if (CGRectEqualToRect(plot.legendRect, CGRectZero)) { plot.legendRect = CGRectUnion(self.graph.legend.frame, rect); } return !plot.hidden; } 

3 – 添加协议CPTPlotSpaceDelegate并实现以下方法:

 -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(UIEvent *)event atPoint:(CGPoint)point { if (CGRectContainsPoint(self.graph.legend.frame, point)) { CGPoint pointInLegend = CGPointMake(point.x - self.graph.legend.frame.origin.x, point.y - self.graph.legend.frame.origin.y); [self.graph.allPlots enumerateObjectsUsingBlock:^(CPTPlot *plot, NSUInteger idx, BOOL *stop) { if (CGRectContainsPoint(plot.legendRect, pointInLegend)) { //here you can do whatever you need plot.hidden = !plot.hidden; [self configureLegend]; *stop = YES; } }]; } return YES; } 

当用户触摸图例项(样例或标签)时,将隐藏图形图。 可能这可以在coreplot中实现。

此致,Almir