-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf-chart.es5.js
76 lines (63 loc) · 1.87 KB
/
sf-chart.es5.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
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
var SfChart = Object.create(HTMLElement.prototype)
SfChart.createdCallback = function() {
Object.defineProperty(this, 'data', {
get: function() {return this._data},
set: function(newData) {
this._data = newData
this.render()
}
})
this.render()
}
SfChart.attributeChangedCallback = function() {
let data = undefined
try {
data = JSON.parse(this.getAttribute('data'))
} catch(e) {}
if (this._data != data) {
this._data = data
}
this.render()
}
SfChart.setData = function(data) {
this._data = data
this.render()
}
SfChart.render = function() {
if (!this._data) return
const width = this.getAttribute('width')
const height = this.getAttribute('height')
const type = this.getAttribute('type')
const xAxisLabel = this.getAttribute('x-axis-label')
const yAxisLabel = this.getAttribute('y-axis-label')
const el = this
el.innerHTML = ''
nv.addGraph(function() {
var chart
if (type === 'line' || type === 'dots') {
chart = nv.models.lineChart()
chart.useInteractiveGuideline(true)
chart.x(function(d) { return d[0] })
chart.y(function(d) { return d[1] })
} else {
chart = nv.models.multiBarChart()
chart.showControls(false)
if (type === 'bar-stacked') chart.stacked(true)
chart.x(function(d) { return d[0] })
chart.y(function(d) { return d[1] })
}
chart.margin({left: 80, bottom: 50})
chart.xAxis.axisLabel(xAxisLabel)
chart.xAxis.tickFormat(function(d) { return el._data.buckets[d] })
chart.yAxis.axisLabel(yAxisLabel)
var curChart = d3.select(el).append('svg')
curChart.datum(el._data.data)
curChart.call(chart)
curChart.attr('width', width)
curChart.attr('height', height)
addResizeListener(el.parentNode, chart.update)
chart.update()
return chart
})
}
document.registerElement('sf-chart', {prototype: SfChart})