1
votes

I have added two bar graphs with a barOffset so that both bar graphs show side-by-side.

I also want to add two scatter plots using the same data with each scatter graph touching the tip of the each bar graphs respectively.

Here is how i added the two scatter graphs.

//Add line graph 1
    CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
    dataSourceLinePlot.identifier = @"Scatter-Plot-1";

    CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 3.0f;
    lineStyle.lineColor = [CPTColor orangeColor];
    dataSourceLinePlot.dataLineStyle = lineStyle;

    dataSourceLinePlot.dataSource = self;
    [graph addPlot:dataSourceLinePlot toPlotSpace:barPlotSpace];

    //Add line graph 2
    CPTScatterPlot *dataSourceLinePlot2 = [[[CPTScatterPlot alloc] init] autorelease];
    dataSourceLinePlot2.identifier = @"Scatter-Plot-2";

    CPTMutableLineStyle *lineStyle2 = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
    lineStyle2.miterLimit = 1.0f;
    lineStyle2.lineWidth = 3.0f;
    lineStyle2.lineColor = [CPTColor greenColor];

    dataSourceLinePlot2.dataLineStyle = lineStyle2;
    dataSourceLinePlot2.dataSource = self;
    [graph addPlot:dataSourceLinePlot2 toPlotSpace:barPlotSpace];

But now both scatter graphs start from first bargraph tip itself when datavalue is same

I want each of them touching only bargraph tip.

How can i do that? Is there something barOffset kind of thing for scatter to move it?

I implemented the delegates correctly and data shows up correctly too

2

2 Answers

0
votes

If barWidthsAreInViewCoordinates is NO (the default), you can just add or subtract the barOffset from the x or y coordinate in the datasource.

0
votes

You can adjust the offset of the plotSpace as well for example to start from 0.4 - barPlotSpace.xRange = CPTPlotRange(location:0.5, length:NSNumber(value: end)) but in that case your entire plot is shifting.

Or better as suggested above: in datasource 'number'

public func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any?
{
    let idt = plot.identifier as! NSString;

    switch CPTBarPlotField(rawValue: Int(field))! {
    case .barLocation:
        if idt.compare("Scatter-Plot-1") == .orderedSame {
            // This will shift the Scatter-Plot-1 to the left
            return NSNumber(value: Double(record) + 0.5)
        } else {
            return record as NSNumber
        }
    case .barTip:
        //Enter your code
        return nil

    default:
        return nil
    }
}