Search code examples
objective-ccore-plotios6.1

Can we show gridlines at the base value or tip of each bars in a barchart using coreplot


Can we show gridlines at the base value or tip of each bars in a barchart using coreplot(iOS).

If there is any approach to do this suggest me.

enter image description here


Solution

  • These are not grid lines since this is no grid, but what you can do is to add additional y axes for each vertical line you want to show. I use this principle to show an indicator line at the mouse position. Code is:

    CPTXYAxis *y = axisSet.yAxis;
    ... some other setup
    
    // The second y axis is used as the current location identifier.
    indicatorLine = [[CPTXYAxis alloc] init];
    indicatorLine.hidden = YES;
    indicatorLine.coordinate = CPTCoordinateY;
    indicatorLine.plotSpace = graph.defaultPlotSpace;
    indicatorLine.labelingPolicy = CPTAxisLabelingPolicyNone;
    indicatorLine.separateLayers = NO;
    indicatorLine.preferredNumberOfMajorTicks = 0;
    indicatorLine.minorTicksPerInterval = 0;
    
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 1;
    lineStyle.lineColor = [CPTColor colorWithGenericGray: 64 / 255.0];
    lineStyle.lineCap = kCGLineCapRound;
    lineStyle.dashPattern = lineStyle.dashPattern = @[@10.0f, @5.0f];
    indicatorLine.axisLineStyle = lineStyle;
    indicatorLine.majorTickLineStyle = nil;
    
    axisSet.axes = @[x, y, indicatorLine];
    

    You can add as many as you want. Set their position (in data coordinate space, that is, your x data values) by

    indicatorLine.orthogonalCoordinateDecimal = CPTDecimalFromFloat(index);
    

    Index is here an x-data point, but can also include intermediate positions.