2
votes

Hi i am looking for a chart where i can draw scatter plot over bar plot(line over bar chart), is it possible to draw line and bar chart in the same graph using core plot?

I tried following in order to get the same but i was not successfull.

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

i tried above and also tried to add the plots in differect plot space i.e. [graph addPlot:linePlot toPlotSpace:secondPlotSpace] still i was not successfull.

i think i am missing something but couldnt find it.

Thanks for help in advance.

1
The code in the question looks fine. Assuming the graph shows up, but the plots are not correct, I suspect the problem is in the datasource methods. - Eric Skroch

1 Answers

0
votes

Did you implement the following method to return the chart values?

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

    }
}