ChartDirector Support Forum Home   Search
Divisions on a line chart.
Posted by Tony on Nov-25-2021 18:19
Attachments:
I've got a graph and I want to set the number of "divisions" that are visible.

I've tried the following code, start with the plot area width and divide by 3 before setting the tick spacing

nTickSpacing = nPlotAreaW / nNumDivisions;
c->xAxis()->setTickDensity(nTickSpacing);

This works when I want 2 or 4 divisions, but if I set it to 3, I still get 2 divisions. And if I set nNumDivisions to any number between 5 and 9, I only get 4. But if I set it to 10, I get 8 divisions.

The Y axis is behaving perfectly.

Am I missing a call or a setting?

The attached graph shows what I mean. It has 3 vertical divisions and 2 horizontal (even though I've instructed it to display 3 horizontal divisions).

Tony.

Re: Divisions on a line chart.
Posted by Peter Kwan on Nov-26-2021 00:00
Hi Tony,

The setTickDensity sets the minimum pixel distance between two ticks when the axis is auto-scaled by ChartDirector. The actual distance can be bigger than the minimum distance.

For example, if the axis scale is 0 to 40, and you set the tick density so that there can be 4 labels on the chart, the labels will be 0, 13.3333333, 26.6666666, 40. If the axis is auto-scaled by ChartDirector, ChartDirector will prefer (0, 10, 20, 30, 40) or (0, 20, 40). The (0, 10, 20, 30, 40) cannot be used because the tick density allow at most 4 labels, so (0, 20, 40) is used.

ChartDirector auto-scaling will ensure the actual labels look "nice" and match the data. The 0, 20, 40 is acceptable while 0, 13.3333333, .... does not look "nice". That is why the actual tick distance can be bigger, as it needs some degree of freedom to satisfy all these requirements.

The only method to ensure there is always 4 labels is to specify the scale with your own code, using Axis.setLinearScale. For example, you can use:

c->xAxis()->setLinearScale(0, 40, 13.333333333333333333333);

or

c->xAxis()->setLinearScale(0, 14, 42);  // for the labels (0, 14, 28, 42)

(The above use hard coded number. In your actual code, you may need to determine the min and max values, and computer the increment to be (max - min) / 3.

In general, fixing the number of labels often results in unnatural labelling of the axis, or the scale may not match the data.

Regards
Peter Kwan