1
- class Charts
2
- constructor : ->
3
- $ =>
4
- @ initLineChart ()
5
- @ initBarChart ()
6
- @ initRadarChart ()
7
- @ initPolarAreaChart ()
8
- @ initPieChart ()
9
- @ initDoughnutChart ()
10
-
11
- initLineChart : ->
12
- data = {
1
+ export class Charts {
2
+
3
+ constructor ( element ) {
4
+ if ( $ ( element ) . data ( 'type' ) == "line-chart" ) {
5
+ this . initLineChart ( element ) ;
6
+ }
7
+ else if ( $ ( element ) . data ( 'type' ) == "bar-chart" ) {
8
+ this . initBarChart ( element ) ;
9
+ }
10
+ else if ( $ ( element ) . data ( 'type' ) == "radar-chart" ) {
11
+ this . initRadarChart ( element ) ;
12
+ }
13
+ else if ( $ ( element ) . data ( 'type' ) == 'polar-area-chart' ) {
14
+ this . initPolarAreaChart ( element ) ;
15
+ }
16
+ else if ( $ ( element ) . data ( 'type' ) == 'pie-chart' ) {
17
+ this . initPieChart ( element ) ;
18
+ }
19
+ else if ( $ ( element ) . data ( 'type' ) == 'doughnut-chart' ) {
20
+ this . initDoughnutChart ( element ) ;
21
+ }
22
+ }
23
+
24
+ initLineChart ( element ) {
25
+ let $lineChart = $ ( element ) ;
26
+ let data = {
13
27
labels : [ "January" , "February" , "March" , "April" , "May" , "June" , "July" ] ,
14
28
datasets : [
15
29
{
@@ -33,20 +47,20 @@ class Charts
33
47
data : [ 28 , 48 , 40 , 19 , 86 , 27 , 90 ]
34
48
}
35
49
]
36
- }
37
-
38
- options = {
50
+ } ;
51
+ let options = {
39
52
responsive : true ,
40
- }
53
+ } ;
41
54
42
- $lineChart = $ (' #line-chart' )
43
-
44
- if $lineChart .length > 0
45
- ctx = $lineChart[0 ].getContext (' 2d' )
46
- myLineChart = new Chart (ctx).Line (data, options)
55
+ if ( $lineChart . length > 0 ) {
56
+ let myLineChart ;
57
+ let ctx = $lineChart [ 0 ] . getContext ( '2d' ) ;
58
+ return myLineChart = new Chart ( ctx ) . Line ( data , options ) ;
59
+ }
60
+ }
47
61
48
- initBarChart : ->
49
- data = {
62
+ initBarChart ( element ) {
63
+ let data = {
50
64
labels : [ "January" , "February" , "March" , "April" , "May" , "June" , "July" ] ,
51
65
datasets : [
52
66
{
@@ -66,20 +80,23 @@ class Charts
66
80
data : [ 28 , 48 , 40 , 19 , 86 , 27 , 90 ]
67
81
}
68
82
]
69
- }
83
+ } ;
70
84
71
- options = {
85
+ let options = {
72
86
responsive : true ,
73
- }
87
+ } ;
74
88
75
- $barChart = $ (' #bar-chart ' )
89
+ let $barChart = $ ( element ) ;
76
90
77
- if $barChart .length > 0
78
- ctx = $barChart[0 ].getContext (' 2d' )
79
- myBarChart = new Chart (ctx).Bar (data, options)
91
+ if ( $barChart . length > 0 ) {
92
+ let myBarChart ;
93
+ let ctx = $barChart [ 0 ] . getContext ( '2d' ) ;
94
+ return myBarChart = new Chart ( ctx ) . Bar ( data , options ) ;
95
+ }
96
+ }
80
97
81
- initRadarChart : ->
82
- data = {
98
+ initRadarChart ( element ) {
99
+ let data = {
83
100
labels : [ "Eating" , "Drinking" , "Sleeping" , "Designing" , "Coding" , "Cycling" , "Running" ] ,
84
101
datasets : [
85
102
{
@@ -103,20 +120,23 @@ class Charts
103
120
data : [ 28 , 48 , 40 , 19 , 96 , 27 , 100 ]
104
121
}
105
122
]
106
- }
123
+ } ;
107
124
108
- options = {
125
+ let options = {
109
126
responsive : true ,
110
- }
127
+ } ;
111
128
112
- $radarChart = $ (' #radar-chart ' )
129
+ let $radarChart = $ ( element ) ;
113
130
114
- if $radarChart .length > 0
115
- ctx = $radarChart[0 ].getContext (' 2d' )
116
- myRadarChart = new Chart (ctx).Radar (data, options)
131
+ if ( $radarChart . length > 0 ) {
132
+ let myRadarChart ;
133
+ let ctx = $radarChart [ 0 ] . getContext ( '2d' ) ;
134
+ return myRadarChart = new Chart ( ctx ) . Radar ( data , options ) ;
135
+ }
136
+ }
117
137
118
- initPolarAreaChart : ->
119
- data = [
138
+ initPolarAreaChart ( element ) {
139
+ let data = [
120
140
{
121
141
value : 300 ,
122
142
color :"#F7464A" ,
@@ -147,20 +167,23 @@ class Charts
147
167
highlight : "#616774" ,
148
168
label : "Dark Grey"
149
169
}
150
- ]
170
+ ] ;
151
171
152
- options = {
172
+ let options = {
153
173
responsive : true ,
154
- }
174
+ } ;
155
175
156
- $polarChart = $ (' #polar-area-chart ' )
176
+ let $polarChart = $ ( element ) ;
157
177
158
- if $polarChart .length > 0
159
- ctx = $polarChart[0 ].getContext (' 2d' )
160
- myPolarAreaChart = new Chart (ctx).PolarArea (data, options)
178
+ if ( $polarChart . length > 0 ) {
179
+ let myPolarAreaChart ;
180
+ let ctx = $polarChart [ 0 ] . getContext ( '2d' ) ;
181
+ return myPolarAreaChart = new Chart ( ctx ) . PolarArea ( data , options ) ;
182
+ }
183
+ }
161
184
162
- initPieChart : ->
163
- data = [
185
+ initPieChart ( element ) {
186
+ let data = [
164
187
{
165
188
value : 300 ,
166
189
color :"#F7464A" ,
@@ -179,21 +202,24 @@ class Charts
179
202
highlight : "#FFC870" ,
180
203
label : "Yellow"
181
204
}
182
- ]
205
+ ] ;
183
206
184
- options = {
207
+ let options = {
185
208
responsive : true ,
186
209
showTooltips : false ,
187
- }
210
+ } ;
188
211
189
- $pieChart = $ (' #pie-chart ' )
212
+ let $pieChart = $ ( element ) ;
190
213
191
- if $pieChart .length > 0
192
- ctx = $pieChart[0 ].getContext (' 2d' )
193
- myPieChart = new Chart (ctx).Pie (data, options)
214
+ if ( $pieChart . length > 0 ) {
215
+ let myPieChart ;
216
+ let ctx = $pieChart [ 0 ] . getContext ( '2d' ) ;
217
+ return myPieChart = new Chart ( ctx ) . Pie ( data , options ) ;
218
+ }
219
+ }
194
220
195
- initDoughnutChart : ->
196
- data = [
221
+ initDoughnutChart ( element ) {
222
+ let data = [
197
223
{
198
224
value : 300 ,
199
225
color :"#F7464A" ,
@@ -212,19 +238,22 @@ class Charts
212
238
highlight : "#FFC870" ,
213
239
label : "Yellow"
214
240
}
215
- ]
241
+ ] ;
216
242
217
- options = {
243
+ let options = {
218
244
responsive : true ,
219
- }
245
+ } ;
220
246
221
- $doughnutChart = $ (' #doughnut-chart ' )
247
+ let $doughnutChart = $ ( element ) ;
222
248
223
- if $doughnutChart .length > 0
224
- ctx = $doughnutChart[0 ].getContext (' 2d' )
225
- myDoughnutChart = new Chart (ctx).Doughnut (data, options)
226
-
227
-
228
- Charts .current = new Charts ()
249
+ if ( $doughnutChart . length > 0 ) {
250
+ let myDoughnutChart ;
251
+ let ctx = $doughnutChart [ 0 ] . getContext ( '2d' ) ;
252
+ return myDoughnutChart = new Chart ( ctx ) . Doughnut ( data , options ) ;
253
+ }
254
+ }
255
+ }
229
256
230
- window .Charts = Charts
257
+ $ ( '.chart' ) . each ( ( index , element ) => {
258
+ element . chart = new Charts ( $ ( element ) ) ;
259
+ } ) ;
0 commit comments