翻譯|使用教程|編輯:龔雪|2025-07-10 11:05:14.883|閱讀 111 次
概述:本文將為大家介紹如何用圖表控件LightningChart JS實現(xiàn)實時并行坐標圖開發(fā),歡迎下載新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
LightningChart JS是Web上性能特高的圖表庫,具有出色的執(zhí)行性能 - 使用高數(shù)據(jù)速率同時監(jiān)控數(shù)十個數(shù)據(jù)源。 GPU加速和WebGL渲染確保您的設備的圖形處理器得到有效利用,從而實現(xiàn)高刷新率和流暢的動畫,常用于貿易,工程,航空航天,醫(yī)藥和其他領域的應用。
實時并行坐標圖是在傳統(tǒng)并行坐標圖基礎上的延伸,能在圖表展示過程中動態(tài)更新數(shù)據(jù)。它適用于多維數(shù)據(jù)可視化:每個維度用一條垂直坐標軸表示,數(shù)據(jù)實例則通過多軸連線展示。
實時并行坐標圖可用于監(jiān)控系統(tǒng)或進程中的實時變化,如傳感器數(shù)據(jù)、事件流等。例如,當同時監(jiān)測溫度、壓力、速度等多維數(shù)據(jù)時,它能幫助快速發(fā)現(xiàn)趨勢、模式或異常。
要遵循這個并行坐標項目,請下載包含所有必要資源的ZIP文件。()
1. 下載提供的模板來學習本教程。
2. 下載模板后,您會看到如下的文件樹:
3. 打開一個新終端,運行npm install命令。
4. 將配置保存在tsconfig.json文件中是很重要的,這個配置將幫助您導入JSON文件作為數(shù)據(jù)對象。
建議更新到最新LightningChart JS 與 XYData 版本,確保支持相關功能。以下為 package.json 中的推薦依賴:
"dependencies": { "@arction/lcjs": "^5.2.0", "@arction/xydata": "^1.4.0", "@lightningchart/lcjs": "^6.1.2", "@lightningchart/xydata": "^1.4.0" }
1. 引入庫
const lcjs = require('@lightningchart/lcjs') const { lightningchart, Tehems, AxisScrollStrategies, LUT, regularColorSteps } = lcjs
2. 注冊(試用)許可證
let license = undefined try { license = 'xxxxxxxxxxxxx' } catch (e) {}
代碼的第一部分初始化lightningChart實例,并使用各種屬性配置Parallel Coordinate Chart。
const chart = lightningChart({license: license}) .ParallelCoordinateChart({ theme: Themes.darkGold, }) .setTitle('Real-Time Parallel Coordinate Chart') .setAxes(['Time', 'A', 'B', 'C', 'D', 'E']) .setSpline(true) .forEachAxis((axis) => axis.setScrollStrategy(AxisScrollStrategies.fittingStepped)) // Time axis is only used for dynamic coloring. chart.getAxis('Time').setVisible(false)
const RandomDataGenerator = (start, mul) => { let prev = start return () => { const y = prev + (Math.random() * 2 - 1) * mul prev = y return y } }
定義 RandomDataGenerator 函數(shù)用于生成隨機數(shù)據(jù)點。它接收兩個參數(shù):start(初始值)和 mul(決定波動范圍的乘數(shù))。
該函數(shù)維護一個 prev 變量跟蹤上次生成的值,下一個值基于限定范圍內的隨機波動計算得出。此函數(shù)用于為"A"、"B"、"C"、"D"、"E"坐標軸生成隨機數(shù)據(jù)。
為每個坐標軸("A"、"B"、"C"、"D"、"E")創(chuàng)建五個 RandomDataGenerator 實例以模擬實時變化數(shù)據(jù)。每個軸具有不同的初始值和乘數(shù),確保生成唯一的隨機數(shù)據(jù)。這些實例命名為 Y0、Y1、Y2、Y3 和 Y4。
const Y0 = RandomDataGenerator(0, 2) const Y1 = RandomDataGenerator(1000, 200) const Y2 = RandomDataGenerator(100, 20) const Y3 = RandomDataGenerator(10, 2) const Y4 = RandomDataGenerator(80, 10)
let tLastCleanup = -10000 setInterval(() => { const tNow = performance.now() const y0 = Y0() const y1 = Y1() const y2 = Y2() const y3 = Y3() const y4 = Y4() chart.addSeries({ automaticColorIndex: 0 }).setData({ Time: tNow, A: y0, B: y1, C: y2, D: y3, E: y4, })
通過 setInterval() 設置間隔函數(shù)每 50 毫秒運行一次。函數(shù)內部通過調用對應的 RandomDataGenerator 實例為每個軸生成新數(shù)據(jù)點。
生成的數(shù)據(jù)與當前時間(tNow)通過 chart.addSeries() 添加到圖表中。這模擬了實時數(shù)據(jù)流入圖表的過程,各軸數(shù)據(jù)持續(xù)更新。
// Manual data cleaning, batched for better performance. if (tNow - tLastCleanup >= 5_000) { // Remove series that are older than 1 minute const series = chart.getSeries().slice() series.forEach((s) => { const data = s.getData() const timestamp = data && data.Time if (timestamp && tNow - timestamp >= 60_000) s.dispose() })
代碼包含每5秒運行的清理機制。清理過程檢查超過一分鐘(60,000毫秒)的序列并將其從圖表中移除以保持性能。tLastCleanup 變量記錄上次清理時間,通過比較當前時間與序列數(shù)據(jù)點的時間戳決定是否應銷毀。
// Manual data cleaning, batched for better performance. if (tNow - tLastCleanup >= 5_000) { // Remove series that are older than 1 minute const series = chart.getSeries().slice() series.forEach((s) => { const data = s.getData() const timestamp = data && data.Time if (timestamp && tNow - timestamp >= 60_000) s.dispose() }) // Color series dynamically based on how new the data points are chart.setLUT({ axis: chart.getAxis('Time'), lut: new LUT({ interpolate: true, steps: regularColorSteps(tNow - 60_000, tNow, chart.getTheme().examples.intensityColorPalette).map((step, i, steps) => ({ ...step, color: step.color.setA(255 * (i / (steps.length - 1))), })), }), }) tLastCleanup = tNow } }, 50)
清理舊序列后,代碼根據(jù)數(shù)據(jù)點時效設置動態(tài)著色。使用查找表(LUT)進行顏色插值,直觀呈現(xiàn)數(shù)據(jù)點的新舊程度。顏色透明度基于數(shù)據(jù)生成時間動態(tài)調整(255 * (i/(steps.length-1))),形成隨時間變化的視覺漸變效果。該LUT應用于"Time"坐標軸,確保圖表直觀反映數(shù)據(jù)時效
圖表初始化
在終端運行 npm start 命令,在本地服務器可視化圖表。
如果您的項目需要處理復雜實時數(shù)據(jù)并進行高效展示,LightningChart JS 是非常值得考慮的專業(yè)級方案。
更多產(chǎn)品信息歡迎“”了解!
慧都是?家?業(yè)數(shù)字化解決?案公司,專注于軟件、?油與?業(yè)領域,以深?的業(yè)務理解和?業(yè)經(jīng)驗,幫助企業(yè)實現(xiàn)智能化轉型與持續(xù)競爭優(yōu)勢。
慧都科技是LightningChart的中國區(qū)的合作伙伴,LightningChart作為數(shù)據(jù)可視化領域的優(yōu)秀產(chǎn)品,通過高性能的GPU加速渲染技術,顯著提升企業(yè)在實時數(shù)據(jù)可視化與海量數(shù)據(jù)處理方面的效率,尤其適用于需要快速呈現(xiàn)和分析數(shù)十億數(shù)據(jù)點的場景。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網(wǎng)