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