需要在每个数据x坐标处使用彩色垂直线的核心图,高度数据y坐标

时间:2010-02-01 02:57:03

标签: iphone charts core-plot

我想使用核心图来绘制我的数据,如下所示:对于每个数据点(x,y),绘制一条从(x,0)开始并运行到(x,y)的垂直线。

数据是药物剂量,与剂量时间相对应。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

听起来像个条形图。设置barsAreHorizontal = NO以生成垂直条。使用barWidthlineStylefill自定义外观。一些示例程序使用条形图 - 查看它们的想法。

答案 1 :(得分:0)

我认为这可能会对你有帮助。

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

    }
}

请注意,在上面的代码中,money数组有钱的价值,所以我认为你的目的可以通过传递数组中的y值来解决。