chartist_js 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1384 @@
1
+ (function(root, factory) {
2
+ if(typeof exports === 'object') {
3
+ module.exports = factory();
4
+ }
5
+ else if(typeof define === 'function' && define.amd) {
6
+ define([], factory);
7
+ }
8
+ else {
9
+ root['Chartist'] = factory();
10
+ }
11
+ }(this, function() {
12
+
13
+ /* Chartist.js 0.1.11
14
+ * Copyright © 2014 Gion Kunz
15
+ * Free to use under the WTFPL license.
16
+ * http://www.wtfpl.net/
17
+ */
18
+ /**
19
+ * The core module of Chartist that is mainly providing static functions and higher level functions for chart modules.
20
+ *
21
+ * @module Chartist.Core
22
+ */
23
+
24
+ // This object is prepared for export via UMD
25
+ var Chartist = {};
26
+ Chartist.version = '0.1.11';
27
+
28
+ (function (window, document, Chartist) {
29
+ 'use strict';
30
+
31
+ // Helps to simplify functional style code
32
+ Chartist.noop = function (n) {
33
+ return n;
34
+ };
35
+
36
+ // Generates a-z from number
37
+ Chartist.alphaNumerate = function (n) {
38
+ // Limit to a-z
39
+ return String.fromCharCode(97 + n % 26);
40
+ };
41
+
42
+ // Simple recursive object extend
43
+ Chartist.extend = function (target, source) {
44
+ target = target || {};
45
+ for (var prop in source) {
46
+ if (typeof source[prop] === 'object') {
47
+ target[prop] = Chartist.extend(target[prop], source[prop]);
48
+ } else {
49
+ target[prop] = source[prop];
50
+ }
51
+ }
52
+ return target;
53
+ };
54
+
55
+ // Get element height / width with fallback to svg BoundingBox or parent container dimensions
56
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=530985
57
+ Chartist.getHeight = function (svgElement) {
58
+ return svgElement.clientHeight || Math.round(svgElement.getBBox().height) || svgElement.parentNode.clientHeight;
59
+ };
60
+
61
+ Chartist.getWidth = function (svgElement) {
62
+ return svgElement.clientWidth || Math.round(svgElement.getBBox().width) || svgElement.parentNode.clientWidth;
63
+ };
64
+
65
+ // Create Chartist SVG element
66
+ Chartist.createSvg = function (query, width, height, className) {
67
+ // Get dom object from query or if already dom object just use it
68
+ var container = query.nodeType ? query : document.querySelector(query),
69
+ svg;
70
+
71
+ // If container was not found we throw up
72
+ if (!container) {
73
+ throw 'Container node with selector "' + query + '" not found';
74
+ }
75
+
76
+ // If already contains our svg object we clear it, set width / height and return
77
+ if (container._ctChart !== undefined) {
78
+ svg = container._ctChart.attr({
79
+ width: width || '100%',
80
+ height: height || '100%'
81
+ }).removeAllClasses().addClass(className);
82
+ // Clear the draw if its already used before so we start fresh
83
+ svg.empty();
84
+
85
+ } else {
86
+ // Create svg object with width and height or use 100% as default
87
+ svg = Chartist.svg('svg').attr({
88
+ width: width || '100%',
89
+ height: height || '100%'
90
+ }).addClass(className);
91
+
92
+ // Add the DOM node to our container
93
+ container.appendChild(svg._node);
94
+ container._ctChart = svg;
95
+ }
96
+
97
+ return svg;
98
+ };
99
+
100
+ // Convert data series into plain array
101
+ Chartist.getDataArray = function (data) {
102
+ var array = [];
103
+
104
+ for (var i = 0; i < data.series.length; i++) {
105
+ // If the series array contains an object with a data property we will use the property
106
+ // otherwise the value directly (array or number)
107
+ array[i] = typeof(data.series[i]) === 'object' && data.series[i].data !== undefined ?
108
+ data.series[i].data : data.series[i];
109
+ }
110
+
111
+ return array;
112
+ };
113
+
114
+ // Add missing values at the end of the arrays
115
+ Chartist.normalizeDataArray = function (dataArray, length) {
116
+ for (var i = 0; i < dataArray.length; i++) {
117
+ if (dataArray[i].length === length) {
118
+ continue;
119
+ }
120
+
121
+ for (var j = dataArray[i].length; j < length; j++) {
122
+ dataArray[i][j] = 0;
123
+ }
124
+ }
125
+
126
+ return dataArray;
127
+ };
128
+
129
+ Chartist.orderOfMagnitude = function (value) {
130
+ return Math.floor(Math.log(Math.abs(value)) / Math.LN10);
131
+ };
132
+
133
+ Chartist.projectLength = function (svg, length, bounds, options) {
134
+ var availableHeight = Chartist.getAvailableHeight(svg, options);
135
+ return (length / bounds.range * availableHeight);
136
+ };
137
+
138
+ Chartist.getAvailableHeight = function (svg, options) {
139
+ return Chartist.getHeight(svg._node) - (options.chartPadding * 2) - options.axisX.offset;
140
+ };
141
+
142
+ // Get highest and lowest value of data array
143
+ Chartist.getHighLow = function (dataArray) {
144
+ var i,
145
+ j,
146
+ highLow = {
147
+ high: -Number.MAX_VALUE,
148
+ low: Number.MAX_VALUE
149
+ };
150
+
151
+ for (i = 0; i < dataArray.length; i++) {
152
+ for (j = 0; j < dataArray[i].length; j++) {
153
+ if (dataArray[i][j] > highLow.high) {
154
+ highLow.high = dataArray[i][j];
155
+ }
156
+
157
+ if (dataArray[i][j] < highLow.low) {
158
+ highLow.low = dataArray[i][j];
159
+ }
160
+ }
161
+ }
162
+
163
+ return highLow;
164
+ };
165
+
166
+ // Find the highest and lowest values in a two dimensional array and calculate scale based on order of magnitude
167
+ Chartist.getBounds = function (svg, normalizedData, options, referenceValue) {
168
+ var i,
169
+ newMin,
170
+ newMax,
171
+ bounds = Chartist.getHighLow(normalizedData);
172
+
173
+ // Overrides of high / low from settings
174
+ bounds.high = options.high || (options.high === 0 ? 0 : bounds.high);
175
+ bounds.low = options.low || (options.low === 0 ? 0 : bounds.low);
176
+
177
+ // Overrides of high / low based on reference value, it will make sure that the invisible reference value is
178
+ // used to generate the chart. This is useful when the chart always needs to contain the position of the
179
+ // invisible reference value in the view i.e. for bipolar scales.
180
+ if (referenceValue || referenceValue === 0) {
181
+ bounds.high = Math.max(referenceValue, bounds.high);
182
+ bounds.low = Math.min(referenceValue, bounds.low);
183
+ }
184
+
185
+ bounds.valueRange = bounds.high - bounds.low;
186
+ bounds.oom = Chartist.orderOfMagnitude(bounds.valueRange);
187
+ bounds.min = Math.floor(bounds.low / Math.pow(10, bounds.oom)) * Math.pow(10, bounds.oom);
188
+ bounds.max = Math.ceil(bounds.high / Math.pow(10, bounds.oom)) * Math.pow(10, bounds.oom);
189
+ bounds.range = bounds.max - bounds.min;
190
+ bounds.step = Math.pow(10, bounds.oom);
191
+ bounds.numberOfSteps = Math.round(bounds.range / bounds.step);
192
+
193
+ // Optimize scale step by checking if subdivision is possible based on horizontalGridMinSpace
194
+ while (true) {
195
+ var length = Chartist.projectLength(svg, bounds.step / 2, bounds, options);
196
+ if (length >= options.axisY.scaleMinSpace) {
197
+ bounds.step /= 2;
198
+ } else {
199
+ break;
200
+ }
201
+ }
202
+
203
+ // Narrow min and max based on new step
204
+ newMin = bounds.min;
205
+ newMax = bounds.max;
206
+ for (i = bounds.min; i <= bounds.max; i += bounds.step) {
207
+ if (i + bounds.step < bounds.low) {
208
+ newMin += bounds.step;
209
+ }
210
+
211
+ if (i - bounds.step > bounds.high) {
212
+ newMax -= bounds.step;
213
+ }
214
+ }
215
+ bounds.min = newMin;
216
+ bounds.max = newMax;
217
+ bounds.range = bounds.max - bounds.min;
218
+
219
+ bounds.values = [];
220
+ for (i = bounds.min; i <= bounds.max; i += bounds.step) {
221
+ bounds.values.push(i);
222
+ }
223
+
224
+ return bounds;
225
+ };
226
+
227
+ Chartist.calculateLabelOffset = function (svg, data, labelClass, labelInterpolationFnc, offsetFnc) {
228
+ var offset = 0;
229
+ for (var i = 0; i < data.length; i++) {
230
+ // If interpolation function returns falsy value we skipp this label
231
+ var interpolated = labelInterpolationFnc(data[i], i);
232
+ if (!interpolated && interpolated !== 0) {
233
+ continue;
234
+ }
235
+
236
+ var label = svg.elem('text', {
237
+ dx: 0,
238
+ dy: 0
239
+ }, labelClass).text('' + interpolated);
240
+
241
+ // Check if this is the largest label and update offset
242
+ offset = Math.max(offset, offsetFnc(label._node));
243
+ // Remove label after offset Calculation
244
+ label.remove();
245
+ }
246
+
247
+ return offset;
248
+ };
249
+
250
+ // Used to iterate over array, interpolate using a interpolation function and executing callback (used for rendering)
251
+ Chartist.interpolateData = function (data, labelInterpolationFnc, callback) {
252
+ for (var index = 0; index < data.length; index++) {
253
+ // If interpolation function returns falsy value we skipp this label
254
+ var interpolatedValue = labelInterpolationFnc(data[index], index);
255
+ if (!interpolatedValue && interpolatedValue !== 0) {
256
+ continue;
257
+ }
258
+
259
+ callback(data, index, interpolatedValue);
260
+ }
261
+ };
262
+
263
+ Chartist.polarToCartesian = function (centerX, centerY, radius, angleInDegrees) {
264
+ var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
265
+
266
+ return {
267
+ x: centerX + (radius * Math.cos(angleInRadians)),
268
+ y: centerY + (radius * Math.sin(angleInRadians))
269
+ };
270
+ };
271
+
272
+ // Initialize chart drawing rectangle (area where chart is drawn) x1,y1 = bottom left / x2,y2 = top right
273
+ Chartist.createChartRect = function (svg, options, xAxisOffset, yAxisOffset) {
274
+ return {
275
+ x1: options.chartPadding + yAxisOffset,
276
+ y1: (options.height || Chartist.getHeight(svg._node)) - options.chartPadding - xAxisOffset,
277
+ x2: (options.width || Chartist.getWidth(svg._node)) - options.chartPadding,
278
+ y2: options.chartPadding,
279
+ width: function () {
280
+ return this.x2 - this.x1;
281
+ },
282
+ height: function () {
283
+ return this.y1 - this.y2;
284
+ }
285
+ };
286
+ };
287
+
288
+ Chartist.createXAxis = function (chartRect, data, grid, labels, options) {
289
+ // Create X-Axis
290
+ data.labels.forEach(function (value, index) {
291
+ var interpolatedValue = options.axisX.labelInterpolationFnc(value, index),
292
+ pos = chartRect.x1 + chartRect.width() / data.labels.length * index;
293
+
294
+ // If interpolated value returns falsey (except 0) we don't draw the grid line
295
+ if (!interpolatedValue && interpolatedValue !== 0) {
296
+ return;
297
+ }
298
+
299
+ if (options.axisX.showGrid) {
300
+ grid.elem('line', {
301
+ x1: pos,
302
+ y1: chartRect.y1,
303
+ x2: pos,
304
+ y2: chartRect.y2
305
+ }, [options.classNames.grid, options.classNames.horizontal].join(' '));
306
+ }
307
+
308
+ if (options.axisX.showLabel) {
309
+ // Use config offset for setting labels of
310
+ var label = labels.elem('text', {
311
+ dx: pos + 2
312
+ }, [options.classNames.label, options.classNames.horizontal].join(' ')).text('' + interpolatedValue);
313
+
314
+ // TODO: should use 'alignment-baseline': 'hanging' but not supported in firefox. Instead using calculated height to offset y pos
315
+ label.attr({
316
+ dy: chartRect.y1 + Chartist.getHeight(label._node) + options.axisX.offset
317
+ });
318
+ }
319
+ });
320
+ };
321
+
322
+ Chartist.createYAxis = function (chartRect, bounds, grid, labels, offset, options) {
323
+ // Create Y-Axis
324
+ bounds.values.forEach(function (value, index) {
325
+ var interpolatedValue = options.axisY.labelInterpolationFnc(value, index),
326
+ pos = chartRect.y1 - chartRect.height() / bounds.values.length * index;
327
+
328
+ // If interpolated value returns falsey (except 0) we don't draw the grid line
329
+ if (!interpolatedValue && interpolatedValue !== 0) {
330
+ return;
331
+ }
332
+
333
+ if (options.axisY.showGrid) {
334
+ grid.elem('line', {
335
+ x1: chartRect.x1,
336
+ y1: pos,
337
+ x2: chartRect.x2,
338
+ y2: pos
339
+ }, [options.classNames.grid, options.classNames.vertical].join(' '));
340
+ }
341
+
342
+ if (options.axisY.showLabel) {
343
+ labels.elem('text', {
344
+ dx: options.axisY.labelAlign === 'right' ? offset - options.axisY.offset + options.chartPadding : options.chartPadding,
345
+ dy: pos - 2,
346
+ 'text-anchor': options.axisY.labelAlign === 'right' ? 'end' : 'start'
347
+ }, [options.classNames.label, options.classNames.vertical].join(' ')).text('' + interpolatedValue);
348
+ }
349
+ });
350
+ };
351
+
352
+ Chartist.projectPoint = function (chartRect, bounds, data, index) {
353
+ return {
354
+ x: chartRect.x1 + chartRect.width() / data.length * index,
355
+ y: chartRect.y1 - chartRect.height() * (data[index] - bounds.min) / (bounds.range + bounds.step)
356
+ };
357
+ };
358
+
359
+ // Provides options handling functionality with callback for options changes triggered by responsive options and media query matches
360
+ // TODO: With multiple media queries the handleMediaChange function is triggered too many times, only need one
361
+ Chartist.optionsProvider = function (defaultOptions, options, responsiveOptions, optionsChangedCallbackFnc) {
362
+ var baseOptions = Chartist.extend(Chartist.extend({}, defaultOptions), options),
363
+ currentOptions,
364
+ mediaQueryListeners = [],
365
+ i;
366
+
367
+ function applyOptions() {
368
+ currentOptions = Chartist.extend({}, baseOptions);
369
+
370
+ if (responsiveOptions) {
371
+ for (i = 0; i < responsiveOptions.length; i++) {
372
+ var mql = window.matchMedia(responsiveOptions[i][0]);
373
+ if (mql.matches) {
374
+ currentOptions = Chartist.extend(currentOptions, responsiveOptions[i][1]);
375
+ }
376
+ }
377
+ }
378
+
379
+ optionsChangedCallbackFnc(currentOptions);
380
+ return currentOptions;
381
+ }
382
+
383
+ if (!window.matchMedia) {
384
+ throw 'window.matchMedia not found! Make sure you\'re using a polyfill.';
385
+ } else if (responsiveOptions) {
386
+
387
+ for (i = 0; i < responsiveOptions.length; i++) {
388
+ var mql = window.matchMedia(responsiveOptions[i][0]);
389
+ mql.addListener(applyOptions);
390
+ mediaQueryListeners.push(mql);
391
+ }
392
+ }
393
+
394
+ return applyOptions();
395
+ };
396
+
397
+ // http://schepers.cc/getting-to-the-point
398
+ Chartist.catmullRom2bezier = function (crp, z) {
399
+ var d = [];
400
+ for (var i = 0, iLen = crp.length; iLen - 2 * !z > i; i += 2) {
401
+ var p = [
402
+ {x: +crp[i - 2], y: +crp[i - 1]},
403
+ {x: +crp[i], y: +crp[i + 1]},
404
+ {x: +crp[i + 2], y: +crp[i + 3]},
405
+ {x: +crp[i + 4], y: +crp[i + 5]}
406
+ ];
407
+ if (z) {
408
+ if (!i) {
409
+ p[0] = {x: +crp[iLen - 2], y: +crp[iLen - 1]};
410
+ } else if (iLen - 4 === i) {
411
+ p[3] = {x: +crp[0], y: +crp[1]};
412
+ } else if (iLen - 2 === i) {
413
+ p[2] = {x: +crp[0], y: +crp[1]};
414
+ p[3] = {x: +crp[2], y: +crp[3]};
415
+ }
416
+ } else {
417
+ if (iLen - 4 === i) {
418
+ p[3] = p[2];
419
+ } else if (!i) {
420
+ p[0] = {x: +crp[i], y: +crp[i + 1]};
421
+ }
422
+ }
423
+ d.push(
424
+ [
425
+ (-p[0].x + 6 * p[1].x + p[2].x) / 6,
426
+ (-p[0].y + 6 * p[1].y + p[2].y) / 6,
427
+ (p[1].x + 6 * p[2].x - p[3].x) / 6,
428
+ (p[1].y + 6 * p[2].y - p[3].y) / 6,
429
+ p[2].x,
430
+ p[2].y
431
+ ]
432
+ );
433
+ }
434
+
435
+ return d;
436
+ };
437
+
438
+ }(window, document, Chartist));;/**
439
+ * Chartist SVG module for simple SVG DOM abstraction
440
+ *
441
+ * @module Chartist.svg
442
+ */
443
+ /* global Chartist */
444
+ (function(window, document, Chartist) {
445
+ 'use strict';
446
+
447
+ Chartist.svg = function(name, attributes, className, parent) {
448
+
449
+ var svgns = 'http://www.w3.org/2000/svg';
450
+
451
+ function attr(node, attributes) {
452
+ Object.keys(attributes).forEach(function(key) {
453
+ node.setAttribute(key, attributes[key]);
454
+ });
455
+
456
+ return node;
457
+ }
458
+
459
+ function elem(svg, name, attributes, className, parentNode) {
460
+ var node = document.createElementNS(svgns, name);
461
+ node._ctSvgElement = svg;
462
+
463
+ if(parentNode) {
464
+ parentNode.appendChild(node);
465
+ }
466
+
467
+ if(attributes) {
468
+ attr(node, attributes);
469
+ }
470
+
471
+ if(className) {
472
+ addClass(node, className);
473
+ }
474
+
475
+ return node;
476
+ }
477
+
478
+ function text(node, t) {
479
+ node.appendChild(document.createTextNode(t));
480
+ }
481
+
482
+ function empty(node) {
483
+ while (node.firstChild) {
484
+ node.removeChild(node.firstChild);
485
+ }
486
+ }
487
+
488
+ function remove(node) {
489
+ node.parentNode.removeChild(node);
490
+ }
491
+
492
+ function classes(node) {
493
+ return node.getAttribute('class') ? node.getAttribute('class').trim().split(/\s+/) : [];
494
+ }
495
+
496
+ function addClass(node, names) {
497
+ node.setAttribute('class',
498
+ classes(node)
499
+ .concat(names.trim().split(/\s+/))
500
+ .filter(function(elem, pos, self) {
501
+ return self.indexOf(elem) === pos;
502
+ }).join(' ')
503
+ );
504
+ }
505
+
506
+ function removeClass(node, names) {
507
+ var removedClasses = names.trim().split(/\s+/);
508
+
509
+ node.setAttribute('class', classes(node).filter(function(name) {
510
+ return removedClasses.indexOf(name) === -1;
511
+ }).join(' '));
512
+ }
513
+
514
+ function removeAllClasses(node) {
515
+ node.className = '';
516
+ }
517
+
518
+ return {
519
+ _node: elem(this, name, attributes, className, parent ? parent._node : undefined),
520
+ _parent: parent,
521
+ parent: function() {
522
+ return this._parent;
523
+ },
524
+ attr: function(attributes) {
525
+ attr(this._node, attributes);
526
+ return this;
527
+ },
528
+ empty: function() {
529
+ empty(this._node);
530
+ return this;
531
+ },
532
+ remove: function() {
533
+ remove(this._node);
534
+ return this;
535
+ },
536
+ elem: function(name, attributes, className) {
537
+ return Chartist.svg(name, attributes, className, this);
538
+ },
539
+ text: function(t) {
540
+ text(this._node, t);
541
+ return this;
542
+ },
543
+ addClass: function(names) {
544
+ addClass(this._node, names);
545
+ return this;
546
+ },
547
+ removeClass: function(names) {
548
+ removeClass(this._node, names);
549
+ return this;
550
+ },
551
+ removeAllClasses: function() {
552
+ removeAllClasses(this._node);
553
+ return this;
554
+ },
555
+ classes: function() {
556
+ return classes(this._node);
557
+ }
558
+ };
559
+ };
560
+
561
+ }(window, document, Chartist));;/**
562
+ * 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.
563
+ *
564
+ * For examples on how to use the line chart please check the examples of the `Chartist.Line` method.
565
+ *
566
+ * @module Chartist.Line
567
+ */
568
+ /* global Chartist */
569
+ (function(window, document, Chartist){
570
+ 'use strict';
571
+
572
+ /**
573
+ * This method creates a new line chart and returns an object handle to the internal closure. Currently you can use the returned object only for updating / redrawing the chart.
574
+ *
575
+ * @memberof Chartist.Line
576
+ * @param {string|HTMLElement} query A selector query string or directly a DOM element
577
+ * @param {object} data The data object that needs to consist of a labels and a series array
578
+ * @param {object} [options] The options object with options that override the default options. Check the examples for a detailed list.
579
+ * @param {array} [responsiveOptions] Specify an array of responsive option arrays which are a media query and options object pair => [[mediaQueryString, optionsObject],[more...]]
580
+ * @return {object} An object with a version and an update method to manually redraw the chart
581
+ * @function
582
+ *
583
+ * @example
584
+ * // These are the default options of the line chart
585
+ * var options = {
586
+ * // Options for X-Axis
587
+ * axisX: {
588
+ * // The offset of the labels to the chart area
589
+ * offset: 10,
590
+ * // If labels should be shown or not
591
+ * showLabel: true,
592
+ * // If the axis grid should be drawn or not
593
+ * showGrid: true,
594
+ * // Interpolation function that allows you to intercept the value from the axis label
595
+ * labelInterpolationFnc: function(value){return value;}
596
+ * },
597
+ * // Options for Y-Axis
598
+ * axisY: {
599
+ * // The offset of the labels to the chart area
600
+ * offset: 15,
601
+ * // If labels should be shown or not
602
+ * showLabel: true,
603
+ * // If the axis grid should be drawn or not
604
+ * showGrid: true,
605
+ * // For the Y-Axis you can set a label alignment property of right or left
606
+ * labelAlign: 'right',
607
+ * // Interpolation function that allows you to intercept the value from the axis label
608
+ * labelInterpolationFnc: function(value){return value;},
609
+ * // This value specifies the minimum height in pixel of the scale steps
610
+ * scaleMinSpace: 30
611
+ * },
612
+ * // Specify a fixed width for the chart as a string (i.e. '100px' or '50%')
613
+ * width: undefined,
614
+ * // Specify a fixed height for the chart as a string (i.e. '100px' or '50%')
615
+ * height: undefined,
616
+ * // If the line should be drawn or not
617
+ * showLine: true,
618
+ * // If dots should be drawn or not
619
+ * showPoint: true,
620
+ * // Specify if the lines should be smoothed (Catmull-Rom-Splines will be used)
621
+ * lineSmooth: true,
622
+ * // Overriding the natural low of the chart allows you to zoom in or limit the charts lowest displayed value
623
+ * low: undefined,
624
+ * // Overriding the natural high of the chart allows you to zoom in or limit the charts highest displayed value
625
+ * high: undefined,
626
+ * // Padding of the chart drawing area to the container element and labels
627
+ * chartPadding: 5,
628
+ * // Override the class names that get used to generate the SVG structure of the chart
629
+ * classNames: {
630
+ * chart: 'ct-chart-line',
631
+ * label: 'ct-label',
632
+ * series: 'ct-series',
633
+ * line: 'ct-line',
634
+ * point: 'ct-point',
635
+ * grid: 'ct-grid',
636
+ * vertical: 'ct-vertical',
637
+ * horizontal: 'ct-horizontal'
638
+ * }
639
+ * };
640
+ *
641
+ * @example
642
+ * // Create a simple line chart
643
+ * var data = {
644
+ * // A labels array that can contain any sort of values
645
+ * labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
646
+ * // Our series array that contains series objects or in this case series data arrays
647
+ * series: [
648
+ * [5, 2, 4, 2, 0]
649
+ * ]
650
+ * };
651
+ *
652
+ * // As options we currently only set a static size of 300x200 px
653
+ * var options = {
654
+ * width: '300px',
655
+ * height: '200px'
656
+ * };
657
+ *
658
+ * // 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
659
+ * Chartist.Line('.ct-chart', data, options);
660
+ *
661
+ * @example
662
+ * // Create a line chart with responsive options
663
+ *
664
+ * var data = {
665
+ * // A labels array that can contain any sort of values
666
+ * labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
667
+ * // Our series array that contains series objects or in this case series data arrays
668
+ * series: [
669
+ * [5, 2, 4, 2, 0]
670
+ * ]
671
+ * };
672
+ *
673
+ * // In adition to the regular options we specify responsive option overrides that will override the default configutation based on the matching media queries.
674
+ * var responsiveOptions = [
675
+ * ['screen and (min-width: 641px) and (max-width: 1024px)', {
676
+ * showPoint: false,
677
+ * axisX: {
678
+ * labelInterpolationFnc: function(value) {
679
+ * // Will return Mon, Tue, Wed etc. on medium screens
680
+ * return value.slice(0, 3);
681
+ * }
682
+ * }
683
+ * }],
684
+ * ['screen and (max-width: 640px)', {
685
+ * showLine: false,
686
+ * axisX: {
687
+ * labelInterpolationFnc: function(value) {
688
+ * // Will return M, T, W etc. on small screens
689
+ * return value[0];
690
+ * }
691
+ * }
692
+ * }]
693
+ * ];
694
+ *
695
+ * Chartist.Line('.ct-chart', data, null, responsiveOptions);
696
+ *
697
+ */
698
+ Chartist.Line = function (query, data, options, responsiveOptions) {
699
+
700
+ var defaultOptions = {
701
+ axisX: {
702
+ offset: 10,
703
+ showLabel: true,
704
+ showGrid: true,
705
+ labelInterpolationFnc: Chartist.noop
706
+ },
707
+ axisY: {
708
+ offset: 15,
709
+ showLabel: true,
710
+ showGrid: true,
711
+ labelAlign: 'right',
712
+ labelInterpolationFnc: Chartist.noop,
713
+ scaleMinSpace: 30
714
+ },
715
+ width: undefined,
716
+ height: undefined,
717
+ showLine: true,
718
+ showPoint: true,
719
+ lineSmooth: true,
720
+ low: undefined,
721
+ high: undefined,
722
+ chartPadding: 5,
723
+ classNames: {
724
+ chart: 'ct-chart-line',
725
+ label: 'ct-label',
726
+ series: 'ct-series',
727
+ line: 'ct-line',
728
+ point: 'ct-point',
729
+ grid: 'ct-grid',
730
+ vertical: 'ct-vertical',
731
+ horizontal: 'ct-horizontal'
732
+ }
733
+ },
734
+ currentOptions,
735
+ svg;
736
+
737
+ function createChart(options) {
738
+ var xAxisOffset,
739
+ yAxisOffset,
740
+ seriesGroups = [],
741
+ bounds,
742
+ normalizedData = Chartist.normalizeDataArray(Chartist.getDataArray(data), data.labels.length);
743
+
744
+ // Create new svg object
745
+ svg = Chartist.createSvg(query, options.width, options.height, options.classNames.chart);
746
+
747
+ // initialize bounds
748
+ bounds = Chartist.getBounds(svg, normalizedData, options);
749
+
750
+ xAxisOffset = options.axisX.offset;
751
+ if (options.axisX.showLabel) {
752
+ xAxisOffset += Chartist.calculateLabelOffset(
753
+ svg,
754
+ data.labels,
755
+ [options.classNames.label, options.classNames.horizontal].join(' '),
756
+ options.axisX.labelInterpolationFnc,
757
+ Chartist.getHeight
758
+ );
759
+ }
760
+
761
+ yAxisOffset = options.axisY.offset;
762
+ if (options.axisY.showLabel) {
763
+ yAxisOffset += Chartist.calculateLabelOffset(
764
+ svg,
765
+ bounds.values,
766
+ [options.classNames.label, options.classNames.horizontal].join(' '),
767
+ options.axisY.labelInterpolationFnc,
768
+ Chartist.getWidth
769
+ );
770
+ }
771
+
772
+ var chartRect = Chartist.createChartRect(svg, options, xAxisOffset, yAxisOffset);
773
+ // Start drawing
774
+ var labels = svg.elem('g'),
775
+ grid = svg.elem('g');
776
+
777
+ Chartist.createXAxis(chartRect, data, grid, labels, options);
778
+ Chartist.createYAxis(chartRect, bounds, grid, labels, yAxisOffset, options);
779
+
780
+ // Draw the series
781
+ // initialize series groups
782
+ for (var i = 0; i < data.series.length; i++) {
783
+ seriesGroups[i] = svg.elem('g');
784
+ // Use series class from series data or if not set generate one
785
+ seriesGroups[i].addClass([
786
+ options.classNames.series,
787
+ (data.series[i].className || options.classNames.series + '-' + Chartist.alphaNumerate(i))
788
+ ].join(' '));
789
+
790
+ var p = Chartist.projectPoint(chartRect, bounds, normalizedData[i], 0),
791
+ pathCoordinates = [p.x, p.y],
792
+ point;
793
+
794
+ // First dot we need to add before loop
795
+ if (options.showPoint) {
796
+ // Small offset for Firefox to render squares correctly
797
+ point = seriesGroups[i].elem('line', {
798
+ x1: p.x,
799
+ y1: p.y,
800
+ x2: p.x + 0.01,
801
+ y2: p.y
802
+ }, options.classNames.point);
803
+ }
804
+
805
+ // First point is created, continue with rest
806
+ for (var j = 1; j < normalizedData[i].length; j++) {
807
+ p = Chartist.projectPoint(chartRect, bounds, normalizedData[i], j);
808
+ pathCoordinates.push(p.x, p.y);
809
+
810
+ //If we should show points we need to create them now to avoid secondary loop
811
+ // Small offset for Firefox to render squares correctly
812
+ if (options.showPoint) {
813
+ point = seriesGroups[i].elem('line', {
814
+ x1: p.x,
815
+ y1: p.y,
816
+ x2: p.x + 0.01,
817
+ y2: p.y
818
+ }, options.classNames.point);
819
+ }
820
+ }
821
+
822
+ if (options.showLine) {
823
+ var svgPathString = 'M' + pathCoordinates[0] + ',' + pathCoordinates[1] + ' ';
824
+
825
+ // If smoothed path and path has more than two points then use catmull rom to bezier algorithm
826
+ if (options.lineSmooth && pathCoordinates.length > 4) {
827
+
828
+ var cr = Chartist.catmullRom2bezier(pathCoordinates);
829
+ for(var k = 0; k < cr.length; k++) {
830
+ svgPathString += 'C' + cr[k].join();
831
+ }
832
+ } else {
833
+ for(var l = 3; l < pathCoordinates.length; l += 2) {
834
+ svgPathString += 'L ' + pathCoordinates[l - 1] + ',' + pathCoordinates[l];
835
+ }
836
+ }
837
+
838
+ seriesGroups[i].elem('path', {
839
+ d: svgPathString
840
+ }, options.classNames.line);
841
+ }
842
+ }
843
+ }
844
+
845
+ // Obtain current options based on matching media queries (if responsive options are given)
846
+ // This will also register a listener that is re-creating the chart based on media changes
847
+ currentOptions = Chartist.optionsProvider(defaultOptions, options, responsiveOptions, function (changedOptions) {
848
+ currentOptions = changedOptions;
849
+ createChart(currentOptions);
850
+ });
851
+
852
+ // TODO: Currently we need to re-draw the chart on window resize. This is usually very bad and will affect performance.
853
+ // This is done because we can't work with relative coordinates when drawing the chart because SVG Path does not
854
+ // work with relative positions yet. We need to check if we can do a viewBox hack to switch to percentage.
855
+ // See http://mozilla.6506.n7.nabble.com/Specyfing-paths-with-percentages-unit-td247474.html
856
+ // Update: can be done using the above method tested here: http://codepen.io/gionkunz/pen/KDvLj
857
+ // The problem is with the label offsets that can't be converted into percentage and affecting the chart container
858
+ window.addEventListener('resize', function () {
859
+ createChart(currentOptions);
860
+ });
861
+
862
+ // Public members
863
+ return {
864
+ version: Chartist.version,
865
+ update: function () {
866
+ createChart(currentOptions);
867
+ }
868
+ };
869
+ };
870
+
871
+ }(window, document, Chartist));
872
+ ;/**
873
+ * The bar chart module of Chartist that can be used to draw unipolar or bipolar bar and grouped bar charts.
874
+ *
875
+ * @module Chartist.Bar
876
+ */
877
+ /* global Chartist */
878
+ (function(window, document, Chartist){
879
+ 'use strict';
880
+
881
+ /**
882
+ * This method creates a new bar chart and returns an object handle with delegations to the internal closure of the bar chart. You can use the returned object to redraw the chart.
883
+ *
884
+ * @memberof Chartist.Bar
885
+ * @param {string|HTMLElement} query A selector query string or directly a DOM element
886
+ * @param {object} data The data object that needs to consist of a labels and a series array
887
+ * @param {object} [options] The options object with options that override the default options. Check the examples for a detailed list.
888
+ * @param {array} [responsiveOptions] Specify an array of responsive option arrays which are a media query and options object pair => [[mediaQueryString, optionsObject],[more...]]
889
+ * @return {object} An object with a version and an update method to manually redraw the chart
890
+ * @function
891
+ *
892
+ * @example
893
+ * // These are the default options of the line chart
894
+ * var options = {
895
+ * // Options for X-Axis
896
+ * axisX: {
897
+ * // The offset of the labels to the chart area
898
+ * offset: 10,
899
+ * // If labels should be shown or not
900
+ * showLabel: true,
901
+ * // If the axis grid should be drawn or not
902
+ * showGrid: true,
903
+ * // Interpolation function that allows you to intercept the value from the axis label
904
+ * labelInterpolationFnc: function(value){return value;}
905
+ * },
906
+ * // Options for Y-Axis
907
+ * axisY: {
908
+ * // The offset of the labels to the chart area
909
+ * offset: 15,
910
+ * // If labels should be shown or not
911
+ * showLabel: true,
912
+ * // If the axis grid should be drawn or not
913
+ * showGrid: true,
914
+ * // For the Y-Axis you can set a label alignment property of right or left
915
+ * labelAlign: 'right',
916
+ * // Interpolation function that allows you to intercept the value from the axis label
917
+ * labelInterpolationFnc: function(value){return value;},
918
+ * // This value specifies the minimum height in pixel of the scale steps
919
+ * scaleMinSpace: 30
920
+ * },
921
+ * // Specify a fixed width for the chart as a string (i.e. '100px' or '50%')
922
+ * width: undefined,
923
+ * // Specify a fixed height for the chart as a string (i.e. '100px' or '50%')
924
+ * height: undefined,
925
+ * // If the line should be drawn or not
926
+ * showLine: true,
927
+ * // If dots should be drawn or not
928
+ * showPoint: true,
929
+ * // Specify if the lines should be smoothed (Catmull-Rom-Splines will be used)
930
+ * lineSmooth: true,
931
+ * // Overriding the natural low of the chart allows you to zoom in or limit the charts lowest displayed value
932
+ * low: undefined,
933
+ * // Overriding the natural high of the chart allows you to zoom in or limit the charts highest displayed value
934
+ * high: undefined,
935
+ * // Padding of the chart drawing area to the container element and labels
936
+ * chartPadding: 5,
937
+ * // Specify the distance in pixel of bars in a group
938
+ * seriesBarDistance: 15,
939
+ * // Override the class names that get used to generate the SVG structure of the chart
940
+ * classNames: {
941
+ * chart: 'ct-chart-bar',
942
+ * label: 'ct-label',
943
+ * series: 'ct-series',
944
+ * bar: 'ct-bar',
945
+ * point: 'ct-point',
946
+ * grid: 'ct-grid',
947
+ * vertical: 'ct-vertical',
948
+ * horizontal: 'ct-horizontal'
949
+ * }
950
+ * };
951
+ *
952
+ * @example
953
+ * // Create a simple bar chart
954
+ * var data = {
955
+ * labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
956
+ * series: [
957
+ * [5, 2, 4, 2, 0]
958
+ * ]
959
+ * };
960
+ *
961
+ * // 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.
962
+ * Chartist.Bar('.ct-chart', data);
963
+ *
964
+ * @example
965
+ * // This example creates a bipolar grouped bar chart where the boundaries are limitted to -10 and 10
966
+ * Chartist.Bar('.ct-chart', {
967
+ * labels: [1, 2, 3, 4, 5, 6, 7],
968
+ * series: [
969
+ * [1, 3, 2, -5, -3, 1, -6],
970
+ * [-5, -2, -4, -1, 2, -3, 1]
971
+ * ]
972
+ * }, {
973
+ * seriesBarDistance: 12,
974
+ * low: -10,
975
+ * heigh: 10
976
+ * });
977
+ *
978
+ */
979
+ Chartist.Bar = function (query, data, options, responsiveOptions) {
980
+
981
+ var defaultOptions = {
982
+ axisX: {
983
+ offset: 10,
984
+ showLabel: true,
985
+ showGrid: true,
986
+ labelInterpolationFnc: Chartist.noop
987
+ },
988
+ axisY: {
989
+ offset: 15,
990
+ showLabel: true,
991
+ showGrid: true,
992
+ labelAlign: 'right',
993
+ labelInterpolationFnc: Chartist.noop,
994
+ scaleMinSpace: 40
995
+ },
996
+ width: undefined,
997
+ height: undefined,
998
+ high: undefined,
999
+ low: undefined,
1000
+ chartPadding: 5,
1001
+ seriesBarDistance: 15,
1002
+ classNames: {
1003
+ chart: 'ct-chart-bar',
1004
+ label: 'ct-label',
1005
+ series: 'ct-series',
1006
+ bar: 'ct-bar',
1007
+ thin: 'ct-thin',
1008
+ thick: 'ct-thick',
1009
+ grid: 'ct-grid',
1010
+ vertical: 'ct-vertical',
1011
+ horizontal: 'ct-horizontal'
1012
+ }
1013
+ },
1014
+ currentOptions,
1015
+ svg;
1016
+
1017
+ function createChart(options) {
1018
+ var xAxisOffset,
1019
+ yAxisOffset,
1020
+ seriesGroups = [],
1021
+ bounds,
1022
+ normalizedData = Chartist.normalizeDataArray(Chartist.getDataArray(data), data.labels.length);
1023
+
1024
+ // Create new svg element
1025
+ svg = Chartist.createSvg(query, options.width, options.height, options.classNames.chart);
1026
+
1027
+ // initialize bounds
1028
+ bounds = Chartist.getBounds(svg, normalizedData, options, 0);
1029
+
1030
+ xAxisOffset = options.axisX.offset;
1031
+ if (options.axisX.showLabel) {
1032
+ xAxisOffset += Chartist.calculateLabelOffset(
1033
+ svg,
1034
+ data.labels,
1035
+ [options.classNames.label, options.classNames.horizontal].join(' '),
1036
+ options.axisX.labelInterpolationFnc,
1037
+ Chartist.getHeight
1038
+ );
1039
+ }
1040
+
1041
+ yAxisOffset = options.axisY.offset;
1042
+ if (options.axisY.showLabel) {
1043
+ yAxisOffset += Chartist.calculateLabelOffset(
1044
+ svg,
1045
+ bounds.values,
1046
+ [options.classNames.label, options.classNames.horizontal].join(' '),
1047
+ options.axisY.labelInterpolationFnc,
1048
+ Chartist.getWidth
1049
+ );
1050
+ }
1051
+
1052
+ var chartRect = Chartist.createChartRect(svg, options, xAxisOffset, yAxisOffset);
1053
+ // Start drawing
1054
+ var labels = svg.elem('g'),
1055
+ grid = svg.elem('g'),
1056
+ // Projected 0 point
1057
+ zeroPoint = Chartist.projectPoint(chartRect, bounds, [0], 0);
1058
+
1059
+ Chartist.createXAxis(chartRect, data, grid, labels, options);
1060
+ Chartist.createYAxis(chartRect, bounds, grid, labels, yAxisOffset, options);
1061
+
1062
+ // Draw the series
1063
+ // initialize series groups
1064
+ for (var i = 0; i < data.series.length; i++) {
1065
+ // Calculating bi-polar value of index for seriesOffset. For i = 0..4 biPol will be -1.5, -0.5, 0.5, 1.5 etc.
1066
+ var biPol = i - (data.series.length - 1) / 2,
1067
+ // Half of the period with between vertical grid lines used to position bars
1068
+ periodHalfWidth = chartRect.width() / normalizedData[i].length / 2;
1069
+
1070
+ seriesGroups[i] = svg.elem('g');
1071
+ // Use series class from series data or if not set generate one
1072
+ seriesGroups[i].addClass([
1073
+ options.classNames.series,
1074
+ (data.series[i].className || options.classNames.series + '-' + Chartist.alphaNumerate(i))
1075
+ ].join(' '));
1076
+
1077
+ for(var j = 0; j < normalizedData[i].length; j++) {
1078
+ var p = Chartist.projectPoint(chartRect, bounds, normalizedData[i], j),
1079
+ bar;
1080
+
1081
+ // Offset to center bar between grid lines and using bi-polar offset for multiple series
1082
+ // TODO: Check if we should really be able to add classes to the series. Should be handles with SASS and semantic / specific selectors
1083
+ p.x += periodHalfWidth + (biPol * options.seriesBarDistance);
1084
+
1085
+ bar = seriesGroups[i].elem('line', {
1086
+ x1: p.x,
1087
+ y1: zeroPoint.y,
1088
+ x2: p.x,
1089
+ y2: p.y
1090
+ }, options.classNames.bar + (data.series[i].barClasses ? ' ' + data.series[i].barClasses : ''));
1091
+ }
1092
+ }
1093
+ }
1094
+
1095
+ // Obtain current options based on matching media queries (if responsive options are given)
1096
+ // This will also register a listener that is re-creating the chart based on media changes
1097
+ currentOptions = Chartist.optionsProvider(defaultOptions, options, responsiveOptions, function (changedOptions) {
1098
+ currentOptions = changedOptions;
1099
+ createChart(currentOptions);
1100
+ });
1101
+
1102
+ // TODO: Currently we need to re-draw the chart on window resize. This is usually very bad and will affect performance.
1103
+ // This is done because we can't work with relative coordinates when drawing the chart because SVG Path does not
1104
+ // work with relative positions yet. We need to check if we can do a viewBox hack to switch to percentage.
1105
+ // See http://mozilla.6506.n7.nabble.com/Specyfing-paths-with-percentages-unit-td247474.html
1106
+ // Update: can be done using the above method tested here: http://codepen.io/gionkunz/pen/KDvLj
1107
+ // The problem is with the label offsets that can't be converted into percentage and affecting the chart container
1108
+ window.addEventListener('resize', function () {
1109
+ createChart(currentOptions);
1110
+ });
1111
+
1112
+ // Public members
1113
+ return {
1114
+ version: Chartist.version,
1115
+ update: function () {
1116
+ createChart(currentOptions);
1117
+ }
1118
+ };
1119
+ };
1120
+
1121
+ }(window, document, Chartist));
1122
+ ;/**
1123
+ * The pie chart module of Chartist that can be used to draw pie, donut or gauge charts
1124
+ *
1125
+ * @module Chartist.Pie
1126
+ */
1127
+ /* global Chartist */
1128
+ (function(window, document, Chartist) {
1129
+ 'use strict';
1130
+
1131
+ /**
1132
+ * This method creates a new pie chart and returns an object that can be used to redraw the chart.
1133
+ *
1134
+ * @memberof Chartist.Pie
1135
+ * @param {string|HTMLElement} query A selector query string or directly a DOM element
1136
+ * @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.
1137
+ * @param {object} [options] The options object with options that override the default options. Check the examples for a detailed list.
1138
+ * @param {array} [responsiveOptions] Specify an array of responsive option arrays which are a media query and options object pair => [[mediaQueryString, optionsObject],[more...]]
1139
+ * @return {object} An object with a version and an update method to manually redraw the chart
1140
+ * @function
1141
+ *
1142
+ * @example
1143
+ * // Default options of the pie chart
1144
+ * var defaultOptions = {
1145
+ * // Specify a fixed width for the chart as a string (i.e. '100px' or '50%')
1146
+ * width: undefined,
1147
+ * // Specify a fixed height for the chart as a string (i.e. '100px' or '50%')
1148
+ * height: undefined,
1149
+ * // Padding of the chart drawing area to the container element and labels
1150
+ * chartPadding: 5,
1151
+ * // Override the class names that get used to generate the SVG structure of the chart
1152
+ * classNames: {
1153
+ * chart: 'ct-chart-pie',
1154
+ * series: 'ct-series',
1155
+ * slice: 'ct-slice',
1156
+ * donut: 'ct-donut',
1157
+ label: 'ct-label'
1158
+ * },
1159
+ * // The start angle of the pie chart in degrees where 0 points north. A higher value offsets the start angle clockwise.
1160
+ * startAngle: 0,
1161
+ * // 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.
1162
+ * total: undefined,
1163
+ * // If specified the donut CSS classes will be used and strokes will be drawn instead of pie slices.
1164
+ * donut: false,
1165
+ * // Specify the donut stroke width, currently done in javascript for convenience. May move to CSS styles in the future.
1166
+ * donutWidth: 60,
1167
+ * // If a label should be shown or not
1168
+ * showLabel: true,
1169
+ * // 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.
1170
+ * labelOffset: 0,
1171
+ * // An interpolation function for the label value
1172
+ * labelInterpolationFnc: function(value, index) {return value;},
1173
+ * // 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.
1174
+ * labelDirection: 'neutral'
1175
+ * };
1176
+ *
1177
+ * @example
1178
+ * // Simple pie chart example with four series
1179
+ * Chartist.Pie('.ct-chart', {
1180
+ * series: [10, 2, 4, 3]
1181
+ * });
1182
+ *
1183
+ * @example
1184
+ * // Drawing a donut chart
1185
+ * Chartist.Pie('.ct-chart', {
1186
+ * series: [10, 2, 4, 3]
1187
+ * }, {
1188
+ * donut: true
1189
+ * });
1190
+ *
1191
+ * @example
1192
+ * // Using donut, startAngle and total to draw a gauge chart
1193
+ * Chartist.Pie('.ct-chart', {
1194
+ * series: [20, 10, 30, 40]
1195
+ * }, {
1196
+ * donut: true,
1197
+ * donutWidth: 20,
1198
+ * startAngle: 270,
1199
+ * total: 200
1200
+ * });
1201
+ *
1202
+ * @example
1203
+ * // Drawing a pie chart with padding and labels that are outside the pie
1204
+ * Chartist.Pie('.ct-chart', {
1205
+ * series: [20, 10, 30, 40]
1206
+ * }, {
1207
+ * chartPadding: 30,
1208
+ * labelOffset: 50,
1209
+ * labelDirection: 'explode'
1210
+ * });
1211
+ */
1212
+ Chartist.Pie = function (query, data, options, responsiveOptions) {
1213
+
1214
+ var defaultOptions = {
1215
+ width: undefined,
1216
+ height: undefined,
1217
+ chartPadding: 5,
1218
+ classNames: {
1219
+ chart: 'ct-chart-pie',
1220
+ series: 'ct-series',
1221
+ slice: 'ct-slice',
1222
+ donut: 'ct-donut',
1223
+ label: 'ct-label'
1224
+ },
1225
+ startAngle: 0,
1226
+ total: undefined,
1227
+ donut: false,
1228
+ donutWidth: 60,
1229
+ showLabel: true,
1230
+ labelOffset: 0,
1231
+ labelInterpolationFnc: Chartist.noop,
1232
+ labelOverflow: false,
1233
+ labelDirection: 'neutral'
1234
+ },
1235
+ currentOptions,
1236
+ svg;
1237
+
1238
+ function determineAnchorPosition(center, label, direction) {
1239
+ var toTheRight = label.x > center.x;
1240
+
1241
+ if(toTheRight && direction === 'explode' ||
1242
+ !toTheRight && direction === 'implode') {
1243
+ return 'start';
1244
+ } else if(toTheRight && direction === 'implode' ||
1245
+ !toTheRight && direction === 'explode') {
1246
+ return 'end';
1247
+ } else {
1248
+ return 'middle';
1249
+ }
1250
+ }
1251
+
1252
+ function createChart(options) {
1253
+ var seriesGroups = [],
1254
+ chartRect,
1255
+ radius,
1256
+ labelRadius,
1257
+ totalDataSum,
1258
+ startAngle = options.startAngle,
1259
+ dataArray = Chartist.getDataArray(data);
1260
+
1261
+ // Create SVG.js draw
1262
+ svg = Chartist.createSvg(query, options.width, options.height, options.classNames.chart);
1263
+ // Calculate charting rect
1264
+ chartRect = Chartist.createChartRect(svg, options, 0, 0);
1265
+ // Get biggest circle radius possible within chartRect
1266
+ radius = Math.min(chartRect.width() / 2, chartRect.height() / 2);
1267
+ // Calculate total of all series to get reference value or use total reference from optional options
1268
+ totalDataSum = options.total || dataArray.reduce(function(previousValue, currentValue) {
1269
+ return previousValue + currentValue;
1270
+ }, 0);
1271
+
1272
+ // If this is a donut chart we need to adjust our radius to enable strokes to be drawn inside
1273
+ // Unfortunately this is not possible with the current SVG Spec
1274
+ // See this proposal for more details: http://lists.w3.org/Archives/Public/www-svg/2003Oct/0000.html
1275
+ radius -= options.donut ? options.donutWidth / 2 : 0;
1276
+
1277
+ // If a donut chart then the label position is at the radius, if regular pie chart it's half of the radius
1278
+ // see https://github.com/gionkunz/chartist-js/issues/21
1279
+ labelRadius = options.donut ? radius : radius / 2;
1280
+ // Add the offset to the labelRadius where a negative offset means closed to the center of the chart
1281
+ labelRadius += options.labelOffset;
1282
+
1283
+ // Calculate end angle based on total sum and current data value and offset with padding
1284
+ var center = {
1285
+ x: chartRect.x1 + chartRect.width() / 2,
1286
+ y: chartRect.y2 + chartRect.height() / 2
1287
+ };
1288
+
1289
+ // Draw the series
1290
+ // initialize series groups
1291
+ for (var i = 0; i < data.series.length; i++) {
1292
+ seriesGroups[i] = svg.elem('g');
1293
+ // Use series class from series data or if not set generate one
1294
+ seriesGroups[i].addClass([
1295
+ options.classNames.series,
1296
+ (data.series[i].className || options.classNames.series + '-' + Chartist.alphaNumerate(i))
1297
+ ].join(' '));
1298
+
1299
+ var endAngle = startAngle + dataArray[i] / totalDataSum * 360;
1300
+ // If we need to draw the arc for all 360 degrees we need to add a hack where we close the circle
1301
+ // with Z and use 359.99 degrees
1302
+ if(endAngle - startAngle === 360) {
1303
+ endAngle -= 0.01;
1304
+ }
1305
+
1306
+ var start = Chartist.polarToCartesian(center.x, center.y, radius, startAngle - (i === 0 ? 0 : 0.2)),
1307
+ end = Chartist.polarToCartesian(center.x, center.y, radius, endAngle),
1308
+ arcSweep = endAngle - startAngle <= 180 ? '0' : '1',
1309
+ d = [
1310
+ // Start at the end point from the cartesian coordinates
1311
+ 'M', end.x, end.y,
1312
+ // Draw arc
1313
+ 'A', radius, radius, 0, arcSweep, 0, start.x, start.y
1314
+ ];
1315
+
1316
+ // If regular pie chart (no donut) we add a line to the center of the circle for completing the pie
1317
+ if(options.donut === false) {
1318
+ d.push('L', center.x, center.y);
1319
+ }
1320
+
1321
+ // Create the SVG path
1322
+ // If this is a donut chart we add the donut class, otherwise just a regular slice
1323
+ var path = seriesGroups[i].elem('path', {
1324
+ d: d.join(' ')
1325
+ }, options.classNames.slice + (options.donut ? ' ' + options.classNames.donut : ''));
1326
+
1327
+ // If this is a donut, we add the stroke-width as style attribute
1328
+ if(options.donut === true) {
1329
+ path.attr({
1330
+ 'style': 'stroke-width: ' + (+options.donutWidth) + 'px'
1331
+ });
1332
+ }
1333
+
1334
+ // If we need to show labels we need to add the label for this slice now
1335
+ if(options.showLabel) {
1336
+ // Position at the labelRadius distance from center and between start and end angle
1337
+ var labelPosition = Chartist.polarToCartesian(center.x, center.y, labelRadius, startAngle + (endAngle - startAngle) / 2),
1338
+ interpolatedValue = options.labelInterpolationFnc(data.labels ? data.labels[i] : dataArray[i], i);
1339
+
1340
+ seriesGroups[i].elem('text', {
1341
+ dx: labelPosition.x,
1342
+ dy: labelPosition.y,
1343
+ 'text-anchor': determineAnchorPosition(center, labelPosition, options.labelDirection),
1344
+ text: '' + interpolatedValue
1345
+ }, options.classNames.label).text('' + interpolatedValue);
1346
+ }
1347
+
1348
+ // Set next startAngle to current endAngle. Use slight offset so there are no transparent hairline issues
1349
+ // (except for last slice)
1350
+ startAngle = endAngle;
1351
+ }
1352
+ }
1353
+
1354
+ // Obtain current options based on matching media queries (if responsive options are given)
1355
+ // This will also register a listener that is re-creating the chart based on media changes
1356
+ currentOptions = Chartist.optionsProvider(defaultOptions, options, responsiveOptions, function (changedOptions) {
1357
+ currentOptions = changedOptions;
1358
+ createChart(currentOptions);
1359
+ });
1360
+
1361
+ // TODO: Currently we need to re-draw the chart on window resize. This is usually very bad and will affect performance.
1362
+ // This is done because we can't work with relative coordinates when drawing the chart because SVG Path does not
1363
+ // work with relative positions yet. We need to check if we can do a viewBox hack to switch to percentage.
1364
+ // See http://mozilla.6506.n7.nabble.com/Specyfing-paths-with-percentages-unit-td247474.html
1365
+ // Update: can be done using the above method tested here: http://codepen.io/gionkunz/pen/KDvLj
1366
+ // The problem is with the label offsets that can't be converted into percentage and affecting the chart container
1367
+ window.addEventListener('resize', function () {
1368
+ createChart(currentOptions);
1369
+ });
1370
+
1371
+ // Public members
1372
+ return {
1373
+ version: Chartist.version,
1374
+ update: function () {
1375
+ createChart(currentOptions);
1376
+ }
1377
+ };
1378
+ };
1379
+
1380
+ }(window, document, Chartist));
1381
+
1382
+ return Chartist;
1383
+
1384
+ }));