Skip to content

Commit

Permalink
[FIX] charts: truncate labels by default
Browse files Browse the repository at this point in the history
This commit ensures that labels are truncated by default in charts.
The truncation can be disabled by passing the `truncate` option to
the chart configuration.

closes #4596

Task: 3958962
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
dhrp-odoo committed Jul 10, 2024
1 parent 2d467e3 commit e990ea2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helpers/figures/charts/chart_ui_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function getDefaultChartJsRuntime(
chart: AbstractChart,
labels: string[],
fontColor: Color,
{ format, locale, truncateLabels }: LocaleFormat & { truncateLabels?: boolean }
{ format, locale, truncateLabels = true }: LocaleFormat & { truncateLabels?: boolean }
): Required<ChartConfiguration> {
const options: ChartOptions = {
// https://www.chartjs.org/docs/latest/general/responsive.html
Expand Down
24 changes: 24 additions & 0 deletions tests/figures/chart/chart_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,30 @@ describe("Linear/Time charts", () => {
expect(chart.data!.datasets![0].data![0]).toEqual({ y: 10, x: formattedValue });
});

test.each(["bar", "line", "pie"] as const)("long labels are truncated in %s chart", (type) => {
setCellContent(model, "A2", "This is a very long label that should be truncated");
setCellContent(model, "B1", "First dataset");
setCellContent(model, "B2", "10");

createChart(
model,
{
type,
dataSets: ["B1:B2"],
labelRange: "A2",
labelsAsText: false,
dataSetsHaveTitle: true,
},
chartId
);

const chart = (model.getters.getChartRuntime(chartId) as BarChartRuntime).chartJsConfig;

expect(chart.data!.labels![0]).toEqual("This is a very long …");
expect((chart.data!.labels![0] as string).length).toBe(MAX_CHAR_LABEL + 1); // +1 for the ellipsis
expect(chart.data!.datasets![0].data![0]).toEqual(10);
});

test("linear chart: label 0 isn't set to undefined", () => {
setCellContent(model, "B2", "0");
setCellContent(model, "B3", "1");
Expand Down

0 comments on commit e990ea2

Please sign in to comment.