@odoo/o-spreadsheet 14.0.0 → 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +20 -0
- package/.husky/post-checkout +9 -0
- package/.husky/pre-commit +39 -0
- package/.prettierignore +1 -0
- package/astToFormula.patch +0 -0
- package/demo/data.js +255 -0
- package/demo/index.html +9 -0
- package/demo/libs/Chart.bundle.js +20755 -0
- package/demo/main.js +101 -0
- package/doc/readme.md +46 -0
- package/package.json +3 -17
- package/rollup.config.js +23 -0
- package/src/base_plugin.ts +102 -0
- package/src/components/autofill.ts +146 -0
- package/src/components/bottom_bar.ts +199 -0
- package/src/components/color_picker.ts +150 -0
- package/src/components/composer/autocomplete_dropdown.ts +135 -0
- package/src/components/composer/composer.ts +487 -0
- package/src/components/composer/content_editable_helper.ts +146 -0
- package/src/components/figures/chart.ts +70 -0
- package/src/components/figures/container.ts +268 -0
- package/src/components/figures/text_figure.ts +36 -0
- package/src/components/grid.ts +635 -0
- package/src/components/helpers/dom_helpers.ts +7 -0
- package/src/components/helpers/drag_and_drop.ts +17 -0
- package/src/components/icons.ts +36 -0
- package/src/components/index.ts +1 -0
- package/src/components/menu.ts +288 -0
- package/src/components/overlay.ts +457 -0
- package/src/components/scrollbar.ts +22 -0
- package/src/components/selection_input.ts +168 -0
- package/src/components/side_panel/cell_is_rule_editor.ts +169 -0
- package/src/components/side_panel/chart_panel.ts +104 -0
- package/src/components/side_panel/color_scale_rule_editor.ts +145 -0
- package/src/components/side_panel/conditional_formatting.ts +396 -0
- package/src/components/side_panel/side_panel.ts +127 -0
- package/src/components/side_panel/translations_terms.ts +45 -0
- package/src/components/spreadsheet.ts +198 -0
- package/src/components/top_bar.ts +535 -0
- package/src/constants.ts +26 -0
- package/src/data.ts +149 -0
- package/src/fonts.ts +41 -0
- package/src/formulas/compiler.ts +258 -0
- package/src/formulas/composer_tokenizer.ts +70 -0
- package/src/formulas/index.ts +15 -0
- package/src/formulas/normalize.ts +52 -0
- package/src/formulas/parser.ts +280 -0
- package/src/formulas/range_tokenizer.ts +97 -0
- package/src/formulas/tokenizer.ts +242 -0
- package/src/functions/arguments.ts +114 -0
- package/src/functions/dates.ts +302 -0
- package/src/functions/helpers.ts +592 -0
- package/src/functions/index.ts +52 -0
- package/src/functions/module_database.ts +282 -0
- package/src/functions/module_date.ts +743 -0
- package/src/functions/module_info.ts +69 -0
- package/src/functions/module_logical.ts +208 -0
- package/src/functions/module_lookup.ts +185 -0
- package/src/functions/module_math.ts +1252 -0
- package/src/functions/module_operators.ts +291 -0
- package/src/functions/module_statistical.ts +882 -0
- package/src/functions/module_text.ts +370 -0
- package/src/helpers/color.ts +30 -0
- package/src/helpers/coordinates.ts +71 -0
- package/src/helpers/grid_manipulation.ts +68 -0
- package/src/helpers/index.ts +7 -0
- package/src/helpers/misc.ts +76 -0
- package/src/helpers/numbers.ts +117 -0
- package/src/helpers/uuid.ts +19 -0
- package/src/helpers/zones.ts +263 -0
- package/src/history.ts +186 -0
- package/src/index.ts +84 -0
- package/src/model.ts +261 -0
- package/src/plugins/autofill.ts +412 -0
- package/src/plugins/chart.ts +404 -0
- package/src/plugins/clipboard.ts +461 -0
- package/src/plugins/conditional_format.ts +445 -0
- package/src/plugins/core.ts +1502 -0
- package/src/plugins/edition.ts +157 -0
- package/src/plugins/evaluation.ts +428 -0
- package/src/plugins/figures.ts +123 -0
- package/src/plugins/formatting.ts +630 -0
- package/src/plugins/highlight.ts +187 -0
- package/src/plugins/index.ts +32 -0
- package/src/plugins/merge.ts +524 -0
- package/src/plugins/renderer.ts +571 -0
- package/src/plugins/selection.ts +562 -0
- package/src/plugins/selection_inputs.ts +326 -0
- package/src/registries/autofill_modifiers.ts +76 -0
- package/src/registries/autofill_rules.ts +98 -0
- package/src/registries/figure_registry.ts +30 -0
- package/src/registries/index.ts +7 -0
- package/src/registries/menu_items_registry.ts +110 -0
- package/src/registries/menus/cell_menu_registry.ts +79 -0
- package/src/registries/menus/col_menu_registry.ts +63 -0
- package/src/registries/menus/index.ts +6 -0
- package/src/registries/menus/menu_items_actions.ts +425 -0
- package/src/registries/menus/row_menu_registry.ts +63 -0
- package/src/registries/menus/sheet_menu_registry.ts +75 -0
- package/src/registries/menus/topbar_menu_registry.ts +204 -0
- package/src/registries/side_panel_registry.ts +26 -0
- package/src/registries/topbar_component_registry.ts +12 -0
- package/src/registry.ts +51 -0
- package/src/translation.ts +44 -0
- package/src/types/autofill.ts +48 -0
- package/src/types/chart.ts +26 -0
- package/src/types/commands.ts +716 -0
- package/src/types/conditional_formatting.ts +124 -0
- package/src/types/env.ts +13 -0
- package/src/types/functions.ts +35 -0
- package/src/types/getters.ts +103 -0
- package/src/types/index.ts +20 -0
- package/src/types/misc.ts +143 -0
- package/src/types/rendering.ts +41 -0
- package/src/types/workbook_data.ts +62 -0
- package/tests/__mocks__/uuid.ts +7 -0
- package/tests/canvas.mock.ts +34 -0
- package/tests/components/__mocks__/content_editable_helper.ts +79 -0
- package/tests/components/__mocks__/scrollbar.ts +18 -0
- package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +29 -0
- package/tests/components/__snapshots__/bottom_bar_test.ts.snap +75 -0
- package/tests/components/__snapshots__/context_menu_test.ts.snap +88 -0
- package/tests/components/__snapshots__/grid_test.ts.snap +60 -0
- package/tests/components/__snapshots__/spreadsheet_test.ts.snap +455 -0
- package/tests/components/__snapshots__/top_bar_test.ts.snap +705 -0
- package/tests/components/autocomplete_dropdown_test.ts +315 -0
- package/tests/components/autofill_test.ts +167 -0
- package/tests/components/bottom_bar_test.ts +262 -0
- package/tests/components/chart_side_panel_test.ts +123 -0
- package/tests/components/composer_test.ts +400 -0
- package/tests/components/context_menu_test.ts +825 -0
- package/tests/components/grid_manipulation_test.ts +163 -0
- package/tests/components/grid_test.ts +581 -0
- package/tests/components/overlay_test.ts +541 -0
- package/tests/components/selection_input_test.ts +259 -0
- package/tests/components/side_panel_test.ts +148 -0
- package/tests/components/spreadsheet_test.ts +194 -0
- package/tests/components/top_bar_test.ts +427 -0
- package/tests/data_test.ts +175 -0
- package/tests/dom_helper.ts +42 -0
- package/tests/formulas/__snapshots__/compiler_test.ts.snap +302 -0
- package/tests/formulas/compiler_test.ts +266 -0
- package/tests/formulas/composer_tokenizer_test.ts +137 -0
- package/tests/formulas/expressions_test.ts +57 -0
- package/tests/formulas/formulas_test.ts +80 -0
- package/tests/formulas/normalize_test.ts +82 -0
- package/tests/formulas/parser_test.ts +224 -0
- package/tests/formulas/range_tokenizer_test.ts +187 -0
- package/tests/formulas/tokenizer_test.ts +164 -0
- package/tests/functions/arguments_test.ts +141 -0
- package/tests/functions/dates_test.ts +1081 -0
- package/tests/functions/functions_test.ts +53 -0
- package/tests/functions/helper_test.ts +98 -0
- package/tests/functions/module_database_test.ts +311 -0
- package/tests/functions/module_date_test.ts +941 -0
- package/tests/functions/module_info_test.ts +139 -0
- package/tests/functions/module_logical_test.ts +369 -0
- package/tests/functions/module_lookup_test.ts +541 -0
- package/tests/functions/module_math_test.ts +2482 -0
- package/tests/functions/module_operators_test.ts +955 -0
- package/tests/functions/module_statistical_test.ts +2014 -0
- package/tests/functions/module_text_test.ts +667 -0
- package/tests/helpers/coordinates_test.ts +25 -0
- package/tests/helpers/misc_test.ts +95 -0
- package/tests/helpers/numbers_test.ts +196 -0
- package/tests/helpers/zones_test.ts +97 -0
- package/tests/helpers.ts +384 -0
- package/tests/menu_items_registry_test.ts +540 -0
- package/tests/model/plugins/__snapshots__/chart_test.ts.snap +90 -0
- package/tests/model/plugins/chart_test.ts +562 -0
- package/tests/model_test.ts +82 -0
- package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +131 -0
- package/tests/plugins/__snapshots__/renderer_test.ts.snap +371 -0
- package/tests/plugins/autofill_test.ts +457 -0
- package/tests/plugins/borders_test.ts +295 -0
- package/tests/plugins/clipboard_test.ts +1180 -0
- package/tests/plugins/conditional_formatting_test.ts +1253 -0
- package/tests/plugins/core_test.ts +614 -0
- package/tests/plugins/edition_test.ts +100 -0
- package/tests/plugins/evaluation_async_test.ts +332 -0
- package/tests/plugins/evaluation_dates_test.ts +79 -0
- package/tests/plugins/evaluation_test.ts +901 -0
- package/tests/plugins/figures_test.ts +327 -0
- package/tests/plugins/formatting_test.ts +391 -0
- package/tests/plugins/grid_manipulation_test.ts +1366 -0
- package/tests/plugins/highlight_test.ts +413 -0
- package/tests/plugins/history_test.ts +142 -0
- package/tests/plugins/import_export_test.ts +276 -0
- package/tests/plugins/merges_test.ts +490 -0
- package/tests/plugins/navigation_test.ts +181 -0
- package/tests/plugins/plugins/evaluation_test.ts +111 -0
- package/tests/plugins/renderer_test.ts +598 -0
- package/tests/plugins/resizing_test.ts +152 -0
- package/tests/plugins/selection_input_test.ts +409 -0
- package/tests/plugins/selection_test.ts +442 -0
- package/tests/plugins/sheets_test.ts +643 -0
- package/tests/plugins/style_test.ts +90 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { ChartColor, ChartConfiguration, ChartData, ChartTooltipItem, ChartType } from "chart.js";
|
|
2
|
+
import { BasePlugin } from "../base_plugin";
|
|
3
|
+
import { chartTerms } from "../components/side_panel/translations_terms";
|
|
4
|
+
import { rangeReference } from "../formulas/parser";
|
|
5
|
+
import { deepCopy, isInside, toXC, toZone, uuidv4, zoneToXc } from "../helpers/index";
|
|
6
|
+
import {
|
|
7
|
+
CancelledReason,
|
|
8
|
+
ChartDefinition,
|
|
9
|
+
Command,
|
|
10
|
+
CommandResult,
|
|
11
|
+
CreateChartDefinition,
|
|
12
|
+
DataSet,
|
|
13
|
+
Figure,
|
|
14
|
+
LAYERS,
|
|
15
|
+
WorkbookData,
|
|
16
|
+
Zone,
|
|
17
|
+
} from "../types/index";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Chart plugin
|
|
21
|
+
*
|
|
22
|
+
* This plugin creates and displays charts
|
|
23
|
+
* */
|
|
24
|
+
|
|
25
|
+
const GraphColors = [
|
|
26
|
+
// the same colors as those used in odoo reporting
|
|
27
|
+
"rgb(31,119,180)",
|
|
28
|
+
"rgb(255,127,14)",
|
|
29
|
+
"rgb(174,199,232)",
|
|
30
|
+
"rgb(255,187,120)",
|
|
31
|
+
"rgb(44,160,44)",
|
|
32
|
+
"rgb(152,223,138)",
|
|
33
|
+
"rgb(214,39,40)",
|
|
34
|
+
"rgb(255,152,150)",
|
|
35
|
+
"rgb(148,103,189)",
|
|
36
|
+
"rgb(197,176,213)",
|
|
37
|
+
"rgb(140,86,75)",
|
|
38
|
+
"rgb(196,156,148)",
|
|
39
|
+
"rgb(227,119,194)",
|
|
40
|
+
"rgb(247,182,210)",
|
|
41
|
+
"rgb(127,127,127)",
|
|
42
|
+
"rgb(199,199,199)",
|
|
43
|
+
"rgb(188,189,34)",
|
|
44
|
+
"rgb(219,219,141)",
|
|
45
|
+
"rgb(23,190,207)",
|
|
46
|
+
"rgb(158,218,229)",
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
export class ChartPlugin extends BasePlugin {
|
|
50
|
+
static getters = ["getChartRuntime"];
|
|
51
|
+
static layers = [LAYERS.Chart];
|
|
52
|
+
|
|
53
|
+
private chartFigures: Set<string> = new Set();
|
|
54
|
+
|
|
55
|
+
// contains the configuration of the chart with it's values like they should be displayed,
|
|
56
|
+
// as well as all the options needed for the chart library to work correctly
|
|
57
|
+
private chartRuntime: { [figureId: string]: ChartConfiguration } = {};
|
|
58
|
+
private outOfDate: Set<string> = new Set<string>();
|
|
59
|
+
|
|
60
|
+
allowDispatch(cmd: Command): CommandResult {
|
|
61
|
+
const success: CommandResult = { status: "SUCCESS" };
|
|
62
|
+
switch (cmd.type) {
|
|
63
|
+
case "UPDATE_CHART":
|
|
64
|
+
case "CREATE_CHART":
|
|
65
|
+
const invalidRanges =
|
|
66
|
+
cmd.definition.dataSets.find((range) => !rangeReference.test(range.split("!").pop()!)) !==
|
|
67
|
+
undefined;
|
|
68
|
+
const invalidLabels = !rangeReference.test(cmd.definition.labelRange.split("!").pop()!);
|
|
69
|
+
return invalidRanges || invalidLabels
|
|
70
|
+
? { status: "CANCELLED", reason: CancelledReason.InvalidChartDefinition }
|
|
71
|
+
: success;
|
|
72
|
+
default:
|
|
73
|
+
return success;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
handle(cmd: Command) {
|
|
78
|
+
switch (cmd.type) {
|
|
79
|
+
case "CREATE_CHART":
|
|
80
|
+
this.addChartFigure(cmd.sheetId, {
|
|
81
|
+
id: cmd.id,
|
|
82
|
+
data: this.createChartDefinition(cmd.definition, cmd.sheetId),
|
|
83
|
+
x: 0,
|
|
84
|
+
y: 0,
|
|
85
|
+
height: 500,
|
|
86
|
+
width: 800,
|
|
87
|
+
tag: "chart",
|
|
88
|
+
});
|
|
89
|
+
break;
|
|
90
|
+
case "UPDATE_CHART": {
|
|
91
|
+
const chartDefinition = this.createChartDefinition(
|
|
92
|
+
cmd.definition,
|
|
93
|
+
this.getChartDefinition(cmd.id).sheetId
|
|
94
|
+
);
|
|
95
|
+
this.dispatch("UPDATE_FIGURE", {
|
|
96
|
+
id: cmd.id,
|
|
97
|
+
data: chartDefinition,
|
|
98
|
+
});
|
|
99
|
+
this.history.updateLocalState(
|
|
100
|
+
["chartRuntime", cmd.id],
|
|
101
|
+
this.mapDefinitionToRuntime(chartDefinition)
|
|
102
|
+
);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case "DELETE_FIGURE":
|
|
106
|
+
if (this.chartFigures.has(cmd.id)) {
|
|
107
|
+
const figures = new Set(this.chartFigures);
|
|
108
|
+
figures.delete(cmd.id);
|
|
109
|
+
this.history.updateLocalState(["chartFigures"], figures);
|
|
110
|
+
this.history.updateLocalState(["chartRuntime", cmd.id], undefined);
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
case "DUPLICATE_SHEET": {
|
|
114
|
+
const newSheetId = cmd.id;
|
|
115
|
+
const figures = [...this.chartFigures]
|
|
116
|
+
.map((figureId) => this.getters.getFigure<ChartDefinition>(figureId))
|
|
117
|
+
.filter((figure) => figure.data.sheetId === cmd.sheet)
|
|
118
|
+
.map(deepCopy);
|
|
119
|
+
for (let figure of figures) {
|
|
120
|
+
figure.data.sheetId = newSheetId;
|
|
121
|
+
figure.id = uuidv4();
|
|
122
|
+
this.addChartFigure(newSheetId, figure);
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case "DELETE_SHEET":
|
|
127
|
+
const figures = new Set(this.chartFigures);
|
|
128
|
+
const runtime = { ...this.chartRuntime };
|
|
129
|
+
for (let figureId of this.chartFigures) {
|
|
130
|
+
const figure = this.getters.getFigure(figureId);
|
|
131
|
+
if (!figure) {
|
|
132
|
+
figures.delete(figureId);
|
|
133
|
+
delete runtime[figureId];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
this.history.updateLocalState(["chartFigures"], figures);
|
|
137
|
+
this.history.updateLocalState(["chartRuntime"], runtime);
|
|
138
|
+
break;
|
|
139
|
+
case "UPDATE_CELL":
|
|
140
|
+
for (let chartId of this.chartFigures) {
|
|
141
|
+
const chart = this.getChartDefinition(chartId);
|
|
142
|
+
if (this.isCellUsedInChart(chart, cmd.col, cmd.row)) {
|
|
143
|
+
this.outOfDate.add(chartId);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
finalize(cmd: Command) {
|
|
151
|
+
switch (cmd.type) {
|
|
152
|
+
case "EVALUATE_CELLS":
|
|
153
|
+
case "START":
|
|
154
|
+
// if there was an async evaluation of cell, there is no way to know which was updated so all charts must be updated
|
|
155
|
+
for (let id in this.chartRuntime) {
|
|
156
|
+
this.outOfDate.add(id);
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
import(data: WorkbookData) {
|
|
163
|
+
for (let sheet of data.sheets) {
|
|
164
|
+
for (let f of sheet.figures) {
|
|
165
|
+
if (f.tag === "chart") {
|
|
166
|
+
this.outOfDate.add(f.id);
|
|
167
|
+
this.chartFigures.add(f.id);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
// Getters
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
getChartRuntime(figureId: string): ChartConfiguration | undefined {
|
|
177
|
+
if (this.outOfDate.has(figureId)) {
|
|
178
|
+
this.chartRuntime[figureId] = this.mapDefinitionToRuntime(this.getChartDefinition(figureId));
|
|
179
|
+
this.outOfDate.delete(figureId);
|
|
180
|
+
}
|
|
181
|
+
return this.chartRuntime[figureId];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
// Private
|
|
186
|
+
// ---------------------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
private getChartDefinition(figureId: string): ChartDefinition {
|
|
189
|
+
return this.getters.getFigure<ChartDefinition>(figureId).data;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private createChartDefinition(
|
|
193
|
+
createCommand: CreateChartDefinition,
|
|
194
|
+
sheetId: string
|
|
195
|
+
): ChartDefinition {
|
|
196
|
+
let dataSets: DataSet[] = [];
|
|
197
|
+
|
|
198
|
+
for (let range of createCommand.dataSets) {
|
|
199
|
+
let zone = toZone(range);
|
|
200
|
+
if (zone.left !== zone.right && zone.top !== zone.bottom) {
|
|
201
|
+
// It's a rectangle. We treat all columns (arbitrary) as different data series.
|
|
202
|
+
for (let column = zone.left; column <= zone.right; column++) {
|
|
203
|
+
const columnZone = {
|
|
204
|
+
left: column,
|
|
205
|
+
right: column,
|
|
206
|
+
top: zone.top,
|
|
207
|
+
bottom: zone.bottom,
|
|
208
|
+
};
|
|
209
|
+
dataSets.push(this.createDataset(columnZone, createCommand.seriesHasTitle));
|
|
210
|
+
}
|
|
211
|
+
} else if (zone.left === zone.right && zone.top === zone.bottom) {
|
|
212
|
+
// A single cell. If it's only the title, the dataset is not added.
|
|
213
|
+
if (!createCommand.seriesHasTitle) {
|
|
214
|
+
dataSets.push({ dataRange: zoneToXc(zone) });
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
dataSets.push(this.createDataset(zone, createCommand.seriesHasTitle));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
title: createCommand.title,
|
|
223
|
+
type: createCommand.type,
|
|
224
|
+
dataSets: dataSets,
|
|
225
|
+
labelRange: createCommand.labelRange,
|
|
226
|
+
sheetId: sheetId,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Create a chart dataset from a Zone.
|
|
232
|
+
* The zone should be a single column or a single row
|
|
233
|
+
*/
|
|
234
|
+
private createDataset(zone: Zone, withTitle: boolean): DataSet {
|
|
235
|
+
if (zone.left !== zone.right && zone.top !== zone.bottom) {
|
|
236
|
+
throw new Error(`Zone should be a single column or row: ${zoneToXc(zone)}`);
|
|
237
|
+
}
|
|
238
|
+
const labelCell = withTitle ? toXC(zone.left, zone.top) : undefined;
|
|
239
|
+
const offset = withTitle ? 1 : 0;
|
|
240
|
+
const isColumn = zone.top !== zone.bottom && zone.left === zone.right;
|
|
241
|
+
const dataRange = zoneToXc({
|
|
242
|
+
top: isColumn ? zone.top + offset : zone.top,
|
|
243
|
+
bottom: zone.bottom,
|
|
244
|
+
left: isColumn ? zone.left : zone.left + offset,
|
|
245
|
+
right: zone.right,
|
|
246
|
+
});
|
|
247
|
+
return { labelCell, dataRange };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private getDefaultConfiguration(
|
|
251
|
+
type: ChartType,
|
|
252
|
+
title: string | undefined,
|
|
253
|
+
labels: string[]
|
|
254
|
+
): ChartConfiguration {
|
|
255
|
+
const config: ChartConfiguration = {
|
|
256
|
+
type,
|
|
257
|
+
options: {
|
|
258
|
+
// https://www.chartjs.org/docs/latest/general/responsive.html
|
|
259
|
+
responsive: true, // will resize when its container is resized
|
|
260
|
+
maintainAspectRatio: false, // doesn't maintain the aspect ration (width/height =2 by default) so the user has the choice of the exact layout
|
|
261
|
+
layout: { padding: { left: 20, right: 20, top: 10, bottom: 10 } },
|
|
262
|
+
elements: {
|
|
263
|
+
line: {
|
|
264
|
+
fill: false, // do not fill the area under line charts
|
|
265
|
+
},
|
|
266
|
+
point: {
|
|
267
|
+
hitRadius: 15, // increased hit radius to display point tooltip when hovering nearby
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
animation: {
|
|
271
|
+
duration: 0, // general animation time
|
|
272
|
+
},
|
|
273
|
+
hover: {
|
|
274
|
+
animationDuration: 10, // duration of animations when hovering an item
|
|
275
|
+
},
|
|
276
|
+
responsiveAnimationDuration: 0, // animation duration after a resize
|
|
277
|
+
title: {
|
|
278
|
+
display: true,
|
|
279
|
+
fontSize: 22,
|
|
280
|
+
fontStyle: "normal",
|
|
281
|
+
text: title,
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
data: {
|
|
285
|
+
labels,
|
|
286
|
+
datasets: [],
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
if (type !== "pie") {
|
|
291
|
+
config.options!.scales = {
|
|
292
|
+
xAxes: [
|
|
293
|
+
{
|
|
294
|
+
ticks: {
|
|
295
|
+
// x axis configuration
|
|
296
|
+
maxRotation: 60,
|
|
297
|
+
minRotation: 15,
|
|
298
|
+
padding: 5,
|
|
299
|
+
labelOffset: 2,
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
yAxes: [
|
|
304
|
+
{
|
|
305
|
+
ticks: {
|
|
306
|
+
// y axis configuration
|
|
307
|
+
beginAtZero: true, // the origin of the y axis is always zero
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
],
|
|
311
|
+
};
|
|
312
|
+
} else {
|
|
313
|
+
config.options!.tooltips = {
|
|
314
|
+
callbacks: {
|
|
315
|
+
title: function (tooltipItems: ChartTooltipItem[], data: ChartData) {
|
|
316
|
+
return data.datasets![tooltipItems[0]!.datasetIndex!].label!;
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return config;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private extractDataFromRange(sheetId: string, reference?: string): any[] {
|
|
325
|
+
return reference ? this.getters.getRangeValues(reference, sheetId).flat(1) : [];
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private mapDefinitionToRuntime(definition: ChartDefinition): ChartConfiguration {
|
|
329
|
+
const labels =
|
|
330
|
+
definition.labelRange !== ""
|
|
331
|
+
? this.getters.getRangeFormattedValues(definition.labelRange, definition.sheetId).flat(1)
|
|
332
|
+
: [];
|
|
333
|
+
const runtime = this.getDefaultConfiguration(definition.type, definition.title, labels);
|
|
334
|
+
|
|
335
|
+
let graphColorIndex = 0;
|
|
336
|
+
const pieColors: ChartColor[] = [];
|
|
337
|
+
if (definition.type === "pie") {
|
|
338
|
+
const maxLength = Math.max(
|
|
339
|
+
...definition.dataSets.map(
|
|
340
|
+
(ds) => this.extractDataFromRange(definition.sheetId, ds.dataRange).length
|
|
341
|
+
)
|
|
342
|
+
);
|
|
343
|
+
for (let i = 0; i <= maxLength; i++) {
|
|
344
|
+
pieColors.push(GraphColors[graphColorIndex]);
|
|
345
|
+
graphColorIndex = ++graphColorIndex % GraphColors.length;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
for (const [dsIndex, ds] of Object.entries(definition.dataSets)) {
|
|
349
|
+
let label;
|
|
350
|
+
if (ds.labelCell) {
|
|
351
|
+
try {
|
|
352
|
+
label = this.getters.evaluateFormula(ds.labelCell, definition.sheetId);
|
|
353
|
+
} catch (e) {
|
|
354
|
+
// We want here to catch issue linked to async formula
|
|
355
|
+
label = `${chartTerms.Series} ${parseInt(dsIndex) + 1}`;
|
|
356
|
+
}
|
|
357
|
+
} else {
|
|
358
|
+
label = `${chartTerms.Series} ${parseInt(dsIndex) + 1}`;
|
|
359
|
+
}
|
|
360
|
+
const dataset = {
|
|
361
|
+
label,
|
|
362
|
+
data: this.extractDataFromRange(definition.sheetId, ds.dataRange),
|
|
363
|
+
lineTension: 0, // 0 -> render straight lines, which is much faster
|
|
364
|
+
borderColor: definition.type !== "pie" ? GraphColors[graphColorIndex] : "#FFFFFF", // white border for pie chart
|
|
365
|
+
backgroundColor: GraphColors[graphColorIndex],
|
|
366
|
+
};
|
|
367
|
+
if (definition.type === "pie") {
|
|
368
|
+
// In case of pie graph, dataset.backgroundColor is an array of string
|
|
369
|
+
// @ts-ignore
|
|
370
|
+
dataset.backgroundColor = pieColors;
|
|
371
|
+
}
|
|
372
|
+
graphColorIndex = ++graphColorIndex % GraphColors.length;
|
|
373
|
+
runtime.data!.datasets!.push(dataset);
|
|
374
|
+
}
|
|
375
|
+
return runtime;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private addChartFigure(sheetId: string, figure: Figure<ChartDefinition>) {
|
|
379
|
+
this.dispatch("CREATE_FIGURE", {
|
|
380
|
+
sheet: sheetId,
|
|
381
|
+
figure,
|
|
382
|
+
});
|
|
383
|
+
this.history.updateLocalState(["chartFigures"], new Set(this.chartFigures).add(figure.id));
|
|
384
|
+
this.history.updateLocalState(
|
|
385
|
+
["chartRuntime", figure.id],
|
|
386
|
+
this.mapDefinitionToRuntime(figure.data)
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
private isCellUsedInChart(chart: ChartDefinition, col: number, row: number): boolean {
|
|
391
|
+
if (isInside(col, row, toZone(chart.labelRange))) {
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
for (let db of chart.dataSets) {
|
|
395
|
+
if (db.dataRange && db.dataRange.length > 0 && isInside(col, row, toZone(db.dataRange))) {
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
if (db.labelCell && db.labelCell.length > 0 && isInside(col, row, toZone(db.labelCell))) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
}
|