2

嗨,我正在寻找一个图表,我可以在条形图上绘制散点图(条形图上的线),是否可以使用核心图在同一个图中绘制折线图和条形图?

我尝试跟随以获得相同的结果,但我没有成功。

CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, width, height)];

CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame: hostingView.bounds];
hostingView.hostedGraph = graph;


CPTBarPlot *cptBarPlot = [[CPTBarPlot alloc] init];
            cptBarPlot.fill = [CPTFill fillWithColor:color];
            cptBarPlot.lineStyle = nil;
            cptBarPlot.identifier = [columnSeries objectForKey:@"id"];
            cptBarPlot.name = [columnSeries objectForKey:@"displayName"];
            cptBarPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(BarInitialX) length:CPTDecimalFromDouble(xAxisLength)];

            cptBarPlot.barWidth = CPTDecimalFromDouble(BarWidth);
            cptBarPlot.dataSource = self;
            //cptBarPlot.opacity = 0.0f;
            cptBarPlot.delegate = self;

[graph addPlot:cptBarPlot];


CPTXYPlotSpace * secondPlotSpace = [[CPTXYPlotSpace alloc]init];
        secondPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(BarInitialX) length:CPTDecimalFromDouble(xAxisLength+1)];
        secondPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromFloat(yAxisLength)];

        //[graph addPlotSpace:secondPlotSpace];

        CPTScatterPlot * linePlot = [[CPTScatterPlot alloc] init];

        CPTMutableLineStyle * lineStyle = [CPTMutableLineStyle lineStyle];
        lineStyle.lineWidth = 1.f;
        lineStyle.lineColor = color;
        lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:3.0f],nil];

        linePlot.dataLineStyle = lineStyle;
        linePlot.identifier = [lineSeries objectForKey:@"id"];
        linePlot.dataSource = self;
        linePlot.name = [lineSeries objectForKey:@"displayName"];

        //linePlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(BarInitialX) length:CPTDecimalFromDouble(xAxisLength)];

        [graph addPlot: linePlot];

我在上面尝试过,还尝试在不同的绘图空间中添加绘图,即 [graph addPlot:linePlot toPlotSpace:secondPlotSpace] 仍然没有成功。

我想我错过了一些东西,但找不到它。

提前感谢您的帮助。

4

1 回答 1

0

您是否实现了以下方法来返回图表值?

   -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
        switch (fieldEnum) {
            case CPTBarPlotFieldBarLocation:
                return ...;
                break;
            case CPTBarPlotFieldBarTip:
                return ...;
                break;
            case CPTBarPlotFieldBarBase:
                return ...;
            default:
                return 0;
                break;
        }
    } else {
        switch (fieldEnum) {
            case CPTScatterPlotFieldX:
                return ...;
                break;
            case CPTScatterPlotFieldY:
                return ...;
                break;
            default:
                return 0;
                break;
        }

    }
}
于 2013-08-15T18:32:05.150 回答