I'm posting a lot of code, but the question is relatively simple:
I'm trying to create a CTPXYGraph as a stand-alone object and then use it as a hostedGraph in another ViewController I have.
For some reason, it crashes! (Gets me to assembly code with EXC_BAD_ACCESS signal)
The ViewController:
RealTimePlot *plot = [[RealTimePlot alloc] init];
CTPXYGraph *graph = [plot createGraph];
self.graphHostingView.hostedGraph = graph;
The graphObject:
#import "CorePlot-CocoaTouch.h"
#interface RealTimePlot : NSObject <CPTPlotDataSource>
{
NSMutableArray *dataForPlot;
}
#property (readwrite, strong) NSMutableArray *dataForPlot;
- (CPTXYGraph *) createGraph;
#end
Implementation:
#implementation RealTimePlot
#synthesize dataForPlot;
- (CPTXYGraph *) createGraph:
// Create graph from theme
CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
graph.paddingLeft = 10.0;
graph.paddingTop = 10.0;
graph.paddingRight = 10.0;
graph.paddingBottom = 10.0;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(3.0)];
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(#"0.5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"2");
x.minorTicksPerInterval = 2;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(2.99) length:CPTDecimalFromFloat(0.02)],
nil];
x.labelExclusionRanges = exclusionRanges;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromString(#"0.5");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"2");
exclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(3.99) length:CPTDecimalFromFloat(0.02)],
nil];
y.labelExclusionRanges = exclusionRanges;
// Create a blue plot area
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor blueColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.identifier = #"Blue Plot";
boundLinePlot.dataSource = self;
[graph addPlot:boundLinePlot];
// Do a blue gradient
CPTColor *areaColor1 = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
areaGradient1.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
boundLinePlot.areaFill = areaGradientFill;
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];
// Add plot symbols
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
boundLinePlot.plotSymbol = plotSymbol;
// Create a green plot area
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 3.f;
lineStyle.lineColor = [CPTColor greenColor];
lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.identifier = #"Green Plot";
dataSourceLinePlot.dataSource = self;
// Put an area gradient under the plot above
CPTColor *areaColor = [CPTColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.8];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]];
areaGradient.angle = -90.0f;
areaGradientFill = [CPTFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPTDecimalFromString(#"1.75");
// Animate in the new plot, as an example
dataSourceLinePlot.opacity = 0.0f;
[graph addPlot:dataSourceLinePlot];
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:#"animateOpacity"];
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for ( i = 0; i < 60; i++ ) {
id x = [NSNumber numberWithFloat:1 + i * 0.05];
id y = [NSNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, #"x", y, #"y", nil]];
}
self.dataForPlot = contentArray;
return graph;
}
RealTimePlot *plot = [[RealTimePlot alloc] init];
is getting dealloc-ed.
It turns out CPTGraphHostingView doesn't retain its hostedGraph ivar.
Related
Hi have followed this tutorial using Core plot framework
http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application
My project compiles, but crash when trying the view.
I think i has to do with UIView / storyboard. Although i've changed the class in my UIView till CPTGraphHostingView i still get the error below
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setHostedGraph:]: unrecognized selector sent to instance 0x6b8afe0'
Any ideas ?
Code below
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//CPTGraphHostingView *hostingView = (CPTGraphHostingView *) self.view;
//CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
//[self.view addSubview:hostingView];
hostingView.hostedGraph = graph;
graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
hostingView.hostedGraph = graph;
graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-6)
length:CPTDecimalFromFloat(12)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-5)
length:CPTDecimalFromFloat(30)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
axisSet.xAxis.majorIntervalLength = [[NSNumber numberWithInt:5] decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = [[NSNumber numberWithInt:5] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;
CPTScatterPlot *xSquaredPlot = [[CPTScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.accessibilityFrame];
xSquaredPlot.identifier = #"X Squared Plot";
CPTMutableLineStyle *ls1 = [CPTMutableLineStyle lineStyle];
ls1.lineWidth = 1.0f;
ls1.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle = ls1;
xSquaredPlot.dataSource = self;
[graph addPlot:xSquaredPlot];
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;
CPTScatterPlot *xInversePlot = [[CPTScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.accessibilityFrame];
xInversePlot.identifier = #"X Inverse Plot";
CPTMutableLineStyle *ls2 = [CPTMutableLineStyle lineStyle];
ls2.lineWidth = 1.0f;
ls2.lineColor = [CPTColor blueColor];
xInversePlot.dataLineStyle = ls2;
xInversePlot.dataSource = self;
[graph addPlot:xInversePlot];
}
I added -Load_All and -ObjC in the compiler options, then it seems to be OK :-)
My code is below:
- (void)viewDidLoad
{
[super viewDidLoad];
[self generateDataSamples];
CPTGraphHostingView *hostingview=[[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hostingview];
graph=[[CPTXYGraph alloc] initWithFrame:self.view.bounds];
hostingview.hostedGraph =graph;
// Border
graph.plotAreaFrame.borderLineStyle = nil;
graph.plotAreaFrame.cornerRadius = 0.0f;
// Paddings
graph.paddingLeft = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingBottom = 0.0f;
graph.plotAreaFrame.paddingLeft = 70.0;
graph.plotAreaFrame.paddingTop = 20.0;
graph.plotAreaFrame.paddingRight = 20.0;
graph.plotAreaFrame.paddingBottom = 80.0;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(7.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(50.0)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(#"1.0");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0.0");
x.minorTicksPerInterval =9;
// x.majorGridLineStyle = majorGridLineStyle;
// x.minorGridLineStyle = minorGridLineStyle;
x.title = #"X Axis";
x.titleOffset = 30.0;
x.titleLocation = CPTDecimalFromString(#"1.25");
// Label y with an automatic label policy.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0.0");
y.minorTicksPerInterval =9;
y.preferredNumberOfMajorTicks = 8;
//y.majorGridLineStyle = majorGridLineStyle;
// y.minorGridLineStyle = minorGridLineStyle;
y.labelOffset = 10.0;
y.title = #"Y Axis";
y.titleOffset = 30.0;
y.titleLocation = CPTDecimalFromString(#"1.0");
// Set axes
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
CPTScatterPlot *datasourceLinePlot =[[CPTScatterPlot alloc] init];
datasourceLinePlot.dataSource =self;
[graph addPlot:datasourceLinePlot];
[datasourceLinePlot release];
[graph release];
[hostingview release];
}
This code generate Scatter-plot using Core-plot library in iphone ...!
Code is Working Perfectly below output.
but i am try highlight points no idea....!
I want Highlight x and y value points like dot....!
Help Me with us...!
You can use a plot symbol and add this to your line plot.
Objective-C
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
plotSymbol.size = CGSizeMake(10.0, 10.0);
datasourceLinePlot.plotSymbol = plotSymbol;
Swift
let plotSymbol = CPTPlotSymbol.ellipse()
plotSymbol.fill = CPTFill(color: CPTColor.white())
plotSymbol.size = CGSize(width: 10.0, height: 10.0)
datasourceLinePlot.plotSymbol = plotSymbol
I'm using coreplot for plotting two graphs in one view. The data that is being plotted changes when a user choses to receive different kinds of data.
The data I receive is being plotted properly. However I'd like to change the range of one of the Y axis when it needs to. (The data could differ from 0 to 40 or from 0.0 to 1.0).
How can I implement this function so that when a user plots data from 0.0 to 1.0 the y-axis it's range also changes to these values
Here's a snippit of my code:
- (void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme fromGraph:(int)graphNumber
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect bounds = layerHostingView.bounds;
layerHostingView.backgroundColor = [UIColor clearColor];
// Create the graph and assign the hosting view.
graph = [[CPTXYGraph alloc] initWithFrame:bounds];
layerHostingView.hostedGraph = graph;
[graph applyTheme:theme];
graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
graph.plotAreaFrame.borderLineStyle = nil;
graph.plotAreaFrame.masksToBorder = NO;
// chang the chart layer orders so the axis line is on top of the bar in the chart.
NSArray *chartLayers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:CPTGraphLayerTypePlots],
[NSNumber numberWithInt:CPTGraphLayerTypeMajorGridLines],
[NSNumber numberWithInt:CPTGraphLayerTypeMinorGridLines],
[NSNumber numberWithInt:CPTGraphLayerTypeAxisLines],
[NSNumber numberWithInt:CPTGraphLayerTypeAxisLabels],
[NSNumber numberWithInt:CPTGraphLayerTypeAxisTitles],
nil];
graph.topDownLayerOrder = chartLayers;
[chartLayers release];
// Add plot space for horizontal bar charts
graph.paddingLeft = 45.0;
graph.paddingTop = 40.0;
graph.paddingRight = 45.0;
graph.paddingBottom = 60.0;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(28.0f)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(45)];
CPTScatterPlot *highPlot2 = [[[CPTScatterPlot alloc] init] autorelease];
highPlot2.identifier = #"Graph2";
CPTMutableLineStyle *highLineStyle2 = [[highPlot2.dataLineStyle mutableCopy] autorelease];
highLineStyle2.lineWidth = 1.f;
highLineStyle2.lineColor = [CPTColor colorWithComponentRed:0.00f green:0.00f blue:0.00f alpha:0.0f];
highPlot2.dataLineStyle = highLineStyle2;
highPlot2.dataSource = self;
CPTFill *areaFill2 = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.00f green:0.92f blue:0.0f alpha:0.58f]];
highPlot2.areaFill = areaFill2;
highPlot2.shadow = Shadow;
highPlot2.areaBaseValue = CPTDecimalFromString(#"0");
[graph addPlot:highPlot2];
// Setup grid line style
CPTMutableLineStyle *majorXGridLineStyle = [CPTMutableLineStyle lineStyle];
majorXGridLineStyle.lineWidth = 1.0f;
majorXGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.60f];
CPTMutableTextStyle *TextStyleBlack = [CPTMutableTextStyle textStyle];
TextStyleBlack.color = [CPTColor blackColor];
TextStyleBlack.fontSize = 15.0;
// Setup x-Axis.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorGridLineStyle = majorXGridLineStyle;
x.majorIntervalLength = CPTDecimalFromString(#"1");
x.minorTicksPerInterval = 1;
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.title = #"Days";
x.timeOffset = 30.0f;
NSArray *exclusionRanges = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(0)], nil];
x.labelExclusionRanges = exclusionRanges;
NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:[sampleDays count]];
int idx = 0;
for (NSString *day in sampleDays)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:day textStyle:x.labelTextStyle];
label.tickLocation = CPTDecimalFromInt(idx);
label.offset = 5.0f;
[labels addObject:label];
[label release];
idx++;
}
x.axisLabels = [NSSet setWithArray:labels];
[labels release];
// Setup y-Axis.
CPTMutableLineStyle *majorYGridLineStyle = [CPTMutableLineStyle lineStyle];
majorYGridLineStyle.lineWidth = 1.0f;
majorYGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.60];
CPTXYAxis *y1 = axisSet.yAxis;
y1.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"10"]decimalValue];
y1.labelShadow = Shadow;
y1.minorTicksPerInterval = 0;
y1.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
y1.labelTextStyle = TextStyleYellow;
y1.labelOffset = 15;
y1.axisLineStyle = nil;
NSArray *yExlusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(0)],
nil];
y1.labelExclusionRanges = yExlusionRanges;
//setup second y axis
CPTXYAxis *y2 = [[(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero] autorelease];
y2.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y2.orthogonalCoordinateDecimal = CPTDecimalFromString(#"27");
y2.minorTicksPerInterval = 0;
y2.preferredNumberOfMajorTicks = 4;
y2.majorGridLineStyle = majorYGridLineStyle;
y2.labelOffset = - 40.0;
y2.coordinate = CPTCoordinateY;
y2.plotSpace = graph.defaultPlotSpace;
y2.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"10"]decimalValue];
y2.minorTickLineStyle = nil;
y2.axisLineStyle = nil;
//add all axis to the graph
graph.axisSet.axes = [NSArray arrayWithObjects:x, y1, y2,nil];
// Create a high plot area
CPTScatterPlot *highPlot = [[[CPTScatterPlot alloc] init] autorelease];
highPlot.identifier = kHighPlot;
CPTMutableLineStyle *highLineStyle = [[highPlot.dataLineStyle mutableCopy] autorelease];
highLineStyle.lineWidth = 1.f;
highLineStyle.lineColor = [CPTColor colorWithComponentRed:0.00f green:0.00f blue:0.00f alpha:0.0f];
highPlot.dataLineStyle = highLineStyle;
highPlot.dataSource = self;
//fillcolor
CPTFill *areaFill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.00f green:0.97f blue:0.0f alpha:0.41f]];
highPlot.areaFill = areaFill;
highPlot.shadow = Shadow;
highPlot.areaBaseValue = CPTDecimalFromString(#"0");
[graph addPlot:highPlot];
// Create the Savings Marker Plot
selectedCoordination = 2;
touchPlot = [[[CPTScatterPlot alloc] initWithFrame:CGRectNull] autorelease];
touchPlot.identifier = kLinePlot;
touchPlot.dataSource = self;
touchPlot.delegate = self;
[self applyTouchPlotColor];
[graph addPlot:touchPlot];
[pool drain];
}
EDIT:
The solution came out to:
A. remove the graph.defaultPlotSpace before initializing the plotspaces.
B. adding both plotspaces
[graph addPlotSpace:plotSpace2];
[graph addPlotSpace:plotSpace];
C. asign both plots to it's own plotspace
[graph addPlot:highPlot toPlotSpace:plotSpace];
[graph addPlot:highPlot2 toPlotSpace:plotSpace2];
The plot ranges are part of the plot space. Just update the yRange whenever you want to change the displayed range. You can do this any time, not just when setting up the graph.
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];
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.
This is my code. X-Axis labels are not shown. I am using core plot.
scatterGraph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = nil;
[scatterGraph applyTheme:theme];
hostView.hostedGraph = scatterGraph;
hostView.backgroundColor = [UIColor clearColor];
hostView.collapsesLayers = NO;
scatterGraph.paddingTop = 25.0;
scatterGraph.paddingRight = 25.0;
scatterGraph.paddingLeft = 25.0;
scatterGraph.paddingBottom = 25;
scatterGraph.plotAreaFrame.masksToBorder = NO;
CPXYPlotSpace *scatterPlot = (CPXYPlotSpace *) scatterGraph.defaultPlotSpace;
scatterPlot.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromInt(yCount)];
float xx = [self findMax:LivePriceFeedArray] - [self findMin:LivePriceFeedArray]+1.0;
scatterPlot.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat([self findMin:LivePriceFeedArray]-0.5) length:CPDecimalFromFloat(xx)];
CPXYAxisSet *axisSet =(CPXYAxisSet *) scatterGraph.axisSet;
CPXYAxis *xAxis = axisSet.xAxis;
axisSet.delegate = self;
xAxis.title = #"Days";
xAxis.titleLocation = CPDecimalFromFloat(4.0f);
xAxis.orthogonalCoordinateDecimal = CPDecimalFromFloat(0.0f);
xAxis.majorIntervalLength = CPDecimalFromFloat(5.0f);
xAxis.labelingPolicy = CPAxisLabelingPolicyNone;
NSUInteger labelLocation = 0;
NSMutableArray *customArray = [[NSMutableArray alloc] initWithCapacity:[xAxisLabels count]];
for (NSNumber *i in customLocations) {
CPAxisLabel *iLabel = [[CPAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:xAxis.labelTextStyle];
iLabel.tickLocation = [i decimalValue];
[customArray addObject:iLabel];
[iLabel release];
}
xAxis.axisLabels = [NSSet setWithArray:customArray];
xAxis.majorTickLocations = [NSSet setWithArray:customLocations ];
CPLineStyle *majorGridLineStyle = [CPLineStyle lineStyle];
xAxis.majorGridLineStyle = [CPLineStyle lineStyle];
CPXYAxis *yAxis = axisSet.yAxis;
yAxis.title = #"Sales Target";
yAxis.majorIntervalLength = CPDecimalFromFloat(1.0f);
yAxis.labelOffset = -5.0f;
yAxis.orthogonalCoordinateDecimal = CPDecimalFromFloat(0.0f);
CPScatterPlot *graphPlot = [[[CPScatterPlot alloc]init] autorelease];
CPMutableLineStyle *lineStyle = [[graphPlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPColor whiteColor];
graphPlot.dataLineStyle = lineStyle;
graphPlot.dataSource = self;
[scatterGraph addPlot:graphPlot];
graphPlot.backgroundColor = [UIColor clearColor];
CPLineStyle *minorGridLineStyle = majorGridLineStyle;
xAxis.minorGridLineStyle = minorGridLineStyle;
yAxis.majorGridLineStyle = majorGridLineStyle;
yAxis.minorGridLineStyle = minorGridLineStyle;
Why are the x-axis labels not shown?
I solved the issue. There was a problem with orthogonal coordinates.
I solved it like this:
scatterPlot.yRange = [CPPlotRange
plotRangeWithLocation:
CPDecimalFromFloat (min)
length:CPDecimalFromFloat(xx)];
xAxis.orthogonalCoordinateDecimal =
CPDecimalFromFloat(min);
Here plotRangeWithLocation for y-axis and orthogonalCoordinateDecimal for x-axis should be the same.
Thanks for the help, Krishnabhadra :)