Search code examples
iosobjective-ccore-plot

How to draw different line color in Core Plot CPTScatterPlot


enter image description here

For now, I can use CPTScatterPlot draw a Trend Chart like this.
30 dots and have red line connect each other.

But I want use different line color between each dots.

This is my code I'm try

    CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.frame];
    self.hostedGraph = graph;

    CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];
    scatterPlot.dataSource = self;
    [graph addPlot:scatterPlot];

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 1.0f;
    lineStyle.lineColor = [CPTColor redColor];
    scatterPlot.dataLineStyle = lineStyle;

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) scatterPlot.plotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@0 length:@(self.dataModel.data.count -1)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:d length:@([c integerValue] - [d integerValue])];

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    CPTXYAxis *y = axisSet.yAxis;
    x.orthogonalPosition = z;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.orthogonalPosition = @0;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;

    CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
    symbolLineStyle.lineColor = [CPTColor blackColor];
    symbolLineStyle.lineWidth = 1.0;

    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    plotSymbol.fill = [CPTFill fillWithColor:[CPTColor redColor]];
    plotSymbol.lineStyle = symbolLineStyle;
    plotSymbol.size = CGSizeMake(5.0, 5.0);
    scatterPlot.plotSymbol = plotSymbol;

Solution

  • You have to use multiple scatter plots for this. Use one for each different color.