0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

HarmonyOS開發(fā)案例:【 switch、chart組件的使用】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-04-25 20:58 ? 次閱讀

介紹

基于switch組件和chart組件,實(shí)現(xiàn)線形圖、占比圖、柱狀圖,并通過switch切換chart組件數(shù)據(jù)的動(dòng)靜態(tài)顯示。要求實(shí)現(xiàn)以下功能:

  1. 實(shí)現(xiàn)靜態(tài)數(shù)據(jù)可視化圖表。
  2. 打開開關(guān),實(shí)現(xiàn)靜態(tài)圖切換為動(dòng)態(tài)可視化圖表。

相關(guān)概念

  • [switch組件]:開關(guān)選擇器,通過開關(guān),開啟或關(guān)閉某個(gè)功能。
  • [chart組件]:圖表組件,用于呈現(xiàn)線形圖、占比圖、柱狀圖界面。

環(huán)境搭建

軟件要求

  • [DevEco Studio]版本:DevEco Studio 3.1 Release及以上版本。
  • OpenHarmony SDK版本:API version 9及以上版本。

硬件要求

  • 開發(fā)板類型:[潤和RK3568開發(fā)板]。
  • OpenHarmony系統(tǒng):3.2 Release及以上版本。

環(huán)境搭建

完成本篇Codelab我們首先要完成開發(fā)環(huán)境的搭建,本示例以RK3568開發(fā)板為例,參照以下步驟進(jìn)行:

  1. [獲取OpenHarmony系統(tǒng)版本]:標(biāo)準(zhǔn)系統(tǒng)解決方案(二進(jìn)制)。以3.2 Release版本為例:
  2. 搭建燒錄環(huán)境。
    1. [完成DevEco Device Tool的安裝]
    2. [完成RK3568開發(fā)板的燒錄]
  3. 搭建開發(fā)環(huán)境。
    1. 開始前請(qǐng)參考[工具準(zhǔn)備],完成DevEco Studio的安裝和開發(fā)環(huán)境配置。
    2. 開發(fā)環(huán)境配置完成后,請(qǐng)參考[使用工程向?qū)創(chuàng)建工程(模板選擇“Empty Ability”)。
    3. 工程創(chuàng)建完成后,選擇使用[真機(jī)進(jìn)行調(diào)測]。
    4. 鴻蒙開發(fā)指導(dǎo)文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

搜狗高速瀏覽器截圖20240326151450.png

代碼結(jié)構(gòu)解讀

本篇Codelab只對(duì)核心代碼進(jìn)行講解,對(duì)于完整代碼,我們會(huì)在gitee中提供。

HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿

├──entry/src/main/js	     // 代碼區(qū)
│  └──MainAbility
│     ├──common
│     │  └──images           // 圖片資源
│     ├──i18n                // 國際化中英文
│     │  ├──en-US.json			
│     │  └──zh-CN.json			
│     ├──pages
│     │  └──index
│     │     ├──index.css     // 首頁樣式文件	
│     │     ├──index.hml     // 首頁布局文件
│     │     └──index.js      // 首頁業(yè)務(wù)處理文件
│     └──app.js              // 程序入口
└──entry/src/main/resources  // 應(yīng)用資源目錄

構(gòu)建主界面

本章節(jié)將介紹應(yīng)用主頁面的實(shí)現(xiàn),頁面從上至下分為兩個(gè)部分:

  1. 使用switch組件實(shí)現(xiàn)切換按鈕,用于控制chart組件數(shù)據(jù)的動(dòng)靜態(tài)顯示。
  2. 使用chart組件依次實(shí)現(xiàn)線形圖、占比圖、柱狀圖。

本應(yīng)用使用div組件用作外層容器,嵌套text、chart、switch等基礎(chǔ)組件,共同呈現(xiàn)圖文顯示的效果。

< !-- index.hml -- >
< div class="container" >
    < !-- 自定義標(biāo)題組件 -- >
    < div class="switch-block" >
        < text class="title" >Switch_Chart< /text >
        < text class="switch-info" >{{ $t('strings.switchInfo') }}< /text >
        < !-- switch按鈕組件 -- >
        < switch onchange="change" >< /switch >
    < /div >
< /div >

在線形圖中,lineOps用于設(shè)置線形圖參數(shù),包括曲線的樣式、端點(diǎn)樣式等。lineData 為線形圖的數(shù)據(jù)。

