What is the logic behind lv_chart_set_axis_tick

Since an answer is taking a bit to long (no problems there by the way) I decided to just investigate myself. See this post as my notes/documentation for lv_chart_set_axis_tick and the callback event attached via lv_obj_add_event_cb(chart, DrawCallbackEvent, LV_EVENT_DRAW_PART_BEGIN, NULL);

The parameters major_len and minor_len. Although it seems obvious in hindsight it was initially a mystery (for me). These 2 are literally the length (in pixels) of the lines on the side of the graph.
Sample 1: major_len = 20, minor_len = 1
image
Sample 2: major_len = 10, minor_len = 10
image
Sample 3: major_len = 1, minor_len = 20 (don’t know why you would do this, but it is possible)
image

Next up the parameters major_cnt and minor_cnt. These 2 indicate the amount of ticks. The major_cnt indicates the total amount of ticks. The minor_cnt indicates the amount of ticks between the major ticks minus 1. Note that a minimum of 1 is required here. If minor_cnt = 0 no ticks are drawn. See also the source (8.3.10) if minor_cnt = 0, total_tick_num becomes 0. This caused the draw_._ticks() method to return early.

Sample 1: major_cnt = 10, minor_cnt = 0
image
Sample 2: major_cnt = 10, minor_cnt = 1
image
Sample 3: major_cnt = 20, minor_cnt = 3
image

The parameter label_en seems obvious, this enables/disables the label(s).
The final parameter draw_size is also unclear. However the documentation explains this better than I can.

Extra size required to draw the tick and labels (start with 20 px and increase if the ticks/labels are clipped)

Next up: The DrawCallbackEvent.
In the event we first get the draw descriptor pointer via lv_obj_draw_part_dsc_t* dsc = lv_event_get_draw_part_dsc(e);

From there we can check what type we are currently drawing. (See the source for the different types). In this case I’m only interested in LV_CHART_DRAW_PART_TICK_LABEL. We can filter for the type via this statement: lv_obj_draw_part_check_type(dsc, &lv_chart_class, LV_CHART_DRAW_PART_TICK_LABEL)

Next we can start filtering on the axis ID via dsc->id. The possible values here can be found in the source.

Next up, if we look at the source before the event callback

char buf[LV_CHART_LABEL_MAX_TEXT_LENGTH];
lv_snprintf(buf, sizeof(buf), "%" LV_PRId32, tick_value);
part_draw_dsc.label_dsc = &label_dsc;
part_draw_dsc.text = buf;
part_draw_dsc.text_length = LV_CHART_LABEL_MAX_TEXT_LENGTH;

lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);

We can see that a buffer is created (of size 16) where the label is stored (temporarily). In the callback we get the opportunity to override this label.
The start of the buffer is at dsc->text. (Note: This callback is also done for minor ticks. However dsc->text is NULL at those moments.)
The length of the buffer can be found in dsc->text_length
Finally the dsc->value contains the current index being drawn. This is the (major) number you get at the axis if nothing else is being done.

Next post is for my scaling of the X and Y axis.

1 Like