-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathember-chart.js
More file actions
90 lines (79 loc) · 3.39 KB
/
ember-chart.js
File metadata and controls
90 lines (79 loc) · 3.39 KB
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
import Ember from 'ember';
import ChartDataUpdater from 'ember-cli-chart/chart-data-updater';
/* global Chart */
export default Ember.Component.extend({
tagName: 'canvas',
attributeBindings: ['width', 'height'],
lineLegendTemp: "<ul class=\"<%=name.toLowerCase()%>-legend\" style=\"list-style-type: none;\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>; width: 8px;height: 8px;display: inline-block;border-radius: 10px;border: solid;border-width: 2px;margin: 5px 5px 0 0;\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
pieLegendTemp: "<ul class=\"<%=name.toLowerCase()%>-legend\" style=\"list-style-type: none;\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>; width: 8px;height: 8px;display: inline-block;border-radius: 10px;border: solid;border-width: 2px;margin: 5px 5px 0 0;\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>",
/**
* This is where ember-chart will look to find the options to pass to the Chart.js chart.
* Subclasses may change this path to create a layer of indirection for the options, while
* preserving the same component API. This is useful if you want to extend this component to
* provide component-specific defaults in your own app.
*/
optionsPath: 'options',
didInsertElement: function(){
var context = this.get('element').getContext('2d');
var data = this.get('data');
var type = Ember.String.classify(this.get('type'));
var template;
switch (type) {
case "Line":
template = this.get('lineLegendTemp');
break;
case "Pie":
template = this.get('pieLegendTemp');
break;
case "Doughnut":
template = this.get('pieLegendTemp');
break;
default:
template = this.get('lineLegendTemp');
break;
}
var options = Ember.merge({
legendTemplate : template
}, this.get(this.get('optionsPath')));
var redraw = this.get('redraw');
var chart = new Chart(context)[type](data, options);
if (this.get('legend')) {
var legend = chart.generateLegend();
this.$().wrap("<div class='chart-parent'></div>");
this.$().parent().append(legend);
}
this.set('redraw', redraw);
this.set('chart', chart);
this.addObserver('data', this, this.updateChart);
this.addObserver('data.[]', this, this.updateChart);
this.addObserver(this.get('optionsPath'), this, this.updateChart);
},
willDestroyElement: function(){
if (this.get('legend')) {
this.$().parent().children('[class$=legend]').remove();
}
this.get('chart').destroy();
this.removeObserver('data', this, this.updateChart);
this.removeObserver('data.[]', this, this.updateChart);
this.removeObserver(this.get('optionsPath'), this, this.updateChart);
},
updateChart: function(){
var chart = this.get('chart');
var data = this.get('data');
var redraw = ChartDataUpdater.create({
data: data,
chart: chart
}).updateByType();
if (this.get('redraw') || redraw) {
this.willDestroyElement();
this.didInsertElement();
} else {
chart.update();
}
if (this.get('legend')) {
this.$().parent().children('[class$=legend]').remove();
var legend = chart.generateLegend();
this.$().parent().append(legend);
}
}
});