Skip to content

Commit

Permalink
Fix #7412 (bar chart start incorrect when multiple axes exist)
Browse files Browse the repository at this point in the history
  • Loading branch information
100pah committed Mar 8, 2018
1 parent aeb5a5b commit 1da2473
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
10 changes: 5 additions & 5 deletions src/component/axis/cartesianAxisHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export function layout(gridModel, axisModel, opt) {
var grid = gridModel.coordinateSystem;
var axis = axisModel.axis;
var layout = {};
var otherAxisOnZeroOf = axis.getAxesOnZeroOf()[0];

var rawAxisPosition = axis.position;
var axisPosition = axis.onZero ? 'onZero' : rawAxisPosition;
var axisPosition = otherAxisOnZeroOf ? 'onZero' : rawAxisPosition;
var axisDim = axis.dim;

var rect = grid.getRect();
Expand All @@ -26,9 +27,8 @@ export function layout(gridModel, axisModel, opt) {
? [rectBound[2] - axisOffset, rectBound[3] + axisOffset]
: [rectBound[0] - axisOffset, rectBound[1] + axisOffset];

if (axis.onZero) {
var otherAxis = grid.getAxis(axisDim === 'x' ? 'y' : 'x', axis.onZeroAxisIndex);
var onZeroCoord = otherAxis.toGlobalCoord(otherAxis.dataToCoord(0));
if (otherAxisOnZeroOf) {
var onZeroCoord = otherAxisOnZeroOf.toGlobalCoord(otherAxisOnZeroOf.dataToCoord(0));
posBound[idx['onZero']] = Math.max(Math.min(onZeroCoord, posBound[1]), posBound[0]);
}

Expand All @@ -45,7 +45,7 @@ export function layout(gridModel, axisModel, opt) {
var dirMap = {top: -1, bottom: 1, left: -1, right: 1};

layout.labelDirection = layout.tickDirection = layout.nameDirection = dirMap[rawAxisPosition];
layout.labelOffset = axis.onZero ? posBound[idx[rawAxisPosition]] - posBound[idx['onZero']] : 0;
layout.labelOffset = otherAxisOnZeroOf ? posBound[idx[rawAxisPosition]] - posBound[idx['onZero']] : 0;

if (axisModel.get('axisTick.inside')) {
layout.tickDirection = -layout.tickDirection;
Expand Down
9 changes: 6 additions & 3 deletions src/coord/cartesian/Axis2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ Axis2D.prototype = {
* Index of axis, can be used as key
*/
index: 0,

/**
* If axis is on the zero position of the other axis
* @type {boolean}
* Implemented in <module:echarts/coord/cartesian/Grid>.
* @return {Array.<module:echarts/coord/cartesian/Axis2D>}
* If not on zero of other axis, return null/undefined.
* If no axes, return an empty array.
*/
onZero: false,
getAxesOnZeroOf: null,

/**
* Axis model
Expand Down
46 changes: 22 additions & 24 deletions src/coord/cartesian/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,44 +127,45 @@ gridProto.update = function (ecModel, api) {
};

function fixAxisOnZero(axesMap, otherAxisDim, axis) {

axis.getAxesOnZeroOf = function () {
// TODO: onZero of multiple axes.
return otherAxis ? [otherAxis] : [];
};

// onZero can not be enabled in these two situations:
// 1. When any other axis is a category axis.
// 2. When no axis is cross 0 point.
var axes = axesMap[otherAxisDim];
var otherAxes = axesMap[otherAxisDim];

var otherAxis;
var axisModel = axis.model;
var onZero = axisModel.get('axisLine.onZero');
var onZeroAxisIndex = axisModel.get('axisLine.onZeroAxisIndex');

if (!axis.onZero) {
if (!onZero) {
return;
}

var onZeroAxisIndex = axis.onZeroAxisIndex;

// If target axis is specified.
if (onZeroAxisIndex != null) {
var otherAxis = axes[onZeroAxisIndex];
if (otherAxis && canNotOnZeroToAxis(otherAxis)) {
axis.onZero = false;
if (canOnZeroToAxis(otherAxes[onZeroAxisIndex])) {
otherAxis = otherAxes[onZeroAxisIndex];
}
return;
}

for (var idx in axes) {
if (axes.hasOwnProperty(idx)) {
var otherAxis = axes[idx];
if (otherAxis && !canNotOnZeroToAxis(otherAxis)) {
onZeroAxisIndex = +idx;
break;
}
// Find the first available other axis.
for (var idx in otherAxes) {
if (otherAxes.hasOwnProperty(idx) && canOnZeroToAxis(otherAxes[idx])) {
otherAxis = otherAxes[idx];
break;
}
}

if (onZeroAxisIndex == null) {
axis.onZero = false;
}
axis.onZeroAxisIndex = onZeroAxisIndex;
}

function canNotOnZeroToAxis(axis) {
return axis.type === 'category' || axis.type === 'time' || !ifAxisCrossZero(axis);
function canOnZeroToAxis(axis) {
return axis && axis.type !== 'category' && axis.type !== 'time' && ifAxisCrossZero(axis);
}

/**
Expand Down Expand Up @@ -447,9 +448,6 @@ gridProto._initCartesian = function (gridModel, ecModel, api) {
axis.onBand = isCategory && axisModel.get('boundaryGap');
axis.inverse = axisModel.get('inverse');

axis.onZero = axisModel.get('axisLine.onZero');
axis.onZeroAxisIndex = axisModel.get('axisLine.onZeroAxisIndex');

// Inject axis into axisModel
axisModel.axis = axis;

Expand Down
5 changes: 4 additions & 1 deletion src/layout/barGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ export function layout(seriesType, ecModel, api) {
var stacked = isDimensionStacked(data, valueDim, baseDim);
var isValueAxisH = valueAxis.isHorizontal();

var valueAxisStart = (baseAxis.onZero || stacked)
var valueAxisStart = (
zrUtil.indexOf(baseAxis.getAxesOnZeroOf(), valueAxis) >= 0
|| stacked
)
? valueAxis.toGlobalCoord(valueAxis.dataToCoord(0))
: valueAxis.getGlobalExtent()[0];

Expand Down
1 change: 1 addition & 0 deletions test/lib/reset.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ body > .main {
background: #fff;
z-index: 99;
width: 300px;
max-height: 99%;
border-left: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
Expand Down

0 comments on commit 1da2473

Please sign in to comment.