Chart with yAxis tick labels inside plot area


#1

Hello. I didn’t find a solution.

I need to stretch the plot area to the whole size of the chart frame and make yAxis label by the right side of yAxis with left aligment.

Could someone help me with it, please?


#2

Hi Andrey,

To make your tickLabels go inside the chart set your “yAxis.style.majorTickStyle.tickGap” to a negative number. Also, set your “yAxis.width” to 2 to hide the space where the tickLabels would usually be placed.

For example:

_chart.yAxis.style.majorTickStyle.tickGap = @-40;
_chart.yAxis.width = @2;

To set the textAlignment of your axis labels you would have to implement the follow SChartDelegate method:

-(void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis {
    tickMark.tickLabel.textAlignment = NSTextAlignmentLeft;
}

Let me know how you get on.

Kind Regards,
Andrew Polkinghorn


#3

Hello. Thanks for your response. But I still have troubles.

_chart.yAxis.style.majorTickStyle.tickGap = @-40;

Sometimes this variable should be equal to @-40 for 0…100,000,000 range, sometimes to @-10 for 0…10 range. It depends on Y value range and I can’t set a constant.

-(void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis {
    tickMark.tickLabel.textAlignment = NSTextAlignmentLeft;
}

I don’t know what’s the reason, but I can’t change aligment this way. I can change color of  tickMark.tickLabel , fontsize, frame, but aligment is always = NSTextAlignmentLeft

Also I can’t change aligment this way: 

_chart.yAxis.style.majorTickStyle.textAlignment = NSTextAlignmentLeft;

#4

Hi Andrey,

I managed to set my label alignment by doing the following:

_chart.yAxis.style.majorTickStyle.textAlignment = NSTextAlignmentLeft;

Can I ask what version of ShinobiCharts you are using?

You are correct that trying to alter the textAlignment in your delegate doesn’t do anything though. This isn’t intended & I’ve raised it as an issue that we will definitely look into. For now though, using the majorTickStyle should do.

To ensure that your Y-axis takes up no room (except for the title) you need to subclass SChartNumberAxis and provide an implementation for the method spaceRequiredToDrawWithTitle:. Below is an example of how I would implement the axis:

-.h

@interface ZeroSpaceAxis : SChartNumberAxis

@end

-.m

@implementation ZeroSpaceAxis

-(float)spaceRequiredToDrawWithTitle:(BOOL)shouldIncludeTitle
{
    if (shouldIncludeTitle) {
        // Work out the size of our axis title.
        CGSize size = [self.title sizeWithFont:self.style.titleStyle.font];
        
        // Return the height of our title label (I've assumed the label is sideways) + a little padding.
        return size.height + 20;
    }
    return 0;
}

@end

Now, you can set this class as your Y-axis, and set a fixed negative tickGap. This combined with the text alignment as I mentioned above should get you the effect you are looking for. If you don’t plan on having a title on your axis, you can simply return 0 from the spaceRequiredToDrawWithTitle: method.

As I mentioned, I raise the issue with tick alignment & our delegate method. If after ensuring that you have the latest version of ShinobiCharts (2.5.6) and you find that you are still having trouble setting the text alignment using the majorTickStyle object then please let us know.

Thanks,

Jan