-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
32 lines (27 loc) · 886 Bytes
/
index.js
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
const html = require('choo/html')
const Chartist = require('chartist')
const DEFAULT_CHART_TYPE = 'Line'
const cache = {
chart: null,
el: null
}
function onload (el, type, data, options) {
if (!cache.chart) {
cache.chart = initChart(el, { type: type, data: data, options: options })
}
}
function initChart (el, config) {
let type = config.type || DEFAULT_CHART_TYPE
let data = config.data || { labels: [], series: [] }
let options = config.options || {}
let responsiveOptions = config.responsiveOptions || []
return new Chartist[type](el, data, options, responsiveOptions)
}
module.exports = function (type, data, options, responsiveOptions) {
if (!cache.el) {
cache.el = html`<div class="ct-chart" onload=${(el) => { onload(el, type, data, options, responsiveOptions)} }></div>`
} else {
cache.chart.update(data, options)
}
return cache.el
}