This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchartist-render.html
More file actions
209 lines (199 loc) · 5.86 KB
/
chartist-render.html
File metadata and controls
209 lines (199 loc) · 5.86 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="chartist-lib.html">
<!--
`chartist-render`
A LRN element
@demo demo/index.html
@microcopy - the mental model for this element
-
-
-
-->
<dom-module id="chartist-render">
<template>
<style>
:host {
display: block;
}
</style>
<div id="chart" chart$="[[__chartId]]" class$="ct-chart [[scale]]"></div>
</template>
<script>
Polymer({
is: 'chartist-render',
listeners: {
'chart.tap': 'makeChart',
'created': '_onCreated'
},
properties: {
/**
* The unique identifier of the chart.
*/
id: {
type: String,
value: 'chart',
},
/**
* The type of chart:bar, line, or pie
*/
type: {
type: String,
value: 'bar',
},
/**
* The scale of the chart. (See https://gionkunz.github.io/chartist-js/api-documentation.html)
*/
scale: {
type: String,
observer: 'makeChart'
},
/**
* The chart title used for accessibility.
*/
chartTitle: {
type: String,
value: null,
observer: 'makeChart'
},
/**
* The chart description used for accessibility.
*/
chartDesc: {
type: String,
value: '',
observer: 'makeChart'
},
/**
* The chart data.
*/
data: {
type: Object,
value: null,
observer: 'makeChart'
},
/**
* The options available at https://gionkunz.github.io/chartist-js/api-documentation.html.
*/
options: {
type: Object,
value: null,
observer: 'makeChart'
},
/**
* The responsive options. (See https://gionkunz.github.io/chartist-js/api-documentation.html.)
*/
responsiveOptions: {
type: Array,
value: [],
observer: 'makeChart'
},
/**
* The show data in table form as well? Default is false.
*/
showTable: {
type: Boolean,
value: false,
observer: 'makeChart'
}
},
attached: function(){
let root = this;
root.__chartId = root._getUniqueId('chartist-render-');
root._chartReady();
},
/**
* Makes chart and returns the chart object.
*/
_checkReady: function(){
let root = this;
setInterval(root._chartReady,500);
},
/**
* Makes chart and returns the chart object.
*/
_chartReady: function(){
let root = this, query = '[chart="'+root.__chartId+'"]', container = document.querySelector(query);
if(container !== null){
root.fire('chartist-render-ready',this);
if (root.data !== null) root.makeChart();
clearInterval(root._checkReady);
}
},
/**
* Makes chart and returns the chart object.
*/
makeChart: function(){
let root = this, chart;
if (root.data !== null && root.querySelector('[chart="'+root.__chartId+'"]') !== null) {
if(root.type == 'bar'){
chart = Chartist.Bar('[chart="'+root.__chartId+'"]', root.data, root.options, root.responsiveOptions);
} else if(root.type == 'line'){
chart = Chartist.Line('[chart="'+root.__chartId+'"]', root.data, root.options, root.responsiveOptions);
} else if(root.type == 'pie'){
chart = Chartist.Pie('[chart="'+root.__chartId+'"]', root.data, root.options, root.responsiveOptions);
}
root.fire('chartist-render-draw',chart);
chart.on('created',function(){
root.addA11yFeatures(chart.container.childNodes[0]);
});
return chart;
} else {
return null;
}
},
/**
* Add accessibility features.
*/
addA11yFeatures: function(svg){
let desc = this.data.labels !== undefined && this.data.labels !== null ? this.chartDesc+this.makeA11yTable(svg) : this.chartDesc;
this._addA11yFeature(svg,'desc',desc);
this._addA11yFeature(svg,'title',this.chartTitle);
svg.setAttribute('aria-labelledby',this.__chartId+'-chart-title '+this.__chartId+'-chart-desc');
},
/**
* Add accessibility features.
*/
makeA11yTable: function(svg){
let title = this.chartTitle !== null ? this.chartTitle : 'A '+this.type+' chart.';
let table = [
'<table summary="Each column is a series of data, and the first column is the data label.">',
'<caption>'+title+'</caption>',
'<tbody>'
];
for (var i=0; i<this.data.labels.length; i++){
table.push('<tr><th scope="row">'+this.data.labels[i]+'</th>');
if (this.type == 'pie') {
table.push('<td>'+this.data.series[i]+'</td>');
} else {
for (var j=0; j<this.data.series.length; j++){
table.push('<td>'+this.data.series[j][i]+'</td>');
}
}
table.push('</tr>');
}
table.push('</tbody></table>');
return table.join('');
},
/**
* For inserting chart title and description.
*/
_addA11yFeature: function(svg,tag,html){
let el = document.createElement(tag);
let first = svg.childNodes[0];
el.innerHTML = html;
el.setAttribute('id',this.__chartId+'-chart-'+tag);
svg.insertBefore(el,first);
},
/*
*
*/
_getUniqueId(prefix){
let id = prefix+Date.now();
while(document.querySelector('[chart="'+id+'"]') !== null){
id = prefix+Date.now();
}
return id;
}
});
</script>
</dom-module>