1# chart开发指导 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @liyujie43--> 5<!--Designer: @weixin_52725220--> 6<!--Tester: @xiong0104--> 7<!--Adviser: @HelloCrease--> 8 9chart为图表组件,用于呈现线形图、柱状图和量规图界面。具体用法请参考[chart](../reference/apis-arkui/arkui-js/js-components-basic-chart.md)。 10 11 12## 创建chart组件 13 14在pages/index目录下的hml文件中创建一个chart组件。 15 16```html 17<!-- xxx.hml --> 18<div class="container"> 19 <chart class="chart-data" type="line" options="{{lineOps}}" datasets="{{lineData}}"></chart> 20</div> 21``` 22 23```css 24/* xxx.css */ 25.container { 26 width: 100%; 27 height: 100%; 28 flex-direction: column; 29 justify-content: center; 30 align-items: center; 31 background-color: #F1F3F5; 32} 33.chart-data { 34 width: 700px; 35 height: 600px; 36} 37``` 38 39```js 40// xxx.js 41export default { 42 data: { 43 lineData: [ 44 { 45 data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716] 46 } 47 ], 48 lineOps: { 49 xAxis: { 50 min: 0, 51 max: 20, 52 display: false, 53 }, 54 yAxis: { 55 min: 0, 56 max: 1000, 57 display: false, 58 }, 59 series: { 60 lineStyle: { 61 width: 15, 62 }, 63 } 64 }, 65 } 66} 67``` 68 69 70 71 72 73## 设置图表类型 74 75chart组件通过设置type属性定义图表类型,如将图表设置为柱状图。 76 77 78```html 79<!-- xxx.hml --> 80<div class="container"> 81 <div class="container"> 82 <div class="switch-block"> 83 <text class="title"> 84 {{ title }} 85 </text> 86 </div> 87 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 88 <tab-content class="tabcontent" scrollable="true"> 89 <tabs > 90 <tab-bar class="tab-bar" mode="fixed"style="margin-bottom: 50px;"> 91 <text class="tab-text">线形图</text> 92 <text class="tab-text">柱状图</text> 93 <text class="tab-text">量规图</text> 94 </tab-bar> 95 <tab-content> 96 <div class="bar-block" style="margin-left: 30px;"> 97 <chart class="chart-data" type="line" ref="linechart" options="{{ lineOps }}" datasets="{{ lineData }}"> 98 </chart> 99 </div> 100 <div class="bar-block"> 101 <chart class="data-bar" type="bar" id="bar-chart" options="{{ barOps }}" datasets="{{ barData }}"> 102 </chart> 103 </div> 104 <div class="chart-block"> 105 <chart type="gauge" ></chart> 106 </div> 107 </tab-content> 108 </tabs> 109 </tab-content> 110 </tabs> 111 </div> 112</div> 113``` 114 115 116```css 117/* xxx.css */ 118.container { 119 width: 100%; 120 height: 100%; 121 flex-direction: column; 122 justify-content: center; 123 background-color: #F1F3F5; 124} 125.tab-bar{ 126 background-color: #F1F3F5; 127} 128.chart-data { 129 width: 700px; 130 height: 600px; 131} 132.title{ 133 margin-left: 50px; 134 margin-top: 50px; 135 font-size: 50px; 136} 137.line-block{ 138 align-items: center; 139 justify-content: center; 140} 141.bar-block{ 142 align-items: center; 143 justify-content: center; 144} 145.chart-block{ 146 width: 90%; 147 margin-left: 30px; 148} 149``` 150 151 152```js 153// xxx.js 154export default { 155 data: { 156 title: "类型展示", 157 barData: [ 158 { 159 fillColor: '#3848e8', 160 data: [763, 550, 551, 554, 731, 654, 525, 696, 595], 161 } 162 ], 163 lineData: [ 164 { 165 strokeColor: '#0081ff', 166 fillColor: '#cce5ff', 167 data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716], 168 gradient: true, 169 } 170 ], 171 lineOps: { 172 xAxis: { 173 min: 0, 174 max: 20, 175 display: false, 176 }, 177 yAxis: { 178 min: 0, 179 max: 1000, 180 display: false, 181 }, 182 series:{ 183 lineStyle: { 184 width: "5px", 185 smooth: true, 186 }, 187 headPoint: { 188 shape:"circle", 189 size: 20, 190 strokeWidth: 5, 191 fillColor: '#ffffff', 192 strokeColor: '#007aff', 193 display: true, 194 }, 195 loop:{ 196 margin: 2, 197 gradient: true 198 } 199 }, 200 }, 201 barOps: { 202 xAxis: { 203 min: 0, 204 max: 20, 205 display: false, 206 axisTick: 10, 207 }, 208 yAxis: { 209 min: 0, 210 max: 1000, 211 }, 212 }, 213 }, 214} 215``` 216 217 218 219> **说明:** 220> 221> chart不支持显示每个点的值。 222 223 224## 设置图表属性 225 226chart组件在options属性中设置对x轴、y轴和数据序列参数的设置,在datasets属性里添加对线条颜色、填充颜色、填充渐变颜色和绘制点集的设置。 227 228 229```html 230<!-- xxx.hml --> 231<div class="container"> 232 <chart class="chart-data" type="line" options="{{lineOps}}" datasets="{{lineData}}"></chart> 233</div> 234``` 235 236 237```css 238/* xxx.css */ 239.container { 240 width: 100%; 241 height: 100%; 242 flex-direction: column; 243 justify-content: center; 244 align-items: center; 245 background-color: #F1F3F5; 246} 247.chart-data { 248 width: 700px; 249 height: 600px; 250} 251``` 252 253 254```js 255// xxx.js 256export default { 257 data: { 258 //线形图数据 259 lineData: [ 260 { 261 strokeColor: '#0081ff', 262 fillColor: '#cce5ff', //填充色 263 data: [463, 250, 251, 254, 431, 354, 225, 396, 295, 328, 491, 205, 313, 275, 475, 553, 491, 380, 357, 416], 264 gradient: true, 265 } 266 ], 267 lineOps: { 268 //x轴参数设置 269 xAxis: { 270 min: 0, 271 max: 20, 272 display: false, 273 }, 274 //y轴参数设置 275 yAxis: { 276 min: 0, 277 max: 1000, 278 display: false, 279 }, 280 //数据序列参数设置 281 series: { 282 //线样式设置 283 lineStyle: { 284 width: "5px", 285 smooth: true, 286 }, 287 //线最前端位置白点的样式和大小 288 headPoint: { 289 shape: "circle", 290 size: 20, 291 strokeWidth: 5, 292 fillColor: '#ffffff', 293 strokeColor: '#007aff', 294 display: true, 295 }, 296 //设置屏幕显示满时,是否需要重头开始绘制 297 loop: { 298 margin: 2, 299 gradient: true 300 } 301 } 302 }, 303 }, 304} 305``` 306 307> **说明:** 308> - options只支持柱状图和线形图设置参数,量规图不生效。 309> 310> - datasets只支持柱状图和线形图设置数据集合,量规图不生效。 311> 312> - series只有线形图支持。 313 314 315## 场景示例 316 317开发者可以根据开关Switch的状态来选择数据展示的状态,当Switch状态为true时,通过定时器来实现数据的动态展示。 318 319 320```html 321<!-- xxx.hml --> 322<div class="container"> 323 <div class="container"> 324 <div class="switch-block"> 325 <text class="title">{{ title }} </text> 326 <switch class="switch" showtext="{{ showText }}" allow-scale="{{ allowScale }}"onchange="change"> 327 </switch> 328 </div> 329 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 330 <tab-content class="tabcontent" scrollable="true"> 331 <div> 332 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 333 <tab-content class="tabcontent" scrollable="true"> 334 <div class="line-class"> 335 <div class="bar-block"> 336 <chart class="chart-data" type="line" ref="linechart" options="{{ lineOps }}" 337 datasets="{{ lineData }}"> 338 </chart> 339 </div> 340 <div class="bar-block"> 341 <chart class="data-bar" type="bar" id="bar-chart" options="{{ barOps }}"datasets="{{ barData }}"> 342 </chart> 343 </div> 344 </div> 345 </tab-content> 346 </tabs> 347 </div> 348 <div> 349 <div class="container"> 350 <list class="todo-wrapper"> 351 <list-item for="{{ barData }}" class="todo-item"> 352 <text class="todo-title">{{ $item.data }}</text> 353 </list-item> 354 </list> 355 <list class="todo-wrapper"> 356 <list-item for="{{ lineData.data }}" class="todo-item"> 357 <text class="todo-title">{{ $item.value }}</text> 358 </list-item> 359 </list> 360 </div> 361 </div> 362 </tab-content> 363 </tabs> 364 </div> 365</div> 366``` 367 368 369```css 370/* xxx.css */ 371.container{ 372 display:flex; 373 flex-direction:column; 374 background-color: #F1F3F5; 375} 376.line-class{ 377 display: flex; 378 flex-direction: column; 379} 380.title{ 381 font-size: 40px; 382 margin-left: 40px; 383} 384.switch-block { 385 margin-top: 30px; 386 width: 98%; 387 height: 80px; 388 display: flex; 389 justify-content: space-between; 390} 391.switch{ 392 font-size: 40px; 393} 394.bar-block { 395 margin-top: 80px; 396 margin-left: 40px; 397 position: relative; 398 width: 90%; 399 border-radius: 10px; 400 background-color: #25FAB27B; 401 height: 40%; 402 justify-content: center; 403} 404``` 405 406 407```js 408// xxx.js 409export default { 410 data: { 411 interval: null, 412 title: "数据展示", 413 allowScale: true, 414 dataLength: 30, 415 barGroup: 3, 416 lineData: null, 417 lineOps: { 418 xAxis: { 419 min: 0, 420 max: 5 421 }, 422 yAxis: { 423 min: 0, 424 max: 100 425 }, 426 series: { 427 lineStyle: { 428 width: '1px', 429 }, 430 headPoint: { 431 shape: 'circle', 432 size: 10, 433 strokeWidth: 2, 434 fillColor: '#ffffff', 435 strokeColor: '#8477DF' 436 }, 437 loop: { 438 margin: 2 439 } 440 } 441 }, 442 barData: [ 443 { 444 fillColor: '#97CF0A2C', 445 data: [20, 20,40, 56] 446 }, 447 { 448 fillColor: '#6D0A7ACF', 449 data: [52, 40, 2, 67] 450 }, 451 { 452 fillColor: '#6A0ACFA1', 453 data: [56, 2, 77, 40] 454 } 455 ], 456 barOps: { 457 xAxis: { 458 min: 0, 459 max: 20, 460 axisTick: 5 461 }, 462 yAxis: { 463 min: 0, 464 max: 100 465 } 466 } 467 }, 468 onInit() { 469 this.changeLine(); 470 }, 471 change(e) { 472 if (e.checked) { 473 this.interval = setInterval(() => { 474 this.changeLine(); 475 this.changeBar(); 476 }, 1000) 477 } else { 478 clearInterval(this.interval); 479 } 480 }, 481 changeLine() { 482 var dataArray = []; 483 for (var i = 0; i < this.dataLength; i++) { 484 var nowValue = Math.floor(Math.random() * 99 + 1); 485 var obj = { 486 "value": nowValue, 487 "description": nowValue + "", 488 "textLocation": "top", 489 "textColor": "#CDCACA", 490 "pointStyle": { 491 "shape": "circle", 492 "size": 5, 493 "fillColor": "#CF0A2C", 494 "strokeColor": "#CF0A2C" 495 } 496 }; 497 dataArray.push(obj); 498 } 499 this.lineData = [ 500 { 501 strokeColor: '#0081ff', 502 fillColor: '#FF07CDC4', 503 data: dataArray, 504 gradient: true, 505 } 506 ] 507 }, 508 changeBar() { 509 for (var i = 0;i < this.barGroup; i++) { 510 var dataArray = this.barData[i].data; 511 for (var j = 0;j < 4; j++) { 512 dataArray[j] = Math.floor(Math.random() * 99 + 1); 513 } 514 } 515 this.barData = this.barData.splice(0, this.barGroup + 1); 516 }, 517 changes(e) { 518 console.log("Tab index: " + e.index); 519 }, 520} 521 522 523 524 525 526``` 527 528