Reputation: 13195
I would like to use core plot to chart my data as follows: for each data point (x,y), draw a vertical line starting at (x,0) and running to (x,y).
The data are medication doses, mapped against time of dose.
Any help would be greatly appreciated!
Upvotes: 0
Views: 3487
Reputation: 8114
I think this might help you.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Core-Plot";
graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[graph applyTheme:theme];
CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1);
UILabel *lbl;
for( int i = 21; i <= 24; i++ ){
lbl = (UILabel *)[self.view viewWithTag:i];
lbl.transform = CGAffineTransformTranslate(verticalFlip, 0, -300);
}
CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
hostingView.hostedLayer = graph;
graph.plotArea.masksToBorder = NO;
graph.paddingLeft = 50.0;
graph.paddingTop = 60.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 50.0;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) length:CPDecimalFromFloat(6)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) length:CPDecimalFromFloat(([[expectedCigArray objectAtIndex:0] floatValue]+5))];
plotSpace.allowsUserInteraction=YES;
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPDecimalFromString(@"1");
x.constantCoordinateValue = CPDecimalFromString(@"0");
x.title = @"Days";
x.titleLocation = CPDecimalFromFloat(1.5f);
CPXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPDecimalFromString(@"5");
y.constantCoordinateValue = CPDecimalFromString(@"0");
y.title = @"Money";
y.titleOffset = 30.0f;
y.titleLocation = CPDecimalFromFloat(10.0f);
CPBarPlot *expectedMoney = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:NO];
expectedCigPlot.identifier = @"expectedCigPlot";
expectedCigPlot.cornerRadius = 2.0f;
expectedCigPlot.barWidth = 15.0f;
expectedCigPlot.dataSource = self;
[graph addPlot:expectedCigPlot toPlotSpace:plotSpace];
}
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
return (moneyArray.count + 1);
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if(fieldEnum == CPScatterPlotFieldX)
{
return [NSNumber numberWithInt: index];
}
else
{
if(plot.identifier == @"money")
{
if(index == 0)
return [NSNumber numberWithDouble:0.0];
return [NSNumber numberWithDouble:[[moneyArray objectAtIndex:(index-1)] doubleValue]];
}
}
}
Note that in the above code money array has value of money, So I think your purpose can be solved by passing the y value in the array.
Upvotes: 0
Reputation: 27381
Sounds like a bar plot. Set barsAreHorizontal = NO
to make vertical bars. Use barWidth
, lineStyle
, and fill
to customize the appearance. Several of the example programs use bar plots—look at them for ideas.
Upvotes: 2