Skip to content

Commit

Permalink
fix(domain): Fix getting Y domain min/max value
Browse files Browse the repository at this point in the history
When has multi xs, the value should be compared based on correct x Axis index value

Fix #685
Close #690
  • Loading branch information
netil committed Dec 6, 2018
1 parent 7c81905 commit 864e112
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
36 changes: 36 additions & 0 deletions spec/internals/domain-spec.js
Expand Up @@ -140,4 +140,40 @@ describe("DOMAIN", function() {
expect(domain[1]).to.be.closeTo(69, 1);
});
});

describe("Multi xs with grouped data", () => {
before(() => {
args = {
data: {
xs: { "Buy": "xBuy", "Sell": "xSell" },
columns: [
["xBuy", "2018-07-05", "2018-07-11"],
["xSell","2018-07-02","2018-07-05"],
["Buy", 33, 3],
["Sell", 7, 33]
],
type: "bar",
groups: [[ "Buy", "Sell"]]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%d/%m/%y'
}
},
y2: {
show: true
}
}
};
});

it("should set max y Axis properly", () => {
const lastTickText = +chart.$.main.selectAll(".bb-axis-y .tick tspan")
.nodes().pop().textContent;

expect(lastTickText).to.be.equal(70);
});
});
});
38 changes: 34 additions & 4 deletions src/data/data.js
Expand Up @@ -74,11 +74,33 @@ extend(ChartInternal.prototype, {
return xValues;
},

getIndexByX(x) {
/**
* Get index number based on given x Axis value
* @param {Date|Number|String} x x Axis to be compared
* @param {Array} basedX x Axis list to be based on
* @return {Number} index number
* @private
*/
getIndexByX(x, basedX) {
const $$ = this;
const data = $$.filterByX($$.data.targets, x);
const targets = $$.data.targets;
let index;

if (basedX) {
const len = basedX.length;

for (index = 0; index < len; index++) {
if (basedX[index] === +x) {
break;
}
}
} else {
const data = $$.filterByX(targets, x);

index = data.length ? data[0].index : null;
}

return data.length ? data[0].index : null;
return index;
},

getXValue(id, i) {
Expand Down Expand Up @@ -442,7 +464,11 @@ extend(ChartInternal.prototype, {
},

getValuesAsIdKeyed(targets) {
const $$ = this;
const ys = {};
const isMultipleX = $$.isMultipleX();
const xs = isMultipleX ? $$.mapTargetsToUniqueXs(targets)
.map(v => (isString(v) ? v : +v)) : null;

targets.forEach(t => {
const data = [];
Expand All @@ -455,7 +481,11 @@ extend(ChartInternal.prototype, {
} else if (isObject(value) && "high" in value) {
data.push(...Object.values(value));
} else {
data.push(value);
if (isMultipleX) {
data[$$.getIndexByX(v.x, xs)] = value;
} else {
data.push(value);
}
}
});

Expand Down
28 changes: 12 additions & 16 deletions src/internals/domain.js
Expand Up @@ -18,47 +18,43 @@ extend(ChartInternal.prototype, {

const dataGroups = config.data_groups;
const ids = $$.mapToIds(targets);
const ys = $$.getValuesAsIdKeyed(targets);

const f = isMin ? d3Min : d3Max;
const ys = $$.getValuesAsIdKeyed(targets);

if (dataGroups.length > 0) {
const hasValue = $$[`has${isMin ? "Negative" : "Positive"}ValueInTargets`](targets);
let baseId;
let idsInGroup;

for (let j = 0; (idsInGroup = dataGroups[j]); j++) {
for (let j = 0, idsInGroup; (idsInGroup = dataGroups[j]); j++) {
// Determine baseId
idsInGroup = idsInGroup.filter(v => ids.indexOf(v) >= 0);

if (idsInGroup.length === 0) {
continue;
}

baseId = idsInGroup[0];
const baseId = idsInGroup[0];
const baseAxisId = $$.axis.getId(baseId);

// Consider values
// Initialize base value. Set to 0 if not match with the condition
if (hasValue && ys[baseId]) {
const setter = isMin ? (v, i) => {
ys[baseId][i] = v < 0 ? v : 0;
} : (v, i) => {
ys[baseId][i] = v > 0 ? v : 0;
};

ys[baseId].forEach(setter);
ys[baseId] = ys[baseId].map(v => (
(isMin ? v < 0 : v > 0) ? v : 0
));
}

// Compute min
for (let k = 1, id; (id = idsInGroup[k]); k++) {
if (!ys[id]) {
continue;
}

const axisId = $$.axis.getId(id);

ys[id].forEach((v, i) => {
const val = +v;
const meetCondition = isMin ? val > 0 : val < 0;

if ($$.axis.getId(id) === $$.axis.getId(baseId) &&
ys[baseId] && !(hasValue && meetCondition)) {
if (axisId === baseAxisId && !(hasValue && meetCondition)) {
ys[baseId][i] += val;
}
});
Expand Down

0 comments on commit 864e112

Please sign in to comment.