3
* Highcharts funnel module
5
* (c) 2010-2014 Torstein Honsi
7
* License: www.highcharts.com/license
10
/*global Highcharts */
11
(function (Highcharts) {
16
var defaultOptions = Highcharts.getOptions(),
17
defaultPlotOptions = defaultOptions.plotOptions,
18
seriesTypes = Highcharts.seriesTypes,
19
merge = Highcharts.merge,
20
noop = function () {},
21
each = Highcharts.each;
23
// set default options
24
defaultPlotOptions.funnel = merge(defaultPlotOptions.pie, {
25
center: ['50%', '50%'],
34
connectorColor: '#606060'
36
size: true, // to avoid adapting to data label size in Pie.drawDataLabels
40
borderColor: '#000000',
47
seriesTypes.funnel = Highcharts.extendClass(seriesTypes.pie, {
51
singularTooltips: true,
54
* Overrides the pie translate method
56
translate: function () {
59
// Get positions - either an integer or a percentage string must be given
60
getLength = function (length, relativeTo) {
61
return (/%$/).test(length) ?
62
relativeTo * parseInt(length, 10) / 100 :
69
options = series.options,
70
reversed = options.reversed,
71
plotWidth = chart.plotWidth,
72
plotHeight = chart.plotHeight,
73
cumulative = 0, // start at top
74
center = options.center,
75
centerX = getLength(center[0], plotWidth),
76
centerY = getLength(center[0], plotHeight),
77
width = getLength(options.width, plotWidth),
80
height = getLength(options.height, plotHeight),
81
neckWidth = getLength(options.neckWidth, plotWidth),
82
neckHeight = getLength(options.neckHeight, plotHeight),
83
neckY = height - neckHeight,
87
half = options.dataLabels.position === 'left' ? 1 : 0,
97
// Return the width at a specific y coordinate
98
series.getWidthAt = getWidthAt = function (y) {
99
return y > height - neckHeight || height === neckHeight ?
101
neckWidth + (width - neckWidth) * ((height - neckHeight - y) / (height - neckHeight));
103
series.getX = function (y, half) {
104
return centerX + (half ? -1 : 1) * ((getWidthAt(reversed ? plotHeight - y : y) / 2) + options.dataLabels.distance);
108
series.center = [centerX, centerY, height];
109
series.centerX = centerX;
112
* Individual point coordinate naming:
114
* x1,y1 _________________ x2,y1
120
* x3,y3 _________ x4,y3
122
* Additional for the base of the neck:
127
* x3,y5 _________ x4,y5
134
each(data, function (point) {
138
each(data, function (point) {
139
// set start and end positions
141
fraction = sum ? point.y / sum : 0;
142
y1 = centerY - height / 2 + cumulative * height;
143
y3 = y1 + fraction * height;
144
//tempWidth = neckWidth + (width - neckWidth) * ((height - neckHeight - y1) / (height - neckHeight));
145
tempWidth = getWidthAt(y1);
146
x1 = centerX - tempWidth / 2;
148
tempWidth = getWidthAt(y3);
149
x3 = centerX - tempWidth / 2;
152
// the entire point is within the neck
154
x1 = x3 = centerX - neckWidth / 2;
155
x2 = x4 = centerX + neckWidth / 2;
157
// the base of the neck
158
} else if (y3 > neckY) {
161
tempWidth = getWidthAt(neckY);
162
x3 = centerX - tempWidth / 2;
171
y5 = (y5 ? height - y5 : null);
182
path.push(x4, y5, x3, y5);
184
path.push(x3, y3, 'Z');
186
// prepare for using shared dr
187
point.shapeType = 'path';
188
point.shapeArgs = { d: path };
191
// for tooltips and data labels
192
point.percentage = fraction * 100;
193
point.plotX = centerX;
194
point.plotY = (y1 + (y5 || y3)) / 2;
196
// Placement of tooltips and data labels
202
// Slice is a noop on funnel points
205
// Mimicking pie data label placement logic
208
cumulative += fraction;
212
* Draw a single point (wedge)
213
* @param {Object} point The point object
214
* @param {Object} color The color of the point
215
* @param {Number} brightness The brightness relative to the color
217
drawPoints: function () {
219
options = series.options,
220
chart = series.chart,
221
renderer = chart.renderer;
223
each(series.data, function (point) {
225
var graphic = point.graphic,
226
shapeArgs = point.shapeArgs;
228
if (!graphic) { // Create the shapes
229
point.graphic = renderer.path(shapeArgs).
232
stroke: options.borderColor,
233
'stroke-width': options.borderWidth
237
} else { // Update the shapes
238
graphic.animate(shapeArgs);
244
* Funnel items don't have angles (#2289)
246
sortByAngle: function (points) {
247
points.sort(function (a, b) {
248
return a.plotY - b.plotY;
253
* Extend the pie data label method
255
drawDataLabels: function () {
256
var data = this.data,
257
labelDistance = this.options.dataLabels.distance,
265
// In the original pie label anticollision logic, the slots are distributed
266
// from one labelDistance above to one labelDistance below the pie. In funnels
267
// we don't want this.
268
this.center[2] -= 2 * labelDistance;
270
// Set the label position array for each point.
273
leftSide = point.half;
274
sign = leftSide ? 1 : -1;
276
x = this.getX(y, leftSide);
278
// set the anchor point for data labels
280
0, // first break of connector
282
x + (labelDistance - 5) * sign, // second break, right outside point shape
284
x + labelDistance * sign, // landing point for connector
286
leftSide ? 'right' : 'left', // alignment
291
seriesTypes.pie.prototype.drawDataLabels.call(this);
297
* Pyramid series type.
298
* A pyramid series is a special type of funnel, without neck and reversed by default.
300
defaultOptions.plotOptions.pyramid = Highcharts.merge(defaultOptions.plotOptions.funnel, {
305
Highcharts.seriesTypes.pyramid = Highcharts.extendClass(Highcharts.seriesTypes.funnel, {