Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] chart: labels should not be truncated in tooltip #5250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/helpers/figures/charts/bar_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getBarChartData,
getBarChartDatasets,
Expand Down Expand Up @@ -228,7 +228,7 @@ export function createBarChartRuntime(chart: BarChart, getters: Getters): BarCha
const config: ChartConfiguration = {
type: "bar",
data: {
labels: chartData.labels.map(truncateLabel),
labels: chartData.labels,
datasets: getBarChartDatasets(definition, chartData),
},
options: {
Expand Down
14 changes: 13 additions & 1 deletion src/helpers/figures/charts/chart_common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { transformZone } from "../../../collaborative/ot/ot_helpers";
import { MAX_CHAR_LABEL } from "../../../constants";
import { _t } from "../../../translation";
import {
AddColumnsRowsCommand,
Expand Down Expand Up @@ -419,10 +420,11 @@ export function formatTickValue(localeFormat: LocaleFormat) {
value = Number(value);
if (isNaN(value)) return value;
const { locale, format } = localeFormat;
return formatValue(value, {
const formattedValue = formatValue(value, {
locale,
format: !format && Math.abs(value) >= 1000 ? "#,##" : format,
});
return truncateLabel(formattedValue);
};
}

Expand All @@ -440,3 +442,13 @@ export function getPieColors(colors: ColorGenerator, dataSetsValues: DatasetValu

return pieColors;
}

export function truncateLabel(label: string | undefined): string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to me that it's more 'formatTickValue' that should move to chart_ui_common rather than the other way around as it's clearly 'UI' but that split in the helpers seems already shady atm

if (!label) {
return "";
}
if (label.length > MAX_CHAR_LABEL) {
return label.substring(0, MAX_CHAR_LABEL) + "…";
}
return label;
}
11 changes: 0 additions & 11 deletions src/helpers/figures/charts/chart_ui_common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ChartOptions } from "chart.js";
import { MAX_CHAR_LABEL } from "../../../constants";
import { Figure } from "../../../types";
import { GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart";
import { ChartRuntime } from "../../../types/chart/chart";
Expand All @@ -23,16 +22,6 @@ export const CHART_COMMON_OPTIONS: ChartOptions = {
animation: false,
};

export function truncateLabel(label: string | undefined): string {
if (!label) {
return "";
}
if (label.length > MAX_CHAR_LABEL) {
return label.substring(0, MAX_CHAR_LABEL) + "…";
}
return label;
}

export function chartToImage(
runtime: ChartRuntime,
figure: Figure,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/figures/charts/combo_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getBarChartData,
getBarChartLayout,
Expand Down Expand Up @@ -231,7 +231,7 @@ export function createComboChartRuntime(chart: ComboChart, getters: Getters): Co
const config: ChartConfiguration = {
type: "bar",
data: {
labels: chartData.labels.map(truncateLabel),
labels: chartData.labels,
datasets: getComboChartDatasets(definition, chartData),
},
options: {
Expand Down
5 changes: 2 additions & 3 deletions src/helpers/figures/charts/line_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getChartShowValues,
getChartTitle,
Expand Down Expand Up @@ -238,8 +238,7 @@ export function createLineChartRuntime(chart: LineChart, getters: Getters): Char
const config: ChartConfiguration = {
type: "line",
data: {
labels:
chartData.axisType !== "time" ? chartData.labels.map(truncateLabel) : chartData.labels,
labels: chartData.labels,
datasets: getLineChartDatasets(definition, chartData),
},
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/figures/charts/pie_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getChartShowValues,
getChartTitle,
Expand Down Expand Up @@ -202,7 +202,7 @@ export function createPieChartRuntime(chart: PieChart, getters: Getters): PieCha
const config: ChartConfiguration = {
type: chart.isDoughnut ? "doughnut" : "pie",
data: {
labels: chartData.labels.map(truncateLabel),
labels: chartData.labels,
datasets: getPieChartDatasets(definition, chartData),
},
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/figures/charts/pyramid_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getBarChartDatasets,
getBarChartLayout,
Expand Down Expand Up @@ -204,7 +204,7 @@ export function createPyramidChartRuntime(
const config: ChartConfiguration = {
type: "bar",
data: {
labels: chartData.labels.map(truncateLabel),
labels: chartData.labels,
datasets: getBarChartDatasets(definition, chartData),
},
options: {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/figures/charts/radar_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getBarChartLayout,
getChartShowValues,
Expand Down Expand Up @@ -223,7 +223,7 @@ export function createRadarChartRuntime(chart: RadarChart, getters: Getters): Ra
const config: ChartConfiguration = {
type: "radar",
data: {
labels: chartData.labels.map(truncateLabel),
labels: chartData.labels,
datasets: getRadarChartDatasets(definition, chartData),
},
options: {
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/figures/charts/runtime/chart_data_extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { isDateTimeFormat } from "../../../format/format";
import { deepCopy, findNextDefinedValue, range } from "../../../misc";
import { isNumber } from "../../../numbers";
import { recomputeZones } from "../../../recompute_zones";
import { truncateLabel } from "../chart_ui_common";

export function getBarChartData(
definition: GenericDefinition<BarChartDefinition>,
Expand Down Expand Up @@ -670,7 +669,7 @@ function getChartDatasetValues(getters: Getters, dataSets: DataSet[]): DatasetVa
: undefined;
label =
cell && labelRange
? truncateLabel(cell.formattedValue)
? cell.formattedValue
: (label = `${ChartTerms.Series} ${parseInt(dsIndex) + 1}`);
} else {
label = `${ChartTerms.Series} ${parseInt(dsIndex) + 1}`;
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/figures/charts/runtime/chartjs_dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
setColorAlpha,
} from "../../../color";
import { TREND_LINE_XAXIS_ID, getPieColors } from "../chart_common";
import { truncateLabel } from "../chart_ui_common";

export function getBarChartDatasets(
definition: GenericDefinition<BarChartDefinition>,
Expand Down Expand Up @@ -116,7 +115,7 @@ export function getWaterfallDatasetAndLabels(

return {
datasets: [dataset],
labels: labelsWithSubTotals.map(truncateLabel),
labels: labelsWithSubTotals,
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/helpers/figures/charts/runtime/chartjs_legend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { ComboChartDefinition } from "../../../../types/chart/combo_chart";
import { RadarChartDefinition } from "../../../../types/chart/radar_chart";
import { ColorGenerator } from "../../../color";
import { chartFontColor, getPieColors } from "../chart_common";
import { chartFontColor, getPieColors, truncateLabel } from "../chart_common";

type ChartLegend = DeepPartial<LegendOptions<any>>;

Expand Down Expand Up @@ -78,7 +78,7 @@ export function getPieChartLegend(
generateLabels: (c) =>
//@ts-ignore
c.data.labels.map((label, index) => ({
text: label,
text: truncateLabel(String(label)),
strokeStyle: colors[index],
fillStyle: colors[index],
pointStyle: "rect",
Expand Down Expand Up @@ -233,7 +233,7 @@ function getCustomLegendLabels(
usePointStyle: true,
generateLabels: (chart: Chart) =>
chart.data.datasets.map((dataset, index) => ({
text: dataset.label ?? "",
text: truncateLabel(dataset.label),
fontColor,
strokeStyle: dataset.borderColor as Color,
fillStyle: dataset.backgroundColor as Color,
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/figures/charts/runtime/chartjs_scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
chartFontColor,
formatTickValue,
getDefinedAxis,
truncateLabel,
} from "../chart_common";

type ChartScales = ChartOptions["scales"];
Expand Down Expand Up @@ -252,6 +253,11 @@ function getChartAxis(
ticks: {
padding: 5,
color: fontColor,
callback: function (tickValue: number) {
// Category axis callback's internal tick value is the index of the label
// https://www.chartjs.org/docs/latest/axes/labelling.html#creating-custom-tick-formats
return truncateLabel(this.getLabelForValue(tickValue));
},
},
grid: {
display: false,
Expand Down
5 changes: 2 additions & 3 deletions src/helpers/figures/charts/scatter_chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
transformChartDefinitionWithDataSetsWithZone,
updateChartRangesWithDataSets,
} from "./chart_common";
import { CHART_COMMON_OPTIONS, truncateLabel } from "./chart_ui_common";
import { CHART_COMMON_OPTIONS } from "./chart_ui_common";
import {
getChartShowValues,
getChartTitle,
Expand Down Expand Up @@ -231,8 +231,7 @@ export function createScatterChartRuntime(
// have less options than the line chart (it only works with linear labels)
type: "line",
data: {
labels:
chartData.axisType !== "time" ? chartData.labels.map(truncateLabel) : chartData.labels,
labels: chartData.labels,
datasets: getScatterChartDatasets(definition, chartData),
},
options: {
Expand Down
17 changes: 14 additions & 3 deletions tests/figures/chart/__snapshots__/chart_plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ exports[`Linear/Time charts snapshot test of chartJS configuration for date char
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"maxTicksLimit": 15,
"padding": 5,
Expand Down Expand Up @@ -485,6 +486,7 @@ exports[`datasource tests create a chart with stacked bar 1`] = `
},
"stacked": true,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -597,6 +599,7 @@ exports[`datasource tests create chart with a dataset of one cell (no title) 1`]
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -651,7 +654,7 @@ exports[`datasource tests create chart with column datasets 1`] = `
18,
],
"fill": false,
"label": "second column datase…",
"label": "second column dataset",
"pointBackgroundColor": "#EA6175",
"tension": 0,
"yAxisID": "y",
Expand Down Expand Up @@ -727,6 +730,7 @@ exports[`datasource tests create chart with column datasets 1`] = `
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -781,7 +785,7 @@ exports[`datasource tests create chart with column datasets with category title
18,
],
"fill": false,
"label": "second column datase…",
"label": "second column dataset",
"pointBackgroundColor": "#EA6175",
"tension": 0,
"yAxisID": "y",
Expand Down Expand Up @@ -857,6 +861,7 @@ exports[`datasource tests create chart with column datasets with category title
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -987,6 +992,7 @@ exports[`datasource tests create chart with column datasets without series title
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -1088,6 +1094,7 @@ exports[`datasource tests create chart with only the dataset title (no data) 1`]
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -1142,7 +1149,7 @@ exports[`datasource tests create chart with rectangle dataset 1`] = `
18,
],
"fill": false,
"label": "second column datase…",
"label": "second column dataset",
"pointBackgroundColor": "#EA6175",
"tension": 0,
"yAxisID": "y",
Expand Down Expand Up @@ -1218,6 +1225,7 @@ exports[`datasource tests create chart with rectangle dataset 1`] = `
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -1348,6 +1356,7 @@ exports[`datasource tests create chart with row datasets 1`] = `
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -1478,6 +1487,7 @@ exports[`datasource tests create chart with row datasets with category title 1`]
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down Expand Up @@ -1608,6 +1618,7 @@ exports[`datasource tests create chart with row datasets without series title 1`
},
"stacked": undefined,
"ticks": {
"callback": [Function],
"color": "#000000",
"padding": 5,
},
Expand Down
1 change: 0 additions & 1 deletion tests/figures/chart/bar_chart_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ describe("bar chart", () => {
expect(options.scales.x.title.text).toBe("xAxis");
expect(options.scales.x.ticks.callback(5)).toBe("5€");
expect(options.scales.y.title.text).toBe("yAxis");
expect(options.scales.y.ticks.callback).toBeUndefined();

const tooltipTestItem = {
parsed: { x: 5, y: "label" },
Expand Down
Loading