< !-- index.hml -- >
< div class="container" >
    ....
    < !-- 線形圖組件 -- >
    < div class="chart-block" >
        < stack class="stack-center" >
            < image class="background-image" src="common/images/bg_png_line.png" >< /image >
            < !-- 線形圖 -- >
            < chart class="chart-data" type="line" ref="linechart" options="{{ lineOps }}" datasets="{{ lineData }}" >
            < /chart >
        < /stack >
        < !-- 線形圖標(biāo)題 -- >
        < text class="text-vertical" >{{ $t('strings.lineTitle') }}< /text >
    < /div >
< /div >

相對(duì)于線形圖,占比圖添加了自定義圖例。其中rainBowData為占比圖的數(shù)據(jù)。

< !-- index.hml -- >
< div class="container" >
    ....
    < !-- 占比圖組件 -- >
    < div class="gauge-block" >
        < div class='flex-row-center full-size' >
            < stack class="flex-row-center rainbow-size" >
                < !-- 占比圖組件 -- >
                < chart class="data-gauge" type="rainbow" segments="{{ rainBowData }}" effects="true"
                        animationduration="2000" >< /chart >
                ...
            < /stack >
            < div class='flex-column' >
                < !-- 自定義圖例 -- >    
                < div class="chart-legend-item" >
                    < div class="chart-legend-icon rainbow-color-photo" >< /div >
                    < text class="chart-legend-text" >{{ this.$t('strings.legendPhoto') }} 64GB< /text >
                < /div >
                ....
            < /div >
        < /div >
        < !-- 占比圖標(biāo)題 -- >
        < text class="text-vertical" >{{ $t('strings.rainBowTitle') }}< /text >
    < /div >
< /div >

在柱狀圖中,barOps用于設(shè)置柱狀圖參數(shù),barData為柱狀圖數(shù)據(jù)。

< !-- index.hml -- >
< div class="container" >
    < div class="bar-block" >
        < div class="flex-column full-size" >
            < !-- 自定義圖例 -- >
            ...
            < stack class="full-size bar-height" >
                < image class="background-image" src="common/images/bg_png_bar.png" >< /image >
                < !-- 柱狀圖 -- >
                < chart class="data-bar" type="bar" id="bar-chart1" options="{{ barOps }}" datasets="{{ barData }}" >  
                < /chart >
            < /stack >
        < /div >
        < !-- 柱狀圖標(biāo)題 -- >
        < text class="text-vertical" >{{ $t('strings.barTitle') }}< /text >
    < /div >
< /div >

動(dòng)態(tài)顯示數(shù)據(jù)

在上一章節(jié)講解了switch組件實(shí)現(xiàn)切換按鈕,接下來實(shí)現(xiàn)switch按鈕的點(diǎn)擊事件。在回調(diào)方法中設(shè)置chart組件靜態(tài)或動(dòng)態(tài)顯示,靜態(tài)時(shí)chart組件顯示靜態(tài)數(shù)據(jù),動(dòng)態(tài)時(shí)利用interval定時(shí)器動(dòng)態(tài)生成并顯示隨機(jī)數(shù)據(jù)。

// index.js
export default {
  ...

  /**
   * switch按鈕點(diǎn)擊事件的回調(diào)方法
   */
  change(event) {
    if (event.checked) {
      // 線形圖、柱狀圖數(shù)據(jù)定時(shí)器
      this.interval = setInterval(() = > {
        // 更新線形圖數(shù)據(jù)
        this.changeLine();
        // 更新柱狀圖數(shù)據(jù)
        this.changeBar();
      }, 1000);
      // 占比圖數(shù)據(jù)定時(shí)器
      this.rainbowInterval = setInterval(() = > {
        // 更新占比圖數(shù)據(jù)
        this.changeGauge();
      }, 3000);
    } else {
      clearInterval(this.interval);
      clearInterval(this.rainbowInterval);
    }
  }
}

實(shí)現(xiàn)changeLine方法更新線形圖數(shù)據(jù)。遍歷所有數(shù)據(jù),重新生成隨機(jī)數(shù)并設(shè)置每個(gè)點(diǎn)的數(shù)據(jù)、形狀、大小和顏色,最后為lineData重新賦值。

// index.js
export default {
  ...
  /**
   * 更新線形圖數(shù)據(jù)
   */
  changeLine() {
    const dataArray = [];
    for (let i = 0; i < this.dataLength; i++) {
      const nowValue = Math.floor(Math.random() * CommonConstants.LINE_RANDOM_MAX + 1);
      const obj = {
        // y軸的值
        'value': nowValue,
        'pointStyle': {
          // 點(diǎn)的形狀
          'shape': 'circle',
          'size': CommonConstants.LINE_POINT_SIZE,
          'fillColor': '#FFFFFF',
          'strokeColor': '#0A59F7'
        }
      };
      dataArray.push(obj);
    }
    this.lineData = [
      {
        // 曲線顏色
        strokeColor: '#0A59F7',
        // 漸變填充顏色
        fillColor: '#0A59F7', 
        data: dataArray,
        gradient: true
      }
    ];
  }
}

