pict-section-form 1.3.10 → 1.3.12
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/package.json
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Scoped CSS for the chart input's grouped legend. Registered once by
|
|
4
|
+
* Pict-Provider-Input-Chart via
|
|
5
|
+
* `pict.CSSMap.addCSS('Pict-Input-Chart-CSS', libCSS, 500)`.
|
|
6
|
+
*
|
|
7
|
+
* Chart.js's own legend is a single flow-wrapped row of swatches. On a control
|
|
8
|
+
* chart that is eight entries of near-identical colour, with the limit lines
|
|
9
|
+
* indistinguishable from the readings, wrapping into an unreadable block. This
|
|
10
|
+
* legend is a vertical list, grouped, with each group toggleable as a unit.
|
|
11
|
+
*
|
|
12
|
+
* Everything is themeable through the --theme-color-* tokens; the hardcoded
|
|
13
|
+
* values are fallbacks.
|
|
14
|
+
*/
|
|
15
|
+
const libCSS = /*css*/`
|
|
16
|
+
.pict-chart-shell { display: flex; align-items: stretch; gap: 0.75rem; width: 100%; }
|
|
17
|
+
.pict-chart-shell.pict-chart-legend-top { flex-direction: column-reverse; }
|
|
18
|
+
.pict-chart-shell.pict-chart-legend-bottom { flex-direction: column; }
|
|
19
|
+
.pict-chart-shell .pict-chart-canvas-area { flex: 1 1 auto; min-width: 0; position: relative; }
|
|
20
|
+
|
|
21
|
+
.pict-chart-legend { flex: 0 0 auto; max-width: 15rem; font-size: 0.78rem; line-height: 1.35;
|
|
22
|
+
color: var(--theme-color-text-primary, #1f2733); user-select: none; }
|
|
23
|
+
.pict-chart-legend-top .pict-chart-legend,
|
|
24
|
+
.pict-chart-legend-bottom .pict-chart-legend { max-width: none; }
|
|
25
|
+
|
|
26
|
+
.pict-chart-legend-group + .pict-chart-legend-group { margin-top: 0.5rem; }
|
|
27
|
+
.pict-chart-legend-group-header { display: flex; align-items: center; gap: 0.35rem; cursor: pointer;
|
|
28
|
+
font-size: 0.68rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase;
|
|
29
|
+
color: var(--theme-color-text-muted, #6b7686); padding: 0.15rem 0; }
|
|
30
|
+
.pict-chart-legend-group-header:hover { color: var(--theme-color-text-primary, #1f2733); }
|
|
31
|
+
.pict-chart-legend-group-caret { font-size: 0.6rem; transition: transform 0.12s ease; }
|
|
32
|
+
.pict-chart-legend-group.pict-collapsed .pict-chart-legend-group-caret { transform: rotate(-90deg); }
|
|
33
|
+
.pict-chart-legend-group.pict-collapsed .pict-chart-legend-items { display: none; }
|
|
34
|
+
/* A group whose series are ALL hidden is dimmed as a whole, so the group toggle
|
|
35
|
+
reads as a control rather than decoration. */
|
|
36
|
+
.pict-chart-legend-group.pict-group-off .pict-chart-legend-group-header { opacity: 0.45; }
|
|
37
|
+
|
|
38
|
+
.pict-chart-legend-items { display: flex; flex-direction: column; gap: 0.1rem; }
|
|
39
|
+
.pict-chart-legend-top .pict-chart-legend-items,
|
|
40
|
+
.pict-chart-legend-bottom .pict-chart-legend-items { flex-direction: row; flex-wrap: wrap; gap: 0.1rem 0.75rem; }
|
|
41
|
+
|
|
42
|
+
.pict-chart-legend-item { display: flex; align-items: center; gap: 0.4rem; cursor: pointer;
|
|
43
|
+
padding: 0.1rem 0.2rem; border-radius: 4px; }
|
|
44
|
+
.pict-chart-legend-item:hover { background: var(--theme-color-background-hover, rgba(0,0,0,0.04)); }
|
|
45
|
+
.pict-chart-legend-item.pict-hidden { opacity: 0.4; }
|
|
46
|
+
.pict-chart-legend-item.pict-hidden .pict-chart-legend-label { text-decoration: line-through; }
|
|
47
|
+
.pict-chart-legend-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
48
|
+
|
|
49
|
+
/* The swatch carries the series' identity: colour, and whether it is a solid
|
|
50
|
+
line, a dashed one, or unconnected markers. A legend that shows every entry
|
|
51
|
+
as the same solid block cannot distinguish a reading from its control limit. */
|
|
52
|
+
.pict-chart-legend-swatch { flex: 0 0 auto; width: 1.4rem; height: 0.7rem; position: relative; }
|
|
53
|
+
.pict-chart-legend-swatch::before { content: ''; position: absolute; left: 0; right: 0; top: 50%;
|
|
54
|
+
transform: translateY(-50%); height: 0.15rem; border-radius: 1px; background: currentColor; }
|
|
55
|
+
.pict-chart-legend-swatch.pict-swatch-dashed::before {
|
|
56
|
+
background: repeating-linear-gradient(to right, currentColor 0 0.28rem, transparent 0.28rem 0.5rem); }
|
|
57
|
+
.pict-chart-legend-swatch.pict-swatch-dotted::before {
|
|
58
|
+
background: repeating-linear-gradient(to right, currentColor 0 0.12rem, transparent 0.12rem 0.28rem); }
|
|
59
|
+
.pict-chart-legend-swatch.pict-swatch-markers::before { display: none; }
|
|
60
|
+
.pict-chart-legend-swatch.pict-swatch-markers::after { content: ''; position: absolute; left: 50%; top: 50%;
|
|
61
|
+
transform: translate(-50%, -50%); width: 0.45rem; height: 0.45rem; border-radius: 50%; background: currentColor; }
|
|
62
|
+
|
|
63
|
+
.pict-chart-legend-toggle { display: inline-flex; align-items: center; gap: 0.3rem; cursor: pointer;
|
|
64
|
+
font-size: 0.68rem; color: var(--theme-color-text-muted, #6b7686);
|
|
65
|
+
background: none; border: 1px solid var(--theme-color-border-light, #e8ebf0);
|
|
66
|
+
border-radius: 5px; padding: 0.1rem 0.4rem; margin-bottom: 0.35rem; }
|
|
67
|
+
.pict-chart-legend-toggle:hover { color: var(--theme-color-text-primary, #1f2733);
|
|
68
|
+
border-color: var(--theme-color-border-strong, #c2c9d2); }
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
module.exports = libCSS;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const libPictSectionInputExtension = require('../Pict-Provider-InputExtension.js');
|
|
2
|
+
const libChartCSS = require('./Pict-Provider-Input-Chart-CSS.js');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* CustomInputHandler class.
|
|
@@ -127,6 +128,220 @@ class CustomInputHandler extends libPictSectionInputExtension
|
|
|
127
128
|
|
|
128
129
|
this.currentChartObjects = {};
|
|
129
130
|
this.currentChartDataObjects = {};
|
|
131
|
+
// Legend visibility + collapsed groups, per input hash, so a redraw does not
|
|
132
|
+
// undo what the user toggled.
|
|
133
|
+
this.currentChartLegendState = {};
|
|
134
|
+
|
|
135
|
+
if (this.pict && this.pict.CSSMap && typeof this.pict.CSSMap.addCSS === 'function')
|
|
136
|
+
{
|
|
137
|
+
this.pict.CSSMap.addCSS('Pict-Input-Chart-CSS', libChartCSS, 500);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Classify a dataset's line style for its legend swatch, so a reading, a
|
|
143
|
+
* dashed control limit and an unconnected marker series are told apart at a
|
|
144
|
+
* glance. Chart.js's own legend draws every entry as the same block.
|
|
145
|
+
*
|
|
146
|
+
* @param {Object} pDataset - The Chart.js dataset.
|
|
147
|
+
* @return {string} A swatch modifier class.
|
|
148
|
+
*/
|
|
149
|
+
_legendSwatchClass(pDataset)
|
|
150
|
+
{
|
|
151
|
+
if (pDataset.showLine === false) { return 'pict-swatch-markers'; }
|
|
152
|
+
const tmpDash = Array.isArray(pDataset.borderDash) ? pDataset.borderDash : null;
|
|
153
|
+
if (!tmpDash || tmpDash.length < 1) { return ''; }
|
|
154
|
+
// A short first segment reads as dotted, a long one as dashed.
|
|
155
|
+
return (Number(tmpDash[0]) <= 3) ? 'pict-swatch-dotted' : 'pict-swatch-dashed';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Group the datasets for the legend. A dataset declares its group with
|
|
160
|
+
* `LegendGroup` on the solver entry (carried onto the dataset as
|
|
161
|
+
* `pictLegendGroup`); anything that does not is "Data". Group ORDER follows
|
|
162
|
+
* first appearance, so configuration controls it rather than sort order.
|
|
163
|
+
*
|
|
164
|
+
* @param {Object} pChart - The Chart.js instance.
|
|
165
|
+
* @return {Array<{Key: string, Items: Array<{Index: number, Dataset: Object}>}>}
|
|
166
|
+
*/
|
|
167
|
+
_legendGroups(pChart)
|
|
168
|
+
{
|
|
169
|
+
const tmpGroups = [];
|
|
170
|
+
const tmpIndex = {};
|
|
171
|
+
const tmpDatasets = (pChart && pChart.data && pChart.data.datasets) || [];
|
|
172
|
+
for (let i = 0; i < tmpDatasets.length; i++)
|
|
173
|
+
{
|
|
174
|
+
const tmpKey = tmpDatasets[i].pictLegendGroup || 'Data';
|
|
175
|
+
if (!(tmpKey in tmpIndex))
|
|
176
|
+
{
|
|
177
|
+
tmpIndex[tmpKey] = tmpGroups.length;
|
|
178
|
+
tmpGroups.push({ Key: tmpKey, Items: [] });
|
|
179
|
+
}
|
|
180
|
+
tmpGroups[tmpIndex[tmpKey]].Items.push({ Index: i, Dataset: tmpDatasets[i] });
|
|
181
|
+
}
|
|
182
|
+
return tmpGroups;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Build (or rebuild) the grouped HTML legend beside a chart's canvas.
|
|
187
|
+
*
|
|
188
|
+
* The legend is created in the DOM rather than in the template because there
|
|
189
|
+
* are several chart templates (the base set plus per-theme copies) and a
|
|
190
|
+
* legend added to only some of them is the kind of divergence that goes
|
|
191
|
+
* unnoticed. Chart.js's built-in legend is switched off whenever this runs.
|
|
192
|
+
*
|
|
193
|
+
* @param {string} pRawHTMLID - The input's Macro.RawHTMLID.
|
|
194
|
+
* @param {Object} pChart - The Chart.js instance.
|
|
195
|
+
* @param {Object} pLegendConfiguration - The resolved ChartLegend config.
|
|
196
|
+
*/
|
|
197
|
+
renderChartLegend(pRawHTMLID, pChart, pLegendConfiguration)
|
|
198
|
+
{
|
|
199
|
+
if (typeof (document) === 'undefined' || !pChart || !pChart.canvas) { return false; }
|
|
200
|
+
const tmpCanvas = pChart.canvas;
|
|
201
|
+
const tmpContainer = tmpCanvas.parentElement;
|
|
202
|
+
if (!tmpContainer) { return false; }
|
|
203
|
+
|
|
204
|
+
const tmpState = this.currentChartLegendState[pRawHTMLID] = this.currentChartLegendState[pRawHTMLID] || { Hidden: false, Collapsed: {} };
|
|
205
|
+
const tmpPosition = pLegendConfiguration.Position || 'right';
|
|
206
|
+
|
|
207
|
+
// Wrap the canvas ONCE: shell > (canvas area | legend).
|
|
208
|
+
//
|
|
209
|
+
// After the first wrap the container's parent is the canvas AREA, not the
|
|
210
|
+
// shell, so testing the immediate parent for 'pict-chart-shell' never matches
|
|
211
|
+
// again and every re-render nests another shell inside the last -- 14 shells
|
|
212
|
+
// and 14 legends for 3 charts after a handful of toggles. Detect the wrapped
|
|
213
|
+
// shape by its own marker and climb to the shell.
|
|
214
|
+
let tmpShell = null;
|
|
215
|
+
const tmpExistingArea = tmpContainer.parentElement;
|
|
216
|
+
if (tmpExistingArea && tmpExistingArea.classList.contains('pict-chart-canvas-area')
|
|
217
|
+
&& tmpExistingArea.parentElement && tmpExistingArea.parentElement.classList.contains('pict-chart-shell'))
|
|
218
|
+
{
|
|
219
|
+
tmpShell = tmpExistingArea.parentElement;
|
|
220
|
+
}
|
|
221
|
+
if (!tmpShell)
|
|
222
|
+
{
|
|
223
|
+
tmpShell = document.createElement('div');
|
|
224
|
+
tmpShell.className = 'pict-chart-shell';
|
|
225
|
+
tmpContainer.parentNode.insertBefore(tmpShell, tmpContainer);
|
|
226
|
+
const tmpArea = document.createElement('div');
|
|
227
|
+
tmpArea.className = 'pict-chart-canvas-area';
|
|
228
|
+
tmpShell.appendChild(tmpArea);
|
|
229
|
+
tmpArea.appendChild(tmpContainer);
|
|
230
|
+
}
|
|
231
|
+
tmpShell.classList.remove('pict-chart-legend-top', 'pict-chart-legend-bottom');
|
|
232
|
+
if (tmpPosition === 'top') { tmpShell.classList.add('pict-chart-legend-top'); }
|
|
233
|
+
if (tmpPosition === 'bottom') { tmpShell.classList.add('pict-chart-legend-bottom'); }
|
|
234
|
+
|
|
235
|
+
let tmpLegend = tmpShell.querySelector(':scope > .pict-chart-legend');
|
|
236
|
+
if (!tmpLegend)
|
|
237
|
+
{
|
|
238
|
+
tmpLegend = document.createElement('div');
|
|
239
|
+
tmpLegend.className = 'pict-chart-legend';
|
|
240
|
+
tmpShell.appendChild(tmpLegend);
|
|
241
|
+
}
|
|
242
|
+
tmpLegend.innerHTML = '';
|
|
243
|
+
|
|
244
|
+
// Show/hide the whole legend. Charts differ in how much room they have,
|
|
245
|
+
// so this is per-chart and remembered for the session.
|
|
246
|
+
if (pLegendConfiguration.AllowHide !== false)
|
|
247
|
+
{
|
|
248
|
+
const tmpToggle = document.createElement('button');
|
|
249
|
+
tmpToggle.type = 'button';
|
|
250
|
+
tmpToggle.className = 'pict-chart-legend-toggle';
|
|
251
|
+
tmpToggle.textContent = tmpState.Hidden ? 'Show legend' : 'Hide legend';
|
|
252
|
+
tmpToggle.addEventListener('click', () =>
|
|
253
|
+
{
|
|
254
|
+
tmpState.Hidden = !tmpState.Hidden;
|
|
255
|
+
this.renderChartLegend(pRawHTMLID, pChart, pLegendConfiguration);
|
|
256
|
+
});
|
|
257
|
+
tmpLegend.appendChild(tmpToggle);
|
|
258
|
+
}
|
|
259
|
+
if (tmpState.Hidden) { return true; }
|
|
260
|
+
|
|
261
|
+
const tmpGroups = this._legendGroups(pChart);
|
|
262
|
+
for (const tmpGroup of tmpGroups)
|
|
263
|
+
{
|
|
264
|
+
const tmpGroupElement = document.createElement('div');
|
|
265
|
+
tmpGroupElement.className = 'pict-chart-legend-group';
|
|
266
|
+
const tmpAllHidden = tmpGroup.Items.every((pItem) => pChart.getDatasetMeta(pItem.Index).hidden === true);
|
|
267
|
+
if (tmpAllHidden) { tmpGroupElement.classList.add('pict-group-off'); }
|
|
268
|
+
if (tmpState.Collapsed[tmpGroup.Key]) { tmpGroupElement.classList.add('pict-collapsed'); }
|
|
269
|
+
|
|
270
|
+
const tmpHeader = document.createElement('div');
|
|
271
|
+
tmpHeader.className = 'pict-chart-legend-group-header';
|
|
272
|
+
const tmpCaret = document.createElement('span');
|
|
273
|
+
tmpCaret.className = 'pict-chart-legend-group-caret';
|
|
274
|
+
tmpCaret.textContent = '▾';
|
|
275
|
+
tmpHeader.appendChild(tmpCaret);
|
|
276
|
+
const tmpHeaderLabel = document.createElement('span');
|
|
277
|
+
tmpHeaderLabel.textContent = tmpGroup.Key;
|
|
278
|
+
tmpHeader.appendChild(tmpHeaderLabel);
|
|
279
|
+
// Header click toggles the GROUP's series on/off; the caret collapses it.
|
|
280
|
+
tmpHeader.addEventListener('click', (pEvent) =>
|
|
281
|
+
{
|
|
282
|
+
if (pEvent.target === tmpCaret)
|
|
283
|
+
{
|
|
284
|
+
tmpState.Collapsed[tmpGroup.Key] = !tmpState.Collapsed[tmpGroup.Key];
|
|
285
|
+
}
|
|
286
|
+
else
|
|
287
|
+
{
|
|
288
|
+
const tmpTurnOn = tmpAllHidden;
|
|
289
|
+
for (const tmpItem of tmpGroup.Items)
|
|
290
|
+
{
|
|
291
|
+
pChart.getDatasetMeta(tmpItem.Index).hidden = tmpTurnOn ? false : true;
|
|
292
|
+
}
|
|
293
|
+
pChart.update();
|
|
294
|
+
}
|
|
295
|
+
this.renderChartLegend(pRawHTMLID, pChart, pLegendConfiguration);
|
|
296
|
+
});
|
|
297
|
+
tmpGroupElement.appendChild(tmpHeader);
|
|
298
|
+
|
|
299
|
+
const tmpItems = document.createElement('div');
|
|
300
|
+
tmpItems.className = 'pict-chart-legend-items';
|
|
301
|
+
for (const tmpItem of tmpGroup.Items)
|
|
302
|
+
{
|
|
303
|
+
const tmpHidden = pChart.getDatasetMeta(tmpItem.Index).hidden === true;
|
|
304
|
+
const tmpItemElement = document.createElement('div');
|
|
305
|
+
tmpItemElement.className = 'pict-chart-legend-item' + (tmpHidden ? ' pict-hidden' : '');
|
|
306
|
+
const tmpSwatch = document.createElement('span');
|
|
307
|
+
tmpSwatch.className = ('pict-chart-legend-swatch ' + this._legendSwatchClass(tmpItem.Dataset)).trim();
|
|
308
|
+
tmpSwatch.style.color = tmpItem.Dataset.borderColor || tmpItem.Dataset.backgroundColor || '#868E96';
|
|
309
|
+
tmpItemElement.appendChild(tmpSwatch);
|
|
310
|
+
const tmpLabel = document.createElement('span');
|
|
311
|
+
tmpLabel.className = 'pict-chart-legend-label';
|
|
312
|
+
tmpLabel.textContent = tmpItem.Dataset.label || `Series ${tmpItem.Index + 1}`;
|
|
313
|
+
tmpLabel.title = tmpLabel.textContent;
|
|
314
|
+
tmpItemElement.appendChild(tmpLabel);
|
|
315
|
+
tmpItemElement.addEventListener('click', () =>
|
|
316
|
+
{
|
|
317
|
+
pChart.getDatasetMeta(tmpItem.Index).hidden = !tmpHidden;
|
|
318
|
+
pChart.update();
|
|
319
|
+
this.renderChartLegend(pRawHTMLID, pChart, pLegendConfiguration);
|
|
320
|
+
});
|
|
321
|
+
tmpItems.appendChild(tmpItemElement);
|
|
322
|
+
}
|
|
323
|
+
tmpGroupElement.appendChild(tmpItems);
|
|
324
|
+
tmpLegend.appendChild(tmpGroupElement);
|
|
325
|
+
}
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Resolve the ChartLegend configuration for an input. Returns null when the
|
|
331
|
+
* grouped legend is not in use, in which case Chart.js's own legend stands.
|
|
332
|
+
*
|
|
333
|
+
* @param {Object} pInput - The PictForm input object.
|
|
334
|
+
* @return {Object|null}
|
|
335
|
+
*/
|
|
336
|
+
resolveChartLegendConfiguration(pInput)
|
|
337
|
+
{
|
|
338
|
+
const tmpPictform = (pInput && pInput.PictForm) || {};
|
|
339
|
+
const tmpLegend = tmpPictform.ChartLegend;
|
|
340
|
+
if (!tmpLegend) { return null; }
|
|
341
|
+
if (tmpLegend === true) { return { Position: 'right' }; }
|
|
342
|
+
if (typeof (tmpLegend) !== 'object') { return null; }
|
|
343
|
+
if (tmpLegend.Enabled === false) { return null; }
|
|
344
|
+
return tmpLegend;
|
|
130
345
|
}
|
|
131
346
|
|
|
132
347
|
/**
|
|
@@ -525,6 +740,14 @@ class CustomInputHandler extends libPictSectionInputExtension
|
|
|
525
740
|
Object.assign(tmpDataObject, tmpCurrentSolverExpression.DatasetOptions);
|
|
526
741
|
}
|
|
527
742
|
|
|
743
|
+
// Which legend group this series belongs to ("Data", "Limits", …).
|
|
744
|
+
// Carried as a pict-prefixed key so it cannot collide with a Chart.js
|
|
745
|
+
// dataset property.
|
|
746
|
+
if (tmpCurrentSolverExpression.LegendGroup)
|
|
747
|
+
{
|
|
748
|
+
tmpDataObject.pictLegendGroup = tmpCurrentSolverExpression.LegendGroup;
|
|
749
|
+
}
|
|
750
|
+
|
|
528
751
|
if (tmpParsingConfiguration.ObjectType === 'array')
|
|
529
752
|
{
|
|
530
753
|
if (Array.isArray(tmpSolvedDataSet))
|
|
@@ -641,6 +864,22 @@ class CustomInputHandler extends libPictSectionInputExtension
|
|
|
641
864
|
// would make a single intent ("make this chart taller") into two coupled settings that are wrong
|
|
642
865
|
// in isolation, so it is derived here. An explicit maintainAspectRatio in the core prototype still
|
|
643
866
|
// wins -- this only fills in the value the caller did not state.
|
|
867
|
+
// The grouped legend replaces Chart.js's own, which is a single wrapped row
|
|
868
|
+
// of identical swatches -- unreadable once a chart carries a reading, a
|
|
869
|
+
// running average and three pairs of control limits.
|
|
870
|
+
if (this.resolveChartLegendConfiguration(pInput))
|
|
871
|
+
{
|
|
872
|
+
if (typeof (tmpChartConfiguration.options) !== 'object' || tmpChartConfiguration.options === null)
|
|
873
|
+
{
|
|
874
|
+
tmpChartConfiguration.options = {};
|
|
875
|
+
}
|
|
876
|
+
if (typeof (tmpChartConfiguration.options.plugins) !== 'object' || tmpChartConfiguration.options.plugins === null)
|
|
877
|
+
{
|
|
878
|
+
tmpChartConfiguration.options.plugins = {};
|
|
879
|
+
}
|
|
880
|
+
tmpChartConfiguration.options.plugins.legend = Object.assign({}, tmpChartConfiguration.options.plugins.legend, { display: false });
|
|
881
|
+
}
|
|
882
|
+
|
|
644
883
|
const tmpChartHeight = parseFloat(tmpPictform.ChartHeight);
|
|
645
884
|
if (isFinite(tmpChartHeight) && (tmpChartHeight > 0))
|
|
646
885
|
{
|
|
@@ -687,6 +926,12 @@ class CustomInputHandler extends libPictSectionInputExtension
|
|
|
687
926
|
this.currentChartObjects[`Object-For-${pInput.Macro.RawHTMLID}`] = new window.Chart(tmpChartCanvasElement, tmpChartConfiguration);
|
|
688
927
|
// TODO: Make this invalidation better.
|
|
689
928
|
this.currentChartDataObjects[`Data-For-${pInput.Macro.RawHTMLID}`] = JSON.stringify(tmpChartConfiguration.data);
|
|
929
|
+
|
|
930
|
+
const tmpLegendConfiguration = this.resolveChartLegendConfiguration(pInput);
|
|
931
|
+
if (tmpLegendConfiguration)
|
|
932
|
+
{
|
|
933
|
+
this.renderChartLegend(pInput.Macro.RawHTMLID, this.currentChartObjects[`Object-For-${pInput.Macro.RawHTMLID}`], tmpLegendConfiguration);
|
|
934
|
+
}
|
|
690
935
|
}
|
|
691
936
|
}
|
|
692
937
|
|
|
@@ -782,6 +1027,11 @@ class CustomInputHandler extends libPictSectionInputExtension
|
|
|
782
1027
|
{
|
|
783
1028
|
this.currentChartObjects[`Object-For-${pInput.Macro.RawHTMLID}`].data = tmpChartConfiguration.data;
|
|
784
1029
|
this.currentChartObjects[`Object-For-${pInput.Macro.RawHTMLID}`].update();
|
|
1030
|
+
const tmpUpdatedLegendConfiguration = this.resolveChartLegendConfiguration(pInput);
|
|
1031
|
+
if (tmpUpdatedLegendConfiguration)
|
|
1032
|
+
{
|
|
1033
|
+
this.renderChartLegend(pInput.Macro.RawHTMLID, this.currentChartObjects[`Object-For-${pInput.Macro.RawHTMLID}`], tmpUpdatedLegendConfiguration);
|
|
1034
|
+
}
|
|
785
1035
|
this.currentChartDataObjects[`Data-For-${pInput.Macro.RawHTMLID}`] = tmpNewChartDataString;
|
|
786
1036
|
}
|
|
787
1037
|
}
|
|
@@ -191,6 +191,135 @@ suite('Pict-Provider-Input-Chart', () =>
|
|
|
191
191
|
});
|
|
192
192
|
});
|
|
193
193
|
|
|
194
|
+
suite('grouped legend', () =>
|
|
195
|
+
{
|
|
196
|
+
const provider = () => new libChartInput(new libPict(), {});
|
|
197
|
+
|
|
198
|
+
test('ChartLegend is off unless configured, leaving Chart.js\'s own legend alone', () =>
|
|
199
|
+
{
|
|
200
|
+
const tmpProvider = provider();
|
|
201
|
+
Expect(tmpProvider.resolveChartLegendConfiguration({ PictForm: {} })).to.equal(null);
|
|
202
|
+
Expect(tmpProvider.resolveChartLegendConfiguration({ PictForm: { ChartLegend: { Enabled: false } } })).to.equal(null);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('ChartLegend: true is shorthand for a right-hand legend', () =>
|
|
206
|
+
{
|
|
207
|
+
Expect(provider().resolveChartLegendConfiguration({ PictForm: { ChartLegend: true } })).to.deep.equal({ Position: 'right' });
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('configuring it disables the built-in legend', () =>
|
|
211
|
+
{
|
|
212
|
+
const tmpPict = new libPict();
|
|
213
|
+
const tmpProvider = new libChartInput(tmpPict, {});
|
|
214
|
+
tmpPict.providers.DynamicSolver = { runSolver: () => [ 1, 2 ] };
|
|
215
|
+
const tmpConfiguration = tmpProvider.getInputChartConfiguration({ }, {
|
|
216
|
+
Name: 'C', Hash: 'C', DataType: 'Object',
|
|
217
|
+
Macro: { RawHTMLID: 'C', InputFullProperties: '', InputChangeHandler: '', ControlAttr: '' },
|
|
218
|
+
PictForm: { InputType: 'Chart', ChartType: 'line', ChartLegend: true, ChartDatasetsSolvers: [ { Label: 'A', DataSolver: 'x' } ] },
|
|
219
|
+
}, {});
|
|
220
|
+
Expect(tmpConfiguration.options.plugins.legend.display).to.equal(false);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('LegendGroup lands on the dataset as pictLegendGroup', () =>
|
|
224
|
+
{
|
|
225
|
+
const tmpPict = new libPict();
|
|
226
|
+
const tmpProvider = new libChartInput(tmpPict, {});
|
|
227
|
+
tmpPict.providers.DynamicSolver = { runSolver: () => [ 1 ] };
|
|
228
|
+
const tmpConfiguration = tmpProvider.getInputChartConfiguration({ }, {
|
|
229
|
+
Name: 'C', Hash: 'C', DataType: 'Object',
|
|
230
|
+
Macro: { RawHTMLID: 'C', InputFullProperties: '', InputChangeHandler: '', ControlAttr: '' },
|
|
231
|
+
PictForm: { InputType: 'Chart', ChartType: 'line', ChartDatasetsSolvers: [
|
|
232
|
+
{ Label: 'Reading', DataSolver: 'x' },
|
|
233
|
+
{ Label: 'Limit', DataSolver: 'y', LegendGroup: 'Limits' } ] },
|
|
234
|
+
}, {});
|
|
235
|
+
Expect(tmpConfiguration.data.datasets[0].pictLegendGroup).to.equal(undefined);
|
|
236
|
+
Expect(tmpConfiguration.data.datasets[1].pictLegendGroup).to.equal('Limits');
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('datasets group by LegendGroup, ungrouped ones falling under Data, in configuration order', () =>
|
|
240
|
+
{
|
|
241
|
+
const tmpChart = { data: { datasets: [
|
|
242
|
+
{ label: 'Reading' },
|
|
243
|
+
{ label: 'Upper', pictLegendGroup: 'Limits' },
|
|
244
|
+
{ label: 'Average' },
|
|
245
|
+
{ label: 'Lower', pictLegendGroup: 'Limits' } ] } };
|
|
246
|
+
const tmpGroups = provider()._legendGroups(tmpChart);
|
|
247
|
+
Expect(tmpGroups.map((pGroup) => pGroup.Key)).to.deep.equal([ 'Data', 'Limits' ]);
|
|
248
|
+
Expect(tmpGroups[0].Items.map((pItem) => pItem.Index)).to.deep.equal([ 0, 2 ]);
|
|
249
|
+
Expect(tmpGroups[1].Items.map((pItem) => pItem.Index)).to.deep.equal([ 1, 3 ]);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test('the swatch distinguishes solid, dashed, dotted and marker-only series', () =>
|
|
253
|
+
{
|
|
254
|
+
const tmpProvider = provider();
|
|
255
|
+
Expect(tmpProvider._legendSwatchClass({}), 'solid').to.equal('');
|
|
256
|
+
Expect(tmpProvider._legendSwatchClass({ borderDash: [ 6, 4 ] }), 'dashed').to.equal('pict-swatch-dashed');
|
|
257
|
+
Expect(tmpProvider._legendSwatchClass({ borderDash: [ 2, 3 ] }), 'dotted').to.equal('pict-swatch-dotted');
|
|
258
|
+
Expect(tmpProvider._legendSwatchClass({ showLine: false }), 'markers').to.equal('pict-swatch-markers');
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test('a marker-only series wins over its dash pattern', () =>
|
|
262
|
+
{
|
|
263
|
+
Expect(provider()._legendSwatchClass({ showLine: false, borderDash: [ 6, 4 ] })).to.equal('pict-swatch-markers');
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test('legend state is per input, so one chart\'s toggles do not move another\'s', () =>
|
|
267
|
+
{
|
|
268
|
+
const tmpProvider = provider();
|
|
269
|
+
tmpProvider.currentChartLegendState['ChartA'] = { Hidden: true, Collapsed: { Limits: true } };
|
|
270
|
+
tmpProvider.currentChartLegendState['ChartB'] = { Hidden: false, Collapsed: {} };
|
|
271
|
+
Expect(tmpProvider.currentChartLegendState['ChartA'].Hidden).to.equal(true);
|
|
272
|
+
Expect(tmpProvider.currentChartLegendState['ChartB'].Hidden).to.equal(false);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
suite('legend re-entrancy', () =>
|
|
277
|
+
{
|
|
278
|
+
// renderChartLegend runs again on every data refresh and on every toggle.
|
|
279
|
+
// It must reuse the wrapper it already built. It did not: after the first
|
|
280
|
+
// wrap the container's parent is the canvas AREA rather than the shell, so
|
|
281
|
+
// the guard never matched and each call nested another shell — 14 shells and
|
|
282
|
+
// 14 legends for 3 charts in the browser.
|
|
283
|
+
const buildChart = () =>
|
|
284
|
+
{
|
|
285
|
+
const tmpCanvas = document.createElement('canvas');
|
|
286
|
+
const tmpContainer = document.createElement('div');
|
|
287
|
+
tmpContainer.appendChild(tmpCanvas);
|
|
288
|
+
document.body.appendChild(tmpContainer);
|
|
289
|
+
return {
|
|
290
|
+
canvas: tmpCanvas,
|
|
291
|
+
data: { datasets: [ { label: 'Reading' }, { label: 'Limit', pictLegendGroup: 'Limits' } ] },
|
|
292
|
+
getDatasetMeta: () => ({ hidden: false }),
|
|
293
|
+
update: () => {},
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
test('re-rendering reuses the shell instead of nesting another', () =>
|
|
298
|
+
{
|
|
299
|
+
const tmpProvider = new libChartInput(new libPict(), {});
|
|
300
|
+
const tmpChart = buildChart();
|
|
301
|
+
for (let i = 0; i < 5; i++)
|
|
302
|
+
{
|
|
303
|
+
tmpProvider.renderChartLegend('ReentrantChart', tmpChart, { Position: 'right' });
|
|
304
|
+
}
|
|
305
|
+
Expect(document.querySelectorAll('.pict-chart-shell').length, 'shells').to.equal(1);
|
|
306
|
+
Expect(document.querySelectorAll('.pict-chart-legend').length, 'legend panels').to.equal(1);
|
|
307
|
+
Expect(document.querySelectorAll('.pict-chart-shell .pict-chart-shell').length, 'nested shells').to.equal(0);
|
|
308
|
+
// Two datasets in two groups → two items, not ten.
|
|
309
|
+
Expect(document.querySelectorAll('.pict-chart-legend-item').length, 'items').to.equal(2);
|
|
310
|
+
Expect(tmpChart.canvas.closest('.pict-chart-canvas-area'), 'canvas still inside the area').to.be.ok;
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test('the canvas is never detached from the document by a re-render', () =>
|
|
314
|
+
{
|
|
315
|
+
const tmpProvider = new libChartInput(new libPict(), {});
|
|
316
|
+
const tmpChart = buildChart();
|
|
317
|
+
tmpProvider.renderChartLegend('AttachedChart', tmpChart, { Position: 'right' });
|
|
318
|
+
tmpProvider.renderChartLegend('AttachedChart', tmpChart, { Position: 'right' });
|
|
319
|
+
Expect(document.body.contains(tmpChart.canvas)).to.equal(true);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
194
323
|
suite('template', () =>
|
|
195
324
|
{
|
|
196
325
|
test('the chart canvas container carries the ChartHeight min-height', () =>
|