Skip to content

Commit

Permalink
fix(options): Fix handling for falsy data.types
Browse files Browse the repository at this point in the history
Check when falsy and non-valid chart type value is specified.

Fix #3125
  • Loading branch information
netil committed Mar 15, 2023
1 parent dd61e7c commit 5eb731a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/ChartInternal/data/convert.ts
Expand Up @@ -76,6 +76,7 @@ export default {
convertDataToTargets(data: {[key:string]: number|null}[], appendXs: boolean): IData[] {
const $$ = this;
const {axis, config, state} = $$;
const chartType = config.data_type;
let isCategorized = false;
let isTimeSeries = false;
let isCustomX = false;
Expand Down Expand Up @@ -207,9 +208,11 @@ export default {
state.hasPositiveValue = $$.hasPositiveValueInTargets(targets);

// set target types
if (config.data_type) {
$$.setTargetType($$.mapToIds(targets)
.filter(id => !(id in config.data_types)), config.data_type);
if (chartType && $$.isValidChartType(chartType)) {
const targetIds = $$.mapToIds(targets)
.filter(id => !(id in config.data_types) || !$$.isValidChartType(config.data_types[id]));

$$.setTargetType(targetIds, chartType);
}

// cache as original id keyed
Expand Down
10 changes: 10 additions & 0 deletions src/ChartInternal/internals/type.ts
Expand Up @@ -6,6 +6,16 @@ import {isArray, isNumber, isString} from "../../module/util";
import {TYPE, TYPE_BY_CATEGORY} from "../../config/const";

export default {
/**
* Check if the given chart type is valid
* @param {string} type Chart type string
* @returns {boolean}
* @private
*/
isValidChartType(type: string): boolean {
return !!(type && Object.values(TYPE).indexOf(type) > -1);
},

setTargetType(targetIds: string[], type: string): void {
const $$ = this;
const {config, state: {withoutFadeIn}} = $$;
Expand Down
35 changes: 35 additions & 0 deletions test/internals/type-spec.ts
Expand Up @@ -15,6 +15,41 @@ describe("TYPES", () => {
chart = util.generate(args);
});

describe("data.type / data.types", () => {
before(() => {
args = {
data: {
type: "bars",
columns: [
["data1", 30, 20, 50, 40, 60, 50],
["data2", 200, 130, 90, 240, 130, 220],
]
}
};
});

it("should generate when wrong data.type is specified.", () => {
const lines = chart.$.line.lines.size();

expect(lines).to.be.equal(2);
});

it("set option: data.types", () => {
args.data.type = "bar";
args.data.types = {
data2: undefined
};
});

it("should generate when falsy data.types value is specified.", () => {
const {bar: {bars}, line: {lines}} = chart.$;

expect(lines).to.be.null;
expect(bars.size()).to.be.equal(12);
});
});


describe("internal.hasArcType", () => {
describe("with data", () => {
before(() => {
Expand Down

0 comments on commit 5eb731a

Please sign in to comment.