pict-section-form 1.3.8 → 1.3.10

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pict-section-form",
3
- "version": "1.3.8",
3
+ "version": "1.3.10",
4
4
  "description": "Pict dynamic forms",
5
5
  "main": "source/Pict-Section-Form.js",
6
6
  "directories": {
@@ -263,7 +263,11 @@ Glug glug glug Oo... -->
263
263
  <div style="width:{~DWAF:Record.PictForm.QuantizedWidth:100~}%; display:inline-block;">
264
264
  <input type="hidden" id="CONFIG-FOR-{~D:Record.Macro.RawHTMLID~}" value="">
265
265
  <h3><span>{~D:Record.Name~}:</span></h3>
266
- <div style="width:100%;"><canvas id="CANVAS-FOR-{~D:Record.Macro.RawHTMLID~}" {~D:Record.Macro.ControlAttr~} {~D:Record.Macro.InputFullProperties~}></canvas></div>
266
+ <!-- ChartHeight (pixels) sets the plot area. It is min-height rather than height so the
267
+ default of 0 is a true no-op for every chart that never sets it: Chart.js keeps sizing
268
+ the canvas by its 2:1 aspect ratio exactly as before. When it IS set, the provider turns
269
+ maintainAspectRatio off so the canvas fills this box instead of ignoring it. -->
270
+ <div style="width:100%; min-height:{~DWAF:Record.PictForm.ChartHeight:0~}px;"><canvas id="CANVAS-FOR-{~D:Record.Macro.RawHTMLID~}" {~D:Record.Macro.ControlAttr~} {~D:Record.Macro.InputFullProperties~}></canvas></div>
267
271
  </div>
268
272
  `
269
273
  },
@@ -514,6 +514,17 @@ class CustomInputHandler extends libPictSectionInputExtension
514
514
  tmpDataObject.stack = tmpCurrentSolverExpression.StackGroup;
515
515
  }
516
516
 
517
+ // DatasetOptions is the general escape hatch: every key is merged onto
518
+ // the Chart.js dataset as-is. Without it a dataset could only carry the
519
+ // handful of properties named above, so a series could not be given a
520
+ // colour, a dash pattern, or told not to join its points -- the config
521
+ // was silently dropped and the chart drew in default colours. It is
522
+ // applied last, so it wins over the named options above.
523
+ if (tmpCurrentSolverExpression.DatasetOptions && (typeof(tmpCurrentSolverExpression.DatasetOptions) === 'object'))
524
+ {
525
+ Object.assign(tmpDataObject, tmpCurrentSolverExpression.DatasetOptions);
526
+ }
527
+
517
528
  if (tmpParsingConfiguration.ObjectType === 'array')
518
529
  {
519
530
  if (Array.isArray(tmpSolvedDataSet))
@@ -604,6 +615,49 @@ class CustomInputHandler extends libPictSectionInputExtension
604
615
  this.applyInputParsingConfiguration(pInput, tmpChartConfiguration, this.defaultLabelParsingConfiguration, 'PictForm.ChartLabelsParsingConfigurationOverride');
605
616
  this.applyInputParsingConfiguration(pInput, tmpChartConfiguration, this.defaultDataParsingConfiguration, 'PictForm.ChartDataParsingConfigurationOverride');
606
617
 
618
+ // A blank is a MISSING reading, not a zero one. Solver-built series routinely carry "" for
619
+ // "this measure has no value here" -- a control limit the agency does not publish, a sample
620
+ // the lab never ran -- and Chart.js coerces that empty string to 0, drawing a line that dives
621
+ // to the axis and reads as a real measurement of zero. Chart.js's own convention for a gap is
622
+ // null, so blanks are normalized to it and the series simply breaks. Set ChartPlotBlanksAsZero
623
+ // to keep the old coercion for a chart that genuinely means zero.
624
+ if (!tmpPictform.ChartPlotBlanksAsZero)
625
+ {
626
+ const tmpDatasets = tmpChartConfiguration?.data?.datasets;
627
+ if (Array.isArray(tmpDatasets))
628
+ {
629
+ for (let i = 0; i < tmpDatasets.length; i++)
630
+ {
631
+ if (!tmpDatasets[i] || !Array.isArray(tmpDatasets[i].data)) { continue; }
632
+ tmpDatasets[i].data = tmpDatasets[i].data.map(
633
+ (pPoint) => ((pPoint === '') || (pPoint === null) || (typeof (pPoint) === 'undefined')) ? null : pPoint);
634
+ }
635
+ }
636
+ }
637
+
638
+ // ChartHeight is one knob with two halves: the template gives the canvas container a min-height,
639
+ // and Chart.js has to be told to stop deriving its own height from the width or it will ignore
640
+ // that box entirely. Requiring a caller to set maintainAspectRatio by hand alongside the height
641
+ // would make a single intent ("make this chart taller") into two coupled settings that are wrong
642
+ // in isolation, so it is derived here. An explicit maintainAspectRatio in the core prototype still
643
+ // wins -- this only fills in the value the caller did not state.
644
+ const tmpChartHeight = parseFloat(tmpPictform.ChartHeight);
645
+ if (isFinite(tmpChartHeight) && (tmpChartHeight > 0))
646
+ {
647
+ if (typeof (tmpChartConfiguration.options) !== 'object' || tmpChartConfiguration.options === null)
648
+ {
649
+ tmpChartConfiguration.options = {};
650
+ }
651
+ if (!('maintainAspectRatio' in tmpChartConfiguration.options))
652
+ {
653
+ tmpChartConfiguration.options.maintainAspectRatio = false;
654
+ }
655
+ if (!('responsive' in tmpChartConfiguration.options))
656
+ {
657
+ tmpChartConfiguration.options.responsive = true;
658
+ }
659
+ }
660
+
607
661
  return tmpChartConfiguration;
608
662
  }
609
663
 
@@ -0,0 +1,205 @@
1
+ /*
2
+ Unit tests for source/providers/inputs/Pict-Provider-Input-Chart.js
3
+
4
+ Covers the two chart-shaping behaviours a dashboard depends on and that
5
+ nothing else guards: how a blank data point is plotted, and how an
6
+ explicit ChartHeight reaches Chart.js. Both were defects found on a live
7
+ control chart -- blanks drew a line diving to zero (reading as a real
8
+ measurement), and a taller canvas was simply not expressible.
9
+ */
10
+
11
+ const libBrowserEnv = require('browser-env');
12
+ libBrowserEnv();
13
+
14
+ const Chai = require('chai');
15
+ const Expect = Chai.expect;
16
+
17
+ const libPict = require('pict');
18
+
19
+ const libChartInput = require('../source/providers/inputs/Pict-Provider-Input-Chart.js');
20
+
21
+ /**
22
+ * Build a chart input provider and drive getInputChartConfiguration() with a
23
+ * pre-baked datasets prototype, which is the same shape the solver-driven
24
+ * parsing steps deposit at data.datasets.
25
+ *
26
+ * @param {Object} pPictForm - The input's PictForm block.
27
+ * @param {Array} pDatasets - Datasets to seed into the core prototype.
28
+ * @return {Object} The resolved Chart.js configuration.
29
+ */
30
+ function resolveChartConfiguration(pPictForm, pDatasets)
31
+ {
32
+ const tmpPict = new libPict();
33
+ const tmpProvider = new libChartInput(tmpPict, {});
34
+ const tmpInput =
35
+ {
36
+ Name: 'ControlChart',
37
+ Hash: 'ControlChart',
38
+ DataType: 'Object',
39
+ Macro: { RawHTMLID: 'ControlChart-input', InputFullProperties: '', InputChangeHandler: '', ControlAttr: '' },
40
+ PictForm: Object.assign(
41
+ {
42
+ InputType: 'Chart',
43
+ ChartType: 'line',
44
+ ChartConfigCorePrototypeRaw: { data: { datasets: pDatasets || [] } },
45
+ }, pPictForm || {}),
46
+ };
47
+ return tmpProvider.getInputChartConfiguration({ }, tmpInput, {});
48
+ }
49
+
50
+ suite('Pict-Provider-Input-Chart', () =>
51
+ {
52
+ suite('blank data points', () =>
53
+ {
54
+ test('a blank reading becomes a null gap rather than plotting as zero', () =>
55
+ {
56
+ // The live case: an agency publishes no upper binder limit, so the band
57
+ // column is "" on most samples. Coerced to 0 it drew a line along the
58
+ // axis that looked like a real limit of zero.
59
+ const tmpConfiguration = resolveChartConfiguration({}, [ { label: 'Band', data: [ '5.55', '', '5.75', '' ] } ]);
60
+ Expect(tmpConfiguration.data.datasets[0].data).to.deep.equal([ '5.55', null, '5.75', null ]);
61
+ });
62
+
63
+ test('null and undefined points are normalized to null as well', () =>
64
+ {
65
+ const tmpConfiguration = resolveChartConfiguration({}, [ { label: 'Band', data: [ null, undefined, '3' ] } ]);
66
+ Expect(tmpConfiguration.data.datasets[0].data).to.deep.equal([ null, null, '3' ]);
67
+ });
68
+
69
+ test('a real zero is left alone — it is a measurement, not a blank', () =>
70
+ {
71
+ const tmpConfiguration = resolveChartConfiguration({}, [ { label: 'QC', data: [ 0, '0', '0.0' ] } ]);
72
+ Expect(tmpConfiguration.data.datasets[0].data).to.deep.equal([ 0, '0', '0.0' ]);
73
+ });
74
+
75
+ test('ChartPlotBlanksAsZero opts back into the old coercion', () =>
76
+ {
77
+ const tmpConfiguration = resolveChartConfiguration({ ChartPlotBlanksAsZero: true }, [ { label: 'Band', data: [ '5.55', '' ] } ]);
78
+ Expect(tmpConfiguration.data.datasets[0].data).to.deep.equal([ '5.55', '' ]);
79
+ });
80
+
81
+ test('every dataset is normalized, not only the first', () =>
82
+ {
83
+ const tmpConfiguration = resolveChartConfiguration({},
84
+ [ { label: 'A', data: [ '1', '' ] }, { label: 'B', data: [ '', '2' ] } ]);
85
+ Expect(tmpConfiguration.data.datasets[0].data).to.deep.equal([ '1', null ]);
86
+ Expect(tmpConfiguration.data.datasets[1].data).to.deep.equal([ null, '2' ]);
87
+ });
88
+
89
+ test('a dataset without a data array is left untouched', () =>
90
+ {
91
+ const tmpConfiguration = resolveChartConfiguration({}, [ { label: 'Empty' } ]);
92
+ Expect(tmpConfiguration.data.datasets[0]).to.not.have.property('data');
93
+ });
94
+ });
95
+
96
+ suite('ChartHeight', () =>
97
+ {
98
+ test('setting a height turns maintainAspectRatio off so the canvas fills its box', () =>
99
+ {
100
+ // Without this the container height the template emits is ignored --
101
+ // Chart.js keeps deriving height from width and the chart stays squat.
102
+ const tmpConfiguration = resolveChartConfiguration({ ChartHeight: 420 }, []);
103
+ Expect(tmpConfiguration.options.maintainAspectRatio).to.equal(false);
104
+ Expect(tmpConfiguration.options.responsive).to.equal(true);
105
+ });
106
+
107
+ test('an explicitly configured maintainAspectRatio still wins', () =>
108
+ {
109
+ const tmpConfiguration = resolveChartConfiguration(
110
+ {
111
+ ChartHeight: 420,
112
+ ChartConfigCorePrototypeRaw: { data: { datasets: [] }, options: { maintainAspectRatio: true } },
113
+ }, []);
114
+ Expect(tmpConfiguration.options.maintainAspectRatio).to.equal(true);
115
+ });
116
+
117
+ test('no ChartHeight leaves the options block alone (unchanged default sizing)', () =>
118
+ {
119
+ const tmpConfiguration = resolveChartConfiguration({}, []);
120
+ Expect(tmpConfiguration.options === undefined || !('maintainAspectRatio' in tmpConfiguration.options)).to.equal(true);
121
+ });
122
+
123
+ test('a zero or non-numeric height is ignored rather than half-applied', () =>
124
+ {
125
+ for (const tmpHeight of [ 0, -10, 'tall', null ])
126
+ {
127
+ const tmpConfiguration = resolveChartConfiguration({ ChartHeight: tmpHeight }, []);
128
+ Expect(tmpConfiguration.options === undefined || !('maintainAspectRatio' in tmpConfiguration.options),
129
+ `height ${JSON.stringify(tmpHeight)} should not apply`).to.equal(true);
130
+ }
131
+ });
132
+ });
133
+
134
+ suite('DatasetOptions', () =>
135
+ {
136
+ // The provider copies a fixed allowlist of properties onto each dataset
137
+ // (ChartType / CustomYAxisID / CustomXAxisID / Tension / PointRadius /
138
+ // StackGroup). Anything else was dropped, so a series could not be given a
139
+ // colour or a dash pattern from configuration at all.
140
+ const solverDataset = (pEntry) =>
141
+ {
142
+ const tmpPict = new libPict();
143
+ const tmpProvider = new libChartInput(tmpPict, {});
144
+ tmpPict.providers.DynamicSolver = { runSolver: () => [ 1, 2, 3 ] };
145
+ const tmpInput =
146
+ {
147
+ Name: 'C', Hash: 'C', DataType: 'Object',
148
+ Macro: { RawHTMLID: 'C-input', InputFullProperties: '', InputChangeHandler: '', ControlAttr: '' },
149
+ PictForm: { InputType: 'Chart', ChartType: 'line', ChartDatasetsSolvers: [ pEntry ] },
150
+ };
151
+ const tmpConfiguration = tmpProvider.getInputChartConfiguration({ }, tmpInput, {});
152
+ return tmpConfiguration.data.datasets[0];
153
+ };
154
+
155
+ test('arbitrary Chart.js dataset properties reach the dataset', () =>
156
+ {
157
+ const tmpDataset = solverDataset({ Label: 'Limit', DataSolver: 'x',
158
+ DatasetOptions: { borderColor: '#E03131', borderDash: [ 2, 3 ], showLine: false } });
159
+ Expect(tmpDataset.borderColor).to.equal('#E03131');
160
+ Expect(tmpDataset.borderDash).to.deep.equal([ 2, 3 ]);
161
+ Expect(tmpDataset.showLine).to.equal(false);
162
+ });
163
+
164
+ test('label and data still come from Label / DataSolver', () =>
165
+ {
166
+ const tmpDataset = solverDataset({ Label: 'Limit', DataSolver: 'x', DatasetOptions: { borderColor: '#000' } });
167
+ Expect(tmpDataset.label).to.equal('Limit');
168
+ Expect(tmpDataset.data).to.deep.equal([ 1, 2, 3 ]);
169
+ });
170
+
171
+ test('it wins over the named options, being applied last', () =>
172
+ {
173
+ const tmpDataset = solverDataset({ Label: 'L', DataSolver: 'x', PointRadius: 9, DatasetOptions: { pointRadius: 2 } });
174
+ Expect(tmpDataset.pointRadius).to.equal(2);
175
+ });
176
+
177
+ test('a dataset without DatasetOptions is unchanged', () =>
178
+ {
179
+ const tmpDataset = solverDataset({ Label: 'L', DataSolver: 'x' });
180
+ Expect(tmpDataset).to.not.have.property('borderColor');
181
+ Expect(Object.keys(tmpDataset)).to.deep.equal([ 'label', 'data' ]);
182
+ });
183
+
184
+ test('a non-object DatasetOptions is ignored rather than throwing', () =>
185
+ {
186
+ for (const tmpBad of [ 'nope', 42, null ])
187
+ {
188
+ const tmpDataset = solverDataset({ Label: 'L', DataSolver: 'x', DatasetOptions: tmpBad });
189
+ Expect(tmpDataset.label, `DatasetOptions ${JSON.stringify(tmpBad)}`).to.equal('L');
190
+ }
191
+ });
192
+ });
193
+
194
+ suite('template', () =>
195
+ {
196
+ test('the chart canvas container carries the ChartHeight min-height', () =>
197
+ {
198
+ const tmpDefaults = require('../source/providers/dynamictemplates/Pict-DynamicTemplates-DefaultFormTemplates.js');
199
+ const tmpChartTemplate = tmpDefaults.Templates.find((pTemplate) => pTemplate.HashPostfix === '-Template-Input-InputType-Chart');
200
+ Expect(tmpChartTemplate).to.be.an('object');
201
+ // Defaults to 0, which is a no-op min-height for every chart that never sets it.
202
+ Expect(tmpChartTemplate.Template).to.contain('min-height:{~DWAF:Record.PictForm.ChartHeight:0~}px');
203
+ });
204
+ });
205
+ });