折線圖 - Line Chart

1
<div id="line-chart" class="chart"></div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import range from 'lodash.range'
import sample from 'lodash.sample'
import throttle from 'lodash.throttle'
import toPixel from '@superlanding/topixel'
import Theme from '../../assets/js/models/Theme'

export default function bindLineCharts() {

  const { LineChart } = window.beyond
  const dom = document.getElementById('line-chart')

  if (! dom) {
    return () => {}
  }

  const padZero = v => v.toString().padStart(2, '0')
  const toXLabel = v => {
    const d = new Date(v)
    return padZero(d.getHours()) + ':' + padZero(d.getMinutes())
  }
  const toYLabel = v => {
    const numStr = (v / 10000).toFixed(1)
    const [firstNum, secondNum] = numStr.split('.')
    if ((firstNum === '0') && (secondNum === '0')) {
      return '0'
    }
    if (secondNum === '0') {
      return firstNum + ''
    }
    return numStr + ''
  }

  const fiveMins = 5 * 60 * 1000
  const chartMenu = document.getElementById('chart-menu')
  const theme = Theme.get()
  const c = new LineChart(dom, {
    theme,
    toXLabel,
    toYLabel,
    lineLabels: ['線段1', '線段2', '線段3'],
    yStep: 2 * 10000,
    onPointMouseOver(mousePos, res) {
      if (res) {
        const { point } = res
        chartMenu.innerHTML = `
          <div>時間: ${toXLabel(point.x)}</div>
          <div>金錢: ${point.y}</div>
        `
        chartMenu.style.left = toPixel(mousePos.x)
        chartMenu.style.top = toPixel(mousePos.y + 20)
        chartMenu.style.display = 'block'
      }
      else {
        chartMenu.style.display = 'none'
      }
    }
  })

  const now = +new Date()
  const getSampleValue = () => sample([1000, 2000, 3000, 4000, 500])
  const points1 = range(1, 20 + 1)
    .map(i => {
      return {
        x: now + (i * fiveMins),
        y: i * getSampleValue()
      }
    })

  const points2 = range(1, 20 + 1)
    .map(i => {
      return {
        x: now + (i * fiveMins),
        y: i * getSampleValue()
      }
    })

  const points3 = range(1, 20 + 1)
    .map(i => {
      return {
        x: now + (i * fiveMins),
        y: i * getSampleValue()
      }
    })

  c.setData([points1, points2, points3])

  let domWidth = dom.offsetWidth

  const handleResize = throttle(() => {
    if (dom.offsetWidth !== domWidth) {
      c.refresh()
    }
    domWidth = dom.offsetWidth
  }, 300)
  window.addEventListener('resize', handleResize)

  const handleThemeChange = () => {
    const theme = Theme.get()
    c.setTheme({ theme })
    c.refresh()
  }
  document.addEventListener('beyond-theme-change', handleThemeChange)

  return function unbindLineCharts() {
    window.removeEventListener('resize', handleResize)
    document.removeEventListener('beyond-theme-change', handleThemeChange)
    c.destroy()
  }
}

Line Chart

constructor

const lineChart = new LineChart(dom, options)
名稱 型別 說明 預設值
dom HTMLElement 需要設定成 LineChart 的元素
options object 選項 {}
options.height integer 圖表的高 dom.offsetHeight
options.width integer 圖表的寬 dom.offsetWidth
options.toXLabel function 轉換成 x 軸標籤的方法 v => v
options.toYLabel function 轉換成 y 軸標籤的方法 v => v
options.xPadding integer 圖表外圍到上下邊界的距離 20
options.yPadding integer 圖表外圍到左右邊界的距離 20
options.xGutter integer x 軸標籤之間可允許的最小距離 100
options.yGutter integer y 軸標籤之間可允許的最小距離 10
options.lineStyles array 線段的色彩樣式 ['#5469d4', '#7c54d4', '#a254d4']
options.bg string 圖表的背景顏色 '#fff'
options.fontSize integer 圖表的字體大小 12
options.xStep integer x 軸標籤與標籤的間隔數,預設會根據 xGutter 計算最多可顯示的間隔數
options.yStep integer y 軸標籤與標籤的間隔數,預設會根據 yGutter 計算最多可顯示的間隔數
options.lineLabels array<string> 要標示的線段標籤文字 []
options.xLabelMargin integer xLabel 到圖表的距離 10
options.yLabelMargin integer yLabel 到圖表的距離 10
options.onPointMouseOver function 當滑鼠接近某個數值的點的時候的事件 ( 通常用來顯示 tooltip ),回傳 mousePos ( 在視窗上的座標 ) 與 res ( 節點的資料,res 有可能是 undefined,這時隱藏 tooltip 即可 )
Line Chart Intro

setData

// const line1 = [{ x, y }, ...]
// const line2 = [{ x, y }, ...]
// const pointsArr = [line1, line2]
lineChart.setData(pointsArr)
名稱 型別 說明
pointsArr array 要設定的線段資料陣列

refresh

重畫圖表 (如果 canvas 外框寬高有變的話,可以用此方法重畫)

lineChart.refresh()

destroy

移除 LineChart 產生的 dom 元素與 event listeners

lineChart.destroy()