UNPKG

react-chart-all

Version:

Simple Chart.js React component npm module. Support npm chart.js version, ES6 syntax and all chart.js types (Line, Bar, Radar, Polar Area, Pie, Doughnut).

1,344 lines (1,099 loc) 1.12 MB
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ /*! * Chart.js * http://chartjs.org/ * Version: {{ version }} * * Copyright 2015 Nick Downie * Released under the MIT license * https://github.com/nnnick/Chart.js/blob/master/LICENSE.md */ var Chart = require('./core/core.js')(); require('./core/core.helpers')(Chart); require('./core/core.element')(Chart); require('./core/core.animation')(Chart); require('./core/core.controller')(Chart); require('./core/core.datasetController')(Chart); require('./core/core.layoutService')(Chart); require('./core/core.legend')(Chart); require('./core/core.scale')(Chart); require('./core/core.scaleService')(Chart); require('./core/core.title')(Chart); require('./core/core.tooltip')(Chart); require('./controllers/controller.bar')(Chart); require('./controllers/controller.bubble')(Chart); require('./controllers/controller.doughnut')(Chart); require('./controllers/controller.line')(Chart); require('./controllers/controller.polarArea')(Chart); require('./controllers/controller.radar')(Chart); require('./scales/scale.category')(Chart); require('./scales/scale.linear')(Chart); require('./scales/scale.logarithmic')(Chart); require('./scales/scale.radialLinear')(Chart); require('./scales/scale.time')(Chart); require('./elements/element.arc')(Chart); require('./elements/element.line')(Chart); require('./elements/element.point')(Chart); require('./elements/element.rectangle')(Chart); require('./charts/Chart.Bar')(Chart); require('./charts/Chart.Bubble')(Chart); require('./charts/Chart.Doughnut')(Chart); require('./charts/Chart.Line')(Chart); require('./charts/Chart.PolarArea')(Chart); require('./charts/Chart.Radar')(Chart); require('./charts/Chart.Scatter')(Chart); window.Chart = module.exports = Chart; },{"./charts/Chart.Bar":2,"./charts/Chart.Bubble":3,"./charts/Chart.Doughnut":4,"./charts/Chart.Line":5,"./charts/Chart.PolarArea":6,"./charts/Chart.Radar":7,"./charts/Chart.Scatter":8,"./controllers/controller.bar":9,"./controllers/controller.bubble":10,"./controllers/controller.doughnut":11,"./controllers/controller.line":12,"./controllers/controller.polarArea":13,"./controllers/controller.radar":14,"./core/core.animation":15,"./core/core.controller":16,"./core/core.datasetController":17,"./core/core.element":18,"./core/core.helpers":19,"./core/core.js":20,"./core/core.layoutService":21,"./core/core.legend":22,"./core/core.scale":23,"./core/core.scaleService":24,"./core/core.title":25,"./core/core.tooltip":26,"./elements/element.arc":27,"./elements/element.line":28,"./elements/element.point":29,"./elements/element.rectangle":30,"./scales/scale.category":31,"./scales/scale.linear":32,"./scales/scale.logarithmic":33,"./scales/scale.radialLinear":34,"./scales/scale.time":35}],2:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { Chart.Bar = function(context, config) { config.type = 'bar'; return new Chart(context, config); }; }; },{}],3:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { Chart.Bubble = function(context, config) { config.type = 'bubble'; return new Chart(context, config); }; }; },{}],4:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { Chart.Doughnut = function(context, config) { config.type = 'doughnut'; return new Chart(context, config); }; }; },{}],5:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { Chart.Line = function(context, config) { config.type = 'line'; return new Chart(context, config); }; }; },{}],6:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { Chart.PolarArea = function(context, config) { config.type = 'polarArea'; return new Chart(context, config); }; }; },{}],7:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; var defaultConfig = { aspectRatio: 1 }; Chart.Radar = function(context, config) { config.options = helpers.configMerge(defaultConfig, config.options); config.type = 'radar'; return new Chart(context, config); }; }; },{}],8:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var defaultConfig = { hover: { mode: 'single' }, scales: { xAxes: [{ type: "linear", // scatter should not use a category axis position: "bottom", id: "x-axis-1" // need an ID so datasets can reference the scale }], yAxes: [{ type: "linear", position: "left", id: "y-axis-1" }] }, tooltips: { callbacks: { title: function(tooltipItems, data) { // Title doesn't make sense for scatter since we format the data as a point return ''; }, label: function(tooltipItem, data) { return '(' + tooltipItem.xLabel + ', ' + tooltipItem.yLabel + ')'; } } } }; // Register the default config for this type Chart.defaults.scatter = defaultConfig; // Scatter charts use line controllers Chart.controllers.scatter = Chart.controllers.line; Chart.Scatter = function(context, config) { config.type = 'scatter'; return new Chart(context, config); }; }; },{}],9:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; Chart.defaults.bar = { hover: { mode: "label" }, scales: { xAxes: [{ type: "category", // Specific to Bar Controller categoryPercentage: 0.8, barPercentage: 0.9, // grid line settings gridLines: { offsetGridLines: true } }], yAxes: [{ type: "linear" }] } }; Chart.controllers.bar = Chart.DatasetController.extend({ initialize: function(chart, datasetIndex) { Chart.DatasetController.prototype.initialize.call(this, chart, datasetIndex); // Use this to indicate that this is a bar dataset. this.getDataset().bar = true; }, // Get the number of datasets that display bars. We use this to correctly calculate the bar width getBarCount: function getBarCount() { var barCount = 0; helpers.each(this.chart.data.datasets, function(dataset) { if (helpers.isDatasetVisible(dataset) && dataset.bar) { ++barCount; } }); return barCount; }, addElements: function() { this.getDataset().metaData = this.getDataset().metaData || []; helpers.each(this.getDataset().data, function(value, index) { this.getDataset().metaData[index] = this.getDataset().metaData[index] || new Chart.elements.Rectangle({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); }, this); }, addElementAndReset: function(index) { this.getDataset().metaData = this.getDataset().metaData || []; var rectangle = new Chart.elements.Rectangle({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); var numBars = this.getBarCount(); this.updateElement(rectangle, index, true, numBars); this.getDataset().metaData.splice(index, 0, rectangle); }, update: function update(reset) { var numBars = this.getBarCount(); helpers.each(this.getDataset().metaData, function(rectangle, index) { this.updateElement(rectangle, index, reset, numBars); }, this); }, updateElement: function updateElement(rectangle, index, reset, numBars) { var xScale = this.getScaleForId(this.getDataset().xAxisID); var yScale = this.getScaleForId(this.getDataset().yAxisID); var yScalePoint; if (yScale.min < 0 && yScale.max < 0) { // all less than 0. use the top yScalePoint = yScale.getPixelForValue(yScale.max); } else if (yScale.min > 0 && yScale.max > 0) { yScalePoint = yScale.getPixelForValue(yScale.min); } else { yScalePoint = yScale.getPixelForValue(0); } helpers.extend(rectangle, { // Utility _chart: this.chart.chart, _xScale: xScale, _yScale: yScale, _datasetIndex: this.index, _index: index, // Desired view properties _model: { x: this.calculateBarX(index, this.index), y: reset ? yScalePoint : this.calculateBarY(index, this.index), // Tooltip label: this.chart.data.labels[index], datasetLabel: this.getDataset().label, // Appearance base: reset ? yScalePoint : this.calculateBarBase(this.index, index), width: this.calculateBarWidth(numBars), backgroundColor: rectangle.custom && rectangle.custom.backgroundColor ? rectangle.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.rectangle.backgroundColor), borderSkipped: rectangle.custom && rectangle.custom.borderSkipped ? rectangle.custom.borderSkipped : this.chart.options.elements.rectangle.borderSkipped, borderColor: rectangle.custom && rectangle.custom.borderColor ? rectangle.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.rectangle.borderColor), borderWidth: rectangle.custom && rectangle.custom.borderWidth ? rectangle.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.rectangle.borderWidth) } }); rectangle.pivot(); }, calculateBarBase: function(datasetIndex, index) { var xScale = this.getScaleForId(this.getDataset().xAxisID); var yScale = this.getScaleForId(this.getDataset().yAxisID); var base = 0; if (yScale.options.stacked) { var value = this.chart.data.datasets[datasetIndex].data[index]; if (value < 0) { for (var i = 0; i < datasetIndex; i++) { var negDS = this.chart.data.datasets[i]; if (helpers.isDatasetVisible(negDS) && negDS.yAxisID === yScale.id && negDS.bar) { base += negDS.data[index] < 0 ? negDS.data[index] : 0; } } } else { for (var j = 0; j < datasetIndex; j++) { var posDS = this.chart.data.datasets[j]; if (helpers.isDatasetVisible(posDS) && posDS.yAxisID === yScale.id && posDS.bar) { base += posDS.data[index] > 0 ? posDS.data[index] : 0; } } } return yScale.getPixelForValue(base); } base = yScale.getPixelForValue(yScale.min); if (yScale.beginAtZero || ((yScale.min <= 0 && yScale.max >= 0) || (yScale.min >= 0 && yScale.max <= 0))) { base = yScale.getPixelForValue(0, 0); //base += yScale.options.gridLines.lineWidth; } else if (yScale.min < 0 && yScale.max < 0) { // All values are negative. Use the top as the base base = yScale.getPixelForValue(yScale.max); } return base; }, getRuler: function() { var xScale = this.getScaleForId(this.getDataset().xAxisID); var yScale = this.getScaleForId(this.getDataset().yAxisID); var datasetCount = this.getBarCount(); var tickWidth = (function() { var min = xScale.getPixelForTick(1) - xScale.getPixelForTick(0); for (var i = 2; i < this.getDataset().data.length; i++) { min = Math.min(xScale.getPixelForTick(i) - xScale.getPixelForTick(i - 1), min); } return min; }).call(this); var categoryWidth = tickWidth * xScale.options.categoryPercentage; var categorySpacing = (tickWidth - (tickWidth * xScale.options.categoryPercentage)) / 2; var fullBarWidth = categoryWidth / datasetCount; var barWidth = fullBarWidth * xScale.options.barPercentage; var barSpacing = fullBarWidth - (fullBarWidth * xScale.options.barPercentage); return { datasetCount: datasetCount, tickWidth: tickWidth, categoryWidth: categoryWidth, categorySpacing: categorySpacing, fullBarWidth: fullBarWidth, barWidth: barWidth, barSpacing: barSpacing }; }, calculateBarWidth: function() { var xScale = this.getScaleForId(this.getDataset().xAxisID); var ruler = this.getRuler(); return xScale.options.stacked ? ruler.categoryWidth : ruler.barWidth; }, // Get bar index from the given dataset index accounting for the fact that not all bars are visible getBarIndex: function(datasetIndex) { var barIndex = 0; for (var j = 0; j < datasetIndex; ++j) { if (helpers.isDatasetVisible(this.chart.data.datasets[j]) && this.chart.data.datasets[j].bar) { ++barIndex; } } return barIndex; }, calculateBarX: function(index, datasetIndex) { var yScale = this.getScaleForId(this.getDataset().yAxisID); var xScale = this.getScaleForId(this.getDataset().xAxisID); var barIndex = this.getBarIndex(datasetIndex); var ruler = this.getRuler(); var leftTick = xScale.getPixelForValue(null, index, datasetIndex, this.chart.isCombo); leftTick -= this.chart.isCombo ? (ruler.tickWidth / 2) : 0; if (xScale.options.stacked) { return leftTick + (ruler.categoryWidth / 2) + ruler.categorySpacing; } return leftTick + (ruler.barWidth / 2) + ruler.categorySpacing + (ruler.barWidth * barIndex) + (ruler.barSpacing / 2) + (ruler.barSpacing * barIndex); }, calculateBarY: function(index, datasetIndex) { var xScale = this.getScaleForId(this.getDataset().xAxisID); var yScale = this.getScaleForId(this.getDataset().yAxisID); var value = this.getDataset().data[index]; if (yScale.options.stacked) { var sumPos = 0, sumNeg = 0; for (var i = 0; i < datasetIndex; i++) { var ds = this.chart.data.datasets[i]; if (helpers.isDatasetVisible(ds) && ds.bar && ds.yAxisID === yScale.id) { if (ds.data[index] < 0) { sumNeg += ds.data[index] || 0; } else { sumPos += ds.data[index] || 0; } } } if (value < 0) { return yScale.getPixelForValue(sumNeg + value); } else { return yScale.getPixelForValue(sumPos + value); } return yScale.getPixelForValue(value); } return yScale.getPixelForValue(value); }, draw: function(ease) { var easingDecimal = ease || 1; helpers.each(this.getDataset().metaData, function(rectangle, index) { var d = this.getDataset().data[index]; if (d !== null && d !== undefined && !isNaN(d)) { rectangle.transition(easingDecimal).draw(); } }, this); }, setHoverStyle: function(rectangle) { var dataset = this.chart.data.datasets[rectangle._datasetIndex]; var index = rectangle._index; rectangle._model.backgroundColor = rectangle.custom && rectangle.custom.hoverBackgroundColor ? rectangle.custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(dataset.hoverBackgroundColor, index, helpers.color(rectangle._model.backgroundColor).saturate(0.5).darken(0.1).rgbString()); rectangle._model.borderColor = rectangle.custom && rectangle.custom.hoverBorderColor ? rectangle.custom.hoverBorderColor : helpers.getValueAtIndexOrDefault(dataset.hoverBorderColor, index, helpers.color(rectangle._model.borderColor).saturate(0.5).darken(0.1).rgbString()); rectangle._model.borderWidth = rectangle.custom && rectangle.custom.hoverBorderWidth ? rectangle.custom.hoverBorderWidth : helpers.getValueAtIndexOrDefault(dataset.hoverBorderWidth, index, rectangle._model.borderWidth); }, removeHoverStyle: function(rectangle) { var dataset = this.chart.data.datasets[rectangle._datasetIndex]; var index = rectangle._index; rectangle._model.backgroundColor = rectangle.custom && rectangle.custom.backgroundColor ? rectangle.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.rectangle.backgroundColor); rectangle._model.borderColor = rectangle.custom && rectangle.custom.borderColor ? rectangle.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.rectangle.borderColor); rectangle._model.borderWidth = rectangle.custom && rectangle.custom.borderWidth ? rectangle.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.rectangle.borderWidth); } }); }; },{}],10:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; Chart.defaults.bubble = { hover: { mode: "single" }, scales: { xAxes: [{ type: "linear", // bubble should probably use a linear scale by default position: "bottom", id: "x-axis-0" // need an ID so datasets can reference the scale }], yAxes: [{ type: "linear", position: "left", id: "y-axis-0" }] }, tooltips: { callbacks: { title: function(tooltipItems, data) { // Title doesn't make sense for scatter since we format the data as a point return ''; }, label: function(tooltipItem, data) { var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || ''; var dataPoint = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]; return datasetLabel + ': (' + dataPoint.x + ', ' + dataPoint.y + ', ' + dataPoint.r + ')'; } } } }; Chart.controllers.bubble = Chart.DatasetController.extend({ addElements: function() { this.getDataset().metaData = this.getDataset().metaData || []; helpers.each(this.getDataset().data, function(value, index) { this.getDataset().metaData[index] = this.getDataset().metaData[index] || new Chart.elements.Point({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); }, this); }, addElementAndReset: function(index) { this.getDataset().metaData = this.getDataset().metaData || []; var point = new Chart.elements.Point({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); // Reset the point this.updateElement(point, index, true); // Add to the points array this.getDataset().metaData.splice(index, 0, point); }, update: function update(reset) { var points = this.getDataset().metaData; var yScale = this.getScaleForId(this.getDataset().yAxisID); var xScale = this.getScaleForId(this.getDataset().xAxisID); var scaleBase; if (yScale.min < 0 && yScale.max < 0) { scaleBase = yScale.getPixelForValue(yScale.max); } else if (yScale.min > 0 && yScale.max > 0) { scaleBase = yScale.getPixelForValue(yScale.min); } else { scaleBase = yScale.getPixelForValue(0); } // Update Points helpers.each(points, function(point, index) { this.updateElement(point, index, reset); }, this); }, updateElement: function(point, index, reset) { var yScale = this.getScaleForId(this.getDataset().yAxisID); var xScale = this.getScaleForId(this.getDataset().xAxisID); var scaleBase; if (yScale.min < 0 && yScale.max < 0) { scaleBase = yScale.getPixelForValue(yScale.max); } else if (yScale.min > 0 && yScale.max > 0) { scaleBase = yScale.getPixelForValue(yScale.min); } else { scaleBase = yScale.getPixelForValue(0); } helpers.extend(point, { // Utility _chart: this.chart.chart, _xScale: xScale, _yScale: yScale, _datasetIndex: this.index, _index: index, // Desired view properties _model: { x: reset ? xScale.getPixelForDecimal(0.5) : xScale.getPixelForValue(this.getDataset().data[index], index, this.index, this.chart.isCombo), y: reset ? scaleBase : yScale.getPixelForValue(this.getDataset().data[index], index, this.index), // Appearance radius: reset ? 0 : point.custom && point.custom.radius ? point.custom.radius : this.getRadius(this.getDataset().data[index]), backgroundColor: point.custom && point.custom.backgroundColor ? point.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.point.backgroundColor), borderColor: point.custom && point.custom.borderColor ? point.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.point.borderColor), borderWidth: point.custom && point.custom.borderWidth ? point.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.point.borderWidth), // Tooltip hitRadius: point.custom && point.custom.hitRadius ? point.custom.hitRadius : helpers.getValueAtIndexOrDefault(this.getDataset().hitRadius, index, this.chart.options.elements.point.hitRadius) } }); point._model.skip = point.custom && point.custom.skip ? point.custom.skip : (isNaN(point._model.x) || isNaN(point._model.y)); point.pivot(); }, getRadius: function(value) { return value.r || this.chart.options.elements.point.radius; }, draw: function(ease) { var easingDecimal = ease || 1; // Transition and Draw the Points helpers.each(this.getDataset().metaData, function(point, index) { point.transition(easingDecimal); point.draw(); }); }, setHoverStyle: function(point) { // Point var dataset = this.chart.data.datasets[point._datasetIndex]; var index = point._index; point._model.radius = point.custom && point.custom.hoverRadius ? point.custom.hoverRadius : (helpers.getValueAtIndexOrDefault(dataset.hoverRadius, index, this.chart.options.elements.point.hoverRadius)) + this.getRadius(this.getDataset().data[point._index]); point._model.backgroundColor = point.custom && point.custom.hoverBackgroundColor ? point.custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(dataset.hoverBackgroundColor, index, helpers.color(point._model.backgroundColor).saturate(0.5).darken(0.1).rgbString()); point._model.borderColor = point.custom && point.custom.hoverBorderColor ? point.custom.hoverBorderColor : helpers.getValueAtIndexOrDefault(dataset.hoverBorderColor, index, helpers.color(point._model.borderColor).saturate(0.5).darken(0.1).rgbString()); point._model.borderWidth = point.custom && point.custom.hoverBorderWidth ? point.custom.hoverBorderWidth : helpers.getValueAtIndexOrDefault(dataset.hoverBorderWidth, index, point._model.borderWidth); }, removeHoverStyle: function(point) { var dataset = this.chart.data.datasets[point._datasetIndex]; var index = point._index; point._model.radius = point.custom && point.custom.radius ? point.custom.radius : this.getRadius(this.getDataset().data[point._index]); point._model.backgroundColor = point.custom && point.custom.backgroundColor ? point.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.point.backgroundColor); point._model.borderColor = point.custom && point.custom.borderColor ? point.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.point.borderColor); point._model.borderWidth = point.custom && point.custom.borderWidth ? point.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.point.borderWidth); } }); }; },{}],11:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; Chart.defaults.doughnut = { animation: { //Boolean - Whether we animate the rotation of the Doughnut animateRotate: true, //Boolean - Whether we animate scaling the Doughnut from the centre animateScale: false }, aspectRatio: 1, hover: { mode: 'single' }, legendCallback: function(chart) { var text = []; text.push('<ul class="' + chart.id + '-legend">'); if (chart.data.datasets.length) { for (var i = 0; i < chart.data.datasets[0].data.length; ++i) { text.push('<li><span style="background-color:' + chart.data.datasets[0].backgroundColor[i] + '">'); if (chart.data.labels[i]) { text.push(chart.data.labels[i]); } text.push('</span></li>'); } } text.push('</ul>'); return text.join(""); }, legend: { labels: { generateLabels: function(data) { if (data.labels.length && data.datasets.length) { return data.labels.map(function(label, i) { return { text: label, fillStyle: data.datasets[0].backgroundColor[i], hidden: isNaN(data.datasets[0].data[i]), // Extra data used for toggling the correct item index: i }; }); } else { return []; } } }, onClick: function(e, legendItem) { helpers.each(this.chart.data.datasets, function(dataset) { dataset.metaHiddenData = dataset.metaHiddenData || []; var idx = legendItem.index; if (!isNaN(dataset.data[idx])) { dataset.metaHiddenData[idx] = dataset.data[idx]; dataset.data[idx] = NaN; } else if (!isNaN(dataset.metaHiddenData[idx])) { dataset.data[idx] = dataset.metaHiddenData[idx]; } }); this.chart.update(); } }, //The percentage of the chart that we cut out of the middle. cutoutPercentage: 50, //The rotation of the chart, where the first data arc begins. rotation: Math.PI * -0.5, //The total circumference of the chart. circumference: Math.PI * 2.0, // Need to override these to give a nice default tooltips: { callbacks: { title: function() { return ''; }, label: function(tooltipItem, data) { return data.labels[tooltipItem.index] + ': ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]; } } } }; Chart.defaults.pie = helpers.clone(Chart.defaults.doughnut); helpers.extend(Chart.defaults.pie, { cutoutPercentage: 0 }); Chart.controllers.doughnut = Chart.controllers.pie = Chart.DatasetController.extend({ linkScales: function() { // no scales for doughnut }, addElements: function() { this.getDataset().metaData = this.getDataset().metaData || []; helpers.each(this.getDataset().data, function(value, index) { this.getDataset().metaData[index] = this.getDataset().metaData[index] || new Chart.elements.Arc({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); }, this); }, addElementAndReset: function(index, colorForNewElement) { this.getDataset().metaData = this.getDataset().metaData || []; var arc = new Chart.elements.Arc({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); if (colorForNewElement && helpers.isArray(this.getDataset().backgroundColor)) { this.getDataset().backgroundColor.splice(index, 0, colorForNewElement); } // Reset the point this.updateElement(arc, index, true); // Add to the points array this.getDataset().metaData.splice(index, 0, arc); }, getVisibleDatasetCount: function getVisibleDatasetCount() { return helpers.where(this.chart.data.datasets, function(ds) { return helpers.isDatasetVisible(ds); }).length; }, // Get index of the dataset in relation to the visible datasets. This allows determining the inner and outer radius correctly getRingIndex: function getRingIndex(datasetIndex) { var ringIndex = 0; for (var j = 0; j < datasetIndex; ++j) { if (helpers.isDatasetVisible(this.chart.data.datasets[j])) { ++ringIndex; } } return ringIndex; }, update: function update(reset) { var availableWidth = this.chart.chartArea.right - this.chart.chartArea.left - this.chart.options.elements.arc.borderWidth; var availableHeight = this.chart.chartArea.bottom - this.chart.chartArea.top - this.chart.options.elements.arc.borderWidth; var minSize = Math.min(availableWidth, availableHeight); var offset = {x: 0, y: 0}; // If the chart's circumference isn't a full circle, calculate minSize as a ratio of the width/height of the arc if (this.chart.options.circumference && this.chart.options.circumference < Math.PI * 2.0) { var startAngle = this.chart.options.rotation % (Math.PI * 2.0); startAngle += Math.PI * 2.0 * (startAngle >= Math.PI ? -1 : startAngle < -Math.PI ? 1 : 0); var endAngle = startAngle + this.chart.options.circumference; var start = {x: Math.cos(startAngle), y: Math.sin(startAngle)}; var end = {x: Math.cos(endAngle), y: Math.sin(endAngle)}; var contains0 = (startAngle <= 0 && 0 <= endAngle) || (startAngle <= Math.PI * 2.0 && Math.PI * 2.0 <= endAngle); var contains90 = (startAngle <= Math.PI * 0.5 && Math.PI * 0.5 <= endAngle) || (startAngle <= Math.PI * 2.5 && Math.PI * 2.5 <= endAngle); var contains180 = (startAngle <= -Math.PI && -Math.PI <= endAngle) || (startAngle <= Math.PI && Math.PI <= endAngle); var contains270 = (startAngle <= -Math.PI * 0.5 && -Math.PI * 0.5 <= endAngle) || (startAngle <= Math.PI * 1.5 && Math.PI * 1.5 <= endAngle); var cutout = this.chart.options.cutoutPercentage / 100.0; var min = {x: contains180 ? -1 : Math.min(start.x * (start.x < 0 ? 1 : cutout), end.x * (end.x < 0 ? 1 : cutout)), y: contains270 ? -1 : Math.min(start.y * (start.y < 0 ? 1 : cutout), end.y * (end.y < 0 ? 1 : cutout))}; var max = {x: contains0 ? 1 : Math.max(start.x * (start.x > 0 ? 1 : cutout), end.x * (end.x > 0 ? 1 : cutout)), y: contains90 ? 1 : Math.max(start.y * (start.y > 0 ? 1 : cutout), end.y * (end.y > 0 ? 1 : cutout))}; var size = {width: (max.x - min.x) * 0.5, height: (max.y - min.y) * 0.5}; minSize = Math.min(availableWidth / size.width, availableHeight / size.height); offset = {x: (max.x + min.x) * -0.5, y: (max.y + min.y) * -0.5}; } this.chart.outerRadius = Math.max(minSize / 2, 0); this.chart.innerRadius = Math.max(this.chart.options.cutoutPercentage ? (this.chart.outerRadius / 100) * (this.chart.options.cutoutPercentage) : 1, 0); this.chart.radiusLength = (this.chart.outerRadius - this.chart.innerRadius) / this.getVisibleDatasetCount(); this.chart.offsetX = offset.x * this.chart.outerRadius; this.chart.offsetY = offset.y * this.chart.outerRadius; this.getDataset().total = 0; helpers.each(this.getDataset().data, function(value) { if (!isNaN(value)) { this.getDataset().total += Math.abs(value); } }, this); this.outerRadius = this.chart.outerRadius - (this.chart.radiusLength * this.getRingIndex(this.index)); this.innerRadius = this.outerRadius - this.chart.radiusLength; helpers.each(this.getDataset().metaData, function(arc, index) { this.updateElement(arc, index, reset); }, this); }, updateElement: function(arc, index, reset) { var centerX = (this.chart.chartArea.left + this.chart.chartArea.right) / 2; var centerY = (this.chart.chartArea.top + this.chart.chartArea.bottom) / 2; var startAngle = this.chart.options.rotation || (Math.PI * -0.5); // non reset case handled later var endAngle = this.chart.options.rotation || (Math.PI * -0.5); // non reset case handled later var circumference = reset && this.chart.options.animation.animateRotate ? 0 : this.calculateCircumference(this.getDataset().data[index]) * ((this.chart.options.circumference || (2.0 * Math.PI)) / (2.0 * Math.PI)); var innerRadius = reset && this.chart.options.animation.animateScale ? 0 : this.innerRadius; var outerRadius = reset && this.chart.options.animation.animateScale ? 0 : this.outerRadius; helpers.extend(arc, { // Utility _chart: this.chart.chart, _datasetIndex: this.index, _index: index, // Desired view properties _model: { x: centerX + this.chart.offsetX, y: centerY + this.chart.offsetY, startAngle: startAngle, endAngle: endAngle, circumference: circumference, outerRadius: outerRadius, innerRadius: innerRadius, backgroundColor: arc.custom && arc.custom.backgroundColor ? arc.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.arc.backgroundColor), hoverBackgroundColor: arc.custom && arc.custom.hoverBackgroundColor ? arc.custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().hoverBackgroundColor, index, this.chart.options.elements.arc.hoverBackgroundColor), borderWidth: arc.custom && arc.custom.borderWidth ? arc.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.arc.borderWidth), borderColor: arc.custom && arc.custom.borderColor ? arc.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.arc.borderColor), label: helpers.getValueAtIndexOrDefault(this.getDataset().label, index, this.chart.data.labels[index]) } }); // Set correct angles if not resetting if (!reset) { if (index === 0) { arc._model.startAngle = this.chart.options.rotation || (Math.PI * -0.5); } else { arc._model.startAngle = this.getDataset().metaData[index - 1]._model.endAngle; } arc._model.endAngle = arc._model.startAngle + arc._model.circumference; } arc.pivot(); }, draw: function(ease) { var easingDecimal = ease || 1; helpers.each(this.getDataset().metaData, function(arc, index) { arc.transition(easingDecimal).draw(); }); }, setHoverStyle: function(arc) { var dataset = this.chart.data.datasets[arc._datasetIndex]; var index = arc._index; arc._model.backgroundColor = arc.custom && arc.custom.hoverBackgroundColor ? arc.custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(dataset.hoverBackgroundColor, index, helpers.color(arc._model.backgroundColor).saturate(0.5).darken(0.1).rgbString()); arc._model.borderColor = arc.custom && arc.custom.hoverBorderColor ? arc.custom.hoverBorderColor : helpers.getValueAtIndexOrDefault(dataset.hoverBorderColor, index, helpers.color(arc._model.borderColor).saturate(0.5).darken(0.1).rgbString()); arc._model.borderWidth = arc.custom && arc.custom.hoverBorderWidth ? arc.custom.hoverBorderWidth : helpers.getValueAtIndexOrDefault(dataset.hoverBorderWidth, index, arc._model.borderWidth); }, removeHoverStyle: function(arc) { var dataset = this.chart.data.datasets[arc._datasetIndex]; var index = arc._index; arc._model.backgroundColor = arc.custom && arc.custom.backgroundColor ? arc.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.arc.backgroundColor); arc._model.borderColor = arc.custom && arc.custom.borderColor ? arc.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.arc.borderColor); arc._model.borderWidth = arc.custom && arc.custom.borderWidth ? arc.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.arc.borderWidth); }, calculateCircumference: function(value) { if (this.getDataset().total > 0 && !isNaN(value)) { return (Math.PI * 1.999999) * (value / this.getDataset().total); } else { return 0; } } }); }; },{}],12:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; Chart.defaults.line = { showLines: true, hover: { mode: "label" }, scales: { xAxes: [{ type: "category", id: 'x-axis-0' }], yAxes: [{ type: "linear", id: 'y-axis-0' }] } }; Chart.controllers.line = Chart.DatasetController.extend({ addElements: function() { this.getDataset().metaData = this.getDataset().metaData || []; this.getDataset().metaDataset = this.getDataset().metaDataset || new Chart.elements.Line({ _chart: this.chart.chart, _datasetIndex: this.index, _points: this.getDataset().metaData }); helpers.each(this.getDataset().data, function(value, index) { this.getDataset().metaData[index] = this.getDataset().metaData[index] || new Chart.elements.Point({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); }, this); }, addElementAndReset: function(index) { this.getDataset().metaData = this.getDataset().metaData || []; var point = new Chart.elements.Point({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); // Reset the point this.updateElement(point, index, true); // Add to the points array this.getDataset().metaData.splice(index, 0, point); // Make sure bezier control points are updated if (this.chart.options.showLines && this.chart.options.elements.line.tension !== 0) this.updateBezierControlPoints(); }, update: function update(reset) { var line = this.getDataset().metaDataset; var points = this.getDataset().metaData; var yScale = this.getScaleForId(this.getDataset().yAxisID); var xScale = this.getScaleForId(this.getDataset().xAxisID); var scaleBase; if (yScale.min < 0 && yScale.max < 0) { scaleBase = yScale.getPixelForValue(yScale.max); } else if (yScale.min > 0 && yScale.max > 0) { scaleBase = yScale.getPixelForValue(yScale.min); } else { scaleBase = yScale.getPixelForValue(0); } // Update Line if (this.chart.options.showLines) { // Utility line._scale = yScale; line._datasetIndex = this.index; // Data line._children = points; // Model line._model = { // Appearance tension: line.custom && line.custom.tension ? line.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension), backgroundColor: line.custom && line.custom.backgroundColor ? line.custom.backgroundColor : (this.getDataset().backgroundColor || this.chart.options.elements.line.backgroundColor), borderWidth: line.custom && line.custom.borderWidth ? line.custom.borderWidth : (this.getDataset().borderWidth || this.chart.options.elements.line.borderWidth), borderColor: line.custom && line.custom.borderColor ? line.custom.borderColor : (this.getDataset().borderColor || this.chart.options.elements.line.borderColor), borderCapStyle: line.custom && line.custom.borderCapStyle ? line.custom.borderCapStyle : (this.getDataset().borderCapStyle || this.chart.options.elements.line.borderCapStyle), borderDash: line.custom && line.custom.borderDash ? line.custom.borderDash : (this.getDataset().borderDash || this.chart.options.elements.line.borderDash), borderDashOffset: line.custom && line.custom.borderDashOffset ? line.custom.borderDashOffset : (this.getDataset().borderDashOffset || this.chart.options.elements.line.borderDashOffset), borderJoinStyle: line.custom && line.custom.borderJoinStyle ? line.custom.borderJoinStyle : (this.getDataset().borderJoinStyle || this.chart.options.elements.line.borderJoinStyle), fill: line.custom && line.custom.fill ? line.custom.fill : (this.getDataset().fill !== undefined ? this.getDataset().fill : this.chart.options.elements.line.fill), // Scale scaleTop: yScale.top, scaleBottom: yScale.bottom, scaleZero: scaleBase }; line.pivot(); } // Update Points helpers.each(points, function(point, index) { this.updateElement(point, index, reset); }, this); if (this.chart.options.showLines && this.chart.options.elements.line.tension !== 0) this.updateBezierControlPoints(); }, getPointBackgroundColor: function(point, index) { var backgroundColor = this.chart.options.elements.point.backgroundColor; var dataset = this.getDataset(); if (point.custom && point.custom.backgroundColor) { backgroundColor = point.custom.backgroundColor; } else if (dataset.pointBackgroundColor) { backgroundColor = helpers.getValueAtIndexOrDefault(dataset.pointBackgroundColor, index, backgroundColor); } else if (dataset.backgroundColor) { backgroundColor = dataset.backgroundColor; } return backgroundColor; }, getPointBorderColor: function(point, index) { var borderColor = this.chart.options.elements.point.borderColor; var dataset = this.getDataset(); if (point.custom && point.custom.borderColor) { borderColor = point.custom.borderColor; } else if (dataset.pointBorderColor) { borderColor = helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderColor, index, borderColor); } else if (dataset.borderColor) { borderColor = dataset.borderColor; } return borderColor; }, getPointBorderWidth: function(point, index) { var borderWidth = this.chart.options.elements.point.borderWidth; var dataset = this.getDataset(); if (point.custom && point.custom.borderWidth !== undefined) { borderWidth = point.custom.borderWidth; } else if (dataset.pointBorderWidth !== undefined) { borderWidth = helpers.getValueAtIndexOrDefault(dataset.pointBorderWidth, index, borderWidth); } else if (dataset.borderWidth !== undefined) { borderWidth = dataset.borderWidth; } return borderWidth; }, updateElement: function(point, index, reset) { var yScale = this.getScaleForId(this.getDataset().yAxisID); var xScale = this.getScaleForId(this.getDataset().xAxisID); var scaleBase; if (yScale.min < 0 && yScale.max < 0) { scaleBase = yScale.getPixelForValue(yScale.max); } else if (yScale.min > 0 && yScale.max > 0) { scaleBase = yScale.getPixelForValue(yScale.min); } else { scaleBase = yScale.getPixelForValue(0); } // Utility point._chart = this.chart.chart; point._xScale = xScale; point._yScale = yScale; point._datasetIndex = this.index; point._index = index; // Desired view properties point._model = { x: xScale.getPixelForValue(this.getDataset().data[index], index, this.index, this.chart.isCombo), y: reset ? scaleBase : this.calculatePointY(this.getDataset().data[index], index, this.index, this.chart.isCombo), // Appearance tension: point.custom && point.custom.tension ? point.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension), radius: point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().radius, index, this.chart.options.elements.point.radius), pointStyle: point.custom && point.custom.pointStyle ? point.custom.pointStyle : helpers.getValueAtIndexOrDefault(this.getDataset().pointStyle, index, this.chart.options.elements.point.pointStyle), backgroundColor: this.getPointBackgroundColor(point, index), borderColor: this.getPointBorderColor(point, index), borderWidth: this.getPointBorderWidth(point, index), // Tooltip hitRadius: point.custom && point.custom.hitRadius ? point.custom.hitRadius : helpers.getValueAtIndexOrDefault(this.getDataset().hitRadius, index, this.chart.options.elements.point.hitRadius) }; point._model.skip = point.custom && point.custom.skip ? point.custom.skip : (isNaN(point._model.x) || isNaN(point._model.y)); }, calculatePointY: function(value, index, datasetIndex, isCombo) { var xScale = this.getScaleForId(this.getDataset().xAxisID); var yScale = this.getScaleForId(this.getDataset().yAxisID); if (yScale.options.stacked) { var sumPos = 0, sumNeg = 0; for (var i = 0; i < datasetIndex; i++) { var ds = this.chart.data.datasets[i]; if (ds.type === 'line' && helpers.isDatasetVisible(ds)) { if (ds.data[index] < 0) { sumNeg += ds.data[index] || 0; } else { sumPos += ds.data[index] || 0; } } } if (value < 0) { return yScale.getPixelForValue(sumNeg + value); } else { return yScale.getPixelForValue(sumPos + value); } } return yScale.getPixelForValue(value); }, updateBezierControlPoints: function() { // Update bezier control points helpers.each(this.getDataset().metaData, function(point, index) { var controlPoints = helpers.splineCurve( helpers.previousItem(this.getDataset().metaData, index)._model, point._model, helpers.nextItem(this.getDataset().metaData, index)._model, point._model.tension ); // Prevent the bezier going outside of the bounds of the graph point._model.controlPointPreviousX = Math.max(Math.min(controlPoints.previous.x, this.chart.chartArea.right), this.chart.chartArea.left); point._model.controlPointPreviousY = Math.max(Math.min(controlPoints.previous.y, this.chart.chartArea.bottom), this.chart.chartArea.top); point._model.controlPointNextX = Math.max(Math.min(controlPoints.next.x, this.chart.chartArea.right), this.chart.chartArea.left); point._model.controlPointNextY = Math.max(Math.min(controlPoints.next.y, this.chart.chartArea.bottom), this.chart.chartArea.top); // Now pivot the point for animation point.pivot(); }, this); }, draw: function(ease) { var easingDecimal = ease || 1; // Transition Point Locations helpers.each(this.getDataset().metaData, function(point) { point.transition(easingDecimal); }); // Transition and Draw the line if (this.chart.options.showLines) this.getDataset().metaDataset.transition(easingDecimal).draw(); // Draw the points helpers.each(this.getDataset().metaData, function(point) { point.draw(); }); }, setHoverStyle: function(point) { // Point var dataset = this.chart.data.datasets[point._datasetIndex]; var index = point._index; point._model.radius = point.custom && point.custom.hoverRadius ? point.custom.hoverRadius : helpers.getValueAtIndexOrDefault(dataset.pointHoverRadius, index, this.chart.options.elements.point.hoverRadius); point._model.backgroundColor = point.custom && point.custom.hoverBackgroundColor ? point.custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(dataset.pointHoverBackgroundColor, index, helpers.color(point._model.backgroundColor).saturate(0.5).darken(0.1).rgbString()); point._model.borderColor = point.custom && point.custom.hoverBorderColor ? point.custom.hoverBorderColor : helpers.getValueAtIndexOrDefault(dataset.pointHoverBorderColor, index, helpers.color(point._model.borderColor).saturate(0.5).darken(0.1).rgbString()); point._model.borderWidth = point.custom && point.custom.hoverBorderWidth ? point.custom.hoverBorderWidth : helpers.getValueAtIndexOrDefault(dataset.pointHoverBorderWidth, index, point._model.borderWidth); }, removeHoverStyle: function(point) { var dataset = this.chart.data.datasets[point._datasetIndex]; var index = point._index; point._model.radius = point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().radius, index, this.chart.options.elements.point.radius); point._model.backgroundColor = this.getPointBackgroundColor(point, index); point._model.borderColor = this.getPointBorderColor(point, index); point._model.borderWidth = this.getPointBorderWidth(point, index); } }); }; },{}],13:[function(require,module,exports){ "use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; Chart.defaults.polarArea = { scale: { type: "radialLinear", lineArc: true // so that lines are circular }, //Boolean - Whether to animate the rotation of the chart animateRotate: true, animateScale: true, aspectRatio: 1, legendCallback: function(chart) { var text = []; text.push('<ul class="' + chart.id + '-legend">'); if (chart.data.datasets.length) { for (var i = 0; i < chart.data.datasets[0].data.length; ++i) { text.push('<li><span style="background-color:' + chart.data.datasets[0].backgroundColor[i] + '">'); if (chart.data.labels[i]) { text.push(chart.data.labels[i]); } text.push('</span></li>'); } } text.push('</ul>'); return text.join(""); }, legend: { labels: { generateLabels: function(data) { if (data.labels.length && data.datasets.length) { return data.labels.map(function(label, i) { return { text: label, fillStyle: data.datasets[0].backgroundColor[i], hidden: isNaN(data.datasets[0].data[i]), // Extra data used for toggling the correct item index: i }; }); } else { return []; } } }, onClick: function(e, legendItem) { helpers.each(this.chart.data.datasets, function(dataset) { dataset.metaHiddenData = dataset.metaHiddenData || []; var idx = legendItem.index; if (!isNaN(dataset.data[idx])) { dataset.metaHiddenData[idx] = dataset.data[idx]; dataset.data[idx] = NaN; } else if (!isNaN(dataset.metaHiddenData[idx])) { dataset.data[idx] = dataset.metaHiddenData[idx]; } }); this.chart.update(); } }, // Need to override these to give a nice default tooltips: { callbacks: { title: function() { return ''; }, label: function(tooltipItem, data) { return data.labels[tooltipItem.index] + ': ' + tooltipItem.yLabel; } } } }; Chart.controllers.polarArea = Chart.DatasetController.extend({ linkScales: function() { // no scales for doughnut }, addElements: function() { this.getDataset().metaData = this.getDataset().metaData || []; helpers.each(this.getDataset().data, function(value, index) { this.getDataset().metaData[index] = this.getDataset().metaData[index] || new Chart.elements.Arc({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); }, this); }, addElementAndReset: function(index) { this.getDataset().metaData = this.getDataset().metaData || []; var arc = new Chart.elements.Arc({ _chart: this.chart.chart, _datasetIndex: this.index, _index: index }); // Reset the point this.updateElement(arc, index, true); // Add to the points array this.getDataset().metaData.splice(index, 0, arc); }, getVisibleDatasetCount: functi