實(shí)現(xiàn)changeGauge方法更新占比圖數(shù)據(jù),每三秒增長5%的數(shù)據(jù)。

// index.js
export default {
  ...
  /**
   * 更新占比圖數(shù)據(jù)
   */
  changeGauge() {
    const sysData = this.rainBowData[this.rainBowData.length - 2];
    sysData.value += addPercent;
    this.percent += addPercent;
    // 小數(shù)相加,保留一位小數(shù)
    this.used = (this.used * coefficients + addSize * coefficients) / coefficients;
    this.systemDataSize = (this.systemDataSize * coefficients + addSize * coefficients) / coefficients;
    // 數(shù)據(jù)總和到達(dá)100%后恢復(fù)初始數(shù)據(jù)
    if (sysData.value + CommonConstants.RAINBOW_OTHER_PERCENT > CommonConstants.RAINBOW_ALL_PERCENT) {
      sysData.value = CommonConstants.RAINBOW_SYSTEM_PERCENT;
      this.percent = CommonConstants.RAINBOW_USED_PERCENT;
      this.used = CommonConstants.RAINBOW_USED_SIZE;
      this.systemDataSize = CommonConstants.RAINBOW_SYSTEM_SIZE;
    }
    this.rainBowData = this.rainBowData.splice(0, this.rainBowData.length);
  },
}

實(shí)現(xiàn)changeBar方法更新柱狀圖數(shù)據(jù)。遍歷柱狀圖所有的數(shù)據(jù)組,獲取每組的數(shù)據(jù)后,再次遍歷每組數(shù)據(jù),生成隨機(jī)數(shù)并為barData重新賦值。

// index.js
export default {
  ...
  /**
   * 更新柱狀圖數(shù)據(jù)
   */
  changeBar() {
    for (let i = 0; i < this.barGroup; i++) {
      const dataArray = this.barData[i].data;
      for (let j = 0; j < this.dataLength; j++) {
        dataArray[j] = Math.floor(Math.random() * CommonConstants.BAR_RANDOM_MAX + 1);
      }
    }
    this.barData = this.barData.splice(0, this.barGroup + 1);
  }
}

