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 12 13``` 14<!-- xxx.hml --> 15<div class="container"> 16 <chart class="chart-data" type="line" options="{{lineOps}}" datasets="{{lineData}}"></chart> 17</div> 18``` 19 20 21 22``` 23/* xxx.css */ 24.container { 25 width: 100%; 26 height: 100%; 27 flex-direction: column; 28 justify-content: center; 29 align-items: center; 30 background-color: #F1F3F5; 31} 32.chart-data { 33 width: 700px; 34 height: 600px; 35} 36``` 37 38 39 40``` 41/* xxx.js */ 42export default { 43 data: { 44 lineData: [ 45 { 46 data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716] 47 } 48 ], 49 lineOps: { 50 xAxis: { 51 min: 0, 52 max: 20, 53 display: false, 54 }, 55 yAxis: { 56 min: 0, 57 max: 1000, 58 display: false, 59 }, 60 series: { 61 lineStyle: { 62 width: 15, 63 }, 64 } 65 }, 66 } 67} 68``` 69 70 71 72 73 74## Setting the Chart Type 75 76Define the chart type using the **type** attribute, for example, setting a chart to a bar chart. 77 78 79``` 80<!-- xxx.hml --> 81<div class="container"> 82 <div class="container"> 83 <div class="switch-block"> 84 <text class="title"> 85 {{ title }} 86 </text> 87 </div> 88 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 89 <tab-content class="tabcontent" scrollable="true"> 90 <tabs > 91 <tab-bar class="tab-bar" mode="fixed"style="margin-bottom: 50px;"> 92 <text class="tab-text">Line chart</text> 93 <text class="tab-text">Bar chart</text> 94 <text class="tab-text">Gauge chart</text> 95 </tab-bar> 96 <tab-content> 97 <div class="bar-block" style="margin-left: 30px;"> 98 <chart class="chart-data"type="line" ref="linechart" options="{{ lineOps }}" datasets="{{ lineData }}"> 99 </chart> 100 </div> 101 <div class="bar-block"> 102 <chart class="data-bar"type="bar" id="bar-chart" options="{{ barOps }}" datasets="{{ barData }}"> 103 </chart> 104 </div> 105 <div class="chart-block"> 106 <chart type="gauge" ></chart> 107 </div> 108 </tab-content> 109 </tabs> 110 </tab-content> 111 </tabs> 112 </div> 113</div> 114``` 115 116 117``` 118/* xxx.css */ 119.container { 120 width: 100%; 121 height: 100%; 122 flex-direction: column; 123 justify-content: center; 124 background-color: #F1F3F5; 125} 126.tab-bar{ 127 background-color: #F1F3F5; 128} 129.chart-data { 130 width: 700px; 131 height: 600px; 132} 133.title{ 134 margin-left: 50px; 135 margin-top: 50px; 136 font-size: 50px; 137} 138.line-block{ 139 align-items: center; 140 justify-content: center; 141} 142.bar-block{ 143 align-items: center; 144 justify-content: center; 145} 146.chart-block{ 147 width: 90%; 148 margin-left: 30px; 149} 150``` 151 152 153``` 154/* xxx.js */ 155export default { 156 data: { 157 title:"Type display", 158 barData: [ 159 { 160 fillColor: '#3848e8', 161 data: [763, 550, 551, 554, 731, 654, 525, 696, 595], 162 } 163 ], 164 lineData: [ 165 { 166 strokeColor: '#0081ff', 167 fillColor: '#cce5ff', 168 data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716], 169 gradient: true, 170 } 171 ], 172 lineOps: { 173 xAxis: { 174 min: 0, 175 max: 20, 176 display: false, 177 }, 178 yAxis: { 179 min: 0, 180 max: 1000, 181 display: false, 182 }, 183 series:{ 184 lineStyle: { 185 width: "5px", 186 smooth: true, 187 }, 188 headPoint: { 189 shape:"circle", 190 size: 20, 191 strokeWidth: 5, 192 fillColor: '#ffffff', 193 strokeColor: '#007aff', 194 display: true, 195 }, 196 loop:{ 197 margin: 2, 198 gradient: true 199 } 200 }, 201 }, 202 barOps: { 203 xAxis: { 204 min: 0, 205 max: 20, 206 display: false, 207 axisTick: 10, 208 }, 209 yAxis: { 210 min: 0, 211 max: 1000, 212 }, 213 }, 214 }, 215} 216``` 217 218 219 220>  **NOTE**<br/>The **<chart>** component does not display the value of each point. 221> 222 223 224## Setting the Chart Attributes 225 226In 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. 227 228 229``` 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``` 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``` 255/* xxx.js */ 256export default { 257 data: { 258 // Line chart data 259 lineData: [ 260 { 261 strokeColor: '#0081ff', 262 fillColor: '#cce5ff', // Fill color 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-axis parameters 269 xAxis: { 270 min: 0, 271 max: 20, 272 display: false, 273 }, 274 // Y-axis parameters 275 yAxis: { 276 min: 0, 277 max: 1000, 278 display: false, 279 }, 280 // Data series parameters 281 series: { 282 // Line style 283 lineStyle: { 284 width: "5px", 285 smooth: true, 286 }, 287 // Style and size of the white point at the start of the line 288 headPoint: { 289 shape: "circle", 290 size: 20, 291 strokeWidth: 5, 292 fillColor: '#ffffff', 293 strokeColor: '#007aff', 294 display: true, 295 }, 296 // Whether to start drawing again when the screen is looped. 297 loop: { 298 margin: 2, 299 gradient: true 300 } 301 } 302 }, 303 }, 304} 305``` 306 307>  **NOTE**<br/> 308> - The **options** attribute supports the settings of bar charts and line charts but does not support those of gauge charts. 309> 310> - The **datasets** attribute supports the datasets for bar charts and line charts but does not support those of gauge charts. 311> 312> - Only line charts support **series**. 313 314 315## Adding Data 316 317Use the **append** method of the **<chart>** component to dynamically add data. 318 319 320``` 321<!-- xxx.hml --> 322<div class="container"> 323 <stack class="chart-region"> 324 <chart class="chart-data" type="line" ref="linechart" options="{{lineOps}}" datasets="{{lineData}}"></chart> 325 </stack> 326 <button value="Add data"onclick="addData"></button> 327</div> 328``` 329 330 331``` 332/* xxx.css */ 333.container { 334 width: 100%; 335 height: 100%; 336 flex-direction: column; 337 justify-content: center; 338 align-items: center; 339 background-color: #F1F3F5; 340} 341.chart-region { 342 height: 400px; 343 width: 700px; 344} 345.chart-data { 346 width: 700px; 347 height: 600px; 348} 349button { 350 width: 100%; 351 height: 50px; 352 background-color: #F4F2F1; 353 text-color: #0C81F3; 354 margin-top: 30px; 355} 356``` 357 358 359``` 360// xxx.js 361export default { 362 data: { 363 lineData: [ 364 { 365 strokeColor: '#de0b6e', 366 fillColor: '#bb09a3', 367 data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716], 368 gradient: true, 369 } 370 ], 371 lineOps: { 372 xAxis: { 373 min: 0, 374 max: 20, 375 display: false, 376 }, 377 yAxis: { 378 min: 0, 379 max: 1000, 380 display: false, 381 }, 382 series: { 383 lineStyle: { 384 width: "5px", 385 smooth: true, 386 }, 387 headPoint: { 388 shape: "circle", 389 size: 20, 390 strokeWidth: 5, 391 fillColor: '#ffffff', 392 strokeColor: '#f8145c', 393 display: true, 394 }, 395 loop: { 396 margin: 2, 397 gradient: true, 398 } 399 } 400 }, 401 }, 402 addData() { this.$refs.linechart.append({ serial: 0, data: [Math.floor(Math.random() * 400) + 200] }) } 403} 404``` 405 406 407 408 409## Example Scenario 410 411Select the data display status using **<switch>**. When **<switch>** is set to **true**, the timer is used to dynamically display data. 412 413 414``` 415<!-- xxx.hml --> 416<div class="container"> 417 <div class="container"> 418 <div class="switch-block"> 419 <text class="title">{{ title }} </text> 420 <switch class="switch" showtext="{{ showText }}" allow-scale="{{ allowScale }}"onchange="change"> 421 </switch> 422 </div> 423 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 424 <tab-content class="tabcontent" scrollable="true"> 425 <div> 426 <tabs class="tabs" index="0" vertical="false" onchange="changes"> 427 <tab-content class="tabcontent" scrollable="true"> 428 <div class="line-class"> 429 <div class="bar-block"> 430 <chart class="chart-data" type="line" ref="linechart" options="{{ lineOps }}" 431 datasets="{{ lineData }}"> 432 </chart> 433 </div> 434 <div class="bar-block"> 435 <chart class="data-bar" type="bar" id="bar-chart" options="{{ barOps }}"datasets="{{ barData }}"> 436 </chart> 437 </div> 438 </div> 439 </tab-content> 440 </tabs> 441 </div> 442 <div> 443 <div class="container"> 444 <list class="todo-wrapper"> 445 <list-item for="{{ barData }}" class="todo-item"> 446 <text class="todo-title">{{ $item.data }}</text> 447 </list-item> 448 </list> 449 <list class="todo-wrapper"> 450 <list-item for="{{ lineData.data }}" class="todo-item"> 451 <text class="todo-title">{{ $item.value }}</text> 452 </list-item> 453 </list> 454 </div> 455 </div> 456 </tab-content> 457 </tabs> 458 </div> 459</div> 460``` 461 462 463``` 464/* xxx.css */ 465.container{ 466 display:flex; 467 flex-direction:column; 468 background-color: #F1F3F5; 469} 470.line-class{ 471 display: flex; 472 flex-direction: column; 473} 474.title{ 475 font-size: 40px; 476 margin-left: 40px; 477} 478.switch-block { 479 margin-top: 30px; 480 width: 98%; 481 height: 80px; 482 display: flex; 483 justify-content: space-between; 484} 485.switch{ 486 font-size: 40px; 487} 488.bar-block { 489 margin-top: 80px; 490 margin-left: 40px; 491 position: relative; 492 width: 90%; 493 border-radius: 10px; 494 background-color: #25FAB27B; 495 height: 40%; 496 justify-content: center; 497} 498``` 499 500 501``` 502// xxx.js 503export default { 504 data: { 505 interval: null, 506 title: "Data Display", 507 allowScale: true, 508 dataLength: 30, 509 barGroup: 3, 510 lineData: null, 511 lineOps: { 512 xAxis: { 513 min: 0, 514 max: 5 515 }, 516 yAxis: { 517 min: 0, 518 max: 100 519 }, 520 series: { 521 lineStyle: { 522 width: '1px', 523 }, 524 headPoint: { 525 shape: 'circle', 526 size: 10, 527 strokeWidth: 2, 528 fillColor: '#ffffff', 529 strokeColor: '#8477DF' 530 }, 531 loop: { 532 margin: 2 533 } 534 } 535 }, 536 barData: [ 537 { 538 fillColor: '#97CF0A2C', 539 data: [20, 20,40, 56] 540 }, 541 { 542 fillColor: '#6D0A7ACF', 543 data: [52, 40, 2, 67] 544 }, 545 { 546 fillColor: '#6A0ACFA1', 547 data: [56, 2, 77, 40] 548 } 549 ], 550 barOps: { 551 xAxis: { 552 min: 0, 553 max: 20, 554 axisTick: 5 555 }, 556 yAxis: { 557 min: 0, 558 max: 100 559 } 560 } 561 }, 562 onInit() { 563 this.changeLine(); 564 }, 565 change(e) { 566 if (e.checked) { 567 this.interval = setInterval(() => { 568 this.changeLine(); 569 this.changeBar(); 570 }, 1000) 571 } else { 572 clearInterval(this.interval); 573 } 574 }, 575 changeLine() { 576 var dataArray = []; 577 for (var i = 0; i < this.dataLength; i++) { 578 var nowValue = Math.floor(Math.random() * 99 + 1); 579 var obj = { 580 "value": nowValue, 581 "description": nowValue + "", 582 "textLocation": "top", 583 "textColor": "#CDCACA", 584 "pointStyle": { 585 "shape": "circle", 586 "size": 5, 587 "fillColor": "#CF0A2C", 588 "strokeColor": "#CF0A2C" 589 } 590 }; 591 dataArray.push(obj); 592 } 593 this.lineData = [ 594 { 595 strokeColor: '#0081ff', 596 fillColor: '#FF07CDC4', 597 data: dataArray, 598 gradient: true, 599 } 600 ] 601 }, 602 changeBar() { 603 for (var i = 0;i < this.barGroup; i++) { 604 var dataArray = this.barData[i].data; 605 for (var j = 0;j < 4; j++) { 606 dataArray[j] = Math.floor(Math.random() * 99 + 1); 607 } 608 } 609 this.barData = this.barData.splice(0, this.barGroup + 1); 610 }, 611 changes(e) { 612 console.log("Tab index: " + e.index); 613 }, 614} 615 616``` 617 618