長條圖 - Bar Chart

1
<div id="bar-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
import throttle from 'lodash.throttle'
import toPixel from '@superlanding/topixel'
import Theme from '../../assets/js/models/Theme'

export default function bindBarCharts() {

  const { BarChart } = window.beyond
  const dom = document.getElementById('bar-chart')

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

  const chartMenu = document.getElementById('chart-menu')
  const theme = Theme.get()

  const b = new BarChart(dom, {
    theme,
    onBarMouseOver(mousePos, res) {
      if (res) {
        const { index, bar } = res
        chartMenu.innerHTML = `
          <div>index: ${index}</div>
          <div>時間: ${bar.label}</div>
          <div>數字: ${bar.value}</div>
        `
        chartMenu.style.left = toPixel(mousePos.x)
        chartMenu.style.top = toPixel(mousePos.y + 20)
        chartMenu.style.display = 'block'
      }
      else {
        chartMenu.style.display = 'none'
      }
    }
  })

  b.setData([
    { label: '1 個月內', value: 0.8 },
    { label: '3 個月內', value: 1.7 },
    { label: '6 個月內', value: 1.1 }
  ])

  let domWidth = dom.offsetWidth

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

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

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

Bar Chart

constructor

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

setData

// const data = [
//   { label: 'Bar 1', value: 0 },
//   { label: 'Bar 2', value: 1 }
// ]
barChart.setData(data)
名稱 型別 說明
data array 要設定的長條圖資料陣列

refresh

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

barChart.refresh()

destroy

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

barChart.destroy()