2015-05-16 04:54:01 +02:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var root = this,
|
|
|
|
Chart = root.Chart,
|
|
|
|
//Cache a local reference to Chart.helpers
|
|
|
|
helpers = Chart.helpers;
|
|
|
|
|
|
|
|
var defaultConfig = {
|
|
|
|
//Boolean - Whether we should show a stroke on each segment
|
|
|
|
segmentShowStroke: true,
|
|
|
|
|
|
|
|
//String - The colour of each segment stroke
|
|
|
|
segmentStrokeColor: "#fff",
|
|
|
|
|
|
|
|
//Number - The width of each segment stroke
|
2015-05-16 06:34:08 +02:00
|
|
|
borderWidth: 2,
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
//The percentage of the chart that we cut out of the middle.
|
2015-05-16 06:34:08 +02:00
|
|
|
cutoutPercentage: 50,
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
// The duration of animations triggered by hover events
|
|
|
|
hoverAnimationDuration: 400,
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
//String - Animation easing effect
|
2015-05-16 06:34:08 +02:00
|
|
|
animationEasing: "easeOutQuart",
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
//Boolean - Whether we animate the rotation of the Doughnut
|
|
|
|
animateRotate: true,
|
|
|
|
|
|
|
|
//Boolean - Whether we animate scaling the Doughnut from the centre
|
|
|
|
animateScale: false,
|
|
|
|
|
|
|
|
//String - A legend template
|
|
|
|
legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].backgroundColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Chart.Type.extend({
|
|
|
|
//Passing in a name registers this chart in the Chart namespace
|
|
|
|
name: "Doughnut",
|
|
|
|
//Providing a defaults will also register the deafults in the chart namespace
|
|
|
|
defaults: defaultConfig,
|
|
|
|
//Initialize is fired when the chart is initialized - Data is passed in as a parameter
|
|
|
|
//Config is automatically merged by the core of Chart.js, and is available at this.options
|
2015-05-20 15:03:22 +02:00
|
|
|
initialize: function() {
|
2015-05-16 04:54:01 +02:00
|
|
|
// Slice Type and defaults
|
|
|
|
this.Slice = Chart.Arc.extend({
|
|
|
|
_chart: this.chart,
|
|
|
|
x: this.chart.width / 2,
|
|
|
|
y: this.chart.height / 2
|
|
|
|
});
|
|
|
|
|
|
|
|
//Set up tooltip events on the chart
|
|
|
|
if (this.options.showTooltips) {
|
|
|
|
helpers.bindEvents(this, this.options.tooltipEvents, this.onHover);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new slice for each piece of data
|
2015-05-16 06:34:08 +02:00
|
|
|
this.data.metaData = [];
|
|
|
|
helpers.each(this.data.data, function(slice, index) {
|
|
|
|
var metaSlice = new this.Slice();
|
|
|
|
if (typeof slice == 'number') {
|
|
|
|
helpers.extend(metaSlice, {
|
|
|
|
value: slice
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
helpers.extend(metaSlice, slice);
|
|
|
|
}
|
|
|
|
helpers.extend(metaSlice, {
|
|
|
|
startAngle: Math.PI * 1.5,
|
|
|
|
circumference: (this.options.animateRotate) ? 0 : this.calculateCircumference(metaSlice.value),
|
|
|
|
outerRadius: (this.options.animateScale) ? 0 : this.outerRadius,
|
|
|
|
innerRadius: (this.options.animateScale) ? 0 : (this.outerRadius / 100) * this.options.percentageInnerCutout,
|
|
|
|
});
|
|
|
|
if (!metaSlice.backgroundColor) {
|
|
|
|
slice.backgroundColor = 'hsl(' + (360 * index / data.length) + ', 100%, 50%)';
|
|
|
|
}
|
|
|
|
metaSlice.save();
|
|
|
|
this.data.metaData.push(metaSlice);
|
2015-05-16 04:54:01 +02:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Create tooltip instance exclusively for this chart with some defaults.
|
|
|
|
this.tooltip = new Chart.Tooltip({
|
|
|
|
_chart: this.chart,
|
|
|
|
_data: this.data,
|
|
|
|
_options: this.options,
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.update();
|
|
|
|
},
|
2015-05-16 06:34:08 +02:00
|
|
|
onHover: function(e) {
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
// If exiting chart
|
|
|
|
if (e.type == 'mouseout') {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastActive = this.lastActive || [];
|
|
|
|
|
|
|
|
// Find Active Elements
|
2015-05-16 06:34:08 +02:00
|
|
|
this.active = this.getSliceAtEvent(e);
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
// On Hover hook
|
|
|
|
if (this.options.onHover) {
|
|
|
|
this.options.onHover.call(this, this.active);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove styling for last active (even if it may still be active)
|
|
|
|
if (this.lastActive.length) {
|
2015-05-16 06:34:08 +02:00
|
|
|
this.lastActive[0].backgroundColor = this.data.data[this.lastActive[0]._index].backgroundColor;
|
2015-05-16 04:54:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Built in hover styling
|
|
|
|
if (this.active.length && this.options.hoverMode) {
|
2015-05-16 06:45:17 +02:00
|
|
|
this.active[0].backgroundColor = this.data.data[this.active[0]._index].hoverBackgroundColor || helpers.color(this.data.data[this.active[0]._index].backgroundColor).saturate(0.5).darken(0.35).rgbString();
|
2015-05-16 04:54:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Built in Tooltips
|
|
|
|
if (this.options.showTooltips) {
|
|
|
|
|
|
|
|
// The usual updates
|
|
|
|
this.tooltip.initialize();
|
|
|
|
|
|
|
|
// Active
|
|
|
|
if (this.active.length) {
|
|
|
|
helpers.extend(this.tooltip, {
|
|
|
|
opacity: 1,
|
|
|
|
_active: this.active,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.tooltip.update();
|
|
|
|
} else {
|
|
|
|
// Inactive
|
|
|
|
helpers.extend(this.tooltip, {
|
|
|
|
opacity: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hover animations
|
|
|
|
this.tooltip.pivot();
|
|
|
|
|
|
|
|
if (!this.animating) {
|
|
|
|
var changed;
|
|
|
|
|
|
|
|
helpers.each(this.active, function(element, index) {
|
|
|
|
if (element !== this.lastActive[index]) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// If entering, leaving, or changing elements, animate the change via pivot
|
|
|
|
if ((!this.lastActive.length && this.active.length) ||
|
|
|
|
(this.lastActive.length && !this.active.length) ||
|
|
|
|
(this.lastActive.length && this.active.length && changed)) {
|
|
|
|
|
|
|
|
this.stop();
|
|
|
|
this.render(this.options.hoverAnimationDuration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember Last Active
|
|
|
|
this.lastActive = this.active;
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
2015-05-16 06:34:08 +02:00
|
|
|
getSliceAtEvent: function(e) {
|
|
|
|
var elements = [];
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
var location = helpers.getRelativePosition(e);
|
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
helpers.each(this.data.metaData, function(slice, index) {
|
|
|
|
if (slice.inRange(location.x, location.y)) elements.push(slice);
|
2015-05-16 04:54:01 +02:00
|
|
|
}, this);
|
2015-05-16 06:34:08 +02:00
|
|
|
return elements;
|
2015-05-16 04:54:01 +02:00
|
|
|
},
|
|
|
|
calculateCircumference: function(value) {
|
|
|
|
if (this.total > 0) {
|
|
|
|
return (Math.PI * 2) * (value / this.total);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update: function() {
|
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
// Calc Total
|
|
|
|
this.total = 0;
|
|
|
|
helpers.each(this.data.data, function(slice) {
|
|
|
|
this.total += Math.abs(slice.value);
|
|
|
|
}, this);
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
this.outerRadius = (helpers.min([this.chart.width, this.chart.height]) - this.options.borderWidth / 2) / 2;
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
// Map new data to data points
|
2015-05-16 06:34:08 +02:00
|
|
|
helpers.each(this.data.metaData, function(slice, index) {
|
|
|
|
|
|
|
|
var datapoint = this.data.data[index];
|
|
|
|
|
|
|
|
helpers.extend(slice, {
|
|
|
|
_index: index,
|
2015-05-16 04:54:01 +02:00
|
|
|
x: this.chart.width / 2,
|
|
|
|
y: this.chart.height / 2,
|
2015-05-16 06:34:08 +02:00
|
|
|
value: datapoint.value,
|
|
|
|
label: datapoint.label,
|
|
|
|
circumference: this.calculateCircumference(datapoint.value),
|
|
|
|
outerRadius: this.outerRadius,
|
|
|
|
innerRadius: (this.outerRadius / 100) * this.options.cutoutPercentage,
|
|
|
|
|
|
|
|
backgroundColor: datapoint.backgroundColor,
|
|
|
|
hoverBackgroundColor: datapoint.hoverBackgroundColor || datapoint.backgroundColor,
|
|
|
|
borderWidth: this.options.borderWidth,
|
2015-05-16 04:54:01 +02:00
|
|
|
borderColor: this.options.segmentStrokeColor,
|
|
|
|
});
|
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
helpers.extend(slice, {
|
|
|
|
endAngle: slice.startAngle + slice.circumference,
|
|
|
|
});
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
if (index === 0) {
|
|
|
|
slice.startAngle = Math.PI * 1.5;
|
|
|
|
}
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
//Check to see if it's the last slice, if not get the next and update its start angle
|
|
|
|
if (index < this.data.data.length - 1) {
|
|
|
|
this.data.metaData[index + 1].startAngle = slice.endAngle;
|
|
|
|
}
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
slice.pivot();
|
2015-05-16 04:54:01 +02:00
|
|
|
|
|
|
|
}, this);
|
2015-05-16 06:34:08 +02:00
|
|
|
|
|
|
|
this.render();
|
2015-05-16 04:54:01 +02:00
|
|
|
},
|
|
|
|
draw: function(easeDecimal) {
|
2015-05-16 06:34:08 +02:00
|
|
|
easeDecimal = easeDecimal || 1;
|
2015-05-16 04:54:01 +02:00
|
|
|
this.clear();
|
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
helpers.each(this.data.metaData, function(slice, index) {
|
|
|
|
slice.transition(easeDecimal).draw();
|
2015-05-16 04:54:01 +02:00
|
|
|
}, this);
|
|
|
|
|
2015-05-16 06:34:08 +02:00
|
|
|
this.tooltip.transition(easeDecimal).draw();
|
2015-05-16 04:54:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Chart.types.Doughnut.extend({
|
|
|
|
name: "Pie",
|
|
|
|
defaults: helpers.merge(defaultConfig, {
|
2015-05-16 06:34:08 +02:00
|
|
|
cutoutPercentage: 0
|
2015-05-16 04:54:01 +02:00
|
|
|
})
|
|
|
|
});
|
2014-06-29 19:36:25 +02:00
|
|
|
|
2014-10-21 03:49:41 +02:00
|
|
|
}).call(this);
|