Skip to content

Commit

Permalink
fix(axis): Correct tick's position implementation (#457)
Browse files Browse the repository at this point in the history
Specify axes names instead of the assumption.

Fix #380
Close #457
  • Loading branch information
netil authored Jun 21, 2018
1 parent 3682a83 commit 09547be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
19 changes: 19 additions & 0 deletions spec/internals/axis-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,5 +1120,24 @@ describe("AXIS", function() {
});
});
});

it("set options axis.rotated=true", () => {
args.axis.rotated = true;
});

it("should be rounded tick text values", () => {
const main = chart.internal.main;

["x", "y", "y2"].forEach(v => {
const pos = args.axis[v].tick.text.position;

main.selectAll(`.${CLASS[`axis${v.toUpperCase()}`]} tspan`).each(function() {
const tspan = d3.select(this);

expect(+tspan.attr("dx")).to.be.equal(pos.x);
expect(+tspan.attr("dy")).to.be.equal(pos.y + (v === "x" ? 3 : 0));
});
});
});
});
});
9 changes: 7 additions & 2 deletions src/axis/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class Axis {
.style("text-anchor", this.textAnchorForY2AxisLabel.bind(this));
}

getXAxis(scale, orient, tickFormat,
getXAxis(axisName, scale, orient, tickFormat,
tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
const $$ = this.owner;
const config = $$.config;
Expand All @@ -61,6 +61,7 @@ export default class Axis {
withOuterTick,
withoutTransition,
config,
axisName,
tickMultiline: config.axis_x_tick_multiline,
tickWidth: config.axis_x_tick_width,
tickTextRotate: withoutRotateTickText ? 0 : config.axis_x_tick_rotate,
Expand Down Expand Up @@ -92,14 +93,15 @@ export default class Axis {
return axis;
}

getYAxis(scale, orient, tickFormat, tickValues,
getYAxis(axisName, scale, orient, tickFormat, tickValues,
withOuterTick, withoutTransition, withoutRotateTickText) {
const $$ = this.owner;
const config = $$.config;
const axisParams = {
withOuterTick,
withoutTransition,
config,
axisName,
tickTextRotate: withoutRotateTickText ? 0 : config.axis_y_tick_rotate
};
const axis = bbAxis(axisParams)
Expand Down Expand Up @@ -402,6 +404,7 @@ export default class Axis {
if (/^y2?$/.test(id)) {
scale = $$[id].copy().domain($$.getYDomain(targetsToShow, id));
axis = this.getYAxis(
id,
scale,
$$[`${id}Orient`],
config[`axis_${id}_tick_format`],
Expand All @@ -413,6 +416,7 @@ export default class Axis {
} else {
scale = $$.x.copy().domain($$.getXDomain(targetsToShow));
axis = this.getXAxis(
"x",
scale,
$$.xOrient,
$$.xAxisTickFormat,
Expand All @@ -421,6 +425,7 @@ export default class Axis {
true,
true
);

this.updateXAxisTickValues(targetsToShow, axis);
}

Expand Down
25 changes: 3 additions & 22 deletions src/axis/bb.axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,6 @@ const getSizeFor1Char = node => {
return (getSizeFor1Char.size = size);
};

/**
* Get axis string by orient
* @param {String} orient Orientation string - top|bottom|left|right
* @param {Boolean} isRotate Whether chart is rotated
* @return {String} x|y|y2
* @private
*/
const getAxisByOrient = (orient, isRotate) => (
isRotate ? {
left: "x",
bottom: "y",
top: "y2",
} : {
bottom: "x",
left: "y",
right: "y2"
}
)[orient];

export default function(params = {}) {
let scale = d3ScaleLinear();
let orient = "bottom";
Expand Down Expand Up @@ -316,9 +297,9 @@ export default function(params = {}) {
const textTransform = r => (r ? `rotate(${r})` : null);
const yForText = r => (r ? 11.5 - 2.5 * (r / 15) * (r > 0 ? 1 : -1) : tickLength);

// Get axis.tick.text.position option value
const axisType = getAxisByOrient(orient, rotate);
const tickTextPos = axisType ? params.config[`axis_${axisType}_tick_text_position`] : {x: 0, y: 0};
// get the axis' tick position configuration
const tickTextPos = params.axisName && /^(x|y|y2)$/.test(params.axisName) ?
params.config[`axis_${params.axisName}_tick_text_position`] : {x: 0, y: 0};

// set <tspan>'s position
tspan
Expand Down
8 changes: 4 additions & 4 deletions src/internals/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ extend(ChartInternal.prototype, {
$$.y2AxisTickValues = $$.axis.getY2AxisTickValues();

$$.xAxis = $$.axis
.getXAxis($$.x, $$.xOrient, $$.xAxisTickFormat,
.getXAxis("x", $$.x, $$.xOrient, $$.xAxisTickFormat,
$$.xAxisTickValues, config.axis_x_tick_outer, withoutTransitionAtInit);

$$.subXAxis = $$.axis
.getXAxis($$.subX, $$.subXOrient, $$.xAxisTickFormat,
.getXAxis("subx", $$.subX, $$.subXOrient, $$.xAxisTickFormat,
$$.xAxisTickValues, config.axis_x_tick_outer);

$$.yAxis = $$.axis
.getYAxis($$.y, $$.yOrient, config.axis_y_tick_format,
.getYAxis("y", $$.y, $$.yOrient, config.axis_y_tick_format,
$$.yAxisTickValues, config.axis_y_tick_outer);

$$.y2Axis = $$.axis
.getYAxis($$.y2, $$.y2Orient, config.axis_y2_tick_format,
.getYAxis("y2", $$.y2, $$.y2Orient, config.axis_y2_tick_format,
$$.y2AxisTickValues, config.axis_y2_tick_outer);

// update for arc
Expand Down

0 comments on commit 09547be

Please sign in to comment.