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 pathprogress-donut.html
More file actions
334 lines (326 loc) · 9.58 KB
/
progress-donut.html
File metadata and controls
334 lines (326 loc) · 9.58 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../materializecss-styles/materializecss-styles.html">
<link rel="import" href="../hax-body-behaviors/hax-body-behaviors.html">
<link rel="import" href="../schema-behaviors/schema-behaviors.html">
<link rel="import" href="../chartist-render/chartist-render.html">
<link rel="import" href="../simple-colors/simple-colors.html">
<!--
`progress-donut`
A LRN element
@demo demo/index.html
@microcopy - the mental model for this element
-
-
-->
<dom-module id="progress-donut">
<template>
<style is="custom-style">
:host {
background-color: var(--simple-colors-background1, #ffffff);
overflow: visible;
display: block;
}
:host #wrapper {
margin: 0 auto;
position: relative;
}
:host #wrapper > * {
position: absolute;
}
:host #wrapper #chart {
left: 0;
top: 0;
}
:host #wrapper,
:host #wrapper #chart {
width: 250px;
height: 250px;
}
:host[size="xs"] #wrapper,
:host[size="xs"] #wrapper #chart {
width: 150px;
height: 150px;
}
:host[size="sm"] #wrapper,
:host[size="sm"] #wrapper #chart {
width: 200px;
height: 200px;
}
:host[size="lg"] #wrapper,
:host[size="lg"] #wrapper #chart {
width: 300px;
height: 300px;
}
:host[size="xl"] #wrapper,
:host[size="xl"] #wrapper #chart {
width: 400px;
height: 400px;
}
:host #wrapper > #image {
left: 20%;
top: 20%;
width: 60%;
height: 60%;
-webkit-clip-path: circle(50% at 50% 50%);
clip-path: circle(50% at 50% 50%);
}
</style>
<div id="wrapper">
<img id="image"
alt$="[[imageAlt]]"
aria-hidden="true"
hidden$="[[!imageSrc]]"
src$="[[imageSrc]]"
style$="[[imageStyle]]">
<chartist-render id="chart"
data$="[[data]]"
chart-desc$="[[desc]]"
chart-title="[[title]]"
scale="ct-square"
options$="[[options]]"
title$="[[title]]"
type="pie" >
</chartist-render>
</div>
</template>
<script>
Polymer({
is: 'progress-donut',
behaviors: [HAXBehaviors.PropertiesBehaviors, simpleColorsBehaviors, SchemaBehaviors.Schema],
listeners: {
'chartist-render-draw': '_onCreated'
},
properties: {
/**
* An array of completed values
*/
complete: {
type: Array,
value: []
},
/**
* The thickness of the donut from 0-100
*/
donutThickness: {
type: Number,
},
/**
* An array of hex codes to use as colors for each section.
* If null, colors are determined by accentColor & dark properties
*/
colors: {
type: Array,
value: null
},
/**
* An array of data for the donut chart
*/
data: {
type: Array,
computed: '_getData(complete)'
},
/**
* Accessible long description
*/
desc: {
type: String,
value: null
},
/**
* An array of data for the donut chart
*/
options: {
type: Array,
computed: '_getOptions(complete,total,size,colors,accentColor,dark)'
},
/**
* The source of the image in the center of the object.
*/
imageSrc: {
type: String,
value: null,
reflectToAttribute: true
},
/**
* The alt text for the image.
*/
imageAlt: {
type: String,
value: null,
reflectToAttribute: true
},
/**
* The style for the image based on the size of the donut
*/
imageStyle: {
type: String,
computed: '_getImageStyle(size)'
},
/**
* The size of the progress-donut: sx, sm, md, lg, or xl. Default is md
*/
size: {
type: String,
value: 'md'
},
/**
* Title
*/
title: {
type: String,
},
/**
* An array of incomplete values
*/
total: {
type: Number,
value: 100
},
},
/**
* Attached to the DOM, now fire.
*/
attached: function() {
// Establish hax property binding
let props = {
'canScale': true,
'canPosition': true,
'canEditSource': false,
'gizmo': {
'title': 'Sample gizmo',
'description': 'The user will be able to see this for selection in a UI.',
'icon': 'av:play-circle-filled',
'color': 'grey',
'groups': ['Video', 'Media'],
'handles': [
{
'type': 'video',
'url': 'source'
}
],
'meta': {
'author': 'Your organization on github'
}
},
'settings': {
'quick': [
{
'property': 'title',
'title': 'Title',
'description': 'The title of the element',
'inputMethod': 'textfield',
'icon': 'editor:title',
},
],
'configure': [
{
'property': 'title',
'title': 'Title',
'description': 'The title of the element',
'inputMethod': 'textfield',
'icon': 'editor:title',
},
],
'advanced': [
]
}
};
this.setHaxProperties(props);
},
/**
* Makes chart and returns the chart object.
*/
_getData: function(complete){
return {"series": complete};
},
/**
* Makes chart and returns the chart object.
*/
_getImageStyle: function(size){
let offset = '22%', diameter = '56%';
if(this.size === 'xs'){
offset = '32%';
diameter = '36%';
} else if(this.size === 'sm'){
offset = '26%';
diameter = '48%';
} else if(this.size === 'lg'){
offset = '20%';
diameter = '60%';
} else if(this.size === 'xl'){
offset = '17%';
diameter = '66%';
}
return 'left: '+offset+'; top: '+offset+'; width: '+diameter+'; height: '+diameter+';';
},
/**
* Makes chart and returns the chart object.
*/
_getOptions: function(complete,total,size,colors,accentColor,dark){
let sum = 0;
for(let i=0;i<complete.length;i++){
sum += parseFloat(complete[i]);
}
return {"donut": true, "showLabel":false, "startAngle": 0, "total": Math.max(sum,total)};
},
/**
* Handles chart creation event.
*/
_onCreated: function(e){
this.__chart = e.detail;
this.makeChart(this.__chart);
},
/**
* Makes chart and returns the chart object.
*/
makeChart: function(chart){
if (chart !== undefined) {
let colors = this.colors, strokeWidth = '10%',
hex = Polymer.SimpleColorsUtility.hexCodes,
accent = this.accentColor !== null ? this.accentColor.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); }): null;
if(colors === undefined || colors === null || colors.length === 0){
if(accent !== null && hex[accent] !== null){
colors = this.dark ? [ hex[accent][9], hex[accent][6], hex[accent][3], hex[accent][7], hex[accent][4] ] : [ hex[accent][0], hex[accent][3], hex[accent][5], hex[accent][2], hex[accent][4] ]
} else {
colors = this.dark ? [ hex.orange[6], hex.pink[4], hex.purple[5], hex.cyan[6], hex.lime[5] ] : [ hex.pink[5], hex.deepPurple[4], hex.blue[3], hex.teal[4], hex.yellow[5] ]
}
}
if(this.size === 'xs'){
strokeWidth = '8%'
} else if(this.size === 'sm'){
strokeWidth = '9%'
} else if(this.size === 'lg'){
strokeWidth = '11%'
} else if(this.size === 'xl'){
strokeWidth = '12%'
}
// From chartist.js docs:
chart.on('draw', function(data) {
data.element._node.style.strokeWidth = strokeWidth;
data.element._node.style.stroke = colors[data.index%colors.length];
if(data.type === 'slice') {
var pathLength = data.element._node.getTotalLength();
data.element.attr({ 'stroke-dasharray': pathLength + 'px ' + pathLength + 'px' });
var animationDefinition = {
'stroke-dashoffset': {
id: 'anim' + data.index,
dur: 500,
from: -pathLength + 'px',
to: '0px',
easing: Chartist.Svg.Easing.easeOutQuint,
fill: 'freeze'
}
};
if(data.index !== 0) {
animationDefinition['stroke-dashoffset'].begin = 'anim' + (data.index - 1) + '.end';
}
data.element.attr({ 'stroke-dashoffset': -pathLength + 'px' });
data.element.animate(animationDefinition, false);
}
});
return chart;
}
}
});
</script>
</dom-module>