(core) Fix chart when x axis is set to a choice list column

Summary:
Bug reported by user: https://gristlabs.getgrist.com/doc/check-ins/p/3#a1.s7.r1183.c19p

Setting x axis to a column of type ChoiceList was breaking chart.

This diff fixes that by splitting the record into several records: one for each choice.

`test/nbrowser/ChartView1.ts` was becoming too big and long to run, so this diff introduces `test/nbrowser/ChartView2.ts` to add more test and `test/nbrowser/chartViewTestUtils.ts` to put all utilities or testing charts.

Test Plan: Adds new test.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3041
This commit is contained in:
Cyprien P
2021-09-30 09:41:36 +02:00
parent d4ea5b3761
commit a0c53f2b61
2 changed files with 51 additions and 2 deletions

View File

@@ -1,7 +1,9 @@
import {typedCompare} from 'app/common/SortFunc';
import {decodeObject} from 'app/plugin/objtypes';
import {Datum} from 'plotly.js';
import range = require('lodash/range');
import uniqBy = require('lodash/uniqBy');
import flatten = require('lodash/flatten');
/**
* Sort all values in a list of series according to the values in the first one.
@@ -31,3 +33,30 @@ export function uniqXValues<T extends {values: Datum[]}>(series: Array<T>): Arra
values: line.values.filter((_val, i) => indexToKeep.has(i))
}));
}
// Creates new version of series that split any entry whose value in the first series is a list into
// multiple entries, one entry for each list's item. For all other series, newly created entries have
// the same value as the original.
export function splitValues<T extends {values: Datum[]}>(series: Array<T>): Array<T> {
return splitValuesByIndex(series, 0);
}
// This method is like splitValues except it splits according to the values of the series at position index.
export function splitValuesByIndex<T extends {values: Datum[]}>(series: Array<T>, index: number): Array<T> {
const decoded = (series[index].values as any[]).map(decodeObject);
return series.map((s, si) => {
if (si === index) {
return {...series[index], values: flatten(decoded)};
}
let values: Datum[] = [];
for (const [i, splitByValue] of decoded.entries()) {
if (Array.isArray(splitByValue)) {
values = values.concat(Array(splitByValue.length).fill(s.values[i]));
} else {
values.push(s.values[i]);
}
}
return {...s, values};
});
}