Skip to content

Commit

Permalink
move common code to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
soumak77 committed Mar 25, 2018
1 parent 469b2fd commit 0695efb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 74 deletions.
23 changes: 22 additions & 1 deletion src/d3-renderers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,30 @@ function getValue(d, prop) {
return d.data ? d.data[prop] : d[prop];
}

function getBarLengthFactor(d) {
var bar_length_factor = 10 / (d.depth - 2);

//different bar_length factors
if (d.parent) {
if (d.parent.parent) {
if (getValue(d.parent.parent, 'id') === 'needs' || getValue(d.parent.parent, 'id') === 'values') {
bar_length_factor = 1;
}
if (d.parent.parent.parent) {
if (getValue(d.parent.parent.parent, 'id') === 'personality') {
bar_length_factor = 1;
}
}
}
}

return bar_length_factor;
}

module.exports = {
isLocatedBottom: isLocatedBottom,
arc: arc,
expandOrFoldSector: expandOrFoldSector,
getValue: getValue
getValue: getValue,
getBarLengthFactor: getBarLengthFactor
};
41 changes: 4 additions & 37 deletions src/d3-renderers/v3/personality-chart-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* global alert */
'use strict';
const d3 = require('d3');
Object.assign(d3,
Expand All @@ -24,9 +23,7 @@ const d3SvgSingleArc = require('../svg-single-arc');
const utils = require('../utils');

function renderChart(widget) {
console.debug('personality-chart-renderer: defining renderChart');
if (!widget.data) {
console.debug('personality-chart-renderer: data not defined.');
if (!widget.data || !widget.loadingDiv) {
return;
}

Expand All @@ -36,11 +33,6 @@ function renderChart(widget) {
return;
}

if (!widget.loadingDiv) {
alert('Widget is not fully initialized, cannot render BarsWidget');
return;
}

widget.switchState(1);
widget._layout();

Expand All @@ -58,23 +50,8 @@ function renderChart(widget) {
var score = widget.getScore(d);

if (!d.children) {
var bar_length_factor = 10 / (d.depth - 2);

//different bar_length factors
if (d.parent) {
if (d.parent.parent) {
if (utils.getValue(d.parent.parent, 'id') === 'needs' || utils.getValue(d.parent.parent, 'id') === 'values') {
bar_length_factor = 1;
}
if (d.parent.parent.parent)
if (utils.getValue(d.parent.parent.parent, 'id') === 'personality') bar_length_factor = 1;
} else {
console.debug(d.name + ': Parent is null!');
}
}

var inner_r = sector_bottom_pad + d.y;
var out_r = sector_bottom_pad + d.y + bar_length_factor * Math.abs(score) * d.dy;
var out_r = sector_bottom_pad + d.y + utils.getBarLengthFactor(d) * Math.abs(score) * d.dy;

var _bar = d3.svg.arc()
.startAngle(d.x)
Expand All @@ -100,16 +77,8 @@ function renderChart(widget) {
dy_init = 5 + d.dx * 20 * Math.PI;
}

var max_label_size = 13,
lable_size = 10;

if ((7.5 + 15 * Math.PI * d.dx) > max_label_size) {
lable_size = max_label_size;
}

widget._childElements.parts[widget.getUniqueId(d, 'sector_leaf_text')]
.attr('dy', dy_init)
.attr('font-size', lable_size)
.attr('text-anchor', lbl_anchor)
.attr('transform', 'translate(' + (out_r + 5) * Math.sin(d.x) + ',' + (-(out_r + 5) * Math.cos(d.x)) + ') ' + 'rotate(' + rotate + ')');
} else {
Expand Down Expand Up @@ -202,15 +171,13 @@ function renderChart(widget) {
return twoArcs;
}

var width = widget.dimW,
height = widget.dimH;
// The flower had a radius of 640 / 1.9 = 336.84 in the original.
var radius = Math.min(width, height) / 3.2;
var radius = Math.min(widget.dimW, widget.dimH) / 3.2;
var sector = twoArcsRender(radius);

// Center the graph of 'g'
widget.vis = widget.d3vis.append('g')
.attr('transform', 'translate(' + (width / 2) + ',' + height / 2 + ')')
.attr('transform', 'translate(' + (widget.dimW / 2) + ',' + widget.dimH / 2 + ')')
.append('g');

var profile = {
Expand Down
40 changes: 4 additions & 36 deletions src/d3-renderers/v4/personality-chart-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* global alert */
'use strict';
const d3 = Object.assign({},
require('d3-color'),
Expand All @@ -26,8 +25,7 @@ const d3SvgSingleArc = require('../svg-single-arc');
const utils = require('../utils');

function renderChart(widget) {
if (!widget.data) {
console.error('personality-chart-renderer: data not defined.');
if (!widget.data || !widget.loadingDiv) {
return;
}

Expand All @@ -37,11 +35,6 @@ function renderChart(widget) {
return;
}

if (!widget.loadingDiv) {
alert('Widget is not fully initialized, cannot render BarsWidget');
return;
}

widget.switchState(1);
widget._layout();

Expand All @@ -59,23 +52,8 @@ function renderChart(widget) {
var score = widget.getScore(d);

if (!d.children) {
var bar_length_factor = 10 / (d.depth - 2);

//different bar_length factors
if (d.parent) {
if (d.parent.parent) {
if (utils.getValue(d.parent.parent, 'id') === 'needs' || utils.getValue(d.parent.parent, 'id') === 'values') {
bar_length_factor = 1;
}
if (d.parent.parent.parent)
if (utils.getValue(d.parent.parent.parent, 'id') === 'personality') bar_length_factor = 1;
} else {
console.debug(d.name + ': Parent is null!');
}
}

var inner_r = sector_bottom_pad + d.y0;
var out_r = sector_bottom_pad + d.y0 + bar_length_factor * Math.abs(score) * (d.y1 - d.y0);
var out_r = sector_bottom_pad + d.y0 + utils.getBarLengthFactor(d) * Math.abs(score) * (d.y1 - d.y0);

var _bar = d3.arc()
.startAngle(d.x0)
Expand All @@ -101,16 +79,8 @@ function renderChart(widget) {
dy_init = 5 + (d.x1 - d.x0) * 20 * Math.PI;
}

var max_label_size = 13,
lable_size = 10;

if ((7.5 + 15 * Math.PI * (d.x1 - d.x0)) > max_label_size) {
lable_size = max_label_size;
}

widget._childElements.parts[widget.getUniqueId(d, 'sector_leaf_text')]
.attr('dy', dy_init)
.attr('font-size', lable_size)
.attr('text-anchor', lbl_anchor)
.attr('transform', 'translate(' + (out_r + 5) * Math.sin(d.x0) + ',' + (-(out_r + 5) * Math.cos(d.x0)) + ') ' + 'rotate(' + rotate + ')');
} else {
Expand Down Expand Up @@ -200,15 +170,13 @@ function renderChart(widget) {
return twoArcs;
}

var width = widget.dimW,
height = widget.dimH;
// The flower had a radius of 640 / 1.9 = 336.84 in the original.
var radius = Math.min(width, height) / 3.2;
var radius = Math.min(widget.dimW, widget.dimH) / 3.2;
var sector = twoArcsRender(radius);

// Center the graph of 'g'
widget.vis = widget.d3vis.append('g')
.attr('transform', 'translate(' + (width / 2) + ',' + height / 2 + ')')
.attr('transform', 'translate(' + (widget.dimW / 2) + ',' + widget.dimH / 2 + ')')
.append('g');

var profile = {
Expand Down

0 comments on commit 0695efb

Please sign in to comment.