Commit 444f2b5f authored by Masayuki Tanaka's avatar Masayuki Tanaka

Fix x tick format for categorized axis

parent 8ed33531
...@@ -2069,7 +2069,9 @@ ...@@ -2069,7 +2069,9 @@
}; };
function categoryAxis() { function categoryAxis() {
var scale = d3.scale.linear(), orient = "bottom", tickMajorSize = 6, /*tickMinorSize = 6,*/ tickEndSize = 6, tickPadding = 3, tickCentered = false, tickTextNum = 10, tickOffset = 0, categories = []; var scale = d3.scale.linear(), orient = "bottom";
var tickMajorSize = 6, /*tickMinorSize = 6,*/ tickEndSize = 6, tickPadding = 3, tickCentered = false, tickTextNum = 10, tickOffset = 0, tickFormat = null;
var categories = [];
function axisX(selection, x) { function axisX(selection, x) {
selection.attr("transform", function (d) { selection.attr("transform", function (d) {
return "translate(" + (x(d) + tickOffset) + ", 0)"; return "translate(" + (x(d) + tickOffset) + ", 0)";
...@@ -2100,6 +2102,10 @@ ...@@ -2100,6 +2102,10 @@
function category(i) { function category(i) {
return i < categories.length ? categories[i] : i; return i < categories.length ? categories[i] : i;
} }
function formattedCategory(i) {
var c = category(i);
return tickFormat ? tickFormat(c) : c;
}
function axis(g) { function axis(g) {
g.each(function () { g.each(function () {
var g = d3.select(this); var g = d3.select(this);
...@@ -2135,7 +2141,7 @@ ...@@ -2135,7 +2141,7 @@
lineUpdate.attr("x1", tickX).attr("x2", tickX).attr("y2", tickMajorSize); lineUpdate.attr("x1", tickX).attr("x2", tickX).attr("y2", tickMajorSize);
textUpdate.attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding); textUpdate.attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding);
text.attr("dy", ".71em").style("text-anchor", "middle"); text.attr("dy", ".71em").style("text-anchor", "middle");
text.text(function (i) { return shouldShowTickText(ticks, i) ? category(i) : ""; }); text.text(function (i) { return shouldShowTickText(ticks, i) ? formattedCategory(i) : ""; });
pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize); pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize);
} }
...@@ -2161,7 +2167,7 @@ ...@@ -2161,7 +2167,7 @@
lineUpdate.attr("x2", -tickMajorSize).attr("y2", 0); lineUpdate.attr("x2", -tickMajorSize).attr("y2", 0);
textUpdate.attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", tickOffset); textUpdate.attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", tickOffset);
text.attr("dy", ".32em").style("text-anchor", "end"); text.attr("dy", ".32em").style("text-anchor", "end");
text.text(function (i) { return shouldShowTickText(ticks, i) ? category(i) : ""; }); text.text(function (i) { return shouldShowTickText(ticks, i) ? formattedCategory(i) : ""; });
pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize); pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize);
break; break;
} }
...@@ -2217,6 +2223,11 @@ ...@@ -2217,6 +2223,11 @@
tickTextNum = x; tickTextNum = x;
return axis; return axis;
}; };
axis.tickFormat = function (format) {
if (!arguments.length) { return tickFormat; }
tickFormat = format;
return axis;
};
axis.tickOffset = function () { axis.tickOffset = function () {
return tickOffset; return tickOffset;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment