Search code examples
iosshinobi

X axis label formatting in Shinobi charts


I have a chart of name versus age, where name is on the x axis and value on Y axis. The problem is the names are getting overlapped on the x axis which do not look good.

I could not find any formatting for showing truncated name values on x axis and complete as we zoom in.

Is there any way to show names with ellipses or other formatting where the names will not overlap?


Solution

  • The alterTickMark: method on SChartDelegate allows you to modify a tickMark (and its corresponding tickLabel) before they are added to the axis.

    You could potentially check the axisRange in this step, and decide if the range.span is small enough that you could display labels truncated or in full.

    E.g.

    -(void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis
    {
        if (!axis.isXAxis)
            return;
    
        if ([axis.axisRange.span doubleValue] > 5)
        {
            NSString *shortText = [tickMark.tickLabel.text substringToIndex:3];
            tickMark.tickLabel.text = [NSString stringWithFormat:@"%@...", shortText];
    
            //Resize, but maintain centering
            CGPoint center = tickMark.tickLabel.center;
            [tickMark.tickLabel sizeToFit];
            tickMark.tickLabel.center = center;
        }
    }
    

    As full disclosure, I work for ShinobiControls.