1e41f4b71Sopenharmony_ci# chart
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **\<chart>** component displays line charts and bar charts.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Child Components
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciNot supported
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Attributes
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci| Name      | Type                       | Mandatory  | Description                                      |
18e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------- |
19e41f4b71Sopenharmony_ci| type     | string                    | No   | Chart type. Dynamic modification is not supported. Available values include:<br>- **bar**: bar chart<br>- **line**: line chart<br>Default value: **line**|
20e41f4b71Sopenharmony_ci| options  | ChartOptions              | Yes   | Chart parameters. You can set the minimum value, maximum value, scale, and line width of the x-axis or y-axis, whether to display the x-axis and y-axis, and whether the line is smooth. Dynamic modification is not supported.|
21e41f4b71Sopenharmony_ci| datasets | Array&lt;ChartDataset&gt; | Yes   | Data sets. You can set multiple datasets and their background colors.    |
22e41f4b71Sopenharmony_ci| id       | string                    | No   | Unique ID of the component.                                |
23e41f4b71Sopenharmony_ci| style    | string                    | No   | Style declaration of the component.                                |
24e41f4b71Sopenharmony_ci| class    | string                    | No   | Style class of the component, which is used to refer to a style table.                         |
25e41f4b71Sopenharmony_ci| ref      | string                    | No   | Reference information of child elements, which is registered with the parent component on **$refs**.|
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci  **Table 1** ChartOptions
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci| Name    | Type         | Mandatory  | Description                                      |
30e41f4b71Sopenharmony_ci| ------ | ----------- | ---- | ---------------------------------------- |
31e41f4b71Sopenharmony_ci| xAxis  | ChartAxis   | Yes   | X-axis parameters. You can set the minimum value, maximum value, and scale of the x-axis, and whether to display the x-axis.         |
32e41f4b71Sopenharmony_ci| yAxis  | ChartAxis   | Yes   | Y-axis parameters. You can set the minimum value, maximum value, and scale of the y-axis, and whether to display the y-axis.         |
33e41f4b71Sopenharmony_ci| series | ChartSeries | No   | Data series parameters which cover the following:<br>- Line style, such as the line width and whether the line is smooth.<br>- Style and size of the white point at the start of the line.<br>**NOTE**<br>Only line charts support this attribute.|
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci  **Table 2** ChartDataset
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci| Name                         | Type                 | Default Value     | Mandatory  | Description                  |
38e41f4b71Sopenharmony_ci| --------------------------- | ------------------- | -------- | ---- | -------------------- |
39e41f4b71Sopenharmony_ci| backgroundColor(deprecated) | &lt;color&gt;       | \#ff6384 | No   | Color of a line or bar. This attribute is not recommended.    |
40e41f4b71Sopenharmony_ci| strokeColor                 | &lt;color&gt;       | \#ff6384 | No   | Line color. Only line charts support this attribute.        |
41e41f4b71Sopenharmony_ci| fillColor                   | &lt;color&gt;       | \#ff6384 | No   | Fill color. For line charts, the value indicates the gradient color to fill.  |
42e41f4b71Sopenharmony_ci| data                        | Array&lt;number&gt; | -        | Yes   | Data of the drawn line or bar.        |
43e41f4b71Sopenharmony_ci| gradient                    | boolean             | false    | No   | Whether to display the gradient color. Only line charts support this attribute.|
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci  **Table 3** ChartAxis
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci| Name      | Type           | Default Value     | Mandatory  | Description                                      |
48e41f4b71Sopenharmony_ci| -------- | ------------- | -------- | ---- | ---------------------------------------- |
49e41f4b71Sopenharmony_ci| min      | number        | 0        | No   | Minimum value of the axis.<br>Negative numbers are not supported. Only line charts support this attribute.                |
50e41f4b71Sopenharmony_ci| max      | number        | 100      | No   | Maximum value of the axis.<br>Negative numbers are not supported. Only line charts support this attribute.                |
51e41f4b71Sopenharmony_ci| axisTick | number        | 10       | No   | Number of scales displayed on the axis.<br>**NOTE**<br>The value ranges from 1 to 20. The display effect depends on the calculation result of Number of pixels occupied by the image width/(**max**-**min**).<br>Lite wearables support integer calculation, and an error may occur in the case of inexhaustible division. Specifically, a segment of space may be left at the end of the x-axis.<br>In the bar chart, the number of bars in each group of data is the same as the number of scales, and the bars are displayed at the scales.|
52e41f4b71Sopenharmony_ci| display  | boolean       | false    | No   | Whether to display the axis.                                  |
53e41f4b71Sopenharmony_ci| color    | &lt;color&gt; | \#c0c0c0 | No   | Axis color.                                    |
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci  **Table 4** ChartSeries
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci| Name         | Type            | Mandatory  | Description                  |
58e41f4b71Sopenharmony_ci| ----------- | -------------- | ---- | -------------------- |
59e41f4b71Sopenharmony_ci| lineStyle   | ChartLineStyle | No   | Line style, such as the line width and whether the line is smooth.     |
60e41f4b71Sopenharmony_ci| headPoint   | PointStyle     | No   | Style and size of the white point at the start of the line.     |
61e41f4b71Sopenharmony_ci| topPoint    | PointStyle     | No   | Style and size of the top point.          |
62e41f4b71Sopenharmony_ci| bottomPoint | PointStyle     | No   | Style and size of the bottom point.          |
63e41f4b71Sopenharmony_ci| loop        | ChartLoop      | No   | Whether to start drawing again when the screen is looped.|
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci  **Table 5** ChartLineStyle
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci| Name    | Type            | Default Value  | Mandatory  | Description   |
68e41f4b71Sopenharmony_ci| ------ | -------------- | ----- | ---- | ----- |
69e41f4b71Sopenharmony_ci| width  | &lt;length&gt; | 1px   | No   | Line width.|
70e41f4b71Sopenharmony_ci| smooth | boolean        | false | No   | Whether the line is smooth.|
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci  **Table 6** PointStyle
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci| Name         | Type            | Default Value     | Mandatory  | Description                                |
75e41f4b71Sopenharmony_ci| ----------- | -------------- | -------- | ---- | ---------------------------------- |
76e41f4b71Sopenharmony_ci| shape       | string         | circle   | No   | Shape of the highlight point. Available values are as follows:<br>- circle|
77e41f4b71Sopenharmony_ci| size        | &lt;length&gt; | 5px      | No   | Size of the highlight point.                           |
78e41f4b71Sopenharmony_ci| strokeWidth | &lt;length&gt; | 1px      | No   | Stroke width.                              |
79e41f4b71Sopenharmony_ci| strokeColor | &lt;color&gt;  | \#ff0000 | No   | Stroke color.                             |
80e41f4b71Sopenharmony_ci| fillColor   | &lt;color&gt;  | \#ff0000 | No   | Fill color.                             |
81e41f4b71Sopenharmony_ci| display     | boolean        | true     | No   | Whether to display the highlight spot.                           |
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci  **Table 7** ChartLoop
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci| Name    | Type            | Default Value | Mandatory  | Description                                      |
86e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | ---- | ---------------------------------------- |
87e41f4b71Sopenharmony_ci| margin | &lt;length&gt; | 1    | No   | Number of erased points (horizontal distance between the latest drawn point and the earliest point). You are not advised to use **margin** together with **topPoint**, **bottomPoint**, or **headPoint** for mini-, small- and standard-system devices. If you do so, there is a possibility that the point is in the erase area and invisible.|
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci## Methods
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci| Methods    | Parameter                                      | Description                                      |
95e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | ---------------------------------------- |
96e41f4b71Sopenharmony_ci| append | {<br>serial: number, // Set the data subscript of the line chart to be updated.<br>data: Array&lt;number&gt;, // Set the new data.<br>} | Dynamically add data to an existing data series. The target series is specified based on **serial**, which is the subscript of the datasets array and starts from 0. **datasets[index].data** is not updated. Only line charts support this attribute. The value is incremented by 1 based on the horizontal coordinate and is related to the **xAxis min/max** setting.|
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci## Events
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci| Name                | Parameter                               | Description         |
102e41f4b71Sopenharmony_ci| ------------------ | --------------------------------- | ----------- |
103e41f4b71Sopenharmony_ci| click              | -                                 | Triggered when the component is clicked. |
104e41f4b71Sopenharmony_ci| longpress          | -                                 | Triggered when the component is long pressed. |
105e41f4b71Sopenharmony_ci| swipe<sup>5+</sup> | [SwipeEvent](js-lite-common-events.md) | Triggered when a user quickly swipes on the component.|
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci## Styles
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci| Name                                | Type                                      | Default Value  | Mandatory  | Description                                      |
111e41f4b71Sopenharmony_ci| ---------------------------------- | ---------------------------------------- | ----- | ---- | ---------------------------------------- |
112e41f4b71Sopenharmony_ci| width                              | &lt;length&gt; \| &lt;percentage&gt;<sup>5+</sup> | -     | No   | Component width.<br><br>If this attribute is not set, the default value **0** is used.       |
113e41f4b71Sopenharmony_ci| height                             | &lt;length&gt; \| &lt;percentage&gt;<sup>5+</sup> | -     | No   | Component height.<br><br>If this attribute is not set, the default value **0** is used.       |
114e41f4b71Sopenharmony_ci| padding                            | &lt;length&gt;                           | 0     | No   | Shorthand attribute to set the padding for all sides.<br>The attribute can have one to four values:<br>- If you set only one value, it specifies the padding for all the four sides.<br>- If you set two values, the first value specifies the top and bottom padding, and the second value specifies the left and right padding.<br>- If you set three values, the first value specifies the top padding, the second value specifies the left and right padding, and the third value specifies the bottom padding.<br>- If you set four values, they respectively specify the padding for top, right, bottom, and left sides (in clockwise order).|
115e41f4b71Sopenharmony_ci| padding-[left\|top\|right\|bottom] | &lt;length&gt;                           | 0     | No   | Left, top, right, and bottom padding.                         |
116e41f4b71Sopenharmony_ci| margin                             | &lt;length&gt; \| &lt;percentage&gt;<sup>5+</sup> | 0     | No   | Shorthand attribute to set the margin for all sides. The attribute can have one to four values:<br>- If you set only one value, it specifies the margin for all the four sides.<br>- If you set two values, the first value specifies the top and bottom margins, and the second value specifies the left and right margins.<br>- If you set three values, the first value specifies the top margin, the second value specifies the left and right margins, and the third value specifies the bottom margin.<br>- If you set four values, they respectively specify the margin for top, right, bottom, and left sides (in clockwise order).|
117e41f4b71Sopenharmony_ci| margin-[left\|top\|right\|bottom]  | &lt;length&gt; \| &lt;percentage&gt;<sup>5+</sup> | 0     | No   | Left, top, right, and bottom margins.                         |
118e41f4b71Sopenharmony_ci| border-width                       | &lt;length&gt;                           | 0     | No   | Shorthand attribute to set the margin for all sides.                      |
119e41f4b71Sopenharmony_ci| border-color                       | &lt;color&gt;                            | black | No   | Shorthand attribute to set the color for all borders.                      |
120e41f4b71Sopenharmony_ci| border-radius                      | &lt;length&gt;                           | -     | No   | Radius of round-corner borders.           |
121e41f4b71Sopenharmony_ci| background-color                   | &lt;color&gt;                            | -     | No   | Background color.                                 |
122e41f4b71Sopenharmony_ci| display                            | string                                   | flex  | No   | How and whether to display the box containing an element. Available values are as follows:<br>- **flex**: flexible layout<br>- **none**: not rendered|
123e41f4b71Sopenharmony_ci| [left\|top]                        | &lt;length&gt; \| &lt;percentage&gt;<sup>6+</sup> | -     | No   | Edge of the element.<br>- **left**: left edge position of the element. This attribute defines the offset between the left edge of the margin area of a positioned element and left edge of its containing block.<br>- **top**: top edge position of the element. This attribute defines the offset between the top edge of a positioned element and that of a block included in the element.|
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci## Example
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci1. Line chart
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci   ```html
130e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
131e41f4b71Sopenharmony_ci   <div class="container">
132e41f4b71Sopenharmony_ci     <chart class="chart" type="line" ref="linechart" options="{{lineOps}}" datasets="{{lineData}}"></chart>
133e41f4b71Sopenharmony_ci     <input class="button" type="button" value="Add data" onclick="addData"/>
134e41f4b71Sopenharmony_ci   </div>
135e41f4b71Sopenharmony_ci   ```
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci   ```css
139e41f4b71Sopenharmony_ci   /* xxx.css */
140e41f4b71Sopenharmony_ci   .container {
141e41f4b71Sopenharmony_ci     flex-direction: column;
142e41f4b71Sopenharmony_ci     justify-content: center;
143e41f4b71Sopenharmony_ci     align-items: center;
144e41f4b71Sopenharmony_ci     width: 454px;
145e41f4b71Sopenharmony_ci     height: 454px;
146e41f4b71Sopenharmony_ci     background-color: white;
147e41f4b71Sopenharmony_ci   }
148e41f4b71Sopenharmony_ci   .chart {
149e41f4b71Sopenharmony_ci     width: 300px;
150e41f4b71Sopenharmony_ci     height: 300px;
151e41f4b71Sopenharmony_ci   }
152e41f4b71Sopenharmony_ci   .button {
153e41f4b71Sopenharmony_ci     width: 280px;
154e41f4b71Sopenharmony_ci     border-radius: 0px;
155e41f4b71Sopenharmony_ci   }
156e41f4b71Sopenharmony_ci   ```
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci   ```js
160e41f4b71Sopenharmony_ci   // xxx.js
161e41f4b71Sopenharmony_ci   export default {
162e41f4b71Sopenharmony_ci     data: {
163e41f4b71Sopenharmony_ci       lineData: [
164e41f4b71Sopenharmony_ci         {
165e41f4b71Sopenharmony_ci           strokeColor: '#0081ff',
166e41f4b71Sopenharmony_ci           fillColor: '#cce5ff',
167e41f4b71Sopenharmony_ci           data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716],
168e41f4b71Sopenharmony_ci           gradient: false,
169e41f4b71Sopenharmony_ci         }
170e41f4b71Sopenharmony_ci       ],
171e41f4b71Sopenharmony_ci       lineOps: {
172e41f4b71Sopenharmony_ci         xAxis: {
173e41f4b71Sopenharmony_ci           min: 0,
174e41f4b71Sopenharmony_ci           max: 20,
175e41f4b71Sopenharmony_ci           display: false,
176e41f4b71Sopenharmony_ci         },
177e41f4b71Sopenharmony_ci         yAxis: {
178e41f4b71Sopenharmony_ci           min: 0,
179e41f4b71Sopenharmony_ci           max: 1000,
180e41f4b71Sopenharmony_ci           display: false,
181e41f4b71Sopenharmony_ci         },
182e41f4b71Sopenharmony_ci         series: {
183e41f4b71Sopenharmony_ci           lineStyle: {
184e41f4b71Sopenharmony_ci             width: "5px",
185e41f4b71Sopenharmony_ci             smooth: true,
186e41f4b71Sopenharmony_ci           },
187e41f4b71Sopenharmony_ci           headPoint: {
188e41f4b71Sopenharmony_ci             shape: "circle",
189e41f4b71Sopenharmony_ci             size: 10,
190e41f4b71Sopenharmony_ci             strokeWidth: 5,
191e41f4b71Sopenharmony_ci             fillColor: '#ffffff',
192e41f4b71Sopenharmony_ci             strokeColor: '#007aff',
193e41f4b71Sopenharmony_ci             display: true,
194e41f4b71Sopenharmony_ci           },
195e41f4b71Sopenharmony_ci           loop: {
196e41f4b71Sopenharmony_ci             margin: 2,
197e41f4b71Sopenharmony_ci             gradient: true,
198e41f4b71Sopenharmony_ci           }
199e41f4b71Sopenharmony_ci         }
200e41f4b71Sopenharmony_ci       },
201e41f4b71Sopenharmony_ci     },
202e41f4b71Sopenharmony_ci     addData() {
203e41f4b71Sopenharmony_ci       this.$refs.linechart.append({
204e41f4b71Sopenharmony_ci         serial: 0,
205e41f4b71Sopenharmony_ci         data: [Math.floor(Math.random() * 400) + 400]
206e41f4b71Sopenharmony_ci       })
207e41f4b71Sopenharmony_ci     }
208e41f4b71Sopenharmony_ci   }
209e41f4b71Sopenharmony_ci   ```
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci   ![lite_line](figures/lite_line.PNG)
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci2. Bar chart
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci   ```html
216e41f4b71Sopenharmony_ci   <!-- xxx.hml -->
217e41f4b71Sopenharmony_ci   <div class="container">
218e41f4b71Sopenharmony_ci     <chart class="chart" type="bar" id="bar-chart" options="{{barOps}}" datasets="{{barData}}"></chart>
219e41f4b71Sopenharmony_ci   </div>
220e41f4b71Sopenharmony_ci   ```
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci   ```css
224e41f4b71Sopenharmony_ci   /* xxx.css */
225e41f4b71Sopenharmony_ci   .container {
226e41f4b71Sopenharmony_ci     flex-direction: column;
227e41f4b71Sopenharmony_ci     justify-content: center;
228e41f4b71Sopenharmony_ci     align-items: center;
229e41f4b71Sopenharmony_ci     width: 454px;
230e41f4b71Sopenharmony_ci     height: 454px;
231e41f4b71Sopenharmony_ci     background-color: white;
232e41f4b71Sopenharmony_ci   }
233e41f4b71Sopenharmony_ci   .chart {
234e41f4b71Sopenharmony_ci     width: 300px;
235e41f4b71Sopenharmony_ci     height: 300px;
236e41f4b71Sopenharmony_ci   }
237e41f4b71Sopenharmony_ci   ```
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci   ```js
241e41f4b71Sopenharmony_ci   // xxx.js
242e41f4b71Sopenharmony_ci   export default {
243e41f4b71Sopenharmony_ci     data: {
244e41f4b71Sopenharmony_ci       barData: [
245e41f4b71Sopenharmony_ci         {
246e41f4b71Sopenharmony_ci           fillColor: '#f07826',
247e41f4b71Sopenharmony_ci           data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628],
248e41f4b71Sopenharmony_ci         },
249e41f4b71Sopenharmony_ci         {
250e41f4b71Sopenharmony_ci           fillColor: '#cce5ff',
251e41f4b71Sopenharmony_ci           data: [535, 776, 615, 444, 694, 785, 677, 609, 562, 410],
252e41f4b71Sopenharmony_ci         },
253e41f4b71Sopenharmony_ci         {
254e41f4b71Sopenharmony_ci           fillColor: '#ff88bb',
255e41f4b71Sopenharmony_ci           data: [673, 500, 574, 483, 702, 583, 437, 506, 693, 657],
256e41f4b71Sopenharmony_ci         },
257e41f4b71Sopenharmony_ci       ],
258e41f4b71Sopenharmony_ci       barOps: {
259e41f4b71Sopenharmony_ci         xAxis: {
260e41f4b71Sopenharmony_ci           min: 0,
261e41f4b71Sopenharmony_ci           max: 20,
262e41f4b71Sopenharmony_ci           display: false,
263e41f4b71Sopenharmony_ci           axisTick: 10
264e41f4b71Sopenharmony_ci         },
265e41f4b71Sopenharmony_ci         yAxis: {
266e41f4b71Sopenharmony_ci           min: 0,
267e41f4b71Sopenharmony_ci           max: 1000,
268e41f4b71Sopenharmony_ci           display: false,
269e41f4b71Sopenharmony_ci         },
270e41f4b71Sopenharmony_ci       },
271e41f4b71Sopenharmony_ci     }
272e41f4b71Sopenharmony_ci   }
273e41f4b71Sopenharmony_ci   ```
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci   ![lite_bar](figures/lite_bar.PNG)
276