1e41f4b71Sopenharmony_ci# chart 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci图表组件,用于呈现线形图、柱状图界面。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 该组件从从API version 4 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 子组件 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci不支持。 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 属性 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 描述 | 18e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------- | 19e41f4b71Sopenharmony_ci| type | string | 否 | 设置图表类型(不支持动态修改),可选项有:<br/>- bar:柱状图。<br/>- line:线形图。<br>默认值:line | 20e41f4b71Sopenharmony_ci| options | ChartOptions | 是 | 图表参数设置,柱状图和线形图必须设置参数设置。可以设置x轴、y轴的最小值、最大值、刻度数、是否显示,线条宽度、是否平滑等。(不支持动态修改) | 21e41f4b71Sopenharmony_ci| datasets | Array<ChartDataset> | 是 | 数据集合,柱状图和线形图必须设置数据集合。可以设置多条数据集及其背景色。 | 22e41f4b71Sopenharmony_ci| id | string | 否 | 组件的唯一标识。 | 23e41f4b71Sopenharmony_ci| style | string | 否 | 组件的样式声明。 | 24e41f4b71Sopenharmony_ci| class | string | 否 | 组件的样式类,用于引用样式表。 | 25e41f4b71Sopenharmony_ci| ref | string | 否 | 用来指定指向子元素的引用信息,该引用将注册到父组件的$refs 属性对象上。 | 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci **表1** ChartOptions 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 描述 | 30e41f4b71Sopenharmony_ci| ------ | ----------- | ---- | ---------------------------------------- | 31e41f4b71Sopenharmony_ci| xAxis | ChartAxis | 是 | x轴参数设置。可以设置x轴最小值、最大值、刻度数以及是否显示。 | 32e41f4b71Sopenharmony_ci| yAxis | ChartAxis | 是 | y轴参数设置。可以设置y轴最小值、最大值、刻度数以及是否显示。 | 33e41f4b71Sopenharmony_ci| series | ChartSeries | 否 | 数据序列参数设置。可以设置:<br>1. 线的样式,如线宽、是否平滑;<br>2. 设置线最前端位置白点的样式和大小。<br/>**说明:**<br>仅线形图支持。 | 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci **表2** ChartDataset 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 38e41f4b71Sopenharmony_ci| --------------------------- | ------------------- | -------- | ---- | -------------------- | 39e41f4b71Sopenharmony_ci| backgroundColor(deprecated) | <color> | \#ff6384 | 否 | 设置线或柱的颜色(不推荐使用)。 | 40e41f4b71Sopenharmony_ci| strokeColor | <color> | \#ff6384 | 否 | 线条颜色。仅线形图支持。 | 41e41f4b71Sopenharmony_ci| fillColor | <color> | \#ff6384 | 否 | 填充颜色。线形图表示填充的渐变颜色。 | 42e41f4b71Sopenharmony_ci| data | Array<number> | - | 是 | 设置绘制线或柱中的点集。 | 43e41f4b71Sopenharmony_ci| gradient | boolean | false | 否 | 设置是否显示填充渐变颜色。仅线形图支持。 | 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci **表3** ChartAxis 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 48e41f4b71Sopenharmony_ci| -------- | ------------- | -------- | ---- | ---------------------------------------- | 49e41f4b71Sopenharmony_ci| min | number | 0 | 否 | 轴的最小值。<br/>不支持负数。仅线形图支持。 | 50e41f4b71Sopenharmony_ci| max | number | 100 | 否 | 轴的最大值。<br/>不支持负数。仅线形图支持。 | 51e41f4b71Sopenharmony_ci| axisTick | number | 10 | 否 | 轴显示的刻度数量。<br/>**说明:**<br/>仅支持1~20,且具体显示的效果与如下计算值有关(图的宽度所占的像素/(max-min))。<br/>因轻量级智能穿戴为整型运行,在除不尽的情况下会有误差产生,具体的表现形式是x轴末尾可能会空出一段。<br/>在柱状图中,每组数据显示的柱子数量与刻度数量一致,且柱子显示在刻度处。 | 52e41f4b71Sopenharmony_ci| display | boolean | false | 否 | 是否显示轴。 | 53e41f4b71Sopenharmony_ci| color | <color> | \#c0c0c0 | 否 | 轴颜色。 | 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci **表4** ChartSeries 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 描述 | 58e41f4b71Sopenharmony_ci| ----------- | -------------- | ---- | -------------------- | 59e41f4b71Sopenharmony_ci| lineStyle | ChartLineStyle | 否 | 线样式设置,如线宽、是否平滑。 | 60e41f4b71Sopenharmony_ci| headPoint | PointStyle | 否 | 线最前端位置白点的样式和大小。 | 61e41f4b71Sopenharmony_ci| topPoint | PointStyle | 否 | 最高点的样式和大小。 | 62e41f4b71Sopenharmony_ci| bottomPoint | PointStyle | 否 | 最低点的样式和大小。 | 63e41f4b71Sopenharmony_ci| loop | ChartLoop | 否 | 设置屏幕显示满时,是否需要重头开始绘制。 | 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci **表5** ChartLineStyle 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 68e41f4b71Sopenharmony_ci| ------ | -------------- | ----- | ---- | ----- | 69e41f4b71Sopenharmony_ci| width | <length> | 1px | 否 | 线宽设置。 | 70e41f4b71Sopenharmony_ci| smooth | boolean | false | 否 | 是否平滑。 | 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci **表6** PointStyle 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 75e41f4b71Sopenharmony_ci| ----------- | -------------- | -------- | ---- | ---------------------------------- | 76e41f4b71Sopenharmony_ci| shape | string | circle | 否 | 高亮点的形状。可选值为:<br/>- circle:圆形。 | 77e41f4b71Sopenharmony_ci| size | <length> | 5px | 否 | 高亮点的大小。 | 78e41f4b71Sopenharmony_ci| strokeWidth | <length> | 1px | 否 | 边框宽度 | 79e41f4b71Sopenharmony_ci| strokeColor | <color> | \#ff0000 | 否 | 边框颜色。 | 80e41f4b71Sopenharmony_ci| fillColor | <color> | \#ff0000 | 否 | 填充颜色。 | 81e41f4b71Sopenharmony_ci| display | boolean | true | 否 | 是否高亮显示。 | 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci **表7** ChartLoop 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 86e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | ---- | ---------------------------------------- | 87e41f4b71Sopenharmony_ci| margin | <length> | 1 | 否 | 擦除点的个数(最新绘制的点与最老的点之间的横向距离)。注意:轻量设备margin和topPoint/bottomPoint/headPoint同时使用时,有概率出现point正好位于擦除区域的情况,导致point不可见,因此不建议同时使用。 | 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci## 方法 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci| 方法 | 参数 | 描述 | 95e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | ---------------------------------------- | 96e41f4b71Sopenharmony_ci| append | {<br/>serial: number, // 设置要更新的线形图数据下标<br/>data: Array<number>, // 设置新增的数据<br/>} | 往已有的数据序列中动态添加数据,根据serial指定目标序列,serial为datasets数组的下标,从0开始。注意:不会更新datasets[index].data。仅线形图支持,按横坐标加1递增(与xAxis min/max设置相关)。 | 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci## 事件 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci| 名称 | 参数 | 描述 | 102e41f4b71Sopenharmony_ci| ------------------ | --------------------------------- | ----------- | 103e41f4b71Sopenharmony_ci| click | - | 点击动作触发该事件。 | 104e41f4b71Sopenharmony_ci| longpress | - | 长按动作触发该事件。 | 105e41f4b71Sopenharmony_ci| swipe<sup>5+</sup> | [SwipeEvent](js-lite-common-events.md) | 组件上快速滑动后触发。 | 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci## 样式 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci| 名称 | 类型 | 默认值 | 必填 | 描述 | 111e41f4b71Sopenharmony_ci| ---------------------------------- | ---------------------------------------- | ----- | ---- | ---------------------------------------- | 112e41f4b71Sopenharmony_ci| width | <length> \| <percentage><sup>5+</sup> | - | 否 | 设置组件自身的宽度。<br/><br/>未设置时组件宽度默认为0。 | 113e41f4b71Sopenharmony_ci| height | <length> \| <percentage><sup>5+</sup> | - | 否 | 设置组件自身的高度。<br/><br/>未设置时组件高度默认为0。 | 114e41f4b71Sopenharmony_ci| padding | <length> | 0 | 否 | 使用简写属性设置所有的内边距属性。<br/> 该属性可以有1到4个值:<br/>- 指定一个值时,该值指定四个边的内边距。<br/>- 指定两个值时,第一个值指定上下两边的内边距,第二个指定左右两边的内边距。<br/>- 指定三个值时,第一个指定上边的内边距,第二个指定左右两边的内边距,第三个指定下边的内边距。<br/>- 指定四个值时分别为上、右、下、左边的内边距(顺时针顺序)。 | 115e41f4b71Sopenharmony_ci| padding-[left\|top\|right\|bottom] | <length> | 0 | 否 | 设置左、上、右、下内边距属性。 | 116e41f4b71Sopenharmony_ci| margin | <length> \| <percentage><sup>5+</sup> | 0 | 否 | 使用简写属性设置所有的外边距属性,该属性可以有1到4个值。<br/>- 只有一个值时,这个值会被指定给全部的四个边。<br/>- 两个值时,第一个值被匹配给上和下,第二个值被匹配给左和右。<br/>- 三个值时,第一个值被匹配给上, 第二个值被匹配给左和右,第三个值被匹配给下。<br/>- 四个值时,会依次按上、右、下、左的顺序匹配 (即顺时针顺序)。 | 117e41f4b71Sopenharmony_ci| margin-[left\|top\|right\|bottom] | <length> \| <percentage><sup>5+</sup> | 0 | 否 | 设置左、上、右、下外边距属性。 | 118e41f4b71Sopenharmony_ci| border-width | <length> | 0 | 否 | 使用简写属性设置元素的所有边框宽度。 | 119e41f4b71Sopenharmony_ci| border-color | <color> | black | 否 | 使用简写属性设置元素的所有边框颜色。 | 120e41f4b71Sopenharmony_ci| border-radius | <length> | - | 否 | border-radius属性是设置元素的外边框圆角半径。 | 121e41f4b71Sopenharmony_ci| background-color | <color> | - | 否 | 设置背景颜色。 | 122e41f4b71Sopenharmony_ci| display | string | flex | 否 | 确定一个元素所产生的框的类型,可选值为:<br/>- flex:弹性布局。<br/>- none:不渲染此元素。 | 123e41f4b71Sopenharmony_ci| [left\|top] | <length> \| <percentage><sup>6+</sup> | - | 否 | left\|top确定元素的偏移位置。<br/>- left属性规定元素的左边缘。该属性定义了定位元素左外边距边界与其包含块左边界之间的偏移。<br/>- top属性规定元素的顶部边缘。该属性定义了一个定位元素的上外边距边界与其包含块上边界之间的偏移。 | 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci## 示例 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci1. 线形图 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  212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci2. 柱状图 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  276