Skip to content

Commit

Permalink
fix(data): Fix idConverter error
Browse files Browse the repository at this point in the history
Prevent data sorting with old id value

Fix #2808
  • Loading branch information
netil authored Aug 3, 2022
1 parent 18cca2c commit 98f7103
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ChartInternal/data/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default {
t.values.forEach((v, i) => (v.index = i));

// this needs to be sorted because its index and value.index is identical
$$.data.xs[t.id].sort((v1, v2) => v1 - v2);
$$.data.xs[t.id]?.sort((v1, v2) => v1 - v2);
});

// cache information about values
Expand Down
5 changes: 4 additions & 1 deletion src/ChartInternal/internals/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export default {
}

return function(d: IDataRow | IArcData | string): string {
const id: string = (d as IDataRow).id || (d as IArcData).data?.id || d as string;
const id: string = (d as IDataRow).id ||
(d as IArcData).data?.id ||
d as string;

const isLine = $$.isTypeOf(id, ["line", "spline", "step"]) || !config.data_types[id];
let color;

Expand Down
5 changes: 3 additions & 2 deletions src/ChartInternal/internals/legend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,11 @@ export default {

const getColor = id => {
const data = $$.getDataById(id);

return $$.levelColor ?
const color = $$.levelColor ?
$$.levelColor(data.values[0].value) :
$$.color(data);

return color;
};

const usePoint = config.legend_usePoint;
Expand Down
37 changes: 37 additions & 0 deletions test/internals/data-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,4 +1118,41 @@ describe("DATA", () => {
});
});
});

describe("data.idConverter", () => {
before(() => {
args = {
data: {
idConverter: function(id) {
return id ? id : "data2";
},
columns: [
["data1", 30, 200, 100],
["", 50, 20, 10]
],
colors: {
data1: "red",
data2: "blue"
}
}
};
})

it("should generate chart without error.", () => {
expect(
chart = util.generate(args)
).to.not.throw;
});

it("data color should be defined correctly.", () => {
const {svg} = chart.$;

["circle", "line", "path"].forEach(type => {
const prop = type === "circle" ? "fill" : "stroke";
const node = svg.select(`[class$=data2] > ${type}`);

expect(node.style(prop)).to.be.equal("blue");
});
});
});
});

0 comments on commit 98f7103

Please sign in to comment.