1# Chart (lv_chart)
2
3## Overview
4
5Charts are a basic object to visualize data points. Currently *Line* charts (connect points with lines and/or draw points on them) and *Bar* charts are supported.
6
7Charts can have:
8- division lines
9- 2 y axis
10- axis ticks and texts on ticks
11- cursors
12- scrolling and zooming
13
14## Parts and Styles
15- `LV_PART_MAIN` The background of the chart. Uses all the typical background and *line* (for the division lines) related style properties. *Padding* makes the series area smaller. For column charts `pad_column` sets the space between the columns of the adjacent indices.
16- `LV_PART_SCROLLBAR` The scrollbar used if the chart is zoomed. See the [Base object](/widgets/obj)'s documentation for details.
17- `LV_PART_ITEMS` Refers to the line or bar series.
18    - Line chart: The *line* properties are used by the lines. `width`, `height`, `bg_color` and `radius` is used to set the appearance of points.
19    - Bar chart: The typical background properties are used to style the bars. `pad_column` sets the space between the columns on the same index.
20- `LV_PART_INDICATOR` Refers to the points on line and scatter chart (small circles or squares).
21- `LV_PART_CURSOR` *Line* properties are used to style the cursors.  `width`, `height`, `bg_color` and `radius` are used to set the appearance of points.
22- `LV_PART_TICKS` *Line* and *Text* style properties are used to style the ticks
23
24## Usage
25
26
27### Chart type
28The following data display types exist:
29- `LV_CHART_TYPE_NONE`  Do not display any data. Can be used to hide the series.
30- `LV_CHART_TYPE_LINE`  Draw lines between the data points and/or points (rectangles or circles) on the data points.
31- `LV_CHART_TYPE_BAR` - Draw bars.
32- `LV_CHART_TYPE_SCATTER` - X/Y chart drawing point's and lines between the points. .
33
34You can specify the display type with `lv_chart_set_type(chart, LV_CHART_TYPE_...)`.
35
36
37### Data series
38You can add any number of series to the charts by `lv_chart_add_series(chart, color, axis)`. This allocates an `lv_chart_series_t` structure which contains the chosen `color` and an array for the data points.
39`axis` can have the following values:
40- `LV_CHART_AXIS_PRIMARY_Y` Left axis
41- `LV_CHART_AXIS_SECONDARY_Y` Right axis
42- `LV_CHART_AXIS_PRIMARY_X` Bottom axis
43- `LV_CHART_AXIS_SECONDARY_X` Top axis
44
45
46`axis` tells which axis's range should be used te scale the values.
47
48`lv_chart_set_ext_y_array(chart, ser, value_array)` makes the chart use an external array for the given series.
49`value_array` should look like this: `lv_coord_t * value_array[num_points]`. The array size needs to be large enough to hold all the points of that series.
50The array's pointer will be saved in the chart so it needs to be global, static or dynamically allocated.
51Note: you should call `lv_chart_refresh(chart)` after the external data source has been updated to update the chart.
52
53The value array of a series can be obtained with `lv_chart_get_y_array(chart, ser)`, which can be used with `ext_array` or *normal array*s.
54
55For `LV_CHART_TYPE_SCATTER` type  `lv_chart_set_ext_x_array(chart, ser, value_array)` and `lv_chart_get_x_array(chart, ser)` can be used as well.
56
57### Modify the data
58You have several options to set the data of series:
591. Set the values manually in the array like `ser1->points[3] = 7` and refresh the chart with `lv_chart_refresh(chart)`.
602. Use `lv_chart_set_value_by_id(chart, ser, id, value)` where `id` is the index of the point you wish to update.
613. Use the `lv_chart_set_next_value(chart, ser, value)`.
624. Initialize all points to a given value with: `lv_chart_set_all_value(chart, ser, value)`.
63
64Use `LV_CHART_POINT_NONE` as value to make the library skip drawing that point, column, or line segment.
65
66For `LV_CHART_TYPE_SCATTER` type  `lv_chart_set_value_by_id2(chart, ser, id, value)` and `lv_chart_set_next_value2(chart, ser, x_valuem y_value)` can be used as well.
67
68
69### Update modes
70`lv_chart_set_next_value` can behave in two ways depending on *update mode*:
71- `LV_CHART_UPDATE_MODE_SHIFT` Shift old data to the left and add the new one to the right.
72- `LV_CHART_UPDATE_MODE_CIRCULAR` - Add the new data in circular fashion, like an ECG diagram.
73
74The update mode can be changed with `lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_...)`.
75
76### Number of points
77The number of points in the series can be modified by `lv_chart_set_point_count(chart, point_num)`. The default value is 10.
78Note: this also affects the number of points processed when an external buffer is assigned to a series, so you need to be sure the external array is large enough.
79
80#### Handling large number of points
81On line charts, if the number of points is greater than the pixels horizontally, the Chart will draw only vertical lines to make the drawing of large amount of data effective.
82If there are, let's say, 10 points to a pixel, LVGL searches the smallest and the largest value and draws a vertical lines between them to ensure no peaks are missed.
83
84### Vertical range
85You can specify the minimum and maximum values in y-direction with `lv_chart_set_range(chart, axis, min, max)`.
86`axis` can be `LV_CHART_AXIS_PRIMARY` (left axis) or `LV_CHART_AXIS_SECONDARY` (right axis).
87
88The value of the points will be scaled proportionally. The default range is: 0..100.
89
90### Division lines
91The number of horizontal and vertical division lines can be modified by `lv_chart_set_div_line_count(chart, hdiv_num, vdiv_num)`.
92The default settings are 3 horizontal and 5 vertical division lines.
93If there is a visible border on a side and no padding on that side, the division line would be drawn on top of the border and therefore it won't be drawn.
94
95### Override default start point for series
96If you want a plot to start from a point other than the default which is `point[0]` of the series, you can set an alternative
97index with the function `lv_chart_set_x_start_point(chart, ser, id)` where `id` is the new index position to start plotting from.
98
99Note that `LV_CHART_UPDATE_MODE_SHIFT` also changes the `start_point`.
100
101### Tick marks and labels
102Ticks and labels can be added to the axis with `lv_chart_set_axis_tick(chart, axis, major_len, minor_len, major_cnt, minor_cnt, label_en, draw_size)`.
103- `axis` can be `LV_CHART_AXIS_X/PRIMARY_Y/SECONDARY_Y`
104- `major_len` is the length of major ticks
105- `minor_len` is the length of minor ticks
106- `major_cnt` is the number of major ticks on the axis
107- `minor_cnt` in the number of minor ticks between two major ticks
108- `label_en` `true`: enable label drawing on major ticks
109- `draw_size` extra size required to draw the tick and labels (start with 20 px and increase if the ticks/labels are clipped)
110
111### Zoom
112The chart can be zoomed independently in x and y directions with `lv_chart_set_zoom_x(chart, factor)` and `lv_chart_set_zoom_y(chart, factor)`.
113If `factor` is 256 there is no zoom. 512 means double zoom, etc. Fractional values are also possible but < 256 value is not allowed.
114
115
116### Cursor
117
118A cursor can be added with `lv_chart_cursor_t * c1 = lv_chart_add_cursor(chart, color, dir);`.
119The possible values of `dir`  `LV_DIR_NONE/RIGHT/UP/LEFT/DOWN/HOR/VER/ALL` or their OR-ed values to tell in which direction(s) should the cursor be drawn.
120
121`lv_chart_set_cursor_pos(chart, cursor, &point)` sets the position of the cursor.
122`pos` is a pointer to an `lv_point_t` variable. E.g. `lv_point_t point = {10, 20};`. If the chart is scrolled the cursor will remain in the same place.
123
124`lv_chart_get_point_pos_by_id(chart, series, id, &point_out)` gets the coordinate of a given point. It's useful to place the cursor at a given point.
125
126`lv_chart_set_cursor_point(chart, cursor, series, point_id)` sticks the cursor at a point. If the point's position changes (new value or scrolling) the cursor will move with the point.
127
128## Events
129- `LV_EVENT_VALUE_CHANGED` Sent when a new point is clicked pressed.  `lv_chart_get_pressed_point(chart)` returns the zero-based index of the pressed point.
130- `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` are sent with the following types:
131   - `LV_CHART_DRAW_PART_DIV_LINE_INIT`  Used before/after drawn the div lines to add masks to any extra drawings. The following fields are set:
132       -  `part`: `LV_PART_MAIN`
133       - `line_dsc`
134   - `LV_CHART_DRAW_PART_DIV_LINE_HOR`, `LV_CHART_DRAW_PART_DIV_LINE_VER` Used for each horizontal and vertical division lines.
135       - `part`: `LV_PART_MAIN`
136       - `id`: index of the line
137       - `p1`, `p2`: points of the line
138       - `line_dsc`
139   - `LV_CHART_DRAW_PART_LINE_AND_POINT` Used on line and scatter charts for lines and points.
140       - `part`: `LV_PART_ITEMS`
141       - `id`: index of the point
142       - `value`: value of `id`th point
143       - `p1`, `p2`: points of the line
144       - `draw_area`: area of the point
145       - `line_dsc`
146       - `rect_dsc`
147       - `sub_part_ptr`: pointer to the series
148   - `LV_CHART_DRAW_PART_BAR` Used on bar charts for the rectangles.
149        - `part`: `LV_PART_ITEMS`
150        - `id`: index of the point
151        - `value`: value of `id`th point
152        - `draw_area`: area of the point
153        - `rect_dsc`:
154        - `sub_part_ptr`: pointer to the series
155   - `LV_CHART_DRAW_PART_CURSOR`  Used on cursor lines and points.
156        - `part`: `LV_PART_CURSOR`
157        - `p1`, `p2`: points of the line
158        - `line_dsc`
159        - `rect_dsc`
160        - `draw_area`: area of the points
161   - `LV_CHART_DRAW_PART_TICK_LABEL`  Used on tick lines and labels.
162        - `part`: `LV_PART_TICKS`
163        - `id`: axis
164        - `value`: value of the tick
165        - `text`: `value` converted to decimal or `NULL` for minor ticks
166        - `line_dsc`,
167        - `label_dsc`,
168
169See the events of the [Base object](/widgets/obj) too.
170
171Learn more about [Events](/overview/event).
172
173## Keys
174No *Keys* are processed by the object type.
175
176Learn more about [Keys](/overview/indev).
177
178## Example
179
180```eval_rst
181
182.. include:: ../../../examples/widgets/chart/index.rst
183
184```
185
186## API
187
188```eval_rst
189
190.. doxygenfile:: lv_chart.h
191  :project: lvgl
192
193```
194