chartism 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3418 @@
1
+ (function (root, factory) {
2
+ if (typeof define === 'function' && define.amd) {
3
+ // AMD. Register as an anonymous module unless amdModuleId is set
4
+ define([], function () {
5
+ return (root['Chartist'] = factory());
6
+ });
7
+ } else if (typeof exports === 'object') {
8
+ // Node. Does not work with strict CommonJS, but
9
+ // only CommonJS-like environments that support module.exports,
10
+ // like Node.
11
+ module.exports = factory();
12
+ } else {
13
+ root['Chartist'] = factory();
14
+ }
15
+ }(this, function () {
16
+
17
+ /* Chartist.js 0.7.4
18
+ * Copyright © 2015 Gion Kunz
19
+ * Free to use under the WTFPL license.
20
+ * http://www.wtfpl.net/
21
+ */
22
+ /**
23
+ * The core module of Chartist that is mainly providing static functions and higher level functions for chart modules.
24
+ *
25
+ * @module Chartist.Core
26
+ */
27
+ var Chartist = {
28
+ version: '0.7.4'
29
+ };
30
+
31
+ (function (window, document, Chartist) {
32
+ 'use strict';
33
+
34
+ /**
35
+ * Helps to simplify functional style code
36
+ *
37
+ * @memberof Chartist.Core
38
+ * @param {*} n This exact value will be returned by the noop function
39
+ * @return {*} The same value that was provided to the n parameter
40
+ */
41
+ Chartist.noop = function (n) {
42
+ return n;
43
+ };
44
+
45
+ /**
46
+ * Generates a-z from a number 0 to 26
47
+ *
48
+ * @memberof Chartist.Core
49
+ * @param {Number} n A number from 0 to 26 that will result in a letter a-z
50
+ * @return {String} A character from a-z based on the input number n
51
+ */
52
+ Chartist.alphaNumerate = function (n) {
53
+ // Limit to a-z
54
+ return String.fromCharCode(97 + n % 26);
55
+ };
56
+
57
+ /**
58
+ * Simple recursive object extend
59
+ *
60
+ * @memberof Chartist.Core
61
+ * @param {Object} target Target object where the source will be merged into
62
+ * @param {Object...} sources This object (objects) will be merged into target and then target is returned
63
+ * @return {Object} An object that has the same reference as target but is extended and merged with the properties of source
64
+ */
65
+ Chartist.extend = function (target) {
66
+ target = target || {};
67
+
68
+ var sources = Array.prototype.slice.call(arguments, 1);
69
+ sources.forEach(function(source) {
70
+ for (var prop in source) {
71
+ if (typeof source[prop] === 'object' && !(source[prop] instanceof Array)) {
72
+ target[prop] = Chartist.extend({}, target[prop], source[prop]);
73
+ } else {
74
+ target[prop] = source[prop];
75
+ }
76
+ }
77
+ });
78
+
79
+ return target;
80
+ };
81
+
82
+ /**
83
+ * Replaces all occurrences of subStr in str with newSubStr and returns a new string.
84
+ *
85
+ * @memberof Chartist.Core
86
+ * @param {String} str
87
+ * @param {String} subStr
88
+ * @param {String} newSubStr
89
+ * @return {String}
90
+ */
91
+ Chartist.replaceAll = function(str, subStr, newSubStr) {
92
+ return str.replace(new RegExp(subStr, 'g'), newSubStr);
93
+ };
94
+
95
+ /**
96
+ * Converts a string to a number while removing the unit if present. If a number is passed then this will be returned unmodified.
97
+ *
98
+ * @memberof Chartist.Core
99
+ * @param {String|Number} value
100
+ * @return {Number} Returns the string as number or NaN if the passed length could not be converted to pixel
101
+ */
102
+ Chartist.stripUnit = function(value) {
103
+ if(typeof value === 'string') {
104
+ value = value.replace(/[^0-9\+-\.]/g, '');
105
+ }
106
+
107
+ return +value;
108
+ };
109
+
110
+ /**
111
+ * Converts a number to a string with a unit. If a string is passed then this will be returned unmodified.
112
+ *
113
+ * @memberof Chartist.Core
114
+ * @param {Number} value
115
+ * @param {String} unit
116
+ * @return {String} Returns the passed number value with unit.
117
+ */
118
+ Chartist.ensureUnit = function(value, unit) {
119
+ if(typeof value === 'number') {
120
+ value = value + unit;
121
+ }
122
+
123
+ return value;
124
+ };
125
+
126
+ /**
127
+ * This is a wrapper around document.querySelector that will return the query if it's already of type Node
128
+ *
129
+ * @memberof Chartist.Core
130
+ * @param {String|Node} query The query to use for selecting a Node or a DOM node that will be returned directly
131
+ * @return {Node}
132
+ */
133
+ Chartist.querySelector = function(query) {
134
+ return query instanceof Node ? query : document.querySelector(query);
135
+ };
136
+
137
+ /**
138
+ * Functional style helper to produce array with given length initialized with undefined values
139
+ *
140
+ * @memberof Chartist.Core
141
+ * @param length
142
+ * @return {Array}
143
+ */
144
+ Chartist.times = function(length) {
145
+ return Array.apply(null, new Array(length));
146
+ };
147
+
148
+ /**
149
+ * Sum helper to be used in reduce functions
150
+ *
151
+ * @memberof Chartist.Core
152
+ * @param previous
153
+ * @param current
154
+ * @return {*}
155
+ */
156
+ Chartist.sum = function(previous, current) {
157
+ return previous + current;
158
+ };
159
+
160
+ /**
161
+ * Map for multi dimensional arrays where their nested arrays will be mapped in serial. The output array will have the length of the largest nested array. The callback function is called with variable arguments where each argument is the nested array value (or undefined if there are no more values).
162
+ *
163
+ * @memberof Chartist.Core
164
+ * @param arr
165
+ * @param cb
166
+ * @return {Array}
167
+ */
168
+ Chartist.serialMap = function(arr, cb) {
169
+ var result = [],
170
+ length = Math.max.apply(null, arr.map(function(e) {
171
+ return e.length;
172
+ }));
173
+
174
+ Chartist.times(length).forEach(function(e, index) {
175
+ var args = arr.map(function(e) {
176
+ return e[index];
177
+ });
178
+
179
+ result[index] = cb.apply(null, args);
180
+ });
181
+
182
+ return result;
183
+ };
184
+
185
+ /**
186
+ * This helper function can be used to round values with certain precision level after decimal. This is used to prevent rounding errors near float point precision limit.
187
+ *
188
+ * @memberof Chartist.Core
189
+ * @param {Number} value The value that should be rounded with precision
190
+ * @param {Number} [digits] The number of digits after decimal used to do the rounding
191
+ * @returns {number} Rounded value
192
+ */
193
+ Chartist.roundWithPrecision = function(value, digits) {
194
+ var precision = Math.pow(10, digits || Chartist.precision);
195
+ return Math.round(value * precision) / precision;
196
+ };
197
+
198
+ /**
199
+ * Precision level used internally in Chartist for rounding. If you require more decimal places you can increase this number.
200
+ *
201
+ * @memberof Chartist.Core
202
+ * @type {number}
203
+ */
204
+ Chartist.precision = 8;
205
+
206
+ /**
207
+ * A map with characters to escape for strings to be safely used as attribute values.
208
+ *
209
+ * @memberof Chartist.Core
210
+ * @type {Object}
211
+ */
212
+ Chartist.escapingMap = {
213
+ '&': '&',
214
+ '<': '&lt;',
215
+ '>': '&gt;',
216
+ '"': '&quot;',
217
+ '\'': '&#039;'
218
+ };
219
+
220
+ /**
221
+ * This function serializes arbitrary data to a string. In case of data that can't be easily converted to a string, this function will create a wrapper object and serialize the data using JSON.stringify. The outcoming string will always be escaped using Chartist.escapingMap.
222
+ * If called with null or undefined the function will return immediately with null or undefined.
223
+ *
224
+ * @memberof Chartist.Core
225
+ * @param {Number|String|Object} data
226
+ * @return {String}
227
+ */
228
+ Chartist.serialize = function(data) {
229
+ if(data === null || data === undefined) {
230
+ return data;
231
+ } else if(typeof data === 'number') {
232
+ data = ''+data;
233
+ } else if(typeof data === 'object') {
234
+ data = JSON.stringify({data: data});
235
+ }
236
+
237
+ return Object.keys(Chartist.escapingMap).reduce(function(result, key) {
238
+ return Chartist.replaceAll(result, key, Chartist.escapingMap[key]);
239
+ }, data);
240
+ };
241
+
242
+ /**
243
+ * This function de-serializes a string previously serialized with Chartist.serialize. The string will always be unescaped using Chartist.escapingMap before it's returned. Based on the input value the return type can be Number, String or Object. JSON.parse is used with try / catch to see if the unescaped string can be parsed into an Object and this Object will be returned on success.
244
+ *
245
+ * @memberof Chartist.Core
246
+ * @param {String} data
247
+ * @return {String|Number|Object}
248
+ */
249
+ Chartist.deserialize = function(data) {
250
+ if(typeof data !== 'string') {
251
+ return data;
252
+ }
253
+
254
+ data = Object.keys(Chartist.escapingMap).reduce(function(result, key) {
255
+ return Chartist.replaceAll(result, Chartist.escapingMap[key], key);
256
+ }, data);
257
+
258
+ try {
259
+ data = JSON.parse(data);
260
+ data = data.data !== undefined ? data.data : data;
261
+ } catch(e) {}
262
+
263
+ return data;
264
+ };
265
+
266
+ /**
267
+ * Create or reinitialize the SVG element for the chart
268
+ *
269
+ * @memberof Chartist.Core
270
+ * @param {Node} container The containing DOM Node object that will be used to plant the SVG element
271
+ * @param {String} width Set the width of the SVG element. Default is 100%
272
+ * @param {String} height Set the height of the SVG element. Default is 100%
273
+ * @param {String} className Specify a class to be added to the SVG element
274
+ * @return {Object} The created/reinitialized SVG element
275
+ */
276
+ Chartist.createSvg = function (container, width, height, className) {
277
+ var svg;
278
+
279
+ width = width || '100%';
280
+ height = height || '100%';
281
+
282
+ // Check if there is a previous SVG element in the container that contains the Chartist XML namespace and remove it
283
+ // Since the DOM API does not support namespaces we need to manually search the returned list http://www.w3.org/TR/selectors-api/
284
+ Array.prototype.slice.call(container.querySelectorAll('svg')).filter(function filterChartistSvgObjects(svg) {
285
+ return svg.getAttribute(Chartist.xmlNs.qualifiedName);
286
+ }).forEach(function removePreviousElement(svg) {
287
+ container.removeChild(svg);
288
+ });
289
+
290
+ // Create svg object with width and height or use 100% as default
291
+ svg = new Chartist.Svg('svg').attr({
292
+ width: width,
293
+ height: height
294
+ }).addClass(className).attr({
295
+ style: 'width: ' + width + '; height: ' + height + ';'
296
+ });
297
+
298
+ // Add the DOM node to our container
299
+ container.appendChild(svg._node);
300
+
301
+ return svg;
302
+ };
303
+
304
+
305
+ /**
306
+ * Reverses the series, labels and series data arrays.
307
+ *
308
+ * @memberof Chartist.Core
309
+ * @param data
310
+ */
311
+ Chartist.reverseData = function(data) {
312
+ data.labels.reverse();
313
+ data.series.reverse();
314
+ for (var i = 0; i < data.series.length; i++) {
315
+ if(typeof(data.series[i]) === 'object' && data.series[i].data !== undefined) {
316
+ data.series[i].data.reverse();
317
+ } else {
318
+ data.series[i].reverse();
319
+ }
320
+ }
321
+ };
322
+
323
+ /**
324
+ * Convert data series into plain array
325
+ *
326
+ * @memberof Chartist.Core
327
+ * @param {Object} data The series object that contains the data to be visualized in the chart
328
+ * @param {Boolean} reverse If true the whole data is reversed by the getDataArray call. This will modify the data object passed as first parameter. The labels as well as the series order is reversed. The whole series data arrays are reversed too.
329
+ * @return {Array} A plain array that contains the data to be visualized in the chart
330
+ */
331
+ Chartist.getDataArray = function (data, reverse) {
332
+ var array = [],
333
+ value,
334
+ localData;
335
+
336
+ // If the data should be reversed but isn't we need to reverse it
337
+ // If it's reversed but it shouldn't we need to reverse it back
338
+ // That's required to handle data updates correctly and to reflect the responsive configurations
339
+ if(reverse && !data.reversed || !reverse && data.reversed) {
340
+ Chartist.reverseData(data);
341
+ data.reversed = !data.reversed;
342
+ }
343
+
344
+ for (var i = 0; i < data.series.length; i++) {
345
+ // If the series array contains an object with a data property we will use the property
346
+ // otherwise the value directly (array or number).
347
+ // We create a copy of the original data array with Array.prototype.push.apply
348
+ localData = typeof(data.series[i]) === 'object' && data.series[i].data !== undefined ? data.series[i].data : data.series[i];
349
+ if(localData instanceof Array) {
350
+ array[i] = [];
351
+ Array.prototype.push.apply(array[i], localData);
352
+ } else {
353
+ array[i] = localData;
354
+ }
355
+
356
+ // Convert object values to numbers
357
+ for (var j = 0; j < array[i].length; j++) {
358
+ value = array[i][j];
359
+ value = value.value === 0 ? 0 : (value.value || value);
360
+ array[i][j] = +value;
361
+ }
362
+ }
363
+
364
+ return array;
365
+ };
366
+
367
+ /**
368
+ * Converts a number into a padding object.
369
+ *
370
+ * @memberof Chartist.Core
371
+ * @param {Object|Number} padding
372
+ * @param {Number} [fallback] This value is used to fill missing values if a incomplete padding object was passed
373
+ * @returns {Object} Returns a padding object containing top, right, bottom, left properties filled with the padding number passed in as argument. If the argument is something else than a number (presumably already a correct padding object) then this argument is directly returned.
374
+ */
375
+ Chartist.normalizePadding = function(padding, fallback) {
376
+ fallback = fallback || 0;
377
+
378
+ return typeof padding === 'number' ? {
379
+ top: padding,
380
+ right: padding,
381
+ bottom: padding,
382
+ left: padding
383
+ } : {
384
+ top: typeof padding.top === 'number' ? padding.top : fallback,
385
+ right: typeof padding.right === 'number' ? padding.right : fallback,
386
+ bottom: typeof padding.bottom === 'number' ? padding.bottom : fallback,
387
+ left: typeof padding.left === 'number' ? padding.left : fallback
388
+ };
389
+ };
390
+
391
+ /**
392
+ * Adds missing values at the end of the array. This array contains the data, that will be visualized in the chart
393
+ *
394
+ * @memberof Chartist.Core
395
+ * @param {Array} dataArray The array that contains the data to be visualized in the chart. The array in this parameter will be modified by function.
396
+ * @param {Number} length The length of the x-axis data array.
397
+ * @return {Array} The array that got updated with missing values.
398
+ */
399
+ Chartist.normalizeDataArray = function (dataArray, length) {
400
+ for (var i = 0; i < dataArray.length; i++) {
401
+ if (dataArray[i].length === length) {
402
+ continue;
403
+ }
404
+
405
+ for (var j = dataArray[i].length; j < length; j++) {
406
+ dataArray[i][j] = 0;
407
+ }
408
+ }
409
+
410
+ return dataArray;
411
+ };
412
+
413
+ Chartist.getMetaData = function(series, index) {
414
+ var value = series.data ? series.data[index] : series[index];
415
+ return value ? Chartist.serialize(value.meta) : undefined;
416
+ };
417
+
418
+ /**
419
+ * Calculate the order of magnitude for the chart scale
420
+ *
421
+ * @memberof Chartist.Core
422
+ * @param {Number} value The value Range of the chart
423
+ * @return {Number} The order of magnitude
424
+ */
425
+ Chartist.orderOfMagnitude = function (value) {
426
+ return Math.floor(Math.log(Math.abs(value)) / Math.LN10);
427
+ };
428
+
429
+ /**
430
+ * Project a data length into screen coordinates (pixels)
431
+ *
432
+ * @memberof Chartist.Core
433
+ * @param {Object} axisLength The svg element for the chart
434
+ * @param {Number} length Single data value from a series array
435
+ * @param {Object} bounds All the values to set the bounds of the chart
436
+ * @return {Number} The projected data length in pixels
437
+ */
438
+ Chartist.projectLength = function (axisLength, length, bounds) {
439
+ return length / bounds.range * axisLength;
440
+ };
441
+
442
+ /**
443
+ * Get the height of the area in the chart for the data series
444
+ *
445
+ * @memberof Chartist.Core
446
+ * @param {Object} svg The svg element for the chart
447
+ * @param {Object} options The Object that contains all the optional values for the chart
448
+ * @return {Number} The height of the area in the chart for the data series
449
+ */
450
+ Chartist.getAvailableHeight = function (svg, options) {
451
+ return Math.max((Chartist.stripUnit(options.height) || svg.height()) - (options.chartPadding.top + options.chartPadding.bottom) - options.axisX.offset, 0);
452
+ };
453
+
454
+ /**
455
+ * Get highest and lowest value of data array. This Array contains the data that will be visualized in the chart.
456
+ *
457
+ * @memberof Chartist.Core
458
+ * @param {Array} dataArray The array that contains the data to be visualized in the chart
459
+ * @return {Object} An object that contains the highest and lowest value that will be visualized on the chart.
460
+ */
461
+ Chartist.getHighLow = function (dataArray) {
462
+ var i,
463
+ j,
464
+ highLow = {
465
+ high: -Number.MAX_VALUE,
466
+ low: Number.MAX_VALUE
467
+ };
468
+
469
+ for (i = 0; i < dataArray.length; i++) {
470
+ for (j = 0; j < dataArray[i].length; j++) {
471
+ if (dataArray[i][j] > highLow.high) {
472
+ highLow.high = dataArray[i][j];
473
+ }
474
+
475
+ if (dataArray[i][j] < highLow.low) {
476
+ highLow.low = dataArray[i][j];
477
+ }
478
+ }
479
+ }
480
+
481
+ return highLow;
482
+ };
483
+
484
+ /**
485
+ * Calculate and retrieve all the bounds for the chart and return them in one array
486
+ *
487
+ * @memberof Chartist.Core
488
+ * @param {Number} axisLength The length of the Axis used for
489
+ * @param {Object} highLow An object containing a high and low property indicating the value range of the chart.
490
+ * @param {Number} scaleMinSpace The minimum projected length a step should result in
491
+ * @param {Number} referenceValue The reference value for the chart.
492
+ * @return {Object} All the values to set the bounds of the chart
493
+ */
494
+ Chartist.getBounds = function (axisLength, highLow, scaleMinSpace, referenceValue) {
495
+ var i,
496
+ newMin,
497
+ newMax,
498
+ bounds = {
499
+ high: highLow.high,
500
+ low: highLow.low
501
+ };
502
+
503
+ // If high and low are the same because of misconfiguration or flat data (only the same value) we need
504
+ // to set the high or low to 0 depending on the polarity
505
+ if(bounds.high === bounds.low) {
506
+ // If both values are 0 we set high to 1
507
+ if(bounds.low === 0) {
508
+ bounds.high = 1;
509
+ } else if(bounds.low < 0) {
510
+ // If we have the same negative value for the bounds we set bounds.high to 0
511
+ bounds.high = 0;
512
+ } else {
513
+ // If we have the same positive value for the bounds we set bounds.low to 0
514
+ bounds.low = 0;
515
+ }
516
+ }
517
+
518
+ // Overrides of high / low based on reference value, it will make sure that the invisible reference value is
519
+ // used to generate the chart. This is useful when the chart always needs to contain the position of the
520
+ // invisible reference value in the view i.e. for bipolar scales.
521
+ if (referenceValue || referenceValue === 0) {
522
+ bounds.high = Math.max(referenceValue, bounds.high);
523
+ bounds.low = Math.min(referenceValue, bounds.low);
524
+ }
525
+
526
+ bounds.valueRange = bounds.high - bounds.low;
527
+ bounds.oom = Chartist.orderOfMagnitude(bounds.valueRange);
528
+ bounds.min = Math.floor(bounds.low / Math.pow(10, bounds.oom)) * Math.pow(10, bounds.oom);
529
+ bounds.max = Math.ceil(bounds.high / Math.pow(10, bounds.oom)) * Math.pow(10, bounds.oom);
530
+ bounds.range = bounds.max - bounds.min;
531
+ bounds.step = Math.pow(10, bounds.oom);
532
+ bounds.numberOfSteps = Math.round(bounds.range / bounds.step);
533
+
534
+ // Optimize scale step by checking if subdivision is possible based on horizontalGridMinSpace
535
+ // If we are already below the scaleMinSpace value we will scale up
536
+ var length = Chartist.projectLength(axisLength, bounds.step, bounds),
537
+ scaleUp = length < scaleMinSpace;
538
+
539
+ while (true) {
540
+ if (scaleUp && Chartist.projectLength(axisLength, bounds.step, bounds) <= scaleMinSpace) {
541
+ bounds.step *= 2;
542
+ } else if (!scaleUp && Chartist.projectLength(axisLength, bounds.step / 2, bounds) >= scaleMinSpace) {
543
+ bounds.step /= 2;
544
+ } else {
545
+ break;
546
+ }
547
+ }
548
+
549
+ // Narrow min and max based on new step
550
+ newMin = bounds.min;
551
+ newMax = bounds.max;
552
+ for (i = bounds.min; i <= bounds.max; i += bounds.step) {
553
+ if (i + bounds.step < bounds.low) {
554
+ newMin += bounds.step;
555
+ }
556
+
557
+ if (i - bounds.step >= bounds.high) {
558
+ newMax -= bounds.step;
559
+ }
560
+ }
561
+ bounds.min = newMin;
562
+ bounds.max = newMax;
563
+ bounds.range = bounds.max - bounds.min;
564
+
565
+ bounds.values = [];
566
+ for (i = bounds.min; i <= bounds.max; i += bounds.step) {
567
+ bounds.values.push(Chartist.roundWithPrecision(i));
568
+ }
569
+
570
+ return bounds;
571
+ };
572
+
573
+ /**
574
+ * Calculate cartesian coordinates of polar coordinates
575
+ *
576
+ * @memberof Chartist.Core
577
+ * @param {Number} centerX X-axis coordinates of center point of circle segment
578
+ * @param {Number} centerY X-axis coordinates of center point of circle segment
579
+ * @param {Number} radius Radius of circle segment
580
+ * @param {Number} angleInDegrees Angle of circle segment in degrees
581
+ * @return {Number} Coordinates of point on circumference
582
+ */
583
+ Chartist.polarToCartesian = function (centerX, centerY, radius, angleInDegrees) {
584
+ var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
585
+
586
+ return {
587
+ x: centerX + (radius * Math.cos(angleInRadians)),
588
+ y: centerY + (radius * Math.sin(angleInRadians))
589
+ };
590
+ };
591
+
592
+ /**
593
+ * Initialize chart drawing rectangle (area where chart is drawn) x1,y1 = bottom left / x2,y2 = top right
594
+ *
595
+ * @memberof Chartist.Core
596
+ * @param {Object} svg The svg element for the chart
597
+ * @param {Object} options The Object that contains all the optional values for the chart
598
+ * @param {Number} [fallbackPadding] The fallback padding if partial padding objects are used
599
+ * @return {Object} The chart rectangles coordinates inside the svg element plus the rectangles measurements
600
+ */
601
+ Chartist.createChartRect = function (svg, options, fallbackPadding) {
602
+ var yOffset = options.axisY ? options.axisY.offset || 0 : 0,
603
+ xOffset = options.axisX ? options.axisX.offset || 0 : 0,
604
+ w = Chartist.stripUnit(options.width) || svg.width(),
605
+ h = Chartist.stripUnit(options.height) || svg.height(),
606
+ normalizedPadding = Chartist.normalizePadding(options.chartPadding, fallbackPadding);
607
+
608
+ return {
609
+ x1: normalizedPadding.left + yOffset,
610
+ y1: Math.max(h - normalizedPadding.bottom - xOffset, normalizedPadding.bottom),
611
+ x2: Math.max(w - normalizedPadding.right, normalizedPadding.right + yOffset),
612
+ y2: normalizedPadding.top,
613
+ width: function () {
614
+ return this.x2 - this.x1;
615
+ },
616
+ height: function () {
617
+ return this.y1 - this.y2;
618
+ }
619
+ };
620
+ };
621
+
622
+ /**
623
+ * Creates a grid line based on a projected value.
624
+ *
625
+ * @memberof Chartist.Core
626
+ * @param projectedValue
627
+ * @param index
628
+ * @param axis
629
+ * @param offset
630
+ * @param length
631
+ * @param group
632
+ * @param classes
633
+ * @param eventEmitter
634
+ */
635
+ Chartist.createGrid = function(projectedValue, index, axis, offset, length, group, classes, eventEmitter) {
636
+ var positionalData = {};
637
+ positionalData[axis.units.pos + '1'] = projectedValue.pos;
638
+ positionalData[axis.units.pos + '2'] = projectedValue.pos;
639
+ positionalData[axis.counterUnits.pos + '1'] = offset;
640
+ positionalData[axis.counterUnits.pos + '2'] = offset + length;
641
+
642
+ var gridElement = group.elem('line', positionalData, classes.join(' '));
643
+
644
+ // Event for grid draw
645
+ eventEmitter.emit('draw',
646
+ Chartist.extend({
647
+ type: 'grid',
648
+ axis: axis,
649
+ index: index,
650
+ group: group,
651
+ element: gridElement
652
+ }, positionalData)
653
+ );
654
+ };
655
+
656
+ /**
657
+ * Creates a label based on a projected value and an axis.
658
+ *
659
+ * @memberof Chartist.Core
660
+ * @param projectedValue
661
+ * @param index
662
+ * @param labels
663
+ * @param axis
664
+ * @param axisOffset
665
+ * @param labelOffset
666
+ * @param group
667
+ * @param classes
668
+ * @param useForeignObject
669
+ * @param eventEmitter
670
+ */
671
+ Chartist.createLabel = function(projectedValue, index, labels, axis, axisOffset, labelOffset, group, classes, useForeignObject, eventEmitter) {
672
+ var labelElement,
673
+ positionalData = {};
674
+ positionalData[axis.units.pos] = projectedValue.pos + labelOffset[axis.units.pos];
675
+ positionalData[axis.counterUnits.pos] = labelOffset[axis.counterUnits.pos];
676
+ positionalData[axis.units.len] = projectedValue.len;
677
+ positionalData[axis.counterUnits.len] = axisOffset;
678
+
679
+ if(useForeignObject) {
680
+ var content = '<span class="' + classes.join(' ') + '">' + labels[index] + '</span>';
681
+ labelElement = group.foreignObject(content, Chartist.extend({
682
+ style: 'overflow: visible;'
683
+ }, positionalData));
684
+ } else {
685
+ labelElement = group.elem('text', positionalData, classes.join(' ')).text(labels[index]);
686
+ }
687
+
688
+ eventEmitter.emit('draw', Chartist.extend({
689
+ type: 'label',
690
+ axis: axis,
691
+ index: index,
692
+ group: group,
693
+ element: labelElement,
694
+ text: labels[index]
695
+ }, positionalData));
696
+ };
697
+
698
+ /**
699
+ * This function creates a whole axis with its grid lines and labels based on an axis model and a chartRect.
700
+ *
701
+ * @memberof Chartist.Core
702
+ * @param axis
703
+ * @param data
704
+ * @param chartRect
705
+ * @param gridGroup
706
+ * @param labelGroup
707
+ * @param useForeignObject
708
+ * @param options
709
+ * @param eventEmitter
710
+ */
711
+ Chartist.createAxis = function(axis, data, chartRect, gridGroup, labelGroup, useForeignObject, options, eventEmitter) {
712
+ var axisOptions = options['axis' + axis.units.pos.toUpperCase()],
713
+ projectedValues = data.map(axis.projectValue.bind(axis)).map(axis.transform),
714
+ labelValues = data.map(axisOptions.labelInterpolationFnc);
715
+
716
+ projectedValues.forEach(function(projectedValue, index) {
717
+ // Skip grid lines and labels where interpolated label values are falsey (execpt for 0)
718
+ if(!labelValues[index] && labelValues[index] !== 0) {
719
+ return;
720
+ }
721
+
722
+ if(axisOptions.showGrid) {
723
+ Chartist.createGrid(projectedValue, index, axis, axis.gridOffset, chartRect[axis.counterUnits.len](), gridGroup, [
724
+ options.classNames.grid,
725
+ options.classNames[axis.units.dir]
726
+ ], eventEmitter);
727
+ }
728
+
729
+ if(axisOptions.showLabel) {
730
+ Chartist.createLabel(projectedValue, index, labelValues, axis, axisOptions.offset, axis.labelOffset, labelGroup, [
731
+ options.classNames.label,
732
+ options.classNames[axis.units.dir]
733
+ ], useForeignObject, eventEmitter);
734
+ }
735
+ });
736
+ };
737
+
738
+ /**
739
+ * Provides options handling functionality with callback for options changes triggered by responsive options and media query matches
740
+ *
741
+ * @memberof Chartist.Core
742
+ * @param {Object} options Options set by user
743
+ * @param {Array} responsiveOptions Optional functions to add responsive behavior to chart
744
+ * @param {Object} eventEmitter The event emitter that will be used to emit the options changed events
745
+ * @return {Object} The consolidated options object from the defaults, base and matching responsive options
746
+ */
747
+ Chartist.optionsProvider = function (options, responsiveOptions, eventEmitter) {
748
+ var baseOptions = Chartist.extend({}, options),
749
+ currentOptions,
750
+ mediaQueryListeners = [],
751
+ i;
752
+
753
+ function updateCurrentOptions(preventChangedEvent) {
754
+ var previousOptions = currentOptions;
755
+ currentOptions = Chartist.extend({}, baseOptions);
756
+
757
+ if (responsiveOptions) {
758
+ for (i = 0; i < responsiveOptions.length; i++) {
759
+ var mql = window.matchMedia(responsiveOptions[i][0]);
760
+ if (mql.matches) {
761
+ currentOptions = Chartist.extend(currentOptions, responsiveOptions[i][1]);
762
+ }
763
+ }
764
+ }
765
+
766
+ if(eventEmitter && !preventChangedEvent) {
767
+ eventEmitter.emit('optionsChanged', {
768
+ previousOptions: previousOptions,
769
+ currentOptions: currentOptions
770
+ });
771
+ }
772
+ }
773
+
774
+ function removeMediaQueryListeners() {
775
+ mediaQueryListeners.forEach(function(mql) {
776
+ mql.removeListener(updateCurrentOptions);
777
+ });
778
+ }
779
+
780
+ if (!window.matchMedia) {
781
+ throw 'window.matchMedia not found! Make sure you\'re using a polyfill.';
782
+ } else if (responsiveOptions) {
783
+
784
+ for (i = 0; i < responsiveOptions.length; i++) {
785
+ var mql = window.matchMedia(responsiveOptions[i][0]);
786
+ mql.addListener(updateCurrentOptions);
787
+ mediaQueryListeners.push(mql);
788
+ }
789
+ }
790
+ // Execute initially so we get the correct options
791
+ updateCurrentOptions(true);
792
+
793
+ return {
794
+ get currentOptions() {
795
+ return Chartist.extend({}, currentOptions);
796
+ },
797
+ removeMediaQueryListeners: removeMediaQueryListeners
798
+ };
799
+ };
800
+
801
+ }(window, document, Chartist));
802
+ ;/**
803
+ * Chartist path interpolation functions.
804
+ *
805
+ * @module Chartist.Interpolation
806
+ */
807
+ /* global Chartist */
808
+ (function(window, document, Chartist) {
809
+ 'use strict';
810
+
811
+ Chartist.Interpolation = {};
812
+
813
+ /**
814
+ * This interpolation function does not smooth the path and the result is only containing lines and no curves.
815
+ *
816
+ * @memberof Chartist.Interpolation
817
+ * @return {Function}
818
+ */
819
+ Chartist.Interpolation.none = function() {
820
+ return function cardinal(pathCoordinates) {
821
+ var path = new Chartist.Svg.Path().move(pathCoordinates[0], pathCoordinates[1]);
822
+
823
+ for(var i = 3; i < pathCoordinates.length; i += 2) {
824
+ path.line(pathCoordinates[i - 1], pathCoordinates[i]);
825
+ }
826
+
827
+ return path;
828
+ };
829
+ };
830
+
831
+ /**
832
+ * Simple smoothing creates horizontal handles that are positioned with a fraction of the length between two data points. You can use the divisor option to specify the amount of smoothing.
833
+ *
834
+ * Simple smoothing can be used instead of `Chartist.Smoothing.cardinal` if you'd like to get rid of the artifacts it produces sometimes. Simple smoothing produces less flowing lines but is accurate by hitting the points and it also doesn't swing below or above the given data point.
835
+ *
836
+ * All smoothing functions within Chartist are factory functions that accept an options parameter. The simple interpolation function accepts one configuration parameter `divisor`, between 1 and ∞, which controls the smoothing characteristics.
837
+ *
838
+ * @example
839
+ * var chart = new Chartist.Line('.ct-chart', {
840
+ * labels: [1, 2, 3, 4, 5],
841
+ * series: [[1, 2, 8, 1, 7]]
842
+ * }, {
843
+ * lineSmooth: Chartist.Interpolation.simple({
844
+ * divisor: 2
845
+ * })
846
+ * });
847
+ *
848
+ *
849
+ * @memberof Chartist.Interpolation
850
+ * @param {Object} options The options of the simple interpolation factory function.
851
+ * @return {Function}
852
+ */
853
+ Chartist.Interpolation.simple = function(options) {
854
+ var defaultOptions = {
855
+ divisor: 2
856
+ };
857
+ options = Chartist.extend({}, defaultOptions, options);
858
+
859
+ var d = 1 / Math.max(1, options.divisor);
860
+
861
+ return function simple(pathCoordinates) {
862
+ var path = new Chartist.Svg.Path().move(pathCoordinates[0], pathCoordinates[1]);
863
+
864
+ for(var i = 2; i < pathCoordinates.length; i += 2) {
865
+ var prevX = pathCoordinates[i - 2],
866
+ prevY = pathCoordinates[i - 1],
867
+ currX = pathCoordinates[i],
868
+ currY = pathCoordinates[i + 1],
869
+ length = (currX - prevX) * d;
870
+
871
+ path.curve(
872
+ prevX + length,
873
+ prevY,
874
+ currX - length,
875
+ currY,
876
+ currX,
877
+ currY
878
+ );
879
+ }
880
+
881
+ return path;
882
+ };
883
+ };
884
+
885
+ /**
886
+ * Cardinal / Catmull-Rome spline interpolation is the default smoothing function in Chartist. It produces nice results where the splines will always meet the points. It produces some artifacts though when data values are increased or decreased rapidly. The line may not follow a very accurate path and if the line should be accurate this smoothing function does not produce the best results.
887
+ *
888
+ * Cardinal splines can only be created if there are more than two data points. If this is not the case this smoothing will fallback to `Chartist.Smoothing.none`.
889
+ *
890
+ * All smoothing functions within Chartist are factory functions that accept an options parameter. The cardinal interpolation function accepts one configuration parameter `tension`, between 0 and 1, which controls the smoothing intensity.
891
+ *
892
+ * @example
893
+ * var chart = new Chartist.Line('.ct-chart', {
894
+ * labels: [1, 2, 3, 4, 5],
895
+ * series: [[1, 2, 8, 1, 7]]
896
+ * }, {
897
+ * lineSmooth: Chartist.Interpolation.cardinal({
898
+ * tension: 1
899
+ * })
900
+ * });
901
+ *
902
+ * @memberof Chartist.Interpolation
903
+ * @param {Object} options The options of the cardinal factory function.
904
+ * @return {Function}
905
+ */
906
+ Chartist.Interpolation.cardinal = function(options) {
907
+ var defaultOptions = {
908
+ tension: 1
909
+ };
910
+
911
+ options = Chartist.extend({}, defaultOptions, options);
912
+
913
+ var t = Math.min(1, Math.max(0, options.tension)),
914
+ c = 1 - t;
915
+
916
+ return function cardinal(pathCoordinates) {
917
+ // If less than two points we need to fallback to no smoothing
918
+ if(pathCoordinates.length <= 4) {
919
+ return Chartist.Interpolation.none()(pathCoordinates);
920
+ }
921
+
922
+ var path = new Chartist.Svg.Path().move(pathCoordinates[0], pathCoordinates[1]),
923
+ z;
924
+
925
+ for (var i = 0, iLen = pathCoordinates.length; iLen - 2 * !z > i; i += 2) {
926
+ var p = [
927
+ {x: +pathCoordinates[i - 2], y: +pathCoordinates[i - 1]},
928
+ {x: +pathCoordinates[i], y: +pathCoordinates[i + 1]},
929
+ {x: +pathCoordinates[i + 2], y: +pathCoordinates[i + 3]},
930
+ {x: +pathCoordinates[i + 4], y: +pathCoordinates[i + 5]}
931
+ ];
932
+ if (z) {
933
+ if (!i) {
934
+ p[0] = {x: +pathCoordinates[iLen - 2], y: +pathCoordinates[iLen - 1]};
935
+ } else if (iLen - 4 === i) {
936
+ p[3] = {x: +pathCoordinates[0], y: +pathCoordinates[1]};
937
+ } else if (iLen - 2 === i) {
938
+ p[2] = {x: +pathCoordinates[0], y: +pathCoordinates[1]};
939
+ p[3] = {x: +pathCoordinates[2], y: +pathCoordinates[3]};
940
+ }
941
+ } else {
942
+ if (iLen - 4 === i) {
943
+ p[3] = p[2];
944
+ } else if (!i) {
945
+ p[0] = {x: +pathCoordinates[i], y: +pathCoordinates[i + 1]};
946
+ }
947
+ }
948
+
949
+ path.curve(
950
+ (t * (-p[0].x + 6 * p[1].x + p[2].x) / 6) + (c * p[2].x),
951
+ (t * (-p[0].y + 6 * p[1].y + p[2].y) / 6) + (c * p[2].y),
952
+ (t * (p[1].x + 6 * p[2].x - p[3].x) / 6) + (c * p[2].x),
953
+ (t * (p[1].y + 6 * p[2].y - p[3].y) / 6) + (c * p[2].y),
954
+ p[2].x,
955
+ p[2].y
956
+ );
957
+ }
958
+
959
+ return path;
960
+ };
961
+ };
962
+
963
+ }(window, document, Chartist));
964
+ ;/**
965
+ * A very basic event module that helps to generate and catch events.
966
+ *
967
+ * @module Chartist.Event
968
+ */
969
+ /* global Chartist */
970
+ (function (window, document, Chartist) {
971
+ 'use strict';
972
+
973
+ Chartist.EventEmitter = function () {
974
+ var handlers = [];
975
+
976
+ /**
977
+ * Add an event handler for a specific event
978
+ *
979
+ * @memberof Chartist.Event
980
+ * @param {String} event The event name
981
+ * @param {Function} handler A event handler function
982
+ */
983
+ function addEventHandler(event, handler) {
984
+ handlers[event] = handlers[event] || [];
985
+ handlers[event].push(handler);
986
+ }
987
+
988
+ /**
989
+ * Remove an event handler of a specific event name or remove all event handlers for a specific event.
990
+ *
991
+ * @memberof Chartist.Event
992
+ * @param {String} event The event name where a specific or all handlers should be removed
993
+ * @param {Function} [handler] An optional event handler function. If specified only this specific handler will be removed and otherwise all handlers are removed.
994
+ */
995
+ function removeEventHandler(event, handler) {
996
+ // Only do something if there are event handlers with this name existing
997
+ if(handlers[event]) {
998
+ // If handler is set we will look for a specific handler and only remove this
999
+ if(handler) {
1000
+ handlers[event].splice(handlers[event].indexOf(handler), 1);
1001
+ if(handlers[event].length === 0) {
1002
+ delete handlers[event];
1003
+ }
1004
+ } else {
1005
+ // If no handler is specified we remove all handlers for this event
1006
+ delete handlers[event];
1007
+ }
1008
+ }
1009
+ }
1010
+
1011
+ /**
1012
+ * Use this function to emit an event. All handlers that are listening for this event will be triggered with the data parameter.
1013
+ *
1014
+ * @memberof Chartist.Event
1015
+ * @param {String} event The event name that should be triggered
1016
+ * @param {*} data Arbitrary data that will be passed to the event handler callback functions
1017
+ */
1018
+ function emit(event, data) {
1019
+ // Only do something if there are event handlers with this name existing
1020
+ if(handlers[event]) {
1021
+ handlers[event].forEach(function(handler) {
1022
+ handler(data);
1023
+ });
1024
+ }
1025
+
1026
+ // Emit event to star event handlers
1027
+ if(handlers['*']) {
1028
+ handlers['*'].forEach(function(starHandler) {
1029
+ starHandler(event, data);
1030
+ });
1031
+ }
1032
+ }
1033
+
1034
+ return {
1035
+ addEventHandler: addEventHandler,
1036
+ removeEventHandler: removeEventHandler,
1037
+ emit: emit
1038
+ };
1039
+ };
1040
+
1041
+ }(window, document, Chartist));
1042
+ ;/**
1043
+ * This module provides some basic prototype inheritance utilities.
1044
+ *
1045
+ * @module Chartist.Class
1046
+ */
1047
+ /* global Chartist */
1048
+ (function(window, document, Chartist) {
1049
+ 'use strict';
1050
+
1051
+ function listToArray(list) {
1052
+ var arr = [];
1053
+ if (list.length) {
1054
+ for (var i = 0; i < list.length; i++) {
1055
+ arr.push(list[i]);
1056
+ }
1057
+ }
1058
+ return arr;
1059
+ }
1060
+
1061
+ /**
1062
+ * Method to extend from current prototype.
1063
+ *
1064
+ * @memberof Chartist.Class
1065
+ * @param {Object} properties The object that serves as definition for the prototype that gets created for the new class. This object should always contain a constructor property that is the desired constructor for the newly created class.
1066
+ * @param {Object} [superProtoOverride] By default extens will use the current class prototype or Chartist.class. With this parameter you can specify any super prototype that will be used.
1067
+ * @return {Function} Constructor function of the new class
1068
+ *
1069
+ * @example
1070
+ * var Fruit = Class.extend({
1071
+ * color: undefined,
1072
+ * sugar: undefined,
1073
+ *
1074
+ * constructor: function(color, sugar) {
1075
+ * this.color = color;
1076
+ * this.sugar = sugar;
1077
+ * },
1078
+ *
1079
+ * eat: function() {
1080
+ * this.sugar = 0;
1081
+ * return this;
1082
+ * }
1083
+ * });
1084
+ *
1085
+ * var Banana = Fruit.extend({
1086
+ * length: undefined,
1087
+ *
1088
+ * constructor: function(length, sugar) {
1089
+ * Banana.super.constructor.call(this, 'Yellow', sugar);
1090
+ * this.length = length;
1091
+ * }
1092
+ * });
1093
+ *
1094
+ * var banana = new Banana(20, 40);
1095
+ * console.log('banana instanceof Fruit', banana instanceof Fruit);
1096
+ * console.log('Fruit is prototype of banana', Fruit.prototype.isPrototypeOf(banana));
1097
+ * console.log('bananas prototype is Fruit', Object.getPrototypeOf(banana) === Fruit.prototype);
1098
+ * console.log(banana.sugar);
1099
+ * console.log(banana.eat().sugar);
1100
+ * console.log(banana.color);
1101
+ */
1102
+ function extend(properties, superProtoOverride) {
1103
+ var superProto = superProtoOverride || this.prototype || Chartist.Class;
1104
+ var proto = Object.create(superProto);
1105
+
1106
+ Chartist.Class.cloneDefinitions(proto, properties);
1107
+
1108
+ var constr = function() {
1109
+ var fn = proto.constructor || function () {},
1110
+ instance;
1111
+
1112
+ // If this is linked to the Chartist namespace the constructor was not called with new
1113
+ // To provide a fallback we will instantiate here and return the instance
1114
+ instance = this === Chartist ? Object.create(proto) : this;
1115
+ fn.apply(instance, Array.prototype.slice.call(arguments, 0));
1116
+
1117
+ // If this constructor was not called with new we need to return the instance
1118
+ // This will not harm when the constructor has been called with new as the returned value is ignored
1119
+ return instance;
1120
+ };
1121
+
1122
+ constr.prototype = proto;
1123
+ constr.super = superProto;
1124
+ constr.extend = this.extend;
1125
+
1126
+ return constr;
1127
+ }
1128
+
1129
+ // Variable argument list clones args > 0 into args[0] and retruns modified args[0]
1130
+ function cloneDefinitions() {
1131
+ var args = listToArray(arguments);
1132
+ var target = args[0];
1133
+
1134
+ args.splice(1, args.length - 1).forEach(function (source) {
1135
+ Object.getOwnPropertyNames(source).forEach(function (propName) {
1136
+ // If this property already exist in target we delete it first
1137
+ delete target[propName];
1138
+ // Define the property with the descriptor from source
1139
+ Object.defineProperty(target, propName,
1140
+ Object.getOwnPropertyDescriptor(source, propName));
1141
+ });
1142
+ });
1143
+
1144
+ return target;
1145
+ }
1146
+
1147
+ Chartist.Class = {
1148
+ extend: extend,
1149
+ cloneDefinitions: cloneDefinitions
1150
+ };
1151
+
1152
+ }(window, document, Chartist));
1153
+ ;/**
1154
+ * Base for all chart types. The methods in Chartist.Base are inherited to all chart types.
1155
+ *
1156
+ * @module Chartist.Base
1157
+ */
1158
+ /* global Chartist */
1159
+ (function(window, document, Chartist) {
1160
+ 'use strict';
1161
+
1162
+ // TODO: Currently we need to re-draw the chart on window resize. This is usually very bad and will affect performance.
1163
+ // This is done because we can't work with relative coordinates when drawing the chart because SVG Path does not
1164
+ // work with relative positions yet. We need to check if we can do a viewBox hack to switch to percentage.
1165
+ // See http://mozilla.6506.n7.nabble.com/Specyfing-paths-with-percentages-unit-td247474.html
1166
+ // Update: can be done using the above method tested here: http://codepen.io/gionkunz/pen/KDvLj
1167
+ // The problem is with the label offsets that can't be converted into percentage and affecting the chart container
1168
+ /**
1169
+ * Updates the chart which currently does a full reconstruction of the SVG DOM
1170
+ *
1171
+ * @param {Object} [data] Optional data you'd like to set for the chart before it will update. If not specified the update method will use the data that is already configured with the chart.
1172
+ * @param {Object} [options] Optional options you'd like to add to the previous options for the chart before it will update. If not specified the update method will use the options that have been already configured with the chart.
1173
+ * @param {Boolean} [override] If set to true, the passed options will be used to extend the options that have been configured already. Otherwise the chart default options will be used as the base
1174
+ * @memberof Chartist.Base
1175
+ */
1176
+ function update(data, options, override) {
1177
+ if(data) {
1178
+ this.data = data;
1179
+ // Event for data transformation that allows to manipulate the data before it gets rendered in the charts
1180
+ this.eventEmitter.emit('data', {
1181
+ type: 'update',
1182
+ data: this.data
1183
+ });
1184
+ }
1185
+
1186
+ if(options) {
1187
+ this.options = Chartist.extend({}, override ? this.options : this.defaultOptions, options);
1188
+
1189
+ // If chartist was not initialized yet, we just set the options and leave the rest to the initialization
1190
+ // Otherwise we re-create the optionsProvider at this point
1191
+ if(!this.initializeTimeoutId) {
1192
+ this.optionsProvider.removeMediaQueryListeners();
1193
+ this.optionsProvider = Chartist.optionsProvider(this.options, this.responsiveOptions, this.eventEmitter);
1194
+ }
1195
+ }
1196
+
1197
+ // Only re-created the chart if it has been initialized yet
1198
+ if(!this.initializeTimeoutId) {
1199
+ this.createChart(this.optionsProvider.currentOptions);
1200
+ }
1201
+
1202
+ // Return a reference to the chart object to chain up calls
1203
+ return this;
1204
+ }
1205
+
1206
+ /**
1207
+ * This method can be called on the API object of each chart and will un-register all event listeners that were added to other components. This currently includes a window.resize listener as well as media query listeners if any responsive options have been provided. Use this function if you need to destroy and recreate Chartist charts dynamically.
1208
+ *
1209
+ * @memberof Chartist.Base
1210
+ */
1211
+ function detach() {
1212
+ window.removeEventListener('resize', this.resizeListener);
1213
+ this.optionsProvider.removeMediaQueryListeners();
1214
+ return this;
1215
+ }
1216
+
1217
+ /**
1218
+ * Use this function to register event handlers. The handler callbacks are synchronous and will run in the main thread rather than the event loop.
1219
+ *
1220
+ * @memberof Chartist.Base
1221
+ * @param {String} event Name of the event. Check the examples for supported events.
1222
+ * @param {Function} handler The handler function that will be called when an event with the given name was emitted. This function will receive a data argument which contains event data. See the example for more details.
1223
+ */
1224
+ function on(event, handler) {
1225
+ this.eventEmitter.addEventHandler(event, handler);
1226
+ return this;
1227
+ }
1228
+
1229
+ /**
1230
+ * Use this function to un-register event handlers. If the handler function parameter is omitted all handlers for the given event will be un-registered.
1231
+ *
1232
+ * @memberof Chartist.Base
1233
+ * @param {String} event Name of the event for which a handler should be removed
1234
+ * @param {Function} [handler] The handler function that that was previously used to register a new event handler. This handler will be removed from the event handler list. If this parameter is omitted then all event handlers for the given event are removed from the list.
1235
+ */
1236
+ function off(event, handler) {
1237
+ this.eventEmitter.removeEventHandler(event, handler);
1238
+ return this;
1239
+ }
1240
+
1241
+ function initialize() {
1242
+ // Add window resize listener that re-creates the chart
1243
+ window.addEventListener('resize', this.resizeListener);
1244
+
1245
+ // Obtain current options based on matching media queries (if responsive options are given)
1246
+ // This will also register a listener that is re-creating the chart based on media changes
1247
+ this.optionsProvider = Chartist.optionsProvider(this.options, this.responsiveOptions, this.eventEmitter);
1248
+ // Register options change listener that will trigger a chart update
1249
+ this.eventEmitter.addEventHandler('optionsChanged', function() {
1250
+ this.update();
1251
+ }.bind(this));
1252
+
1253
+ // Before the first chart creation we need to register us with all plugins that are configured
1254
+ // Initialize all relevant plugins with our chart object and the plugin options specified in the config
1255
+ if(this.options.plugins) {
1256
+ this.options.plugins.forEach(function(plugin) {
1257
+ if(plugin instanceof Array) {
1258
+ plugin[0](this, plugin[1]);
1259
+ } else {
1260
+ plugin(this);
1261
+ }
1262
+ }.bind(this));
1263
+ }
1264
+
1265
+ // Event for data transformation that allows to manipulate the data before it gets rendered in the charts
1266
+ this.eventEmitter.emit('data', {
1267
+ type: 'initial',
1268
+ data: this.data
1269
+ });
1270
+
1271
+ // Create the first chart
1272
+ this.createChart(this.optionsProvider.currentOptions);
1273
+
1274
+ // As chart is initialized from the event loop now we can reset our timeout reference
1275
+ // This is important if the chart gets initialized on the same element twice
1276
+ this.initializeTimeoutId = undefined;
1277
+ }
1278
+
1279
+ /**
1280
+ * Constructor of chart base class.
1281
+ *
1282
+ * @param query
1283
+ * @param data
1284
+ * @param defaultOptions
1285
+ * @param options
1286
+ * @param responsiveOptions
1287
+ * @constructor
1288
+ */
1289
+ function Base(query, data, defaultOptions, options, responsiveOptions) {
1290
+ this.container = Chartist.querySelector(query);
1291
+ this.data = data;
1292
+ this.defaultOptions = defaultOptions;
1293
+ this.options = options;
1294
+ this.responsiveOptions = responsiveOptions;
1295
+ this.eventEmitter = Chartist.EventEmitter();
1296
+ this.supportsForeignObject = Chartist.Svg.isSupported('Extensibility');
1297
+ this.supportsAnimations = Chartist.Svg.isSupported('AnimationEventsAttribute');
1298
+ this.resizeListener = function resizeListener(){
1299
+ this.update();
1300
+ }.bind(this);
1301
+
1302
+ if(this.container) {
1303
+ // If chartist was already initialized in this container we are detaching all event listeners first
1304
+ if(this.container.__chartist__) {
1305
+ if(this.container.__chartist__.initializeTimeoutId) {
1306
+ // If the initializeTimeoutId is still set we can safely assume that the initialization function has not
1307
+ // been called yet from the event loop. Therefore we should cancel the timeout and don't need to detach
1308
+ window.clearTimeout(this.container.__chartist__.initializeTimeoutId);
1309
+ } else {
1310
+ // The timeout reference has already been reset which means we need to detach the old chart first
1311
+ this.container.__chartist__.detach();
1312
+ }
1313
+ }
1314
+
1315
+ this.container.__chartist__ = this;
1316
+ }
1317
+
1318
+ // Using event loop for first draw to make it possible to register event listeners in the same call stack where
1319
+ // the chart was created.
1320
+ this.initializeTimeoutId = setTimeout(initialize.bind(this), 0);
1321
+ }
1322
+
1323
+ // Creating the chart base class
1324
+ Chartist.Base = Chartist.Class.extend({
1325
+ constructor: Base,
1326
+ optionsProvider: undefined,
1327
+ container: undefined,
1328
+ svg: undefined,
1329
+ eventEmitter: undefined,
1330
+ createChart: function() {
1331
+ throw new Error('Base chart type can\'t be instantiated!');
1332
+ },
1333
+ update: update,
1334
+ detach: detach,
1335
+ on: on,
1336
+ off: off,
1337
+ version: Chartist.version,
1338
+ supportsForeignObject: false
1339
+ });
1340
+
1341
+ }(window, document, Chartist));
1342
+ ;/**
1343
+ * Chartist SVG module for simple SVG DOM abstraction
1344
+ *
1345
+ * @module Chartist.Svg
1346
+ */
1347
+ /* global Chartist */
1348
+ (function(window, document, Chartist) {
1349
+ 'use strict';
1350
+
1351
+ var svgNs = 'http://www.w3.org/2000/svg',
1352
+ xmlNs = 'http://www.w3.org/2000/xmlns/',
1353
+ xhtmlNs = 'http://www.w3.org/1999/xhtml';
1354
+
1355
+ Chartist.xmlNs = {
1356
+ qualifiedName: 'xmlns:ct',
1357
+ prefix: 'ct',
1358
+ uri: 'http://gionkunz.github.com/chartist-js/ct'
1359
+ };
1360
+
1361
+ /**
1362
+ * Chartist.Svg creates a new SVG object wrapper with a starting element. You can use the wrapper to fluently create sub-elements and modify them.
1363
+ *
1364
+ * @memberof Chartist.Svg
1365
+ * @constructor
1366
+ * @param {String|SVGElement} name The name of the SVG element to create or an SVG dom element which should be wrapped into Chartist.Svg
1367
+ * @param {Object} attributes An object with properties that will be added as attributes to the SVG element that is created. Attributes with undefined values will not be added.
1368
+ * @param {String} className This class or class list will be added to the SVG element
1369
+ * @param {Object} parent The parent SVG wrapper object where this newly created wrapper and it's element will be attached to as child
1370
+ * @param {Boolean} insertFirst If this param is set to true in conjunction with a parent element the newly created element will be added as first child element in the parent element
1371
+ */
1372
+ function Svg(name, attributes, className, parent, insertFirst) {
1373
+ // If Svg is getting called with an SVG element we just return the wrapper
1374
+ if(name instanceof SVGElement) {
1375
+ this._node = name;
1376
+ } else {
1377
+ this._node = document.createElementNS(svgNs, name);
1378
+
1379
+ // If this is an SVG element created then custom namespace
1380
+ if(name === 'svg') {
1381
+ this._node.setAttributeNS(xmlNs, Chartist.xmlNs.qualifiedName, Chartist.xmlNs.uri);
1382
+ }
1383
+
1384
+ if(attributes) {
1385
+ this.attr(attributes);
1386
+ }
1387
+
1388
+ if(className) {
1389
+ this.addClass(className);
1390
+ }
1391
+
1392
+ if(parent) {
1393
+ if (insertFirst && parent._node.firstChild) {
1394
+ parent._node.insertBefore(this._node, parent._node.firstChild);
1395
+ } else {
1396
+ parent._node.appendChild(this._node);
1397
+ }
1398
+ }
1399
+ }
1400
+ }
1401
+
1402
+ /**
1403
+ * Set attributes on the current SVG element of the wrapper you're currently working on.
1404
+ *
1405
+ * @memberof Chartist.Svg
1406
+ * @param {Object|String} attributes An object with properties that will be added as attributes to the SVG element that is created. Attributes with undefined values will not be added. If this parameter is a String then the function is used as a getter and will return the attribute value.
1407
+ * @param {String} ns If specified, the attributes will be set as namespace attributes with ns as prefix.
1408
+ * @return {Object|String} The current wrapper object will be returned so it can be used for chaining or the attribute value if used as getter function.
1409
+ */
1410
+ function attr(attributes, ns) {
1411
+ if(typeof attributes === 'string') {
1412
+ if(ns) {
1413
+ return this._node.getAttributeNS(ns, attributes);
1414
+ } else {
1415
+ return this._node.getAttribute(attributes);
1416
+ }
1417
+ }
1418
+
1419
+ Object.keys(attributes).forEach(function(key) {
1420
+ // If the attribute value is undefined we can skip this one
1421
+ if(attributes[key] === undefined) {
1422
+ return;
1423
+ }
1424
+
1425
+ if(ns) {
1426
+ this._node.setAttributeNS(ns, [Chartist.xmlNs.prefix, ':', key].join(''), attributes[key]);
1427
+ } else {
1428
+ this._node.setAttribute(key, attributes[key]);
1429
+ }
1430
+ }.bind(this));
1431
+
1432
+ return this;
1433
+ }
1434
+
1435
+ /**
1436
+ * Create a new SVG element whose wrapper object will be selected for further operations. This way you can also create nested groups easily.
1437
+ *
1438
+ * @memberof Chartist.Svg
1439
+ * @param {String} name The name of the SVG element that should be created as child element of the currently selected element wrapper
1440
+ * @param {Object} [attributes] An object with properties that will be added as attributes to the SVG element that is created. Attributes with undefined values will not be added.
1441
+ * @param {String} [className] This class or class list will be added to the SVG element
1442
+ * @param {Boolean} [insertFirst] If this param is set to true in conjunction with a parent element the newly created element will be added as first child element in the parent element
1443
+ * @return {Chartist.Svg} Returns a Chartist.Svg wrapper object that can be used to modify the containing SVG data
1444
+ */
1445
+ function elem(name, attributes, className, insertFirst) {
1446
+ return new Chartist.Svg(name, attributes, className, this, insertFirst);
1447
+ }
1448
+
1449
+ /**
1450
+ * Returns the parent Chartist.SVG wrapper object
1451
+ *
1452
+ * @return {Chartist.Svg} Returns a Chartist.Svg wrapper around the parent node of the current node. If the parent node is not existing or it's not an SVG node then this function will return null.
1453
+ */
1454
+ function parent() {
1455
+ return this._node.parentNode instanceof SVGElement ? new Chartist.Svg(this._node.parentNode) : null;
1456
+ }
1457
+
1458
+ /**
1459
+ * This method returns a Chartist.Svg wrapper around the root SVG element of the current tree.
1460
+ *
1461
+ * @return {Chartist.Svg} The root SVG element wrapped in a Chartist.Svg element
1462
+ */
1463
+ function root() {
1464
+ var node = this._node;
1465
+ while(node.nodeName !== 'svg') {
1466
+ node = node.parentNode;
1467
+ }
1468
+ return new Chartist.Svg(node);
1469
+ }
1470
+
1471
+ /**
1472
+ * Find the first child SVG element of the current element that matches a CSS selector. The returned object is a Chartist.Svg wrapper.
1473
+ *
1474
+ * @param {String} selector A CSS selector that is used to query for child SVG elements
1475
+ * @return {Chartist.Svg} The SVG wrapper for the element found or null if no element was found
1476
+ */
1477
+ function querySelector(selector) {
1478
+ var foundNode = this._node.querySelector(selector);
1479
+ return foundNode ? new Chartist.Svg(foundNode) : null;
1480
+ }
1481
+
1482
+ /**
1483
+ * Find the all child SVG elements of the current element that match a CSS selector. The returned object is a Chartist.Svg.List wrapper.
1484
+ *
1485
+ * @param {String} selector A CSS selector that is used to query for child SVG elements
1486
+ * @return {Chartist.Svg.List} The SVG wrapper list for the element found or null if no element was found
1487
+ */
1488
+ function querySelectorAll(selector) {
1489
+ var foundNodes = this._node.querySelectorAll(selector);
1490
+ return foundNodes.length ? new Chartist.Svg.List(foundNodes) : null;
1491
+ }
1492
+
1493
+ /**
1494
+ * This method creates a foreignObject (see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject) that allows to embed HTML content into a SVG graphic. With the help of foreignObjects you can enable the usage of regular HTML elements inside of SVG where they are subject for SVG positioning and transformation but the Browser will use the HTML rendering capabilities for the containing DOM.
1495
+ *
1496
+ * @memberof Chartist.Svg
1497
+ * @param {Node|String} content The DOM Node, or HTML string that will be converted to a DOM Node, that is then placed into and wrapped by the foreignObject
1498
+ * @param {String} [attributes] An object with properties that will be added as attributes to the foreignObject element that is created. Attributes with undefined values will not be added.
1499
+ * @param {String} [className] This class or class list will be added to the SVG element
1500
+ * @param {Boolean} [insertFirst] Specifies if the foreignObject should be inserted as first child
1501
+ * @return {Chartist.Svg} New wrapper object that wraps the foreignObject element
1502
+ */
1503
+ function foreignObject(content, attributes, className, insertFirst) {
1504
+ // If content is string then we convert it to DOM
1505
+ // TODO: Handle case where content is not a string nor a DOM Node
1506
+ if(typeof content === 'string') {
1507
+ var container = document.createElement('div');
1508
+ container.innerHTML = content;
1509
+ content = container.firstChild;
1510
+ }
1511
+
1512
+ // Adding namespace to content element
1513
+ content.setAttribute('xmlns', xhtmlNs);
1514
+
1515
+ // Creating the foreignObject without required extension attribute (as described here
1516
+ // http://www.w3.org/TR/SVG/extend.html#ForeignObjectElement)
1517
+ var fnObj = this.elem('foreignObject', attributes, className, insertFirst);
1518
+
1519
+ // Add content to foreignObjectElement
1520
+ fnObj._node.appendChild(content);
1521
+
1522
+ return fnObj;
1523
+ }
1524
+
1525
+ /**
1526
+ * This method adds a new text element to the current Chartist.Svg wrapper.
1527
+ *
1528
+ * @memberof Chartist.Svg
1529
+ * @param {String} t The text that should be added to the text element that is created
1530
+ * @return {Chartist.Svg} The same wrapper object that was used to add the newly created element
1531
+ */
1532
+ function text(t) {
1533
+ this._node.appendChild(document.createTextNode(t));
1534
+ return this;
1535
+ }
1536
+
1537
+ /**
1538
+ * This method will clear all child nodes of the current wrapper object.
1539
+ *
1540
+ * @memberof Chartist.Svg
1541
+ * @return {Chartist.Svg} The same wrapper object that got emptied
1542
+ */
1543
+ function empty() {
1544
+ while (this._node.firstChild) {
1545
+ this._node.removeChild(this._node.firstChild);
1546
+ }
1547
+
1548
+ return this;
1549
+ }
1550
+
1551
+ /**
1552
+ * This method will cause the current wrapper to remove itself from its parent wrapper. Use this method if you'd like to get rid of an element in a given DOM structure.
1553
+ *
1554
+ * @memberof Chartist.Svg
1555
+ * @return {Chartist.Svg} The parent wrapper object of the element that got removed
1556
+ */
1557
+ function remove() {
1558
+ this._node.parentNode.removeChild(this._node);
1559
+ return this.parent();
1560
+ }
1561
+
1562
+ /**
1563
+ * This method will replace the element with a new element that can be created outside of the current DOM.
1564
+ *
1565
+ * @memberof Chartist.Svg
1566
+ * @param {Chartist.Svg} newElement The new Chartist.Svg object that will be used to replace the current wrapper object
1567
+ * @return {Chartist.Svg} The wrapper of the new element
1568
+ */
1569
+ function replace(newElement) {
1570
+ this._node.parentNode.replaceChild(newElement._node, this._node);
1571
+ return newElement;
1572
+ }
1573
+
1574
+ /**
1575
+ * This method will append an element to the current element as a child.
1576
+ *
1577
+ * @memberof Chartist.Svg
1578
+ * @param {Chartist.Svg} element The Chartist.Svg element that should be added as a child
1579
+ * @param {Boolean} [insertFirst] Specifies if the element should be inserted as first child
1580
+ * @return {Chartist.Svg} The wrapper of the appended object
1581
+ */
1582
+ function append(element, insertFirst) {
1583
+ if(insertFirst && this._node.firstChild) {
1584
+ this._node.insertBefore(element._node, this._node.firstChild);
1585
+ } else {
1586
+ this._node.appendChild(element._node);
1587
+ }
1588
+
1589
+ return this;
1590
+ }
1591
+
1592
+ /**
1593
+ * Returns an array of class names that are attached to the current wrapper element. This method can not be chained further.
1594
+ *
1595
+ * @memberof Chartist.Svg
1596
+ * @return {Array} A list of classes or an empty array if there are no classes on the current element
1597
+ */
1598
+ function classes() {
1599
+ return this._node.getAttribute('class') ? this._node.getAttribute('class').trim().split(/\s+/) : [];
1600
+ }
1601
+
1602
+ /**
1603
+ * Adds one or a space separated list of classes to the current element and ensures the classes are only existing once.
1604
+ *
1605
+ * @memberof Chartist.Svg
1606
+ * @param {String} names A white space separated list of class names
1607
+ * @return {Chartist.Svg} The wrapper of the current element
1608
+ */
1609
+ function addClass(names) {
1610
+ this._node.setAttribute('class',
1611
+ this.classes(this._node)
1612
+ .concat(names.trim().split(/\s+/))
1613
+ .filter(function(elem, pos, self) {
1614
+ return self.indexOf(elem) === pos;
1615
+ }).join(' ')
1616
+ );
1617
+
1618
+ return this;
1619
+ }
1620
+
1621
+ /**
1622
+ * Removes one or a space separated list of classes from the current element.
1623
+ *
1624
+ * @memberof Chartist.Svg
1625
+ * @param {String} names A white space separated list of class names
1626
+ * @return {Chartist.Svg} The wrapper of the current element
1627
+ */
1628
+ function removeClass(names) {
1629
+ var removedClasses = names.trim().split(/\s+/);
1630
+
1631
+ this._node.setAttribute('class', this.classes(this._node).filter(function(name) {
1632
+ return removedClasses.indexOf(name) === -1;
1633
+ }).join(' '));
1634
+
1635
+ return this;
1636
+ }
1637
+
1638
+ /**
1639
+ * Removes all classes from the current element.
1640
+ *
1641
+ * @memberof Chartist.Svg
1642
+ * @return {Chartist.Svg} The wrapper of the current element
1643
+ */
1644
+ function removeAllClasses() {
1645
+ this._node.setAttribute('class', '');
1646
+
1647
+ return this;
1648
+ }
1649
+
1650
+ /**
1651
+ * Get element height with fallback to svg BoundingBox or parent container dimensions:
1652
+ * See [bugzilla.mozilla.org](https://bugzilla.mozilla.org/show_bug.cgi?id=530985)
1653
+ *
1654
+ * @memberof Chartist.Svg
1655
+ * @return {Number} The elements height in pixels
1656
+ */
1657
+ function height() {
1658
+ return this._node.clientHeight || Math.round(this._node.getBBox().height) || this._node.parentNode.clientHeight;
1659
+ }
1660
+
1661
+ /**
1662
+ * Get element width with fallback to svg BoundingBox or parent container dimensions:
1663
+ * See [bugzilla.mozilla.org](https://bugzilla.mozilla.org/show_bug.cgi?id=530985)
1664
+ *
1665
+ * @memberof Chartist.Core
1666
+ * @return {Number} The elements width in pixels
1667
+ */
1668
+ function width() {
1669
+ return this._node.clientWidth || Math.round(this._node.getBBox().width) || this._node.parentNode.clientWidth;
1670
+ }
1671
+
1672
+ /**
1673
+ * The animate function lets you animate the current element with SMIL animations. You can add animations for multiple attributes at the same time by using an animation definition object. This object should contain SMIL animation attributes. Please refer to http://www.w3.org/TR/SVG/animate.html for a detailed specification about the available animation attributes. Additionally an easing property can be passed in the animation definition object. This can be a string with a name of an easing function in `Chartist.Svg.Easing` or an array with four numbers specifying a cubic Bézier curve.
1674
+ * **An animations object could look like this:**
1675
+ * ```javascript
1676
+ * element.animate({
1677
+ * opacity: {
1678
+ * dur: 1000,
1679
+ * from: 0,
1680
+ * to: 1
1681
+ * },
1682
+ * x1: {
1683
+ * dur: '1000ms',
1684
+ * from: 100,
1685
+ * to: 200,
1686
+ * easing: 'easeOutQuart'
1687
+ * },
1688
+ * y1: {
1689
+ * dur: '2s',
1690
+ * from: 0,
1691
+ * to: 100
1692
+ * }
1693
+ * });
1694
+ * ```
1695
+ * **Automatic unit conversion**
1696
+ * For the `dur` and the `begin` animate attribute you can also omit a unit by passing a number. The number will automatically be converted to milli seconds.
1697
+ * **Guided mode**
1698
+ * The default behavior of SMIL animations with offset using the `begin` attribute is that the attribute will keep it's original value until the animation starts. Mostly this behavior is not desired as you'd like to have your element attributes already initialized with the animation `from` value even before the animation starts. Also if you don't specify `fill="freeze"` on an animate element or if you delete the animation after it's done (which is done in guided mode) the attribute will switch back to the initial value. This behavior is also not desired when performing simple one-time animations. For one-time animations you'd want to trigger animations immediately instead of relative to the document begin time. That's why in guided mode Chartist.Svg will also use the `begin` property to schedule a timeout and manually start the animation after the timeout. If you're using multiple SMIL definition objects for an attribute (in an array), guided mode will be disabled for this attribute, even if you explicitly enabled it.
1699
+ * If guided mode is enabled the following behavior is added:
1700
+ * - Before the animation starts (even when delayed with `begin`) the animated attribute will be set already to the `from` value of the animation
1701
+ * - `begin` is explicitly set to `indefinite` so it can be started manually without relying on document begin time (creation)
1702
+ * - The animate element will be forced to use `fill="freeze"`
1703
+ * - The animation will be triggered with `beginElement()` in a timeout where `begin` of the definition object is interpreted in milli seconds. If no `begin` was specified the timeout is triggered immediately.
1704
+ * - After the animation the element attribute value will be set to the `to` value of the animation
1705
+ * - The animate element is deleted from the DOM
1706
+ *
1707
+ * @memberof Chartist.Svg
1708
+ * @param {Object} animations An animations object where the property keys are the attributes you'd like to animate. The properties should be objects again that contain the SMIL animation attributes (usually begin, dur, from, and to). The property begin and dur is auto converted (see Automatic unit conversion). You can also schedule multiple animations for the same attribute by passing an Array of SMIL definition objects. Attributes that contain an array of SMIL definition objects will not be executed in guided mode.
1709
+ * @param {Boolean} guided Specify if guided mode should be activated for this animation (see Guided mode). If not otherwise specified, guided mode will be activated.
1710
+ * @param {Object} eventEmitter If specified, this event emitter will be notified when an animation starts or ends.
1711
+ * @return {Chartist.Svg} The current element where the animation was added
1712
+ */
1713
+ function animate(animations, guided, eventEmitter) {
1714
+ if(guided === undefined) {
1715
+ guided = true;
1716
+ }
1717
+
1718
+ Object.keys(animations).forEach(function createAnimateForAttributes(attribute) {
1719
+
1720
+ function createAnimate(animationDefinition, guided) {
1721
+ var attributeProperties = {},
1722
+ animate,
1723
+ timeout,
1724
+ easing;
1725
+
1726
+ // Check if an easing is specified in the definition object and delete it from the object as it will not
1727
+ // be part of the animate element attributes.
1728
+ if(animationDefinition.easing) {
1729
+ // If already an easing Bézier curve array we take it or we lookup a easing array in the Easing object
1730
+ easing = animationDefinition.easing instanceof Array ?
1731
+ animationDefinition.easing :
1732
+ Chartist.Svg.Easing[animationDefinition.easing];
1733
+ delete animationDefinition.easing;
1734
+ }
1735
+
1736
+ // If numeric dur or begin was provided we assume milli seconds
1737
+ animationDefinition.begin = Chartist.ensureUnit(animationDefinition.begin, 'ms');
1738
+ animationDefinition.dur = Chartist.ensureUnit(animationDefinition.dur, 'ms');
1739
+
1740
+ if(easing) {
1741
+ animationDefinition.calcMode = 'spline';
1742
+ animationDefinition.keySplines = easing.join(' ');
1743
+ animationDefinition.keyTimes = '0;1';
1744
+ }
1745
+
1746
+ // Adding "fill: freeze" if we are in guided mode and set initial attribute values
1747
+ if(guided) {
1748
+ animationDefinition.fill = 'freeze';
1749
+ // Animated property on our element should already be set to the animation from value in guided mode
1750
+ attributeProperties[attribute] = animationDefinition.from;
1751
+ this.attr(attributeProperties);
1752
+
1753
+ // In guided mode we also set begin to indefinite so we can trigger the start manually and put the begin
1754
+ // which needs to be in ms aside
1755
+ timeout = Chartist.stripUnit(animationDefinition.begin || 0);
1756
+ animationDefinition.begin = 'indefinite';
1757
+ }
1758
+
1759
+ animate = this.elem('animate', Chartist.extend({
1760
+ attributeName: attribute
1761
+ }, animationDefinition));
1762
+
1763
+ if(guided) {
1764
+ // If guided we take the value that was put aside in timeout and trigger the animation manually with a timeout
1765
+ setTimeout(function() {
1766
+ // If beginElement fails we set the animated attribute to the end position and remove the animate element
1767
+ // This happens if the SMIL ElementTimeControl interface is not supported or any other problems occured in
1768
+ // the browser. (Currently FF 34 does not support animate elements in foreignObjects)
1769
+ try {
1770
+ animate._node.beginElement();
1771
+ } catch(err) {
1772
+ // Set animated attribute to current animated value
1773
+ attributeProperties[attribute] = animationDefinition.to;
1774
+ this.attr(attributeProperties);
1775
+ // Remove the animate element as it's no longer required
1776
+ animate.remove();
1777
+ }
1778
+ }.bind(this), timeout);
1779
+ }
1780
+
1781
+ if(eventEmitter) {
1782
+ animate._node.addEventListener('beginEvent', function handleBeginEvent() {
1783
+ eventEmitter.emit('animationBegin', {
1784
+ element: this,
1785
+ animate: animate._node,
1786
+ params: animationDefinition
1787
+ });
1788
+ }.bind(this));
1789
+ }
1790
+
1791
+ animate._node.addEventListener('endEvent', function handleEndEvent() {
1792
+ if(eventEmitter) {
1793
+ eventEmitter.emit('animationEnd', {
1794
+ element: this,
1795
+ animate: animate._node,
1796
+ params: animationDefinition
1797
+ });
1798
+ }
1799
+
1800
+ if(guided) {
1801
+ // Set animated attribute to current animated value
1802
+ attributeProperties[attribute] = animationDefinition.to;
1803
+ this.attr(attributeProperties);
1804
+ // Remove the animate element as it's no longer required
1805
+ animate.remove();
1806
+ }
1807
+ }.bind(this));
1808
+ }
1809
+
1810
+ // If current attribute is an array of definition objects we create an animate for each and disable guided mode
1811
+ if(animations[attribute] instanceof Array) {
1812
+ animations[attribute].forEach(function(animationDefinition) {
1813
+ createAnimate.bind(this)(animationDefinition, false);
1814
+ }.bind(this));
1815
+ } else {
1816
+ createAnimate.bind(this)(animations[attribute], guided);
1817
+ }
1818
+
1819
+ }.bind(this));
1820
+
1821
+ return this;
1822
+ }
1823
+
1824
+ Chartist.Svg = Chartist.Class.extend({
1825
+ constructor: Svg,
1826
+ attr: attr,
1827
+ elem: elem,
1828
+ parent: parent,
1829
+ root: root,
1830
+ querySelector: querySelector,
1831
+ querySelectorAll: querySelectorAll,
1832
+ foreignObject: foreignObject,
1833
+ text: text,
1834
+ empty: empty,
1835
+ remove: remove,
1836
+ replace: replace,
1837
+ append: append,
1838
+ classes: classes,
1839
+ addClass: addClass,
1840
+ removeClass: removeClass,
1841
+ removeAllClasses: removeAllClasses,
1842
+ height: height,
1843
+ width: width,
1844
+ animate: animate
1845
+ });
1846
+
1847
+ /**
1848
+ * This method checks for support of a given SVG feature like Extensibility, SVG-animation or the like. Check http://www.w3.org/TR/SVG11/feature for a detailed list.
1849
+ *
1850
+ * @memberof Chartist.Svg
1851
+ * @param {String} feature The SVG 1.1 feature that should be checked for support.
1852
+ * @return {Boolean} True of false if the feature is supported or not
1853
+ */
1854
+ Chartist.Svg.isSupported = function(feature) {
1855
+ return document.implementation.hasFeature('www.http://w3.org/TR/SVG11/feature#' + feature, '1.1');
1856
+ };
1857
+
1858
+ /**
1859
+ * This Object contains some standard easing cubic bezier curves. Then can be used with their name in the `Chartist.Svg.animate`. You can also extend the list and use your own name in the `animate` function. Click the show code button to see the available bezier functions.
1860
+ *
1861
+ * @memberof Chartist.Svg
1862
+ */
1863
+ var easingCubicBeziers = {
1864
+ easeInSine: [0.47, 0, 0.745, 0.715],
1865
+ easeOutSine: [0.39, 0.575, 0.565, 1],
1866
+ easeInOutSine: [0.445, 0.05, 0.55, 0.95],
1867
+ easeInQuad: [0.55, 0.085, 0.68, 0.53],
1868
+ easeOutQuad: [0.25, 0.46, 0.45, 0.94],
1869
+ easeInOutQuad: [0.455, 0.03, 0.515, 0.955],
1870
+ easeInCubic: [0.55, 0.055, 0.675, 0.19],
1871
+ easeOutCubic: [0.215, 0.61, 0.355, 1],
1872
+ easeInOutCubic: [0.645, 0.045, 0.355, 1],
1873
+ easeInQuart: [0.895, 0.03, 0.685, 0.22],
1874
+ easeOutQuart: [0.165, 0.84, 0.44, 1],
1875
+ easeInOutQuart: [0.77, 0, 0.175, 1],
1876
+ easeInQuint: [0.755, 0.05, 0.855, 0.06],
1877
+ easeOutQuint: [0.23, 1, 0.32, 1],
1878
+ easeInOutQuint: [0.86, 0, 0.07, 1],
1879
+ easeInExpo: [0.95, 0.05, 0.795, 0.035],
1880
+ easeOutExpo: [0.19, 1, 0.22, 1],
1881
+ easeInOutExpo: [1, 0, 0, 1],
1882
+ easeInCirc: [0.6, 0.04, 0.98, 0.335],
1883
+ easeOutCirc: [0.075, 0.82, 0.165, 1],
1884
+ easeInOutCirc: [0.785, 0.135, 0.15, 0.86],
1885
+ easeInBack: [0.6, -0.28, 0.735, 0.045],
1886
+ easeOutBack: [0.175, 0.885, 0.32, 1.275],
1887
+ easeInOutBack: [0.68, -0.55, 0.265, 1.55]
1888
+ };
1889
+
1890
+ Chartist.Svg.Easing = easingCubicBeziers;
1891
+
1892
+ /**
1893
+ * This helper class is to wrap multiple `Chartist.Svg` elements into a list where you can call the `Chartist.Svg` functions on all elements in the list with one call. This is helpful when you'd like to perform calls with `Chartist.Svg` on multiple elements.
1894
+ * An instance of this class is also returned by `Chartist.Svg.querySelectorAll`.
1895
+ *
1896
+ * @memberof Chartist.Svg
1897
+ * @param {Array<Node>|NodeList} nodeList An Array of SVG DOM nodes or a SVG DOM NodeList (as returned by document.querySelectorAll)
1898
+ * @constructor
1899
+ */
1900
+ function SvgList(nodeList) {
1901
+ var list = this;
1902
+
1903
+ this.svgElements = [];
1904
+ for(var i = 0; i < nodeList.length; i++) {
1905
+ this.svgElements.push(new Chartist.Svg(nodeList[i]));
1906
+ }
1907
+
1908
+ // Add delegation methods for Chartist.Svg
1909
+ Object.keys(Chartist.Svg.prototype).filter(function(prototypeProperty) {
1910
+ return ['constructor',
1911
+ 'parent',
1912
+ 'querySelector',
1913
+ 'querySelectorAll',
1914
+ 'replace',
1915
+ 'append',
1916
+ 'classes',
1917
+ 'height',
1918
+ 'width'].indexOf(prototypeProperty) === -1;
1919
+ }).forEach(function(prototypeProperty) {
1920
+ list[prototypeProperty] = function() {
1921
+ var args = Array.prototype.slice.call(arguments, 0);
1922
+ list.svgElements.forEach(function(element) {
1923
+ Chartist.Svg.prototype[prototypeProperty].apply(element, args);
1924
+ });
1925
+ return list;
1926
+ };
1927
+ });
1928
+ }
1929
+
1930
+ Chartist.Svg.List = Chartist.Class.extend({
1931
+ constructor: SvgList
1932
+ });
1933
+ }(window, document, Chartist));
1934
+ ;/**
1935
+ * Chartist SVG path module for SVG path description creation and modification.
1936
+ *
1937
+ * @module Chartist.Svg.Path
1938
+ */
1939
+ /* global Chartist */
1940
+ (function(window, document, Chartist) {
1941
+ 'use strict';
1942
+
1943
+ /**
1944
+ * Contains the descriptors of supported element types in a SVG path. Currently only move, line and curve are supported.
1945
+ *
1946
+ * @memberof Chartist.Svg.Path
1947
+ * @type {Object}
1948
+ */
1949
+ var elementDescriptions = {
1950
+ m: ['x', 'y'],
1951
+ l: ['x', 'y'],
1952
+ c: ['x1', 'y1', 'x2', 'y2', 'x', 'y'],
1953
+ a: ['rx', 'ry', 'xAr', 'lAf', 'sf', 'x', 'y']
1954
+ };
1955
+
1956
+ /**
1957
+ * Default options for newly created SVG path objects.
1958
+ *
1959
+ * @memberof Chartist.Svg.Path
1960
+ * @type {Object}
1961
+ */
1962
+ var defaultOptions = {
1963
+ // The accuracy in digit count after the decimal point. This will be used to round numbers in the SVG path. If this option is set to false then no rounding will be performed.
1964
+ accuracy: 3
1965
+ };
1966
+
1967
+ function element(command, params, pathElements, pos, relative) {
1968
+ pathElements.splice(pos, 0, Chartist.extend({
1969
+ command: relative ? command.toLowerCase() : command.toUpperCase()
1970
+ }, params));
1971
+ }
1972
+
1973
+ function forEachParam(pathElements, cb) {
1974
+ pathElements.forEach(function(pathElement, pathElementIndex) {
1975
+ elementDescriptions[pathElement.command.toLowerCase()].forEach(function(paramName, paramIndex) {
1976
+ cb(pathElement, paramName, pathElementIndex, paramIndex, pathElements);
1977
+ });
1978
+ });
1979
+ }
1980
+
1981
+ /**
1982
+ * Used to construct a new path object.
1983
+ *
1984
+ * @memberof Chartist.Svg.Path
1985
+ * @param {Boolean} close If set to true then this path will be closed when stringified (with a Z at the end)
1986
+ * @param {Object} options Options object that overrides the default objects. See default options for more details.
1987
+ * @constructor
1988
+ */
1989
+ function SvgPath(close, options) {
1990
+ this.pathElements = [];
1991
+ this.pos = 0;
1992
+ this.close = close;
1993
+ this.options = Chartist.extend({}, defaultOptions, options);
1994
+ }
1995
+
1996
+ /**
1997
+ * Gets or sets the current position (cursor) inside of the path. You can move around the cursor freely but limited to 0 or the count of existing elements. All modifications with element functions will insert new elements at the position of this cursor.
1998
+ *
1999
+ * @memberof Chartist.Svg.Path
2000
+ * @param {Number} [pos] If a number is passed then the cursor is set to this position in the path element array.
2001
+ * @return {Chartist.Svg.Path|Number} If the position parameter was passed then the return value will be the path object for easy call chaining. If no position parameter was passed then the current position is returned.
2002
+ */
2003
+ function position(pos) {
2004
+ if(pos !== undefined) {
2005
+ this.pos = Math.max(0, Math.min(this.pathElements.length, pos));
2006
+ return this;
2007
+ } else {
2008
+ return this.pos;
2009
+ }
2010
+ }
2011
+
2012
+ /**
2013
+ * Removes elements from the path starting at the current position.
2014
+ *
2015
+ * @memberof Chartist.Svg.Path
2016
+ * @param {Number} count Number of path elements that should be removed from the current position.
2017
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2018
+ */
2019
+ function remove(count) {
2020
+ this.pathElements.splice(this.pos, count);
2021
+ return this;
2022
+ }
2023
+
2024
+ /**
2025
+ * Use this function to add a new move SVG path element.
2026
+ *
2027
+ * @memberof Chartist.Svg.Path
2028
+ * @param {Number} x The x coordinate for the move element.
2029
+ * @param {Number} y The y coordinate for the move element.
2030
+ * @param {Boolean} [relative] If set to true the move element will be created with relative coordinates (lowercase letter)
2031
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2032
+ */
2033
+ function move(x, y, relative) {
2034
+ element('M', {
2035
+ x: +x,
2036
+ y: +y
2037
+ }, this.pathElements, this.pos++, relative);
2038
+ return this;
2039
+ }
2040
+
2041
+ /**
2042
+ * Use this function to add a new line SVG path element.
2043
+ *
2044
+ * @memberof Chartist.Svg.Path
2045
+ * @param {Number} x The x coordinate for the line element.
2046
+ * @param {Number} y The y coordinate for the line element.
2047
+ * @param {Boolean} [relative] If set to true the line element will be created with relative coordinates (lowercase letter)
2048
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2049
+ */
2050
+ function line(x, y, relative) {
2051
+ element('L', {
2052
+ x: +x,
2053
+ y: +y
2054
+ }, this.pathElements, this.pos++, relative);
2055
+ return this;
2056
+ }
2057
+
2058
+ /**
2059
+ * Use this function to add a new curve SVG path element.
2060
+ *
2061
+ * @memberof Chartist.Svg.Path
2062
+ * @param {Number} x1 The x coordinate for the first control point of the bezier curve.
2063
+ * @param {Number} y1 The y coordinate for the first control point of the bezier curve.
2064
+ * @param {Number} x2 The x coordinate for the second control point of the bezier curve.
2065
+ * @param {Number} y2 The y coordinate for the second control point of the bezier curve.
2066
+ * @param {Number} x The x coordinate for the target point of the curve element.
2067
+ * @param {Number} y The y coordinate for the target point of the curve element.
2068
+ * @param {Boolean} [relative] If set to true the curve element will be created with relative coordinates (lowercase letter)
2069
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2070
+ */
2071
+ function curve(x1, y1, x2, y2, x, y, relative) {
2072
+ element('C', {
2073
+ x1: +x1,
2074
+ y1: +y1,
2075
+ x2: +x2,
2076
+ y2: +y2,
2077
+ x: +x,
2078
+ y: +y
2079
+ }, this.pathElements, this.pos++, relative);
2080
+ return this;
2081
+ }
2082
+
2083
+ /**
2084
+ * Use this function to add a new non-bezier curve SVG path element.
2085
+ *
2086
+ * @memberof Chartist.Svg.Path
2087
+ * @param {Number} rx The radius to be used for the x-axis of the arc.
2088
+ * @param {Number} ry The radius to be used for the y-axis of the arc.
2089
+ * @param {Number} xAr Defines the orientation of the arc
2090
+ * @param {Number} lAf Large arc flag
2091
+ * @param {Number} sf Sweep flag
2092
+ * @param {Number} x The x coordinate for the target point of the curve element.
2093
+ * @param {Number} y The y coordinate for the target point of the curve element.
2094
+ * @param {Boolean} [relative] If set to true the curve element will be created with relative coordinates (lowercase letter)
2095
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2096
+ */
2097
+ function arc(rx, ry, xAr, lAf, sf, x, y, relative) {
2098
+ element('A', {
2099
+ rx: +rx,
2100
+ ry: +ry,
2101
+ xAr: +xAr,
2102
+ lAf: +lAf,
2103
+ sf: +sf,
2104
+ x: +x,
2105
+ y: +y
2106
+ }, this.pathElements, this.pos++, relative);
2107
+ return this;
2108
+ }
2109
+
2110
+ /**
2111
+ * Parses an SVG path seen in the d attribute of path elements, and inserts the parsed elements into the existing path object at the current cursor position. Any closing path indicators (Z at the end of the path) will be ignored by the parser as this is provided by the close option in the options of the path object.
2112
+ *
2113
+ * @memberof Chartist.Svg.Path
2114
+ * @param {String} path Any SVG path that contains move (m), line (l) or curve (c) components.
2115
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2116
+ */
2117
+ function parse(path) {
2118
+ // Parsing the SVG path string into an array of arrays [['M', '10', '10'], ['L', '100', '100']]
2119
+ var chunks = path.replace(/([A-Za-z])([0-9])/g, '$1 $2')
2120
+ .replace(/([0-9])([A-Za-z])/g, '$1 $2')
2121
+ .split(/[\s,]+/)
2122
+ .reduce(function(result, element) {
2123
+ if(element.match(/[A-Za-z]/)) {
2124
+ result.push([]);
2125
+ }
2126
+
2127
+ result[result.length - 1].push(element);
2128
+ return result;
2129
+ }, []);
2130
+
2131
+ // If this is a closed path we remove the Z at the end because this is determined by the close option
2132
+ if(chunks[chunks.length - 1][0].toUpperCase() === 'Z') {
2133
+ chunks.pop();
2134
+ }
2135
+
2136
+ // Using svgPathElementDescriptions to map raw path arrays into objects that contain the command and the parameters
2137
+ // For example {command: 'M', x: '10', y: '10'}
2138
+ var elements = chunks.map(function(chunk) {
2139
+ var command = chunk.shift(),
2140
+ description = elementDescriptions[command.toLowerCase()];
2141
+
2142
+ return Chartist.extend({
2143
+ command: command
2144
+ }, description.reduce(function(result, paramName, index) {
2145
+ result[paramName] = +chunk[index];
2146
+ return result;
2147
+ }, {}));
2148
+ });
2149
+
2150
+ // Preparing a splice call with the elements array as var arg params and insert the parsed elements at the current position
2151
+ var spliceArgs = [this.pos, 0];
2152
+ Array.prototype.push.apply(spliceArgs, elements);
2153
+ Array.prototype.splice.apply(this.pathElements, spliceArgs);
2154
+ // Increase the internal position by the element count
2155
+ this.pos += elements.length;
2156
+
2157
+ return this;
2158
+ }
2159
+
2160
+ /**
2161
+ * This function renders to current SVG path object into a final SVG string that can be used in the d attribute of SVG path elements. It uses the accuracy option to round big decimals. If the close parameter was set in the constructor of this path object then a path closing Z will be appended to the output string.
2162
+ *
2163
+ * @memberof Chartist.Svg.Path
2164
+ * @return {String}
2165
+ */
2166
+ function stringify() {
2167
+ var accuracyMultiplier = Math.pow(10, this.options.accuracy);
2168
+
2169
+ return this.pathElements.reduce(function(path, pathElement) {
2170
+ var params = elementDescriptions[pathElement.command.toLowerCase()].map(function(paramName) {
2171
+ return this.options.accuracy ?
2172
+ (Math.round(pathElement[paramName] * accuracyMultiplier) / accuracyMultiplier) :
2173
+ pathElement[paramName];
2174
+ }.bind(this));
2175
+
2176
+ return path + pathElement.command + params.join(',');
2177
+ }.bind(this), '') + (this.close ? 'Z' : '');
2178
+ }
2179
+
2180
+ /**
2181
+ * Scales all elements in the current SVG path object. There is an individual parameter for each coordinate. Scaling will also be done for control points of curves, affecting the given coordinate.
2182
+ *
2183
+ * @memberof Chartist.Svg.Path
2184
+ * @param {Number} x The number which will be used to scale the x, x1 and x2 of all path elements.
2185
+ * @param {Number} y The number which will be used to scale the y, y1 and y2 of all path elements.
2186
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2187
+ */
2188
+ function scale(x, y) {
2189
+ forEachParam(this.pathElements, function(pathElement, paramName) {
2190
+ pathElement[paramName] *= paramName[0] === 'x' ? x : y;
2191
+ });
2192
+ return this;
2193
+ }
2194
+
2195
+ /**
2196
+ * Translates all elements in the current SVG path object. The translation is relative and there is an individual parameter for each coordinate. Translation will also be done for control points of curves, affecting the given coordinate.
2197
+ *
2198
+ * @memberof Chartist.Svg.Path
2199
+ * @param {Number} x The number which will be used to translate the x, x1 and x2 of all path elements.
2200
+ * @param {Number} y The number which will be used to translate the y, y1 and y2 of all path elements.
2201
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2202
+ */
2203
+ function translate(x, y) {
2204
+ forEachParam(this.pathElements, function(pathElement, paramName) {
2205
+ pathElement[paramName] += paramName[0] === 'x' ? x : y;
2206
+ });
2207
+ return this;
2208
+ }
2209
+
2210
+ /**
2211
+ * This function will run over all existing path elements and then loop over their attributes. The callback function will be called for every path element attribute that exists in the current path.
2212
+ * The method signature of the callback function looks like this:
2213
+ * ```javascript
2214
+ * function(pathElement, paramName, pathElementIndex, paramIndex, pathElements)
2215
+ * ```
2216
+ * If something else than undefined is returned by the callback function, this value will be used to replace the old value. This allows you to build custom transformations of path objects that can't be achieved using the basic transformation functions scale and translate.
2217
+ *
2218
+ * @memberof Chartist.Svg.Path
2219
+ * @param {Function} transformFnc The callback function for the transformation. Check the signature in the function description.
2220
+ * @return {Chartist.Svg.Path} The current path object for easy call chaining.
2221
+ */
2222
+ function transform(transformFnc) {
2223
+ forEachParam(this.pathElements, function(pathElement, paramName, pathElementIndex, paramIndex, pathElements) {
2224
+ var transformed = transformFnc(pathElement, paramName, pathElementIndex, paramIndex, pathElements);
2225
+ if(transformed || transformed === 0) {
2226
+ pathElement[paramName] = transformed;
2227
+ }
2228
+ });
2229
+ return this;
2230
+ }
2231
+
2232
+ /**
2233
+ * This function clones a whole path object with all its properties. This is a deep clone and path element objects will also be cloned.
2234
+ *
2235
+ * @memberof Chartist.Svg.Path
2236
+ * @return {Chartist.Svg.Path}
2237
+ */
2238
+ function clone() {
2239
+ var c = new Chartist.Svg.Path(this.close);
2240
+ c.pos = this.pos;
2241
+ c.pathElements = this.pathElements.slice().map(function cloneElements(pathElement) {
2242
+ return Chartist.extend({}, pathElement);
2243
+ });
2244
+ c.options = Chartist.extend({}, this.options);
2245
+ return c;
2246
+ }
2247
+
2248
+ Chartist.Svg.Path = Chartist.Class.extend({
2249
+ constructor: SvgPath,
2250
+ position: position,
2251
+ remove: remove,
2252
+ move: move,
2253
+ line: line,
2254
+ curve: curve,
2255
+ arc: arc,
2256
+ scale: scale,
2257
+ translate: translate,
2258
+ transform: transform,
2259
+ parse: parse,
2260
+ stringify: stringify,
2261
+ clone: clone
2262
+ });
2263
+
2264
+ Chartist.Svg.Path.elementDescriptions = elementDescriptions;
2265
+ }(window, document, Chartist));
2266
+ ;/**
2267
+ * Axis base class used to implement different axis types
2268
+ *
2269
+ * @module Chartist.Axis
2270
+ */
2271
+ /* global Chartist */
2272
+ (function (window, document, Chartist) {
2273
+ 'use strict';
2274
+
2275
+ var axisUnits = {
2276
+ x: {
2277
+ pos: 'x',
2278
+ len: 'width',
2279
+ dir: 'horizontal',
2280
+ rectStart: 'x1',
2281
+ rectEnd: 'x2',
2282
+ rectOffset: 'y2'
2283
+ },
2284
+ y: {
2285
+ pos: 'y',
2286
+ len: 'height',
2287
+ dir: 'vertical',
2288
+ rectStart: 'y2',
2289
+ rectEnd: 'y1',
2290
+ rectOffset: 'x1'
2291
+ }
2292
+ };
2293
+
2294
+ function Axis(units, chartRect, transform, labelOffset, options) {
2295
+ this.units = units;
2296
+ this.counterUnits = units === axisUnits.x ? axisUnits.y : axisUnits.x;
2297
+ this.chartRect = chartRect;
2298
+ this.axisLength = chartRect[units.rectEnd] - chartRect[units.rectStart];
2299
+ this.gridOffset = chartRect[units.rectOffset];
2300
+ this.transform = transform;
2301
+ this.labelOffset = labelOffset;
2302
+ this.options = options;
2303
+ }
2304
+
2305
+ Chartist.Axis = Chartist.Class.extend({
2306
+ constructor: Axis,
2307
+ projectValue: function(value, index, data) {
2308
+ throw new Error('Base axis can\'t be instantiated!');
2309
+ }
2310
+ });
2311
+
2312
+ Chartist.Axis.units = axisUnits;
2313
+
2314
+ }(window, document, Chartist));
2315
+ ;/**
2316
+ * The linear scale axis uses standard linear scale projection of values along an axis.
2317
+ *
2318
+ * @module Chartist.LinearScaleAxis
2319
+ */
2320
+ /* global Chartist */
2321
+ (function (window, document, Chartist) {
2322
+ 'use strict';
2323
+
2324
+ function LinearScaleAxis(axisUnit, chartRect, transform, labelOffset, options) {
2325
+ Chartist.LinearScaleAxis.super.constructor.call(this,
2326
+ axisUnit,
2327
+ chartRect,
2328
+ transform,
2329
+ labelOffset,
2330
+ options);
2331
+
2332
+ this.bounds = Chartist.getBounds(this.axisLength, options.highLow, options.scaleMinSpace, options.referenceValue);
2333
+ }
2334
+
2335
+ function projectValue(value) {
2336
+ return {
2337
+ pos: this.axisLength * (value - this.bounds.min) / (this.bounds.range + this.bounds.step),
2338
+ len: Chartist.projectLength(this.axisLength, this.bounds.step, this.bounds)
2339
+ };
2340
+ }
2341
+
2342
+ Chartist.LinearScaleAxis = Chartist.Axis.extend({
2343
+ constructor: LinearScaleAxis,
2344
+ projectValue: projectValue
2345
+ });
2346
+
2347
+ }(window, document, Chartist));
2348
+ ;/**
2349
+ * Step axis for step based charts like bar chart or step based line chart
2350
+ *
2351
+ * @module Chartist.StepAxis
2352
+ */
2353
+ /* global Chartist */
2354
+ (function (window, document, Chartist) {
2355
+ 'use strict';
2356
+
2357
+ function StepAxis(axisUnit, chartRect, transform, labelOffset, options) {
2358
+ Chartist.StepAxis.super.constructor.call(this,
2359
+ axisUnit,
2360
+ chartRect,
2361
+ transform,
2362
+ labelOffset,
2363
+ options);
2364
+
2365
+ this.stepLength = this.axisLength / (options.stepCount - (options.stretch ? 1 : 0));
2366
+ }
2367
+
2368
+ function projectValue(value, index) {
2369
+ return {
2370
+ pos: this.stepLength * index,
2371
+ len: this.stepLength
2372
+ };
2373
+ }
2374
+
2375
+ Chartist.StepAxis = Chartist.Axis.extend({
2376
+ constructor: StepAxis,
2377
+ projectValue: projectValue
2378
+ });
2379
+
2380
+ }(window, document, Chartist));
2381
+ ;/**
2382
+ * The Chartist line chart can be used to draw Line or Scatter charts. If used in the browser you can access the global `Chartist` namespace where you find the `Line` function as a main entry point.
2383
+ *
2384
+ * For examples on how to use the line chart please check the examples of the `Chartist.Line` method.
2385
+ *
2386
+ * @module Chartist.Line
2387
+ */
2388
+ /* global Chartist */
2389
+ (function(window, document, Chartist){
2390
+ 'use strict';
2391
+
2392
+ /**
2393
+ * Default options in line charts. Expand the code view to see a detailed list of options with comments.
2394
+ *
2395
+ * @memberof Chartist.Line
2396
+ */
2397
+ var defaultOptions = {
2398
+ // Options for X-Axis
2399
+ axisX: {
2400
+ // The offset of the labels to the chart area
2401
+ offset: 30,
2402
+ // Allows you to correct label positioning on this axis by positive or negative x and y offset.
2403
+ labelOffset: {
2404
+ x: 0,
2405
+ y: 0
2406
+ },
2407
+ // If labels should be shown or not
2408
+ showLabel: true,
2409
+ // If the axis grid should be drawn or not
2410
+ showGrid: true,
2411
+ // Interpolation function that allows you to intercept the value from the axis label
2412
+ labelInterpolationFnc: Chartist.noop
2413
+ },
2414
+ // Options for Y-Axis
2415
+ axisY: {
2416
+ // The offset of the labels to the chart area
2417
+ offset: 40,
2418
+ // Allows you to correct label positioning on this axis by positive or negative x and y offset.
2419
+ labelOffset: {
2420
+ x: 0,
2421
+ y: 0
2422
+ },
2423
+ // If labels should be shown or not
2424
+ showLabel: true,
2425
+ // If the axis grid should be drawn or not
2426
+ showGrid: true,
2427
+ // Interpolation function that allows you to intercept the value from the axis label
2428
+ labelInterpolationFnc: Chartist.noop,
2429
+ // This value specifies the minimum height in pixel of the scale steps
2430
+ scaleMinSpace: 20
2431
+ },
2432
+ // Specify a fixed width for the chart as a string (i.e. '100px' or '50%')
2433
+ width: undefined,
2434
+ // Specify a fixed height for the chart as a string (i.e. '100px' or '50%')
2435
+ height: undefined,
2436
+ // If the line should be drawn or not
2437
+ showLine: true,
2438
+ // If dots should be drawn or not
2439
+ showPoint: true,
2440
+ // If the line chart should draw an area
2441
+ showArea: false,
2442
+ // The base for the area chart that will be used to close the area shape (is normally 0)
2443
+ areaBase: 0,
2444
+ // Specify if the lines should be smoothed. This value can be true or false where true will result in smoothing using the default smoothing interpolation function Chartist.Interpolation.cardinal and false results in Chartist.Interpolation.none. You can also choose other smoothing / interpolation functions available in the Chartist.Interpolation module, or write your own interpolation function. Check the examples for a brief description.
2445
+ lineSmooth: true,
2446
+ // Overriding the natural low of the chart allows you to zoom in or limit the charts lowest displayed value
2447
+ low: undefined,
2448
+ // Overriding the natural high of the chart allows you to zoom in or limit the charts highest displayed value
2449
+ high: undefined,
2450
+ // Padding of the chart drawing area to the container element and labels as a number or padding object {top: 5, right: 5, bottom: 5, left: 5}
2451
+ chartPadding: 5,
2452
+ // When set to true, the last grid line on the x-axis is not drawn and the chart elements will expand to the full available width of the chart. For the last label to be drawn correctly you might need to add chart padding or offset the last label with a draw event handler.
2453
+ fullWidth: false,
2454
+ // If true the whole data is reversed including labels, the series order as well as the whole series data arrays.
2455
+ reverseData: false,
2456
+ // Override the class names that get used to generate the SVG structure of the chart
2457
+ classNames: {
2458
+ chart: 'ct-chart-line',
2459
+ label: 'ct-label',
2460
+ labelGroup: 'ct-labels',
2461
+ series: 'ct-series',
2462
+ line: 'ct-line',
2463
+ point: 'ct-point',
2464
+ area: 'ct-area',
2465
+ grid: 'ct-grid',
2466
+ gridGroup: 'ct-grids',
2467
+ vertical: 'ct-vertical',
2468
+ horizontal: 'ct-horizontal'
2469
+ }
2470
+ };
2471
+
2472
+ /**
2473
+ * Creates a new chart
2474
+ *
2475
+ */
2476
+ function createChart(options) {
2477
+ var seriesGroups = [],
2478
+ normalizedData = Chartist.normalizeDataArray(Chartist.getDataArray(this.data, options.reverseData), this.data.labels.length),
2479
+ normalizedPadding = Chartist.normalizePadding(options.chartPadding, defaultOptions.padding);
2480
+
2481
+ // Create new svg object
2482
+ this.svg = Chartist.createSvg(this.container, options.width, options.height, options.classNames.chart);
2483
+
2484
+ var chartRect = Chartist.createChartRect(this.svg, options, defaultOptions.padding);
2485
+
2486
+ var highLow = Chartist.getHighLow(normalizedData);
2487
+ // Overrides of high / low from settings
2488
+ highLow.high = +options.high || (options.high === 0 ? 0 : highLow.high);
2489
+ highLow.low = +options.low || (options.low === 0 ? 0 : highLow.low);
2490
+
2491
+ var axisX = new Chartist.StepAxis(
2492
+ Chartist.Axis.units.x,
2493
+ chartRect,
2494
+ function xAxisTransform(projectedValue) {
2495
+ projectedValue.pos = chartRect.x1 + projectedValue.pos;
2496
+ return projectedValue;
2497
+ },
2498
+ {
2499
+ x: options.axisX.labelOffset.x,
2500
+ y: chartRect.y1 + options.axisX.labelOffset.y + (this.supportsForeignObject ? 5 : 20)
2501
+ },
2502
+ {
2503
+ stepCount: this.data.labels.length,
2504
+ stretch: options.fullWidth
2505
+ }
2506
+ );
2507
+
2508
+ var axisY = new Chartist.LinearScaleAxis(
2509
+ Chartist.Axis.units.y,
2510
+ chartRect,
2511
+ function yAxisTransform(projectedValue) {
2512
+ projectedValue.pos = chartRect.y1 - projectedValue.pos;
2513
+ return projectedValue;
2514
+ },
2515
+ {
2516
+ x: normalizedPadding.left + options.axisY.labelOffset.x + (this.supportsForeignObject ? -10 : 0),
2517
+ y: options.axisY.labelOffset.y + (this.supportsForeignObject ? -15 : 0)
2518
+ },
2519
+ {
2520
+ highLow: highLow,
2521
+ scaleMinSpace: options.axisY.scaleMinSpace
2522
+ }
2523
+ );
2524
+
2525
+ // Start drawing
2526
+ var labelGroup = this.svg.elem('g').addClass(options.classNames.labelGroup),
2527
+ gridGroup = this.svg.elem('g').addClass(options.classNames.gridGroup);
2528
+
2529
+ Chartist.createAxis(
2530
+ axisX,
2531
+ this.data.labels,
2532
+ chartRect,
2533
+ gridGroup,
2534
+ labelGroup,
2535
+ this.supportsForeignObject,
2536
+ options,
2537
+ this.eventEmitter
2538
+ );
2539
+
2540
+ Chartist.createAxis(
2541
+ axisY,
2542
+ axisY.bounds.values,
2543
+ chartRect,
2544
+ gridGroup,
2545
+ labelGroup,
2546
+ this.supportsForeignObject,
2547
+ options,
2548
+ this.eventEmitter
2549
+ );
2550
+
2551
+ // Draw the series
2552
+ this.data.series.forEach(function(series, seriesIndex) {
2553
+ seriesGroups[seriesIndex] = this.svg.elem('g');
2554
+
2555
+ // Write attributes to series group element. If series name or meta is undefined the attributes will not be written
2556
+ seriesGroups[seriesIndex].attr({
2557
+ 'series-name': series.name,
2558
+ 'meta': Chartist.serialize(series.meta)
2559
+ }, Chartist.xmlNs.uri);
2560
+
2561
+ // Use series class from series data or if not set generate one
2562
+ seriesGroups[seriesIndex].addClass([
2563
+ options.classNames.series,
2564
+ (series.className || options.classNames.series + '-' + Chartist.alphaNumerate(seriesIndex))
2565
+ ].join(' '));
2566
+
2567
+ var pathCoordinates = [];
2568
+
2569
+ normalizedData[seriesIndex].forEach(function(value, valueIndex) {
2570
+ var p = {
2571
+ x: chartRect.x1 + axisX.projectValue(value, valueIndex, normalizedData[seriesIndex]).pos,
2572
+ y: chartRect.y1 - axisY.projectValue(value, valueIndex, normalizedData[seriesIndex]).pos
2573
+ };
2574
+ pathCoordinates.push(p.x, p.y);
2575
+
2576
+ //If we should show points we need to create them now to avoid secondary loop
2577
+ // Small offset for Firefox to render squares correctly
2578
+ if (options.showPoint) {
2579
+ var point = seriesGroups[seriesIndex].elem('line', {
2580
+ x1: p.x,
2581
+ y1: p.y,
2582
+ x2: p.x + 0.01,
2583
+ y2: p.y
2584
+ }, options.classNames.point).attr({
2585
+ 'value': value,
2586
+ 'meta': Chartist.getMetaData(series, valueIndex)
2587
+ }, Chartist.xmlNs.uri);
2588
+
2589
+ this.eventEmitter.emit('draw', {
2590
+ type: 'point',
2591
+ value: value,
2592
+ index: valueIndex,
2593
+ group: seriesGroups[seriesIndex],
2594
+ element: point,
2595
+ x: p.x,
2596
+ y: p.y
2597
+ });
2598
+ }
2599
+ }.bind(this));
2600
+
2601
+ // TODO: Nicer handling of conditions, maybe composition?
2602
+ if (options.showLine || options.showArea) {
2603
+ var smoothing = typeof options.lineSmooth === 'function' ?
2604
+ options.lineSmooth : (options.lineSmooth ? Chartist.Interpolation.cardinal() : Chartist.Interpolation.none()),
2605
+ path = smoothing(pathCoordinates);
2606
+
2607
+ if(options.showLine) {
2608
+ var line = seriesGroups[seriesIndex].elem('path', {
2609
+ d: path.stringify()
2610
+ }, options.classNames.line, true).attr({
2611
+ 'values': normalizedData[seriesIndex]
2612
+ }, Chartist.xmlNs.uri);
2613
+
2614
+ this.eventEmitter.emit('draw', {
2615
+ type: 'line',
2616
+ values: normalizedData[seriesIndex],
2617
+ path: path.clone(),
2618
+ chartRect: chartRect,
2619
+ index: seriesIndex,
2620
+ group: seriesGroups[seriesIndex],
2621
+ element: line
2622
+ });
2623
+ }
2624
+
2625
+ if(options.showArea) {
2626
+ // If areaBase is outside the chart area (< low or > high) we need to set it respectively so that
2627
+ // the area is not drawn outside the chart area.
2628
+ var areaBase = Math.max(Math.min(options.areaBase, axisY.bounds.max), axisY.bounds.min);
2629
+
2630
+ // We project the areaBase value into screen coordinates
2631
+ var areaBaseProjected = chartRect.y1 - axisY.projectValue(areaBase).pos;
2632
+
2633
+ // Clone original path and splice our new area path to add the missing path elements to close the area shape
2634
+ var areaPath = path.clone();
2635
+ // Modify line path and add missing elements for area
2636
+ areaPath.position(0)
2637
+ .remove(1)
2638
+ .move(chartRect.x1, areaBaseProjected)
2639
+ .line(pathCoordinates[0], pathCoordinates[1])
2640
+ .position(areaPath.pathElements.length)
2641
+ .line(pathCoordinates[pathCoordinates.length - 2], areaBaseProjected);
2642
+
2643
+ // Create the new path for the area shape with the area class from the options
2644
+ var area = seriesGroups[seriesIndex].elem('path', {
2645
+ d: areaPath.stringify()
2646
+ }, options.classNames.area, true).attr({
2647
+ 'values': normalizedData[seriesIndex]
2648
+ }, Chartist.xmlNs.uri);
2649
+
2650
+ this.eventEmitter.emit('draw', {
2651
+ type: 'area',
2652
+ values: normalizedData[seriesIndex],
2653
+ path: areaPath.clone(),
2654
+ chartRect: chartRect,
2655
+ index: seriesIndex,
2656
+ group: seriesGroups[seriesIndex],
2657
+ element: area
2658
+ });
2659
+ }
2660
+ }
2661
+ }.bind(this));
2662
+
2663
+ this.eventEmitter.emit('created', {
2664
+ bounds: axisY.bounds,
2665
+ chartRect: chartRect,
2666
+ axisX: axisX,
2667
+ axisY: axisY,
2668
+ svg: this.svg,
2669
+ options: options
2670
+ });
2671
+ }
2672
+
2673
+ /**
2674
+ * This method creates a new line chart.
2675
+ *
2676
+ * @memberof Chartist.Line
2677
+ * @param {String|Node} query A selector query string or directly a DOM element
2678
+ * @param {Object} data The data object that needs to consist of a labels and a series array
2679
+ * @param {Object} [options] The options object with options that override the default options. Check the examples for a detailed list.
2680
+ * @param {Array} [responsiveOptions] Specify an array of responsive option arrays which are a media query and options object pair => [[mediaQueryString, optionsObject],[more...]]
2681
+ * @return {Object} An object which exposes the API for the created chart
2682
+ *
2683
+ * @example
2684
+ * // Create a simple line chart
2685
+ * var data = {
2686
+ * // A labels array that can contain any sort of values
2687
+ * labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
2688
+ * // Our series array that contains series objects or in this case series data arrays
2689
+ * series: [
2690
+ * [5, 2, 4, 2, 0]
2691
+ * ]
2692
+ * };
2693
+ *
2694
+ * // As options we currently only set a static size of 300x200 px
2695
+ * var options = {
2696
+ * width: '300px',
2697
+ * height: '200px'
2698
+ * };
2699
+ *
2700
+ * // In the global name space Chartist we call the Line function to initialize a line chart. As a first parameter we pass in a selector where we would like to get our chart created. Second parameter is the actual data object and as a third parameter we pass in our options
2701
+ * new Chartist.Line('.ct-chart', data, options);
2702
+ *
2703
+ * @example
2704
+ * // Use specific interpolation function with configuration from the Chartist.Interpolation module
2705
+ *
2706
+ * var chart = new Chartist.Line('.ct-chart', {
2707
+ * labels: [1, 2, 3, 4, 5],
2708
+ * series: [
2709
+ * [1, 1, 8, 1, 7]
2710
+ * ]
2711
+ * }, {
2712
+ * lineSmooth: Chartist.Interpolation.cardinal({
2713
+ * tension: 0.2
2714
+ * })
2715
+ * });
2716
+ *
2717
+ * @example
2718
+ * // Create a line chart with responsive options
2719
+ *
2720
+ * var data = {
2721
+ * // A labels array that can contain any sort of values
2722
+ * labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
2723
+ * // Our series array that contains series objects or in this case series data arrays
2724
+ * series: [
2725
+ * [5, 2, 4, 2, 0]
2726
+ * ]
2727
+ * };
2728
+ *
2729
+ * // In adition to the regular options we specify responsive option overrides that will override the default configutation based on the matching media queries.
2730
+ * var responsiveOptions = [
2731
+ * ['screen and (min-width: 641px) and (max-width: 1024px)', {
2732
+ * showPoint: false,
2733
+ * axisX: {
2734
+ * labelInterpolationFnc: function(value) {
2735
+ * // Will return Mon, Tue, Wed etc. on medium screens
2736
+ * return value.slice(0, 3);
2737
+ * }
2738
+ * }
2739
+ * }],
2740
+ * ['screen and (max-width: 640px)', {
2741
+ * showLine: false,
2742
+ * axisX: {
2743
+ * labelInterpolationFnc: function(value) {
2744
+ * // Will return M, T, W etc. on small screens
2745
+ * return value[0];
2746
+ * }
2747
+ * }
2748
+ * }]
2749
+ * ];
2750
+ *
2751
+ * new Chartist.Line('.ct-chart', data, null, responsiveOptions);
2752
+ *
2753
+ */
2754
+ function Line(query, data, options, responsiveOptions) {
2755
+ Chartist.Line.super.constructor.call(this,
2756
+ query,
2757
+ data,
2758
+ defaultOptions,
2759
+ Chartist.extend({}, defaultOptions, options),
2760
+ responsiveOptions);
2761
+ }
2762
+
2763
+ // Creating line chart type in Chartist namespace
2764
+ Chartist.Line = Chartist.Base.extend({
2765
+ constructor: Line,
2766
+ createChart: createChart
2767
+ });
2768
+
2769
+ }(window, document, Chartist));
2770
+ ;/**
2771
+ * The bar chart module of Chartist that can be used to draw unipolar or bipolar bar and grouped bar charts.
2772
+ *
2773
+ * @module Chartist.Bar
2774
+ */
2775
+ /* global Chartist */
2776
+ (function(window, document, Chartist){
2777
+ 'use strict';
2778
+
2779
+ /**
2780
+ * Default options in bar charts. Expand the code view to see a detailed list of options with comments.
2781
+ *
2782
+ * @memberof Chartist.Bar
2783
+ */
2784
+ var defaultOptions = {
2785
+ // Options for X-Axis
2786
+ axisX: {
2787
+ // The offset of the chart drawing area to the border of the container
2788
+ offset: 30,
2789
+ // Allows you to correct label positioning on this axis by positive or negative x and y offset.
2790
+ labelOffset: {
2791
+ x: 0,
2792
+ y: 0
2793
+ },
2794
+ // If labels should be shown or not
2795
+ showLabel: true,
2796
+ // If the axis grid should be drawn or not
2797
+ showGrid: true,
2798
+ // Interpolation function that allows you to intercept the value from the axis label
2799
+ labelInterpolationFnc: Chartist.noop,
2800
+ // This value specifies the minimum width in pixel of the scale steps
2801
+ scaleMinSpace: 40
2802
+ },
2803
+ // Options for Y-Axis
2804
+ axisY: {
2805
+ // The offset of the chart drawing area to the border of the container
2806
+ offset: 40,
2807
+ // Allows you to correct label positioning on this axis by positive or negative x and y offset.
2808
+ labelOffset: {
2809
+ x: 0,
2810
+ y: 0
2811
+ },
2812
+ // If labels should be shown or not
2813
+ showLabel: true,
2814
+ // If the axis grid should be drawn or not
2815
+ showGrid: true,
2816
+ // Interpolation function that allows you to intercept the value from the axis label
2817
+ labelInterpolationFnc: Chartist.noop,
2818
+ // This value specifies the minimum height in pixel of the scale steps
2819
+ scaleMinSpace: 20
2820
+ },
2821
+ // Specify a fixed width for the chart as a string (i.e. '100px' or '50%')
2822
+ width: undefined,
2823
+ // Specify a fixed height for the chart as a string (i.e. '100px' or '50%')
2824
+ height: undefined,
2825
+ // Overriding the natural high of the chart allows you to zoom in or limit the charts highest displayed value
2826
+ high: undefined,
2827
+ // Overriding the natural low of the chart allows you to zoom in or limit the charts lowest displayed value
2828
+ low: undefined,
2829
+ // Padding of the chart drawing area to the container element and labels as a number or padding object {top: 5, right: 5, bottom: 5, left: 5}
2830
+ chartPadding: 5,
2831
+ // Specify the distance in pixel of bars in a group
2832
+ seriesBarDistance: 15,
2833
+ // If set to true this property will cause the series bars to be stacked and form a total for each series point. This will also influence the y-axis and the overall bounds of the chart. In stacked mode the seriesBarDistance property will have no effect.
2834
+ stackBars: false,
2835
+ // Inverts the axes of the bar chart in order to draw a horizontal bar chart. Be aware that you also need to invert your axis settings as the Y Axis will now display the labels and the X Axis the values.
2836
+ horizontalBars: false,
2837
+ // If true the whole data is reversed including labels, the series order as well as the whole series data arrays.
2838
+ reverseData: false,
2839
+ // Override the class names that get used to generate the SVG structure of the chart
2840
+ classNames: {
2841
+ chart: 'ct-chart-bar',
2842
+ label: 'ct-label',
2843
+ labelGroup: 'ct-labels',
2844
+ series: 'ct-series',
2845
+ bar: 'ct-bar',
2846
+ grid: 'ct-grid',
2847
+ gridGroup: 'ct-grids',
2848
+ vertical: 'ct-vertical',
2849
+ horizontal: 'ct-horizontal'
2850
+ }
2851
+ };
2852
+
2853
+ /**
2854
+ * Creates a new chart
2855
+ *
2856
+ */
2857
+ function createChart(options) {
2858
+ var seriesGroups = [],
2859
+ normalizedData = Chartist.normalizeDataArray(Chartist.getDataArray(this.data, options.reverseData), this.data.labels.length),
2860
+ normalizedPadding = Chartist.normalizePadding(options.chartPadding, defaultOptions.padding),
2861
+ highLow;
2862
+
2863
+ // Create new svg element
2864
+ this.svg = Chartist.createSvg(this.container, options.width, options.height, options.classNames.chart);
2865
+
2866
+ if(options.stackBars) {
2867
+ // If stacked bars we need to calculate the high low from stacked values from each series
2868
+ var serialSums = Chartist.serialMap(normalizedData, function serialSums() {
2869
+ return Array.prototype.slice.call(arguments).reduce(Chartist.sum, 0);
2870
+ });
2871
+
2872
+ highLow = Chartist.getHighLow([serialSums]);
2873
+ } else {
2874
+ highLow = Chartist.getHighLow(normalizedData);
2875
+ }
2876
+ // Overrides of high / low from settings
2877
+ highLow.high = +options.high || (options.high === 0 ? 0 : highLow.high);
2878
+ highLow.low = +options.low || (options.low === 0 ? 0 : highLow.low);
2879
+
2880
+ var chartRect = Chartist.createChartRect(this.svg, options, defaultOptions.padding);
2881
+
2882
+ var valueAxis,
2883
+ labelAxis,
2884
+ axisX,
2885
+ axisY;
2886
+
2887
+ if(options.horizontalBars) {
2888
+ labelAxis = axisY = new Chartist.StepAxis(
2889
+ Chartist.Axis.units.y,
2890
+ chartRect,
2891
+ function timeAxisTransform(projectedValue) {
2892
+ projectedValue.pos = chartRect.y1 - projectedValue.pos;
2893
+ return projectedValue;
2894
+ },
2895
+ {
2896
+ x: normalizedPadding.left + options.axisY.labelOffset.x + (this.supportsForeignObject ? -10 : 0),
2897
+ y: options.axisY.labelOffset.y - chartRect.height() / this.data.labels.length
2898
+ },
2899
+ {
2900
+ stepCount: this.data.labels.length,
2901
+ stretch: options.fullHeight
2902
+ }
2903
+ );
2904
+
2905
+ valueAxis = axisX = new Chartist.LinearScaleAxis(
2906
+ Chartist.Axis.units.x,
2907
+ chartRect,
2908
+ function valueAxisTransform(projectedValue) {
2909
+ projectedValue.pos = chartRect.x1 + projectedValue.pos;
2910
+ return projectedValue;
2911
+ },
2912
+ {
2913
+ x: options.axisX.labelOffset.x,
2914
+ y: chartRect.y1 + options.axisX.labelOffset.y + (this.supportsForeignObject ? 5 : 20)
2915
+ },
2916
+ {
2917
+ highLow: highLow,
2918
+ scaleMinSpace: options.axisX.scaleMinSpace,
2919
+ referenceValue: 0
2920
+ }
2921
+ );
2922
+ } else {
2923
+ labelAxis = axisX = new Chartist.StepAxis(
2924
+ Chartist.Axis.units.x,
2925
+ chartRect,
2926
+ function timeAxisTransform(projectedValue) {
2927
+ projectedValue.pos = chartRect.x1 + projectedValue.pos;
2928
+ return projectedValue;
2929
+ },
2930
+ {
2931
+ x: options.axisX.labelOffset.x,
2932
+ y: chartRect.y1 + options.axisX.labelOffset.y + (this.supportsForeignObject ? 5 : 20)
2933
+ },
2934
+ {
2935
+ stepCount: this.data.labels.length
2936
+ }
2937
+ );
2938
+
2939
+ valueAxis = axisY = new Chartist.LinearScaleAxis(
2940
+ Chartist.Axis.units.y,
2941
+ chartRect,
2942
+ function valueAxisTransform(projectedValue) {
2943
+ projectedValue.pos = chartRect.y1 - projectedValue.pos;
2944
+ return projectedValue;
2945
+ },
2946
+ {
2947
+ x: normalizedPadding.left + options.axisY.labelOffset.x + (this.supportsForeignObject ? -10 : 0),
2948
+ y: options.axisY.labelOffset.y + (this.supportsForeignObject ? -15 : 0)
2949
+ },
2950
+ {
2951
+ highLow: highLow,
2952
+ scaleMinSpace: options.axisY.scaleMinSpace,
2953
+ referenceValue: 0
2954
+ }
2955
+ );
2956
+ }
2957
+
2958
+ // Start drawing
2959
+ var labelGroup = this.svg.elem('g').addClass(options.classNames.labelGroup),
2960
+ gridGroup = this.svg.elem('g').addClass(options.classNames.gridGroup),
2961
+ // Projected 0 point
2962
+ zeroPoint = options.horizontalBars ? (chartRect.x1 + valueAxis.projectValue(0).pos) : (chartRect.y1 - valueAxis.projectValue(0).pos),
2963
+ // Used to track the screen coordinates of stacked bars
2964
+ stackedBarValues = [];
2965
+
2966
+ Chartist.createAxis(
2967
+ labelAxis,
2968
+ this.data.labels,
2969
+ chartRect,
2970
+ gridGroup,
2971
+ labelGroup,
2972
+ this.supportsForeignObject,
2973
+ options,
2974
+ this.eventEmitter
2975
+ );
2976
+
2977
+ Chartist.createAxis(
2978
+ valueAxis,
2979
+ valueAxis.bounds.values,
2980
+ chartRect,
2981
+ gridGroup,
2982
+ labelGroup,
2983
+ this.supportsForeignObject,
2984
+ options,
2985
+ this.eventEmitter
2986
+ );
2987
+
2988
+ // Draw the series
2989
+ this.data.series.forEach(function(series, seriesIndex) {
2990
+ // Calculating bi-polar value of index for seriesOffset. For i = 0..4 biPol will be -1.5, -0.5, 0.5, 1.5 etc.
2991
+ var biPol = seriesIndex - (this.data.series.length - 1) / 2,
2992
+ // Half of the period width between vertical grid lines used to position bars
2993
+ periodHalfLength = chartRect[labelAxis.units.len]() / normalizedData[seriesIndex].length / 2;
2994
+
2995
+ seriesGroups[seriesIndex] = this.svg.elem('g');
2996
+
2997
+ // Write attributes to series group element. If series name or meta is undefined the attributes will not be written
2998
+ seriesGroups[seriesIndex].attr({
2999
+ 'series-name': series.name,
3000
+ 'meta': Chartist.serialize(series.meta)
3001
+ }, Chartist.xmlNs.uri);
3002
+
3003
+ // Use series class from series data or if not set generate one
3004
+ seriesGroups[seriesIndex].addClass([
3005
+ options.classNames.series,
3006
+ (series.className || options.classNames.series + '-' + Chartist.alphaNumerate(seriesIndex))
3007
+ ].join(' '));
3008
+
3009
+ normalizedData[seriesIndex].forEach(function(value, valueIndex) {
3010
+ var projected = {
3011
+ x: chartRect.x1 + (options.horizontalBars ? valueAxis : labelAxis).projectValue(value, valueIndex, normalizedData[seriesIndex]).pos,
3012
+ y: chartRect.y1 - (options.horizontalBars ? labelAxis : valueAxis).projectValue(value, valueIndex, normalizedData[seriesIndex]).pos
3013
+ },
3014
+ bar,
3015
+ previousStack;
3016
+
3017
+ // Offset to center bar between grid lines
3018
+ projected[labelAxis.units.pos] += periodHalfLength * (options.horizontalBars ? -1 : 1);
3019
+ // Using bi-polar offset for multiple series if no stacked bars are used
3020
+ projected[labelAxis.units.pos] += options.stackBars ? 0 : biPol * options.seriesBarDistance * (options.horizontalBars ? -1 : 1);
3021
+
3022
+ // Enter value in stacked bar values used to remember previous screen value for stacking up bars
3023
+ previousStack = stackedBarValues[valueIndex] || zeroPoint;
3024
+ stackedBarValues[valueIndex] = previousStack - (zeroPoint - projected[labelAxis.counterUnits.pos]);
3025
+
3026
+ var positions = {};
3027
+ positions[labelAxis.units.pos + '1'] = projected[labelAxis.units.pos];
3028
+ positions[labelAxis.units.pos + '2'] = projected[labelAxis.units.pos];
3029
+ // If bars are stacked we use the stackedBarValues reference and otherwise base all bars off the zero line
3030
+ positions[labelAxis.counterUnits.pos + '1'] = options.stackBars ? previousStack : zeroPoint;
3031
+ positions[labelAxis.counterUnits.pos + '2'] = options.stackBars ? stackedBarValues[valueIndex] : projected[labelAxis.counterUnits.pos];
3032
+
3033
+ bar = seriesGroups[seriesIndex].elem('line', positions, options.classNames.bar).attr({
3034
+ 'value': value,
3035
+ 'meta': Chartist.getMetaData(series, valueIndex)
3036
+ }, Chartist.xmlNs.uri);
3037
+
3038
+ this.eventEmitter.emit('draw', Chartist.extend({
3039
+ type: 'bar',
3040
+ value: value,
3041
+ index: valueIndex,
3042
+ chartRect: chartRect,
3043
+ group: seriesGroups[seriesIndex],
3044
+ element: bar
3045
+ }, positions));
3046
+ }.bind(this));
3047
+ }.bind(this));
3048
+
3049
+ this.eventEmitter.emit('created', {
3050
+ bounds: valueAxis.bounds,
3051
+ chartRect: chartRect,
3052
+ axisX: axisX,
3053
+ axisY: axisY,
3054
+ svg: this.svg,
3055
+ options: options
3056
+ });
3057
+ }
3058
+
3059
+ /**
3060
+ * This method creates a new bar chart and returns API object that you can use for later changes.
3061
+ *
3062
+ * @memberof Chartist.Bar
3063
+ * @param {String|Node} query A selector query string or directly a DOM element
3064
+ * @param {Object} data The data object that needs to consist of a labels and a series array
3065
+ * @param {Object} [options] The options object with options that override the default options. Check the examples for a detailed list.
3066
+ * @param {Array} [responsiveOptions] Specify an array of responsive option arrays which are a media query and options object pair => [[mediaQueryString, optionsObject],[more...]]
3067
+ * @return {Object} An object which exposes the API for the created chart
3068
+ *
3069
+ * @example
3070
+ * // Create a simple bar chart
3071
+ * var data = {
3072
+ * labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
3073
+ * series: [
3074
+ * [5, 2, 4, 2, 0]
3075
+ * ]
3076
+ * };
3077
+ *
3078
+ * // In the global name space Chartist we call the Bar function to initialize a bar chart. As a first parameter we pass in a selector where we would like to get our chart created and as a second parameter we pass our data object.
3079
+ * new Chartist.Bar('.ct-chart', data);
3080
+ *
3081
+ * @example
3082
+ * // This example creates a bipolar grouped bar chart where the boundaries are limitted to -10 and 10
3083
+ * new Chartist.Bar('.ct-chart', {
3084
+ * labels: [1, 2, 3, 4, 5, 6, 7],
3085
+ * series: [
3086
+ * [1, 3, 2, -5, -3, 1, -6],
3087
+ * [-5, -2, -4, -1, 2, -3, 1]
3088
+ * ]
3089
+ * }, {
3090
+ * seriesBarDistance: 12,
3091
+ * low: -10,
3092
+ * high: 10
3093
+ * });
3094
+ *
3095
+ */
3096
+ function Bar(query, data, options, responsiveOptions) {
3097
+ Chartist.Bar.super.constructor.call(this,
3098
+ query,
3099
+ data,
3100
+ defaultOptions,
3101
+ Chartist.extend({}, defaultOptions, options),
3102
+ responsiveOptions);
3103
+ }
3104
+
3105
+ // Creating bar chart type in Chartist namespace
3106
+ Chartist.Bar = Chartist.Base.extend({
3107
+ constructor: Bar,
3108
+ createChart: createChart
3109
+ });
3110
+
3111
+ }(window, document, Chartist));
3112
+ ;/**
3113
+ * The pie chart module of Chartist that can be used to draw pie, donut or gauge charts
3114
+ *
3115
+ * @module Chartist.Pie
3116
+ */
3117
+ /* global Chartist */
3118
+ (function(window, document, Chartist) {
3119
+ 'use strict';
3120
+
3121
+ /**
3122
+ * Default options in line charts. Expand the code view to see a detailed list of options with comments.
3123
+ *
3124
+ * @memberof Chartist.Pie
3125
+ */
3126
+ var defaultOptions = {
3127
+ // Specify a fixed width for the chart as a string (i.e. '100px' or '50%')
3128
+ width: undefined,
3129
+ // Specify a fixed height for the chart as a string (i.e. '100px' or '50%')
3130
+ height: undefined,
3131
+ // Padding of the chart drawing area to the container element and labels as a number or padding object {top: 5, right: 5, bottom: 5, left: 5}
3132
+ chartPadding: 5,
3133
+ // Override the class names that get used to generate the SVG structure of the chart
3134
+ classNames: {
3135
+ chart: 'ct-chart-pie',
3136
+ series: 'ct-series',
3137
+ slice: 'ct-slice',
3138
+ donut: 'ct-donut',
3139
+ label: 'ct-label'
3140
+ },
3141
+ // The start angle of the pie chart in degrees where 0 points north. A higher value offsets the start angle clockwise.
3142
+ startAngle: 0,
3143
+ // An optional total you can specify. By specifying a total value, the sum of the values in the series must be this total in order to draw a full pie. You can use this parameter to draw only parts of a pie or gauge charts.
3144
+ total: undefined,
3145
+ // If specified the donut CSS classes will be used and strokes will be drawn instead of pie slices.
3146
+ donut: false,
3147
+ // Specify the donut stroke width, currently done in javascript for convenience. May move to CSS styles in the future.
3148
+ donutWidth: 60,
3149
+ // If a label should be shown or not
3150
+ showLabel: true,
3151
+ // Label position offset from the standard position which is half distance of the radius. This value can be either positive or negative. Positive values will position the label away from the center.
3152
+ labelOffset: 0,
3153
+ // An interpolation function for the label value
3154
+ labelInterpolationFnc: Chartist.noop,
3155
+ // Label direction can be 'neutral', 'explode' or 'implode'. The labels anchor will be positioned based on those settings as well as the fact if the labels are on the right or left side of the center of the chart. Usually explode is useful when labels are positioned far away from the center.
3156
+ labelDirection: 'neutral',
3157
+ // If true the whole data is reversed including labels, the series order as well as the whole series data arrays.
3158
+ reverseData: false
3159
+ };
3160
+
3161
+ /**
3162
+ * Determines SVG anchor position based on direction and center parameter
3163
+ *
3164
+ * @param center
3165
+ * @param label
3166
+ * @param direction
3167
+ * @return {string}
3168
+ */
3169
+ function determineAnchorPosition(center, label, direction) {
3170
+ var toTheRight = label.x > center.x;
3171
+
3172
+ if(toTheRight && direction === 'explode' ||
3173
+ !toTheRight && direction === 'implode') {
3174
+ return 'start';
3175
+ } else if(toTheRight && direction === 'implode' ||
3176
+ !toTheRight && direction === 'explode') {
3177
+ return 'end';
3178
+ } else {
3179
+ return 'middle';
3180
+ }
3181
+ }
3182
+
3183
+ /**
3184
+ * Creates the pie chart
3185
+ *
3186
+ * @param options
3187
+ */
3188
+ function createChart(options) {
3189
+ var seriesGroups = [],
3190
+ chartRect,
3191
+ radius,
3192
+ labelRadius,
3193
+ totalDataSum,
3194
+ startAngle = options.startAngle,
3195
+ dataArray = Chartist.getDataArray(this.data, options.reverseData);
3196
+
3197
+ // Create SVG.js draw
3198
+ this.svg = Chartist.createSvg(this.container, options.width, options.height, options.classNames.chart);
3199
+ // Calculate charting rect
3200
+ chartRect = Chartist.createChartRect(this.svg, options, defaultOptions.padding);
3201
+ // Get biggest circle radius possible within chartRect
3202
+ radius = Math.min(chartRect.width() / 2, chartRect.height() / 2);
3203
+ // Calculate total of all series to get reference value or use total reference from optional options
3204
+ totalDataSum = options.total || dataArray.reduce(function(previousValue, currentValue) {
3205
+ return previousValue + currentValue;
3206
+ }, 0);
3207
+
3208
+ // If this is a donut chart we need to adjust our radius to enable strokes to be drawn inside
3209
+ // Unfortunately this is not possible with the current SVG Spec
3210
+ // See this proposal for more details: http://lists.w3.org/Archives/Public/www-svg/2003Oct/0000.html
3211
+ radius -= options.donut ? options.donutWidth / 2 : 0;
3212
+
3213
+ // If a donut chart then the label position is at the radius, if regular pie chart it's half of the radius
3214
+ // see https://github.com/gionkunz/chartist-js/issues/21
3215
+ labelRadius = options.donut ? radius : radius / 2;
3216
+ // Add the offset to the labelRadius where a negative offset means closed to the center of the chart
3217
+ labelRadius += options.labelOffset;
3218
+
3219
+ // Calculate end angle based on total sum and current data value and offset with padding
3220
+ var center = {
3221
+ x: chartRect.x1 + chartRect.width() / 2,
3222
+ y: chartRect.y2 + chartRect.height() / 2
3223
+ };
3224
+
3225
+ // Check if there is only one non-zero value in the series array.
3226
+ var hasSingleValInSeries = this.data.series.filter(function(val) {
3227
+ return val !== 0;
3228
+ }).length === 1;
3229
+
3230
+ // Draw the series
3231
+ // initialize series groups
3232
+ for (var i = 0; i < this.data.series.length; i++) {
3233
+ seriesGroups[i] = this.svg.elem('g', null, null, true);
3234
+
3235
+ // If the series is an object and contains a name we add a custom attribute
3236
+ if(this.data.series[i].name) {
3237
+ seriesGroups[i].attr({
3238
+ 'series-name': this.data.series[i].name,
3239
+ 'meta': Chartist.serialize(this.data.series[i].meta)
3240
+ }, Chartist.xmlNs.uri);
3241
+ }
3242
+
3243
+ // Use series class from series data or if not set generate one
3244
+ seriesGroups[i].addClass([
3245
+ options.classNames.series,
3246
+ (this.data.series[i].className || options.classNames.series + '-' + Chartist.alphaNumerate(i))
3247
+ ].join(' '));
3248
+
3249
+ var endAngle = startAngle + dataArray[i] / totalDataSum * 360;
3250
+ // If we need to draw the arc for all 360 degrees we need to add a hack where we close the circle
3251
+ // with Z and use 359.99 degrees
3252
+ if(endAngle - startAngle === 360) {
3253
+ endAngle -= 0.01;
3254
+ }
3255
+
3256
+ var start = Chartist.polarToCartesian(center.x, center.y, radius, startAngle - (i === 0 || hasSingleValInSeries ? 0 : 0.2)),
3257
+ end = Chartist.polarToCartesian(center.x, center.y, radius, endAngle);
3258
+
3259
+ // Create a new path element for the pie chart. If this isn't a donut chart we should close the path for a correct stroke
3260
+ var path = new Chartist.Svg.Path(!options.donut)
3261
+ .move(end.x, end.y)
3262
+ .arc(radius, radius, 0, endAngle - startAngle > 180, 0, start.x, start.y);
3263
+
3264
+ // If regular pie chart (no donut) we add a line to the center of the circle for completing the pie
3265
+ if(!options.donut) {
3266
+ path.line(center.x, center.y);
3267
+ }
3268
+
3269
+ // Create the SVG path
3270
+ // If this is a donut chart we add the donut class, otherwise just a regular slice
3271
+ var pathElement = seriesGroups[i].elem('path', {
3272
+ d: path.stringify()
3273
+ }, options.classNames.slice + (options.donut ? ' ' + options.classNames.donut : ''));
3274
+
3275
+ // Adding the pie series value to the path
3276
+ pathElement.attr({
3277
+ 'value': dataArray[i]
3278
+ }, Chartist.xmlNs.uri);
3279
+
3280
+ // If this is a donut, we add the stroke-width as style attribute
3281
+ if(options.donut) {
3282
+ pathElement.attr({
3283
+ 'style': 'stroke-width: ' + (+options.donutWidth) + 'px'
3284
+ });
3285
+ }
3286
+
3287
+ // Fire off draw event
3288
+ this.eventEmitter.emit('draw', {
3289
+ type: 'slice',
3290
+ value: dataArray[i],
3291
+ totalDataSum: totalDataSum,
3292
+ index: i,
3293
+ group: seriesGroups[i],
3294
+ element: pathElement,
3295
+ path: path.clone(),
3296
+ center: center,
3297
+ radius: radius,
3298
+ startAngle: startAngle,
3299
+ endAngle: endAngle
3300
+ });
3301
+
3302
+ // If we need to show labels we need to add the label for this slice now
3303
+ if(options.showLabel) {
3304
+ // Position at the labelRadius distance from center and between start and end angle
3305
+ var labelPosition = Chartist.polarToCartesian(center.x, center.y, labelRadius, startAngle + (endAngle - startAngle) / 2),
3306
+ interpolatedValue = options.labelInterpolationFnc(this.data.labels ? this.data.labels[i] : dataArray[i], i);
3307
+
3308
+ var labelElement = seriesGroups[i].elem('text', {
3309
+ dx: labelPosition.x,
3310
+ dy: labelPosition.y,
3311
+ 'text-anchor': determineAnchorPosition(center, labelPosition, options.labelDirection)
3312
+ }, options.classNames.label).text('' + interpolatedValue);
3313
+
3314
+ // Fire off draw event
3315
+ this.eventEmitter.emit('draw', {
3316
+ type: 'label',
3317
+ index: i,
3318
+ group: seriesGroups[i],
3319
+ element: labelElement,
3320
+ text: '' + interpolatedValue,
3321
+ x: labelPosition.x,
3322
+ y: labelPosition.y
3323
+ });
3324
+ }
3325
+
3326
+ // Set next startAngle to current endAngle. Use slight offset so there are no transparent hairline issues
3327
+ // (except for last slice)
3328
+ startAngle = endAngle;
3329
+ }
3330
+
3331
+ this.eventEmitter.emit('created', {
3332
+ chartRect: chartRect,
3333
+ svg: this.svg,
3334
+ options: options
3335
+ });
3336
+ }
3337
+
3338
+ /**
3339
+ * This method creates a new pie chart and returns an object that can be used to redraw the chart.
3340
+ *
3341
+ * @memberof Chartist.Pie
3342
+ * @param {String|Node} query A selector query string or directly a DOM element
3343
+ * @param {Object} data The data object in the pie chart needs to have a series property with a one dimensional data array. The values will be normalized against each other and don't necessarily need to be in percentage. The series property can also be an array of objects that contain a data property with the value and a className property to override the CSS class name for the series group.
3344
+ * @param {Object} [options] The options object with options that override the default options. Check the examples for a detailed list.
3345
+ * @param {Array} [responsiveOptions] Specify an array of responsive option arrays which are a media query and options object pair => [[mediaQueryString, optionsObject],[more...]]
3346
+ * @return {Object} An object with a version and an update method to manually redraw the chart
3347
+ *
3348
+ * @example
3349
+ * // Simple pie chart example with four series
3350
+ * new Chartist.Pie('.ct-chart', {
3351
+ * series: [10, 2, 4, 3]
3352
+ * });
3353
+ *
3354
+ * @example
3355
+ * // Drawing a donut chart
3356
+ * new Chartist.Pie('.ct-chart', {
3357
+ * series: [10, 2, 4, 3]
3358
+ * }, {
3359
+ * donut: true
3360
+ * });
3361
+ *
3362
+ * @example
3363
+ * // Using donut, startAngle and total to draw a gauge chart
3364
+ * new Chartist.Pie('.ct-chart', {
3365
+ * series: [20, 10, 30, 40]
3366
+ * }, {
3367
+ * donut: true,
3368
+ * donutWidth: 20,
3369
+ * startAngle: 270,
3370
+ * total: 200
3371
+ * });
3372
+ *
3373
+ * @example
3374
+ * // Drawing a pie chart with padding and labels that are outside the pie
3375
+ * new Chartist.Pie('.ct-chart', {
3376
+ * series: [20, 10, 30, 40]
3377
+ * }, {
3378
+ * chartPadding: 30,
3379
+ * labelOffset: 50,
3380
+ * labelDirection: 'explode'
3381
+ * });
3382
+ *
3383
+ * @example
3384
+ * // Overriding the class names for individual series
3385
+ * new Chartist.Pie('.ct-chart', {
3386
+ * series: [{
3387
+ * data: 20,
3388
+ * className: 'my-custom-class-one'
3389
+ * }, {
3390
+ * data: 10,
3391
+ * className: 'my-custom-class-two'
3392
+ * }, {
3393
+ * data: 70,
3394
+ * className: 'my-custom-class-three'
3395
+ * }]
3396
+ * });
3397
+ */
3398
+ function Pie(query, data, options, responsiveOptions) {
3399
+ Chartist.Pie.super.constructor.call(this,
3400
+ query,
3401
+ data,
3402
+ defaultOptions,
3403
+ Chartist.extend({}, defaultOptions, options),
3404
+ responsiveOptions);
3405
+ }
3406
+
3407
+ // Creating pie chart type in Chartist namespace
3408
+ Chartist.Pie = Chartist.Base.extend({
3409
+ constructor: Pie,
3410
+ createChart: createChart,
3411
+ determineAnchorPosition: determineAnchorPosition
3412
+ });
3413
+
3414
+ }(window, document, Chartist));
3415
+
3416
+ return Chartist;
3417
+
3418
+ }));