Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ef816e

Browse files
committed
DRYing up tick logic
1 parent 37780bd commit 7ef816e

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

src/traces/parcoords/parcoords.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ var Lib = require('../../lib');
1414
var d3 = require('d3');
1515
var Drawing = require('../../components/drawing');
1616

17-
1817
function keyFun(d) {return d.key;}
1918

2019
function repeat(d) {return [d];}
@@ -65,23 +64,27 @@ function ordinalScaleSnap(scale, v) {
6564
return a[a.length - 1];
6665
}
6766

67+
function toText(formatter, texts) {
68+
return function(v, i) {
69+
if(texts) {
70+
var text = texts[i];
71+
if(text === null || text === undefined) {
72+
return formatter(v);
73+
} else {
74+
return text;
75+
}
76+
} else {
77+
return formatter(v);
78+
}
79+
};
80+
}
81+
6882
function domainScale(height, padding, dimension) {
6983
var extent = dimensionExtent(dimension);
70-
var ticktext = dimension.ticktext;
84+
var texts = dimension.ticktext;
7185
return dimension.tickvals ?
7286
d3.scale.ordinal()
73-
.domain(dimension.tickvals.map(function(v, i) {
74-
if(ticktext) {
75-
var text = ticktext[i];
76-
if(text === undefined || text === null) {
77-
return d3.format(dimension.tickformat)(v);
78-
} else {
79-
return text;
80-
}
81-
} else {
82-
return d3.format(dimension.tickformat)(v);
83-
}
84-
}))
87+
.domain(dimension.tickvals.map(toText(d3.format(dimension.tickformat), texts)))
8588
.range(dimension.tickvals
8689
.map(function(d) {return (d - extent[0]) / (extent[1] - extent[0]);})
8790
.map(function(d) {return (height - padding + d * (padding - (height - padding)));})) :
@@ -569,18 +572,7 @@ module.exports = function(root, svg, styledData, layout, callbacks) {
569572
.outerTickSize(2)
570573
.ticks(wantedTickCount, d.tickFormat) // works for continuous scales only...
571574
.tickValues(d.ordinal ? // and this works for ordinal scales
572-
sdom.map(function(dd, i) {
573-
if(texts) {
574-
var text = texts[i];
575-
if(text === null || text === undefined) {
576-
return dd;
577-
} else {
578-
return text;
579-
}
580-
} else {
581-
return dd;
582-
}
583-
}) :
575+
sdom.map(toText(Lib.identity), texts) :
584576
null)
585577
.tickFormat(d.ordinal ? function(d) {return d;} : null)
586578
.scale(scale));

0 commit comments

Comments
 (0)