forked from trading-peter/chart-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-behavior.html
63 lines (51 loc) · 1.32 KB
/
context-behavior.html
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
<script>
var ChartBehaviors = ChartBehaviors || {};
/** @polymerBehavior */
ChartBehaviors.ContextBehavior = Polymer.dedupingMixin(function(superClass) {
return class extends superClass {
_measure(cb) {
function measure() {
if (this.offsetHeight) {
cb(true);
} else {
cb(false);
}
}
requestAnimationFrame(measure.bind(this));
}
_queue() {
if (this.hasData) {
this._measure(hasHeight => {
if (hasHeight) {
this.updateChart();
}
});
}
}
updateChart() {
this.async(function () {
if (this.chart) {
this.chart.stop();
this.mixin(this.chart.data, this.data);
this.chart.update();
} else {
this.async(function () {
if (this.hasData) {
this.chart = new Chart(this.ctx, {
type: this.type,
data: this.data,
options: this.options
});
}
}, null, 0);
}
}, null, 0);
}
connectedCallback() {
super.connectedCallback();
this.ctx = this.$.canvas.getContext('2d');
this._queue();
}
}
});
</script>