CorePlot:如何设置触摸事件,当用户点击图表上的CandleStick时,它是否可以显示Candle X和Y值?

时间:2011-10-24 07:23:24

标签: iphone charts core-plot

CorePlot:如何设置触摸事件,当用户点击图表上的CandleStick时,它是否可以显示Candle X和Y值?

左侧的值不正确,因为图表可以放大和缩小 ,因此可能需要包括比例值的值。 但我不知道规模值......

有没有办法获得CandleStick图表点击屏幕?

graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTStocksTheme];
[graph applyTheme:theme];

graph.frame = self.view.bounds;

graph.paddingLeft = 20.0; 
graph.paddingTop = 20.0; 
graph.paddingRight = 40.0; 
graph.paddingBottom = 60.0;

[graphHost setAllowPinchScaling:YES];

graph.plotAreaFrame.masksToBorder = NO;
graph.plotAreaFrame.cornerRadius = 0.0f;
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor blackColor];
borderLineStyle.lineWidth = 2.0f;
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
self.graphHost.hostedGraph = graph;

// Axes
CPTXYAxisSet *xyAxisSet = (id)graph.axisSet;

CPTXYAxis *xAxis = xyAxisSet.xAxis;
CPTMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy];
lineStyle.lineCap = kCGLineCapButt;
xAxis.axisLineStyle = lineStyle;

[lineStyle release];
xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;


xAxis.isFloatingAxis=YES;
xAxis.minorTicksPerInterval = 4;
xAxis.preferredNumberOfMajorTicks = 9;
xAxis.orthogonalCoordinateDecimal=CPTDecimalFromString(@"110");




CPTXYAxis *yAxis = xyAxisSet.yAxis;


yAxis.isFloatingAxis=YES;
yAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;

CPTConstraints yConstraints = {CPTConstraintNone, CPTConstraintFixed};
yAxis.constraints = yConstraints;


CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
dataSourceLinePlot.delegate=self;
dataSourceLinePlot.identifier = @"Data Source Plot";
dataSourceLinePlot.dataLineStyle = nil;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];

CPTColor *areaColor = [CPTColor clearColor];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]];
areaGradient.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPTDecimalFromDouble(200.0);

areaColor = [CPTColor colorWithComponentRed:0.0 green:1.0 blue:0.0 alpha:0.6];

areaGradient = [CPTGradient gradientWithBeginningColor:[CPTColor clearColor] endingColor:areaColor];
areaGradient.angle = -90.0f;
areaGradientFill = [CPTFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill2 = areaGradientFill;
dataSourceLinePlot.areaBaseValue2 = CPTDecimalFromDouble(200.0);

// OHLC plot
CPTMutableLineStyle *whiteLineStyle = [CPTMutableLineStyle lineStyle];
whiteLineStyle.lineColor = [CPTColor blackColor];
whiteLineStyle.lineWidth = 1.0f;

CPTTradingRangePlot *ohlcPlot = [[[CPTTradingRangePlot alloc] initWithFrame:graph.bounds] autorelease];
ohlcPlot.identifier = @"OHLC";
ohlcPlot.lineStyle = whiteLineStyle;
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
whiteTextStyle.fontSize = 11.0;
ohlcPlot.labelTextStyle = whiteTextStyle;
ohlcPlot.labelOffset = 30.0;
ohlcPlot.stickLength = 12.0f;
ohlcPlot.dataSource = self;
//ohlcPlot.plotStyle = CPTTradingRangePlotStyleOHLC;
ohlcPlot.plotStyle = CPTTradingRangePlotStyleCandleStick;

ohlcPlot.increaseFill = [(CPTFill *)[CPTFill alloc] initWithColor:[CPTColor greenColor]];
ohlcPlot.decreaseFill = [(CPTFill *)[CPTFill alloc] initWithColor:[CPTColor redColor]];

[graph addPlot:ohlcPlot];

// Add plot space for horizontal bar charts
CPTXYPlotSpace *volumePlotSpace= [[CPTXYPlotSpace alloc] init];


volumePlotSpace.delegate=self;
volumePlotSpace.globalXRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(30.0f)];
volumePlotSpace.globalYRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(800.0f) length:CPTDecimalFromFloat(100.0f)];
volumePlotSpace.allowsUserInteraction=TRUE;    

volumePlotSpace.allowsUserInteraction=YES;

volumePlotSpace.identifier = @"Volume Plot Space";
[graph addPlotSpace:volumePlotSpace];
[volumePlotSpace release];

// Data puller
NSDate *start = [NSDate dateWithTimeIntervalSinceNow:-60.0 * 60.0 * 24.0 * 7.0 * 12.0]; // 12 weeks ago
NSDate *end = [NSDate date];
APYahooDataPuller *dp = [[APYahooDataPuller alloc] initWithTargetSymbol:@"AAPL" targetStartDate:start targetEndDate:end];
[self setDatapuller:dp];
[dp setDelegate:self];
[dp release];

enter image description here

1 个答案:

答案 0 :(得分:1)

Core Plot中尚未支持此功能。您可以将CPTTradingRangePlot子类化并自己实现触摸处理。请查看CPTBarPlot和CPTScatterPlot,了解如何执行此操作。