sf

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 鴻蒙
    +關(guān)注

    關(guān)注

    57

    文章

    2287

    瀏覽量

    42629
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    1954

    瀏覽量

    29897
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3607

    瀏覽量

    15958
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    HarmonyOS開發(fā)案例:【 slider組件的使用】

    主要介紹slider滑動(dòng)條組件的使用。如圖所示拖動(dòng)對(duì)應(yīng)滑動(dòng)條調(diào)節(jié)風(fēng)車的旋轉(zhuǎn)速度以及縮放比例。
    的頭像 發(fā)表于 04-25 22:02 ?823次閱讀
    <b class='flag-5'>HarmonyOS</b><b class='flag-5'>開發(fā)案</b>例:【 slider<b class='flag-5'>組件</b>的使用】

    HarmonyOS開發(fā)案例:【使用List組件實(shí)現(xiàn)商品列表】

    OpenHarmony ArkTS提供了豐富的接口和組件,開發(fā)者可以根據(jù)實(shí)際場景和開發(fā)需求,選用不同的組件和接口。
    的頭像 發(fā)表于 05-10 16:41 ?1166次閱讀
    <b class='flag-5'>HarmonyOS</b><b class='flag-5'>開發(fā)案</b>例:【使用List<b class='flag-5'>組件</b>實(shí)現(xiàn)商品列表】

    HarmonyOS開發(fā)案例:【W(wǎng)eb組件實(shí)現(xiàn)抽獎(jiǎng)】

    基于ArkTS的聲明式開發(fā)范式的樣例,主要介紹了Web組件如何加載本地和云端H5小程序。
    的頭像 發(fā)表于 05-09 18:31 ?1264次閱讀
    <b class='flag-5'>HarmonyOS</b><b class='flag-5'>開發(fā)案</b>例:【W(wǎng)eb<b class='flag-5'>組件</b>實(shí)現(xiàn)抽獎(jiǎng)】

    HarmonyOS開發(fā)案例:【基礎(chǔ)組件Slider的使用】

    學(xué)習(xí)如何使用聲明式UI編程框架的基礎(chǔ)組件。本篇Codelab將會(huì)使用Image組件、Slider組件、Text組件共同實(shí)現(xiàn)一個(gè)可調(diào)節(jié)的風(fēng)車動(dòng)畫
    的頭像 發(fā)表于 05-10 16:01 ?608次閱讀
    <b class='flag-5'>HarmonyOS</b><b class='flag-5'>開發(fā)案</b>例:【基礎(chǔ)<b class='flag-5'>組件</b>Slider的使用】

    HarmonyOS開發(fā)案例:【常用組件與布局】

    HarmonyOS ArkUI提供了豐富多樣的UI組件,您可以使用這些組件輕松地編寫出更加豐富、漂亮的界面。
    的頭像 發(fā)表于 05-09 18:20 ?1086次閱讀
    <b class='flag-5'>HarmonyOS</b><b class='flag-5'>開發(fā)案</b>例:【常用<b class='flag-5'>組件</b>與布局】

    HarmonyOS IoT 硬件開發(fā)案例分享

    ``許思維老師HiSpark Wi-Fi IoT 開發(fā)案例分享:案例一:AHT20溫濕度傳感器開發(fā)、調(diào)試;案例二:oled屏驅(qū)動(dòng)庫移植,調(diào)試;案例三:用OLED屏播放視頻,Wi-Fi 和 TCP/IP 綜合應(yīng)用。 ``
    發(fā)表于 10-27 17:30

    【基于HarmonyOS開發(fā)購物應(yīng)用】AnBetter Codelab第二期記錄帖

    功能介紹使用UI組件開發(fā)出一個(gè)HarmonyOS購物應(yīng)用。HarmonyOS開發(fā)者提供了多種組件
    發(fā)表于 04-17 00:55

    HarmonyOS應(yīng)用開發(fā)資料(Svg組件

    1、HarmonyOS應(yīng)用開發(fā)-Svg組件circle  該組件從API version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。2、
    發(fā)表于 03-17 14:49

    組件資料】HarmonyOS三方件開發(fā)指南

    1、HarmonyOS三方件開發(fā)指南——LoadingView組件1.LoadingView組件功能介紹1.1.功能介紹:LoadingView組件
    發(fā)表于 03-21 11:18

    HarmonyOS應(yīng)用開發(fā)-SwitchApplication體驗(yàn)

    一、組件說明使用了JS UI中的switch、chart組件。 您可以通過切換switch開關(guān)設(shè)置cha
    發(fā)表于 08-19 14:52

    HarmonyOS實(shí)現(xiàn)靜態(tài)與動(dòng)態(tài)數(shù)據(jù)可視化圖表

    一. 樣例介紹 本篇Codelab基于switch組件chart組件,實(shí)現(xiàn)線形圖、占比圖、柱狀圖,并通過switch切換
    發(fā)表于 09-06 14:21

    HarmonyOS鴻蒙原生應(yīng)用開發(fā)設(shè)計(jì)- 服務(wù)組件

    HarmonyOS設(shè)計(jì)文檔中,為大家提供了一些已經(jīng)設(shè)計(jì)好的原生服務(wù)組件庫,開發(fā)者可以根據(jù)需要直接引用。 開發(fā)者直接使用官方提供的服務(wù)組件庫樣
    發(fā)表于 10-24 16:12

    華為開發(fā)HarmonyOS零基礎(chǔ)入門:UI組件設(shè)計(jì)開發(fā)實(shí)踐

    華為開發(fā)HarmonyOS零基礎(chǔ)入門:UI組件設(shè)計(jì)開發(fā)實(shí)踐之圖庫應(yīng)用介紹,應(yīng)用數(shù)據(jù)加載顯示模型圖片加載渲染功能快速在其他應(yīng)用上。
    的頭像 發(fā)表于 10-23 10:58 ?1609次閱讀
    華為<b class='flag-5'>開發(fā)</b>者<b class='flag-5'>HarmonyOS</b>零基礎(chǔ)入門:UI<b class='flag-5'>組件</b>設(shè)計(jì)<b class='flag-5'>開發(fā)</b>實(shí)踐

    華為開發(fā)者分論壇HarmonyOS學(xué)生公開課-OpenHarmony Codelabs開發(fā)案

    2021華為開發(fā)者分論壇HarmonyOS學(xué)生公開課-OpenHarmony Codelabs開發(fā)案
    的頭像 發(fā)表于 10-24 11:25 ?1852次閱讀
    華為<b class='flag-5'>開發(fā)</b>者分論壇<b class='flag-5'>HarmonyOS</b>學(xué)生公開課-OpenHarmony Codelabs<b class='flag-5'>開發(fā)案</b>例

    如何開發(fā)一個(gè)helm chart

    使用helmfile時(shí),我們首先得了解helm的使用,以及如何開發(fā)一個(gè)helm chart。
    的頭像 發(fā)表于 05-16 09:28 ?605次閱讀