1# <chart> Development 2 3 4The **<chart>** component displays line charts, gauge charts, and bar charts. For details, see [chart](../reference/arkui-js/js-components-basic-chart.md). 5 6 7## Creating a <chart> Component 8 9Create a **<chart>** component in the .hml file under **pages/index**. 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![en-us_image_0000001231683156](figures/en-us_image_0000001231683156.png) 66 67 68## Setting the Chart Type 69 70Define the chart type using the **type** attribute, for example, setting a chart to a bar chart. 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">Line chart</text> 87 <text class="tab-text">Bar chart</text> 88 <text class="tab-text">Gauge chart</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:"Type display", 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![en-us_image_0000001275803181](figures/en-us_image_0000001275803181.gif) 213 214> **NOTE** 215> 216> The **<chart>** component does not display the value of each point. 217 218 219## Setting the Chart Attributes 220 221In the **options** attribute of the **<chart>** component, you can set the x-axis, y-axis, and data series parameters. In the **datasets** attribute, you can set the line color, fill color, gradient fill color, and drawing point set. 222 223 224```html 225<!-- xxx.hml --> 226<div class="container"> 227 <chart class="chart-data" type="line" options="{{lineOps}}" datasets="{{lineData}}"></chart> 228</div> 229``` 230 231 232```css 233/* xxx.css */ 234.container { 235 width: 100%; 236 height: 100%; 237 flex-direction: column; 238 justify-content: center; 239 align-items: center; 240 background-color: #F1F3F5; 241} 242.chart-data { 243 width: 700px; 244 height: 600px; 245} 246``` 247 248 249```js 250// xxx.js 251export default { 252 data: { 253 // Line chart data 254 lineData: [ 255 { 256 strokeColor: '#0081ff', 257 fillColor: '#cce5ff', // Fill color 258 data: [463, 250, 251, 254, 431, 354, 225, 396, 295, 328, 491, 205, 313, 275, 475, 553, 491, 380, 357, 416], 259 gradient: true, 260 } 261 ], 262 lineOps: { 263 // X-axis parameters 264 xAxis: { 265 min: 0, 266 max: 20, 267 display: false, 268 }, 269 // Y-axis parameters 270 yAxis: { 271 min: 0, 272 max: 1000, 273 display: false, 274 }, 275 // Data series parameters 276 series: { 277 // Line style 278 lineStyle: { 279 width: "5px", 280 smooth: true, 281 }, 282 // Style and size of the white point at the start of the line 283 headPoint: { 284 shape: "circle", 285 size: 20, 286 strokeWidth: 5, 287 fillColor: '#ffffff', 288 strokeColor: '#007aff', 289 display: true, 290 }, 291 // Whether to start drawing again when the screen is looped. 292 loop: { 293 margin: 2, 294 gradient: true 295 } 296 } 297 }, 298 }, 299} 300``` 301 302> **NOTE** 303> - The **options** attribute supports the settings of bar charts and line charts but does not support those of gauge charts. 304> 305> - The **datasets** attribute supports the datasets for bar charts and line charts but does not support those of gauge charts. 306> 307> - Only line charts support **series**. 308 309 310## Adding Data 311 312Use the **append** method of the **<chart>** component to dynamically add data. 313 314 315```html 316<!-- xxx.hml --> 317<div class="container"> 318 <stack class="chart-region"> 319 <chart class="chart-data" type="line" ref="linechart" options="{{lineOps}}" datasets="{{lineData}}"></chart> 320 </stack> 321 <button value="Add data" onclick="addData"></button> 322</div> 323``` 324 325 326```css 327/* xxx.css */ 328.container { 329 flex-direction: column; 330 justify-content: center; 331 align-items: center; 332 background-color: #F1F3F5; 333} 334.chart-region { 335 height: 400px; 336 width: 700px; 337} 338.chart-data { 339 width: 700px; 340 height: 600px; 341} 342button { 343 width: 100%; 344 height: 50px; 345 background-color: #F4F2F1; 346 text-color: #0C81F3; 347 margin-top: 30px; 348} 349``` 350 351 352```js 353// xxx.js 354export default { 355 data: { 356 lineData: [ 357 { 358 strokeColor: '#de0b6e', 359 fillColor: '#bb09a3', 360 data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716], 361 gradient: true, 362 } 363 ], 364 lineOps: { 365 xAxis: { 366 min: 0, 367 max: 20, 368 display: false, 369 }, 370 yAxis: { 371 min: 0, 372 max: 1000, 373 display: false, 374 }, 375 series: { 376 lineStyle: { 377 width: "5px", 378 smooth: true, 379 }, 380 headPoint: { 381 shape: "circle", 382 size: 20, 383 strokeWidth: 5, 384 fillColor: '#ffffff', 385 strokeColor: '#f8145c', 386 display: true, 387 }, 388 loop: { 389 margin: 2, 390 gradient: true, 391 } 392 } 393 }, 394 }, 395 addData() { 396 this.$refs.linechart.append({ 397 serial: 0, 398 data: [Math.floor(Math.random() * 400) + 200] 399 }) 400 } 401} 402``` 403 404![en-us_image_0000001275803177](figures/en-us_image_0000001275803177.gif) 405 406 407## Example Scenario 408 409Select the data display status using **<switch>**. When **<switch>** is set to **true**, the timer is used to dynamically display data. 410 411 412```html 413<!-- xxx.hml --> 414<div class="container"> 415 <div class="container"> 416 <div class="switch-block"> 417 <text class="title">{{ title }} </text> 418 <switch class="switch" showtext="{{ showText }}" allow-scale="{{ allowScale }}"onchange="change"> 419 </switch> 420 </div> 421 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 422 <tab-content class="tabcontent" scrollable="true"> 423 <div> 424 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 425 <tab-content class="tabcontent" scrollable="true"> 426 <div class="line-class"> 427 <div class="bar-block"> 428 <chart class="chart-data" type="line" ref="linechart" options="{{ lineOps }}" 429 datasets="{{ lineData }}"> 430 </chart> 431 </div> 432 <div class="bar-block"> 433 <chart class="data-bar" type="bar" id="bar-chart" options="{{ barOps }}"datasets="{{ barData }}"> 434 </chart> 435 </div> 436 </div> 437 </tab-content> 438 </tabs> 439 </div> 440 <div> 441 <div class="container"> 442 <list class="todo-wrapper"> 443 <list-item for="{{ barData }}" class="todo-item"> 444 <text class="todo-title">{{ $item.data }}</text> 445 </list-item> 446 </list> 447 <list class="todo-wrapper"> 448 <list-item for="{{ lineData.data }}" class="todo-item"> 449 <text class="todo-title">{{ $item.value }}</text> 450 </list-item> 451 </list> 452 </div> 453 </div> 454 </tab-content> 455 </tabs> 456 </div> 457</div> 458``` 459 460 461```css 462/* xxx.css */ 463.container{ 464 display:flex; 465 flex-direction:column; 466 background-color: #F1F3F5; 467} 468.line-class{ 469 display: flex; 470 flex-direction: column; 471} 472.title{ 473 font-size: 40px; 474 margin-left: 40px; 475} 476.switch-block { 477 margin-top: 30px; 478 width: 98%; 479 height: 80px; 480 display: flex; 481 justify-content: space-between; 482} 483.switch{ 484 font-size: 40px; 485} 486.bar-block { 487 margin-top: 80px; 488 margin-left: 40px; 489 position: relative; 490 width: 90%; 491 border-radius: 10px; 492 background-color: #25FAB27B; 493 height: 40%; 494 justify-content: center; 495} 496``` 497 498 499```js 500// xxx.js 501export default { 502 data: { 503 interval: null, 504 title: "Data Display", 505 allowScale: true, 506 dataLength: 30, 507 barGroup: 3, 508 lineData: null, 509 lineOps: { 510 xAxis: { 511 min: 0, 512 max: 5 513 }, 514 yAxis: { 515 min: 0, 516 max: 100 517 }, 518 series: { 519 lineStyle: { 520 width: '1px', 521 }, 522 headPoint: { 523 shape: 'circle', 524 size: 10, 525 strokeWidth: 2, 526 fillColor: '#ffffff', 527 strokeColor: '#8477DF' 528 }, 529 loop: { 530 margin: 2 531 } 532 } 533 }, 534 barData: [ 535 { 536 fillColor: '#97CF0A2C', 537 data: [20, 20,40, 56] 538 }, 539 { 540 fillColor: '#6D0A7ACF', 541 data: [52, 40, 2, 67] 542 }, 543 { 544 fillColor: '#6A0ACFA1', 545 data: [56, 2, 77, 40] 546 } 547 ], 548 barOps: { 549 xAxis: { 550 min: 0, 551 max: 20, 552 axisTick: 5 553 }, 554 yAxis: { 555 min: 0, 556 max: 100 557 } 558 } 559 }, 560 onInit() { 561 this.changeLine(); 562 }, 563 change(e) { 564 if (e.checked) { 565 this.interval = setInterval(() => { 566 this.changeLine(); 567 this.changeBar(); 568 }, 1000) 569 } else { 570 clearInterval(this.interval); 571 } 572 }, 573 changeLine() { 574 var dataArray = []; 575 for (var i = 0; i < this.dataLength; i++) { 576 var nowValue = Math.floor(Math.random() * 99 + 1); 577 var obj = { 578 "value": nowValue, 579 "description": nowValue + "", 580 "textLocation": "top", 581 "textColor": "#CDCACA", 582 "pointStyle": { 583 "shape": "circle", 584 "size": 5, 585 "fillColor": "#CF0A2C", 586 "strokeColor": "#CF0A2C" 587 } 588 }; 589 dataArray.push(obj); 590 } 591 this.lineData = [ 592 { 593 strokeColor: '#0081ff', 594 fillColor: '#FF07CDC4', 595 data: dataArray, 596 gradient: true, 597 } 598 ] 599 }, 600 changeBar() { 601 for (var i = 0;i < this.barGroup; i++) { 602 var dataArray = this.barData[i].data; 603 for (var j = 0;j < 4; j++) { 604 dataArray[j] = Math.floor(Math.random() * 99 + 1); 605 } 606 } 607 this.barData = this.barData.splice(0, this.barGroup + 1); 608 }, 609 changes(e) { 610 console.log("Tab index: " + e.index); 611 }, 612} 613 614``` 615 616![en-us_image_0000001232162328](figures/en-us_image_0000001232162328.gif)