From 274fccd2d6e4c8075e100426ef74c7c53b836583 Mon Sep 17 00:00:00 2001 From: Peteraa Date: Fri, 31 Oct 2014 10:59:48 +0100 Subject: [PATCH 1/2] Update d3-funnel-charts.js --- src/d3-funnel-charts.js | 56 +++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/d3-funnel-charts.js b/src/d3-funnel-charts.js index 133df2b..15feb73 100644 --- a/src/d3-funnel-charts.js +++ b/src/d3-funnel-charts.js @@ -16,10 +16,23 @@ This is used to calculate the slope, so the chart's view can be changed by changing this value */ + // Extending the input parameters to include color: + this.customColorScaleDomains = []; + this.customColorScaleRange = []; + + // Create the option for people to force the funnel sections to be of equal size, default is false + this.equalSizedFunnelSteps = typeof options.equalSizedFunnelSteps !== 'undefined' ? options.equalSizedFunnelSteps :false; + this.data = options.data; this.totalEngagement = 0; + for(var i = 0; i < this.data.length; i++){ - this.totalEngagement += this.data[i][1]; + this.totalEngagement += this.data[i][1]; + // Extending the input parameters to include color: + if (this.data[i][2] !== 'undefined') { + this.customColorScaleDomains.push(this.data[i][0]); + this.customColorScaleRange.push(this.data[i][2]); + } } this.width = typeof options.width !== 'undefined' ? options.width : DEFAULT_WIDTH; this.height = typeof options.height !== 'undefined' ? options.height : DEFAULT_HEIGHT; @@ -33,6 +46,11 @@ return this.data[ind][0]; }; + window.FunnelChart.prototype._getFunction = function (ind) { + /* Get function of a category at index 'ind' in this.data */ + return typeof this.data[ind][3] === 'function' ? this.data[ind][3] : function () { }; + }; + window.FunnelChart.prototype._getEngagementCount = function(ind){ /* Get engagement value of a category at index 'ind' in this.data */ return this.data[ind][1]; @@ -46,8 +64,14 @@ // reached end of funnel if(dataInd >= chart.data.length) return; - // math to calculate coordinates of the next base - area = chart.data[dataInd][1]*chart._totalArea/chart.totalEngagement; + if (chart.equalSizedFunnelSteps) { + // math to calculate coordinates of the next base + area = chart.totalEngagement / chart.data.length * chart._totalArea / chart.totalEngagement; + } else { + // math to calculate coordinates of the next base + area = chart.data[dataInd][1] * chart._totalArea / chart.totalEngagement; + } + prevBaseLength = prevRightX - prevLeftX; nextBaseLength = Math.sqrt((chart._slope * prevBaseLength * prevBaseLength - 4 * area)/chart._slope); nextLeftX = (prevBaseLength - nextBaseLength)/2 + prevLeftX; @@ -82,14 +106,25 @@ .x(function(d) { return d[0]; }) .y(function(d) { return d[1]; }); - // Automatically generates colors for each trapezoid in funnel - var colorScale = d3.scale.category10(); - - var paths = this._createPaths(); + + + // Color management + if (this.customColorScaleDomains.length != 0 && this.customColorScaleRange.length != 0) { + // assign the colors to the colorScale + var colorScale = d3.scale.ordinal() + .domain(this.customColorScaleDomains) + .range(this.customColorScaleRange); + } else { + // Automatically generates colors for each trapezoid in funnel + var colorScale = d3.scale.category10(); + } + + var paths = this._createPaths(); function drawTrapezoids(funnel, i){ - var trapezoid = funnelSvg + var trapezoid = funnelSvg .append('svg:path') + .on("click", function(d) { funnel._getFunction(i)(funnel._getLabel(i)); } ) .attr('d', function(d){ return funnelPath( [paths[i][0], paths[i][1], paths[i][2], @@ -108,9 +143,11 @@ .attr("d", function(d){return funnelPath(paths[i]);}) .attr("fill", function(d){return colorScale(i);}); - funnelSvg + funnelSvg .append('svg:text') + .text(funnel._getLabel(i) + ': ' + funnel._getEngagementCount(i)) + .on("click", function (d) { funnel._getFunction(i)(funnel._getLabel(i)); }) .attr("x", function(d){ return funnel.width/2; }) .attr("y", function(d){ return (paths[i][0][1] + paths[i][1][1])/2;}) // Average height of bases @@ -124,7 +161,6 @@ }); } } - drawTrapezoids(this, 0); }; })(); From b6b65a3c01b7dc7d59f8ee214827911264be1b4b Mon Sep 17 00:00:00 2001 From: Peteraa Date: Tue, 11 Nov 2014 22:02:39 +0100 Subject: [PATCH 2/2] call back function not retarded --- src/d3-funnel-charts.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/d3-funnel-charts.js b/src/d3-funnel-charts.js index 15feb73..d367a98 100644 --- a/src/d3-funnel-charts.js +++ b/src/d3-funnel-charts.js @@ -23,6 +23,10 @@ // Create the option for people to force the funnel sections to be of equal size, default is false this.equalSizedFunnelSteps = typeof options.equalSizedFunnelSteps !== 'undefined' ? options.equalSizedFunnelSteps :false; + // Create the option for people to make the trapezoids clickable. + this.callBack = (typeof options.callBack !== 'undefined') && (typeof options.callBack === 'function') ? options.callBack : function () { }; + + this.data = options.data; this.totalEngagement = 0; @@ -47,8 +51,8 @@ }; window.FunnelChart.prototype._getFunction = function (ind) { - /* Get function of a category at index 'ind' in this.data */ - return typeof this.data[ind][3] === 'function' ? this.data[ind][3] : function () { }; + /* Get call back function */ + return this.callBack; }; window.FunnelChart.prototype._getEngagementCount = function(ind){