我有一个带有3 SChartLineSeries
的图表。我想同时显示3个同时的目标点(来自与他的系列相对应的十字准线)。
就像我在这张图片上使用dataPoints一样,但是我希望它在与Crosshairs相同的X位置上。
我该如何实现?我试图识别委托方法中的“长按”事件,但我不知道该怎么做。
有了十字准线,我只有这样:
编辑:
我试过了:
- (void)sChart:(ShinobiChart *)chart toggledSelectionForPoint:(SChartDataPoint *)dataPoint inSeries:(SChartSeries *)series atPixelCoordinate:(CGPoint)pixelPoint
{
for (SChartLineSeries *serie in chart.series) {
for (SChartDataPoint *dp in serie.dataSeries.dataPoints){
if (dp.xValue == dataPoint.xValue){
dp.selected = YES;
serie.crosshairEnabled = YES;
serie.style.selectedPointStyle.color = [UIColor blackColor];
serie.style.selectedPointStyle.radius = [NSNumber numberWithInt:8];
serie.style.selectedPointStyle.showPoints = YES;
}
}
}
}
并将对象“
SChartCrosshair
”子类化,我可以识别“crosshairChartGotLongPressAt
”上的长按事件。 最佳答案
我已经在ShinobiControls团队支持的帮助下进行了管理。
在子类化的'SChartCrosshair
'上,我必须遍历与所触摸的'Crosshair
'X位置匹配的点,然后将经过数学运算的点添加到数组中,以便在'drawCrosshairLines
'中使用它们。为此,我通过了解SChartAxis
对象的方法使用了点的像素版本:'pixelValueForDataValue
'。
我也在图表上画一条垂直线。
@implementation CustomCrossHair
-(void)drawCrosshairLines
{
CGContextRef context = UIGraphicsGetCurrentContext();
SChartDataPoint *firstDataPixelPoint = arrayPixelPoints[0];
double xFirst = [firstDataPixelPoint.xValue doubleValue];
CGContextMoveToPoint(context,xFirst, 0.f);
CGContextAddLineToPoint(context,xFirst, self.chart.canvas.glView.frame.size.height);
for (SChartDataPoint *dataPixelPoint in arrayPixelPoints){
double yVal = [dataPixelPoint.yValue doubleValue];
double xVal = [dataPixelPoint.xValue doubleValue];
CGRect rectangle = CGRectMake(xVal,yVal, 7, 7);
CGContextAddEllipseInRect(context, rectangle);
}
CGContextStrokePath(context);
}
-(void)moveToPosition:(SChartPoint)coords andDisplayDataPoint:(SChartPoint)dataPoint fromSeries:(SChartCartesianSeries *)series andSeriesDataPoint:(id<SChartData>)dataseriesPoint
{
[super moveToPosition:coords andDisplayDataPoint:dataPoint fromSeries:series andSeriesDataPoint:dataseriesPoint];
arrayPixelPoints = [NSMutableArray array];
SChartDataPoint *dataSPoint = dataseriesPoint;
int i = 0;
for (SChartLineSeries *serie in self.chart.series){
i++;
for (SChartDataPoint *dp in serie.dataSeries.dataPoints){
if ([dp.xValue doubleValue] == [dataSPoint.xValue doubleValue]){
float yPixel = [self.chart.yAxis pixelValueForDataValue:dp.yValue];
float xPixel = [self.chart.xAxis pixelValueForDataValue:dp.xValue];
SChartDataPoint *dataPointPixel = [[SChartDataPoint alloc] init];
dataPointPixel.xValue = [NSNumber numberWithFloat:xPixel];
dataPointPixel.yValue = [NSNumber numberWithFloat:yPixel];
[arrayPixelPoints addObject:dataPointPixel];
}
}
}
if (i == self.chart.series.count) [self drawCrosshairLines];
}