CorePlot: how to set touch event, when user click the CandleStick on the chart, then it can show the Candle X and Y Value?

大兔子大兔子 提交于 2019-12-05 07:21:38

问题


CorePlot: how to set touch event, when user click the CandleStick on the chart, then it can show the Candle X and Y Value?

The value in left side is not correct,because the chart can be zoom in and zoom out , so may be the value need to include the scale value. But i don't know the scale value....

so is there any way to get the CandleStick Chart By click the screen?

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];


回答1:


This is not natively supported yet in Core Plot. You could subclass CPTTradingRangePlot and implement the touch handling yourself. Look at CPTBarPlot and CPTScatterPlot for ideas on how to do it.



来源:https://stackoverflow.com/questions/7872416/coreplot-how-to-set-touch-event-when-user-click-the-candlestick-on-the-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!