I went through the tutorial here: http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application, and I can get a graph to show, but I don't see any tick marks or labels on my axis set. Is there something simple I'm overlooking? Below is my code to create the graph, and I also implemented the delegate functions. I am seeing the axis and the plot, but not axis tick marks or labels.
Thanks for any help you can give.
- (void) showGraph {
graph = [[CPXYGraph alloc] initWithFrame: CGRectMake(0,0, 320, 320)];
CPLayerHostingView *graphView = [[CPLayerHostingView alloc] initWithFrame:CGRectMake(0,0, 320, 320)];
graphView.backgroundColor = [UIColor blueColor];
AppDelegate *t = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[t.mapVC.view addSubview:graphView];
graphView.hostedLayer = graph;
graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;
float minElevation = 0;
float maxElevation = 10;
float minTime = 0;
float maxTime = 5;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(minTime)
length:CPDecimalFromFloat(maxTime)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(minElevation)
length:CPDecimalFromFloat(maxElevation)];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPLineStyle *blackStyle = [CPLineStyle lineStyle];
blackStyle.lineColor = [CPColor blackColor];
blackStyle.lineWidth = 5.0f;
CPLineStyle *whiteStyle = [CPLineStyle lineStyle];
whiteStyle.lineColor = [CPColor whiteColor];
whiteStyle.lineWidth = 5.0f;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"5"]decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = whiteStyle;
axisSet.xAxis.minorTickLineStyle = whiteStyle;
axisSet.xAxis.axisLineStyle = blackStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 10.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"5"]decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = blackStyle;
axisSet.yAxis.minorTickLineStyle = blackStyle;
axisSet.yAxis.axisLineStyle = whiteStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 10.0f;
axisSet.yAxis.labelOffset = 3.0f;
CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc]
initWithFrame:graph.bounds] autorelease];
xSquaredPlot.identifier = #"X Squared Plot";
xSquaredPlot.dataLineStyle.lineWidth = 2.0f;
xSquaredPlot.dataLineStyle.lineColor = [CPColor greenColor];
xSquaredPlot.dataSource = self;
[graph addPlot:xSquaredPlot];
CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;
}
You need some space around the outside of the axes. Core Plot doesn't yet do this for you automatically.
Try adding some padding to graph.plotArea.plotGroup and graph.plotArea.axisSet. See the bar plot in the iPhone test app for an example.
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 have implemented a CorePlot graph in my iOS application, however I am unable to dynamically size the y-Axis based on the data points in the set. Some datasets range from 10-50, while others would range from 400-500. In both cases, I would like to have y-origin (0) visible.
I have tried using the scaletofitplots method:
[graph.defaultPlotSpace scaleToFitPlots:[graph allPlots]];
but this has no effect on the graphs, they still show the default Y-Axis range of 0 to 1.
The only way that I can change the y-Axis is to do it manually via this:
graph.defaultPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f)) length:CPTDecimalFromFloat(500.0f)];
however this doesn't work well for graphs with smaller ranges (10-50), as you can imagine.
Is there any method to retrieve the highest y-value from my dataset so that I can manually set the maximum y-Axis to that value? Or is there a better alternative?
EDIT : Here's the file I am using:
#import "TUTSimpleScatterPlot.h"
#implementation TUTSimpleScatterPlot
#synthesize hostingView = _hostingView;
#synthesize graph = _graph;
#synthesize graphData = _graphData;
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data {
self = [super init];
if (self != nil) {
self.hostingView = hostingView;
self.graphData = data;
self.graph = nil;
}
return self;
}
-(void)initialisePlot {
if ((self.hostingView == nil) || (self.graphData == nil)) {
NSLog(#"TUTSimpleScatterPlot: Cannot Initialise plot with hosting view and data");
return;
}
if (self.graph != nil) {
NSLog(#"TUTSimpleScatterPlot: Graph Object already exists.");
}
NSDate *refDate = [NSDate date];
NSTimeInterval oneDay = 24 * 60 * 60;
CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
self.graph.plotAreaFrame.paddingBottom = 20.0f;
self.graph.plotAreaFrame.paddingLeft = 35.0f;
self.hostingView.hostedGraph = self.graph;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *lineStyleThin = [CPTMutableLineStyle lineStyle];
lineStyleThin.lineColor = [CPTColor whiteColor];
lineStyleThin.lineWidth = 1.0f;
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = #"HelveticaNeue-Medium";
textStyle.fontSize = 10;
textStyle.color = [CPTColor whiteColor];
CPTMutableLineStyle *plotSymbolLineStyle = [CPTMutableLineStyle lineStyle];
plotSymbolLineStyle.lineColor = [CPTColor whiteColor];
plotSymbolLineStyle.lineWidth = 1.0f;
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.lineStyle = plotSymbolLineStyle;
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.4 green:0.6 blue:0.8 alpha:1.0]];
plotSymbol.size = CGSizeMake(8.0, 8.0);
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init];
plot.dataSource = self;
plot.identifier = #"mainPlot";
[plotSpace scaleToFitPlots:[graph allPlots]];
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromDouble(615)];
plotSpace.yRange = yRange;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(oneDay - (oneDay * 6.3f)) length:CPTDecimalFromFloat(oneDay * 6.6f)];
plotSpace.allowsUserInteraction = YES;
CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromDouble(615.0f)];
plotSpace.globalYRange = globalYRange;
// Modify the graph's axis with a label, line style, etc
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 1.0f;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay*1);
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0.0");
axisSet.xAxis.minorTicksPerInterval = 0;
axisSet.xAxis.majorTickLength = 0.0f;
axisSet.xAxis.majorGridLineStyle = majorGridLineStyle;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyleThin;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(#"50.0");
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTickLength = 3.0;
axisSet.yAxis.majorTickLength = 5.0;
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);
axisSet.yAxis.majorGridLineStyle = majorGridLineStyle;
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
CPTMutableLineStyle *theLineStyle = [CPTMutableLineStyle lineStyle];
theLineStyle.lineColor = [CPTColor colorWithComponentRed:0.3 green:0.6 blue:0.9 alpha:1.0];
theLineStyle.lineWidth = 2.0f;
plot.dataLineStyle = theLineStyle;
plot.plotSymbol = plotSymbol;
[self.graph addPlot:plot];
}
It looks like you're using -scaleToFitPlots: correctly. This method will update the plot data if needed. Make sure the proper data is available to the datasource before you call it.
Edit based on the new code:
-scaleToFitPlots: is being called before you add the plot to the graph. Therefore the -allPlots array is empty and there is nothing to scale. Move the scaling operation after the plot is built and added to 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.
I'm building a graph with core plot, that works fine except that I do not see any labels on the x and y axis. I can only see major and minor ticks but no value next to them. There must be something I'm missing but I do not know what. Also, is it possible to add a label for each axis, for instance: x (hours), y (value).
The graph I am working on is part of the first row of a listview.
The code is:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"GraphCustomViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[GraphCustomViewCell class]])
{
cell = (GraphCustomViewCell *)currentObject;
break;
}
}
CGRect frame = cell.contentView.bounds;
CPLayerHostingView *hostingView = [[CPLayerHostingView alloc] initWithFrame: frame];
[cell addSubview:hostingView];
// Create graph
CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
graph = (CPXYGraph *)[theme newGraph];
// Add graph to view
hostingView.hostedLayer = graph;
graph.paddingLeft = 50.0;
graph.paddingTop = 5.0;
graph.paddingRight = 5.0;
graph.paddingBottom = 25.0;
// Adjust graph
float xmax = [[[self arrayFromData] objectForKey:#"data"] count];
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
length:CPDecimalFromFloat(xmax)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
length:CPDecimalFromFloat(3000)];
CPTextStyle *blackText = [[CPTextStyle textStyle] color:[CPColor blackColor]];
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
lineStyle.lineWidth = 1.0f;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"5"] decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 4.0f;
axisSet.xAxis.majorTickLength = 8.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.labelTextStyle = blackText;
axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"500"] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 4.0f;
axisSet.yAxis.majorTickLength = 8.0f;
axisSet.yAxis.labelOffset = 1.0f;
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"MyGraph";
dataSourceLinePlot.dataLineStyle.lineWidth = 1.f;
dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Put an area gradient under the plot above
CPColor *areaColor = [CPColor colorWithComponentRed:0.3
green:1.0
blue:0.3
alpha:0.3];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor
endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPDecimalFromString(#"1.75");
Thanks a lot,
Regards,
Luc
ps: quick edit, I have checked with graph.name = "my graph", but nothing is displayed on the graph
OK, this was due to the fact there were too many labels to display and also because the axis did not went far enough in the negative area => no place to display the labels. But, graph.name does not display a name of the graph...