使用zrender.js繪製體溫單(2)

来源:https://www.cnblogs.com/hprBlog/archive/2019/10/31/11770148.html
-Advertisement-
Play Games

今天我們來畫折線圖 效果圖 以下為模擬數據 首先創建filterData方法 用於過濾數據 text文本 line線段 area圓 tag暫時用不到 今天說的是折線所以創建zrLine 方法 我們在新增一個文件夾創建utli.js這個文件夾的作用為我們把創建線創建圓的公共方法寫在這個js文件里 ut ...


今天我們來畫折線圖 效果圖

 以下為模擬數據

[{"time":19,"text":"入\n院\n19\n時\n11\n分","position":42,"cellMin":29.0,"cellSplit":0.2,"type":"text","color":"red","shape":null},{"time":22,"text":"手\n術","position":42,"cellMin":29.0,"cellSplit":0.2,"type":"text","color":"red","shape":null},{"time":129,"text":"手\n術","position":42,"cellMin":29.0,"cellSplit":0.2,"type":"text","color":"red","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":30.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":31.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":32.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":33.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":34.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":35.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":36.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":37.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":38.0,"type":"baseline","color":"#000","shape":null},{"cellMin":28.0,"cellSplit":0.2,"y":39.0,"type":"baseline","color":"red","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":40.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":41.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"y":42.0,"type":"baseline","color":"#000","shape":null},{"cellMin":29.0,"cellSplit":0.2,"array":[{"time":19,"tips":"體溫37.1","value":"37.1","shape":"x","Break":"false","type":"temperature","extraArr":[],"others":[]},{"time":21,"tips":"體溫36.9","value":"36.9","shape":"x","Break":"false","type":"temperature","extraArr":[],"others":[]},{"time":30,"tips":"體溫36.5","value":"36.5","shape":"x","Break":"false","type":"temperature","extraArr":[],"others":[]},{"time":38,"tips":"體溫36.6","value":"36.6","shape":"x","Break":"false","type":"temperature","extraArr":[],"others":[]},{"time":54,"tips":"體溫36.7","value":"36.7","shape":"x","Break":"false","type":"temperature","extraArr":[],"others":[]}],"type":"line","color":"blue","shape":"x-circle"},{"cellMin":-10.0,"cellSplit":2.0,"array":[{"time":19,"shape":"empty-circle","tips":"呼吸20","value":"20","Break":"false"},{"time":21,"shape":"empty-circle","tips":"呼吸20","value":"20","Break":"false"},{"time":30,"shape":"empty-circle","tips":"呼吸19","value":"19","Break":"false"},{"time":38,"shape":"empty-circle","tips":"呼吸18","value":"18","Break":"false"},{"time":54,"shape":"empty-circle","tips":"呼吸19","value":"19","Break":"false"}],"type":"line","color":"black","shape":"empty-circle"},{"cellMin":-2.0,"cellSplit":1.0,"array":[{"time":19,"tips":"疼痛7","value":"7","Break":"false","type":"pain","extraArr":[{"extra":"3","extraColor":"red","extraTips":"疼痛評價3"}],"others":[]},{"time":23,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[],"others":[]},{"time":27,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[],"others":[]},{"time":33,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[],"others":[]},{"time":39,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[{"extra":"3","extraColor":"red","extraTips":"疼痛評價3"}],"others":[]},{"time":44,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[],"others":[]},{"time":51,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[],"others":[]},{"time":58,"tips":"疼痛3","value":"3","Break":"false","type":"pain","extraArr":[{"extra":"3","extraColor":"red","extraTips":"疼痛評價3"}],"others":[]}],"type":"line","color":"red","shape":"empty-circle"},{"bgColor":"rgba(255,0,0,0.7)","cellMin":30.0,"cellSplit":2.0,"array":[{"time":19,"v1":69,"v1Tips":"心率69","v2":69,"v2Tips":"脈搏69","Break":"false"},{"time":21,"v1":70,"v1Tips":"心率70","v2":70,"v2Tips":"脈搏70","Break":"false"},{"time":30,"v1":83,"v1Tips":"心率83","v2":83,"v2Tips":"脈搏83","Break":"false"},{"time":38,"v1":78,"v1Tips":"心率78","v2":78,"v2Tips":"脈搏78","Break":"false"},{"time":54,"v1":77,"v1Tips":"心率77","v2":77,"v2Tips":"脈搏77","Break":"false"}],"type":"area","color":"red","shape":null},{"text":null,"y":"28","cellMin":-10.0,"cellSplit":2.0,"array":[],"type":"tag","color":"black","shape":null},{"text":null,"y":null,"cellMin":30.0,"cellSplit":2.0,"array":[],"type":"tag","color":"black","shape":null}]

  首先創建filterData方法 用於過濾數據 text文本 line線段 area圓 tag暫時用不到 今天說的是折線所以創建zrLine 方法

filterData(){
      const data = chartData
      
      data.forEach(el => {
        switch (el.type) {
          case "text":
            // this.zrText(el)
            break;
          case "line":
            this.zrLine(el)
            break;
          case "area":
            this.zrPolyline(el)
            break;
          case "tag":
            this.zrTag(el)
            break;
        
          default:
            break;
        }
        });
    }
我們在新增一個文件夾創建utli.js這個文件夾的作用為我們把創建線創建圓的公共方法寫在這個js文件里

 utli.js 我們先說 createLine createCircle 

 createLine 需要傳5個參數分別為開始點的橫縱坐標 結束點的橫縱坐標 還有線的樣式

 createCircle 需要傳4個參數分別為 圓點的橫縱坐標 圓的半徑 和樣式  

addHover 也需要 這時我們需要在init 方法里添加一段代碼(上一章創建的初始化方法) 這段代碼為創建一個div到時我們滑鼠移到圓上會彈出文本信息的時候回用到
var div = document.createElement("div")
      div.classList.add("tips")
      document.getElementById("main").append(div)
View Code

 utli.js

//線段
export const createLine = (x1,y1,x2,y2,style)=>{
    return new zrender.Line({
        shape:{
            x1:x1,
            y1:y1,
            x2:x2,
            y2:y2
        },
        style:style,
    });
};
// cx 橫坐標 cy縱坐標 r半徑 空心圓
export const createCircle = (cx,cy,r,style)=>{
    return new zrender.Circle({
        shape:{
            cx:cx,
            cy:cy,
            r:r
        },
        style:style,
        zlevel:4
    })
}
//添加horver事件 el 元素對象 config 一些配置項 x x軸坐標 y y軸坐標 shapeOn滑鼠移入一些屬性配置 shapeOn滑鼠移出一些屬性配置 shape配置項看官網  
export const addHover = (el,config,x,y,shapeOn,shapeOut) => {
    const domTips = document.getElementsByClassName("tips")
    el.on('mouseover',function(){
        domTips[0].innerHTML = config.tips
        domTips[0].setAttribute("style",`position:absolute;top:${y-13}px;left:${x}px;display:block;font-size:10px;background-color:rgba(0,0,0,.7);padding:3px;border-radius:3px;color:#fff`)
        el.animateTo({
            shape:shapeOn
        },100,0)
    }).on('mouseout',function () {
        domTips[0].setAttribute("style",`display:none`)
        el.animateTo({
            shape:shapeOut
          },100,0)
    })
}
//多邊形
export const createPolygon = (points,style) => {
    return new zrender.Polyline({
        shape:{
            points:points,
        },
        style:style
    })
}

 

zrLine方法里的第一段代碼 判斷這個折線拐點是需要空心圓還是實心圓還是其他的形狀 都通過shape決定 color為圓的邊框顏色填充色為白色 先定義一個style變數到時好實現自定義

 

 

   var style = {}
      switch (data.shape) {
        case "x-circle":
          style = {
            stroke:data.color,
            fill:"#fff",
            text:"x",
          }
          break;
        case "empty-circle":
          style = {
            stroke:data.color,
            fill:"#fff",
            text:"",
          }
          break;
      
        default:
          break;
      }

這裡需要在添加2個方法 

getX
//獲取X坐標 data當前時間點
    getX(data){
      let XShareOne = this.XShareOne()
      return data * XShareOne
    },
View Code transformY
//轉換y軸坐標點為正確坐標點 因為y軸坐標是頂點為0遞增的 所有用總高度減去原來坐標的高度剩下的高度就是正確坐標點
    //i代表一個格子代表幾個高度
    transformY(data,i){
      let YHeight = this.YShareOne()
      
      //計算出剩餘高度
      let surplusHeight = this.canavsHeight - (YHeight/i) * data
      return surplusHeight
    },

 

這段代碼意思是先把數據遍歷出來 在通過time屬性計算出x坐標 value值計算出y坐標 x軸左邊基本是以time為基本來計算的 y軸坐標可能會隨數據變化而有所改變 Break屬性為是否斷線 如果需要斷線就位true

      data.array.forEach((el,i) =>{
        if (i > 0) {
          let XShareOne = this.XShareOne()

          let firstX = this.getX(data.array[i-1].time)  
          let firstY = this.transformY(data.array[i-1].value,1)
          let x = this.getX(data.array[i].time)
          let y = this.transformY(data.array[i].value,1)
          
          if (data.array[i-1].Break == "false") {
            let line = createLine(firstX,firstY,x,y,{
                stroke:"#af2377",
                lineWidth:2,
            })
            this.zr.add(line)
          }
        }
        
        if (el.extraArr && el.extraArr.length > 0) {
            el.extraArr.forEach((item,a) => {
              let x = this.getX(el.time)
              let y = this.transformY(el.value,1)

              let lastY =  this.transformY(item.extra,1)
              let dottedLine = createLine(x,y,x,lastY,{
                  stroke:"#af2377",
                  lineWidth:2,
                  lineDash:[2,2]
              })
              this.zr.add(dottedLine)

              el.extraArr.forEach((item,a) => {
                let getY = this.transformY(item.extra,1)
                
                let Circle = createCircle(x,getY,5,{
                  stroke:item.extraColor,
                  fill:"#fff",
                })
                this.zr.add(Circle)
                addHover(Circle,{
                    tips:item.extraTips,
                },x,getY,{
                    r:8,
                  },{
                    r:5,
                })
              })
            })
         }
        let getX = this.getX(el.time)
        let getY = this.transformY(el.value,1)

        let Circle = createCircle(getX,getY,5,style)
        this.zr.add(Circle)
        addHover(Circle,el,getX,getY,{
            r:8,
          },{
             r:5,
        })
      })

這步完成折線圖應該就畫好了 

下次我們講陰影的畫法    

 https://gitee.com/hpr/Temperature.git git地址


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.基礎類型 A. 基礎類型有5種,Number,String,Boolean,Null,Undefined B. 基礎類型沒有堆的概念,堆只針對引用類型。 所有基礎類型都是以key-value形式存放在棧中,其中,基礎類型的值不可變,除非是重新賦值, 比如: 2.引用類型 引用類型,如Array, ...
  • 鏈接如下: https://blog.csdn.net/qq_33769914/article/details/83856268 https://www.cnblogs.com/YuKiee/p/9651240.html ...
  • Webpack 是一個前端資源載入/打包工具。它將根據模塊的依賴關係進行靜態分析,然後將這些模塊按照指定的規則生成對應的靜態資源。 基於 Webpack3.0 測試通過。 從圖中我們可以看出,Webpack 可以將多種靜態資源 js、css、less 轉換成一個靜態文件,減少了頁面的請求。 接下來我 ...
  • //非同步1<template> <div class="addequipment org"> <div class="top"> <div class="topfirst">菜單管理</div> <div class="addequi" @click='addNew'>添加</div> </div>... ...
  • [toc] Vue 單元測試 "官網:https://vue test utils.vuejs.org/zh" 定義: 單元測試是用來對一個模塊、一個函數或者一個類來進行正確性檢驗的測試工作。 指令: package.json文件 "test:unit": "vue cli service test ...
  • 1.從微信小程式的官網扣下來的demo,實際測試中,發現6s ios10 系統不相容,裡面的內容出不來 2.實際效果如下: 底部的tabbar沒出來 3.把 cover-view 改成 view 把 cover-image 改成 image 標簽 4. 記得把你的css 樣式也改一下啊 預設的 cl ...
  • 先理解兩個概念:基本類型和引用類型的值 1、基本類型和引用類型的值 (1)定義: 基本類型:指簡單的數據段,比如按值訪問的js五種基本數據類型undefined、null、boolean、number、srtring 引用類型的值:指保存在記憶體中的對象,註意一點,js不允許直接操作對象的空間 (2) ...
  • 組件允許你將 UI 拆分為獨立可復用的代碼片段,並對每個片段進行獨立構思。所以當在動手寫代碼之前,要分析UI,如何劃分組件是一個需要在動手之前想清楚的問題,最重要的就是獨立以及可復用。獨立代表不會影響到其他組件,可復用代表省去了很多重覆工作量,所以這兩部分在劃分組件的時候要重點思考。 組件,從概念上 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...