pict-section-form 1.4.2 → 1.4.3
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/docs/examples/dynamic_analysis/README.md +18 -0
- package/docs/examples/dynamic_analysis/dynamic_analysis_application.js +1601 -267
- package/docs/input_providers/009-chart.md +49 -1
- package/example_applications/dynamic_analysis/Dynamic-Analysis-Application.js +35 -0
- package/package.json +1 -1
- package/source/providers/inputs/Pict-Provider-Input-Chart.js +343 -0
- package/test/Pict-Provider-Input-Chart_tests.js +247 -0
|
@@ -649,6 +649,253 @@ suite('Pict-Provider-Input-Chart', () =>
|
|
|
649
649
|
});
|
|
650
650
|
});
|
|
651
651
|
|
|
652
|
+
suite('value labels', () =>
|
|
653
|
+
{
|
|
654
|
+
const provider = () => new libChartInput(new libPict(), {});
|
|
655
|
+
const labelConfiguration = (pValueLabels) => provider().resolveChartValueLabelConfiguration({ PictForm: { ChartValueLabels: pValueLabels } });
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* A stand-in for a drawn Chart.js chart: a 0..400 x 0..200 plot area with
|
|
659
|
+
* one point element per value, placed at (40 * index + 20, pY(value)).
|
|
660
|
+
*/
|
|
661
|
+
const drawnChart = (pDatasets, pOptions) =>
|
|
662
|
+
{
|
|
663
|
+
const tmpOptions = pOptions || {};
|
|
664
|
+
const fY = tmpOptions.y || ((pValue) => 200 - pValue);
|
|
665
|
+
const tmpMetas = pDatasets.map((pDataset) => (
|
|
666
|
+
{
|
|
667
|
+
hidden: !!pDataset.hiddenInLegend,
|
|
668
|
+
vScale: { axis: 'y' },
|
|
669
|
+
controller: { getParsed: (pIndex) => ({ x: pIndex, y: pDataset.data[pIndex] }) },
|
|
670
|
+
data: pDataset.data.map((pValue, pIndex) => (
|
|
671
|
+
{
|
|
672
|
+
x: (40 * pIndex) + 20,
|
|
673
|
+
y: fY(pValue),
|
|
674
|
+
skip: (pValue === null),
|
|
675
|
+
options: { radius: 3, borderColor: pDataset.borderColor },
|
|
676
|
+
})),
|
|
677
|
+
}));
|
|
678
|
+
return {
|
|
679
|
+
chartArea: { left: 0, top: 0, right: 400, bottom: 200 },
|
|
680
|
+
options: { color: 'rgb(10, 20, 30)', font: { family: 'Arial' } },
|
|
681
|
+
data: { datasets: pDatasets },
|
|
682
|
+
getDatasetMeta: (pIndex) => tmpMetas[pIndex],
|
|
683
|
+
isDatasetVisible: (pIndex) => !tmpMetas[pIndex].hidden,
|
|
684
|
+
};
|
|
685
|
+
};
|
|
686
|
+
// Every character measures 6px wide
|
|
687
|
+
const measure = (pText) => pText.length * 6;
|
|
688
|
+
|
|
689
|
+
test('ChartValueLabels is off unless configured, leaving values to the hover tooltip', () =>
|
|
690
|
+
{
|
|
691
|
+
Expect(labelConfiguration(undefined)).to.equal(null);
|
|
692
|
+
Expect(labelConfiguration(false)).to.equal(null);
|
|
693
|
+
Expect(labelConfiguration({ Enabled: false })).to.equal(null);
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
test('ChartValueLabels: true labels every dataset above its points and hides overlaps', () =>
|
|
697
|
+
{
|
|
698
|
+
Expect(labelConfiguration(true)).to.deep.equal(
|
|
699
|
+
{ Datasets: 'All', Position: 'top', Offset: 4, Padding: 2, FontSize: 11, FontWeight: 'normal', HideOverlaps: true });
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
test('an unknown Position or a non-numeric size falls back to the default', () =>
|
|
703
|
+
{
|
|
704
|
+
const tmpConfiguration = labelConfiguration({ Position: 'sideways', FontSize: 'big', Offset: -3 });
|
|
705
|
+
Expect(tmpConfiguration.Position).to.equal('top');
|
|
706
|
+
Expect(tmpConfiguration.FontSize).to.equal(11);
|
|
707
|
+
Expect(tmpConfiguration.Offset).to.equal(4);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
test('configuring it adds the drawing plugin and room above the plot', () =>
|
|
711
|
+
{
|
|
712
|
+
const tmpConfiguration = resolveChartConfiguration({ ChartValueLabels: true }, [ { label: 'A', data: [ 1, 2 ] } ]);
|
|
713
|
+
Expect(tmpConfiguration.plugins).to.have.length(1);
|
|
714
|
+
Expect(tmpConfiguration.plugins[0].id).to.equal('pictValueLabels');
|
|
715
|
+
Expect(tmpConfiguration.plugins[0].afterDatasetsDraw).to.be.a('function');
|
|
716
|
+
// FontSize 11 + Offset 4 + Padding 2 on each side
|
|
717
|
+
Expect(tmpConfiguration.options.layout.padding).to.deep.equal({ top: 19 });
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
test('an explicit layout padding in the manifest is left alone', () =>
|
|
721
|
+
{
|
|
722
|
+
const tmpConfiguration = resolveChartConfiguration(
|
|
723
|
+
{ ChartValueLabels: true, ChartConfigCorePrototypeRaw: { options: { layout: { padding: 8 } }, data: { datasets: [] } } });
|
|
724
|
+
Expect(tmpConfiguration.options.layout.padding).to.equal(8);
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
test('without it no plugin is added', () =>
|
|
728
|
+
{
|
|
729
|
+
const tmpConfiguration = resolveChartConfiguration({}, [ { label: 'A', data: [ 1, 2 ] } ]);
|
|
730
|
+
Expect(tmpConfiguration.plugins).to.equal(undefined);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
test('ValueLabels on a dataset solver entry lands on the dataset as pictValueLabels', () =>
|
|
734
|
+
{
|
|
735
|
+
const tmpPict = new libPict();
|
|
736
|
+
const tmpProvider = new libChartInput(tmpPict, {});
|
|
737
|
+
tmpPict.providers.DynamicSolver = { runSolver: () => [ 1 ] };
|
|
738
|
+
const tmpConfiguration = tmpProvider.getInputChartConfiguration({ }, {
|
|
739
|
+
Name: 'C', Hash: 'C', DataType: 'Object',
|
|
740
|
+
Macro: { RawHTMLID: 'C', InputFullProperties: '', InputChangeHandler: '', ControlAttr: '' },
|
|
741
|
+
PictForm: { InputType: 'Chart', ChartType: 'line', ChartDatasetsSolvers: [
|
|
742
|
+
{ Label: 'Reading', DataSolver: 'x', ValueLabels: true },
|
|
743
|
+
{ Label: 'Limit', DataSolver: 'y', ValueLabels: false },
|
|
744
|
+
{ Label: 'Other', DataSolver: 'z' } ] },
|
|
745
|
+
}, {});
|
|
746
|
+
Expect(tmpConfiguration.data.datasets.map((pDataset) => pDataset.pictValueLabels)).to.deep.equal([ true, false, undefined ]);
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
test('values are formatted without floating-point noise, or to Decimals places', () =>
|
|
750
|
+
{
|
|
751
|
+
const tmpProvider = provider();
|
|
752
|
+
Expect(tmpProvider._formatChartValueLabel(0.1 + 0.2, {})).to.equal('0.3');
|
|
753
|
+
Expect(tmpProvider._formatChartValueLabel('5.55', {})).to.equal('5.55');
|
|
754
|
+
Expect(tmpProvider._formatChartValueLabel(5.5, { Decimals: 2 })).to.equal('5.50');
|
|
755
|
+
Expect(tmpProvider._formatChartValueLabel(0, {})).to.equal('0');
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
test('a blank or non-numeric value gets no label', () =>
|
|
759
|
+
{
|
|
760
|
+
const tmpProvider = provider();
|
|
761
|
+
Expect(tmpProvider._formatChartValueLabel(null, {})).to.equal(null);
|
|
762
|
+
Expect(tmpProvider._formatChartValueLabel('', {})).to.equal(null);
|
|
763
|
+
Expect(tmpProvider._formatChartValueLabel(NaN, {})).to.equal(null);
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
test('a label sits centred above its point, clear of the marker', () =>
|
|
767
|
+
{
|
|
768
|
+
const tmpLabels = provider()._collectChartValueLabels(drawnChart([ { data: [ 50 ] } ]), labelConfiguration(true), measure);
|
|
769
|
+
Expect(tmpLabels).to.have.length(1);
|
|
770
|
+
// Point at (20, 150): radius 3 + offset 4 above it, 15px tall (11 + 2 + 2)
|
|
771
|
+
Expect(tmpLabels[0]).to.include({ Text: '50', Left: 12, Top: 128, Width: 16, Height: 15, X: 20, Y: 135.5, Color: 'rgb(10, 20, 30)' });
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
test('a label that would leave the top of the plot flips below its point', () =>
|
|
775
|
+
{
|
|
776
|
+
const tmpLabels = provider()._collectChartValueLabels(drawnChart([ { data: [ 195 ] } ]), labelConfiguration(true), measure);
|
|
777
|
+
// Point at y=5: above would start at -17, so it goes to 5 + 7
|
|
778
|
+
Expect(tmpLabels[0].Top).to.equal(12);
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
test('a label near a side edge is pulled back inside the plot', () =>
|
|
782
|
+
{
|
|
783
|
+
const tmpChart = drawnChart([ { data: [ 50 ] } ]);
|
|
784
|
+
tmpChart.getDatasetMeta(0).data[0].x = 398;
|
|
785
|
+
const tmpLabels = provider()._collectChartValueLabels(tmpChart, labelConfiguration(true), measure);
|
|
786
|
+
Expect(tmpLabels[0].Left + tmpLabels[0].Width).to.equal(400);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
test('Position bottom puts the label under its point', () =>
|
|
790
|
+
{
|
|
791
|
+
const tmpLabels = provider()._collectChartValueLabels(drawnChart([ { data: [ 50 ] } ]), labelConfiguration({ Position: 'bottom' }), measure);
|
|
792
|
+
Expect(tmpLabels[0].Top).to.equal(157);
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
test('blank points, hidden datasets and datasets opted out get no labels', () =>
|
|
796
|
+
{
|
|
797
|
+
const tmpChart = drawnChart([
|
|
798
|
+
{ data: [ 10, null, 30 ] },
|
|
799
|
+
{ data: [ 40 ], hiddenInLegend: true },
|
|
800
|
+
{ data: [ 50 ], pictValueLabels: false },
|
|
801
|
+
]);
|
|
802
|
+
const tmpLabels = provider()._collectChartValueLabels(tmpChart, labelConfiguration(true), measure);
|
|
803
|
+
Expect(tmpLabels.map((pLabel) => pLabel.Text)).to.deep.equal([ '10', '30' ]);
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
test('Datasets: OptIn labels only the datasets that ask for it', () =>
|
|
807
|
+
{
|
|
808
|
+
const tmpChart = drawnChart([ { data: [ 10 ] }, { data: [ 20 ], pictValueLabels: true } ]);
|
|
809
|
+
const tmpLabels = provider()._collectChartValueLabels(tmpChart, labelConfiguration({ Datasets: 'OptIn' }), measure);
|
|
810
|
+
Expect(tmpLabels.map((pLabel) => pLabel.Text)).to.deep.equal([ '20' ]);
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
test('Color: Dataset draws each label in its series colour', () =>
|
|
814
|
+
{
|
|
815
|
+
const tmpChart = drawnChart([ { data: [ 10 ], borderColor: '#E03131' } ]);
|
|
816
|
+
const tmpLabels = provider()._collectChartValueLabels(tmpChart, labelConfiguration({ Color: 'Dataset' }), measure);
|
|
817
|
+
Expect(tmpLabels[0].Color).to.equal('#E03131');
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
test('a label that collides moves to the other side of its point when that is clear', () =>
|
|
821
|
+
{
|
|
822
|
+
// Two series one pixel apart at the same x: the second's label goes underneath
|
|
823
|
+
const tmpChart = drawnChart([ { data: [ 50, 100 ] }, { data: [ 51, 10 ] } ]);
|
|
824
|
+
const tmpProvider = provider();
|
|
825
|
+
const tmpLabels = tmpProvider._layoutChartValueLabels(tmpProvider._collectChartValueLabels(tmpChart, labelConfiguration(true), measure), true);
|
|
826
|
+
Expect(tmpLabels.map((pLabel) => `${pLabel.DatasetIndex}:${pLabel.Text}`)).to.deep.equal([ '0:50', '0:100', '1:51', '1:10' ]);
|
|
827
|
+
// Point at y=151, radius 3 + offset 4 below it
|
|
828
|
+
Expect(tmpLabels[2]).to.include({ Top: 156, Y: 163.5 });
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
test('a label with both sides taken is hidden; the earlier datasets keep theirs', () =>
|
|
832
|
+
{
|
|
833
|
+
const tmpChart = drawnChart([ { data: [ 50 ] }, { data: [ 51 ] }, { data: [ 50 ] } ]);
|
|
834
|
+
const tmpProvider = provider();
|
|
835
|
+
const tmpLabels = tmpProvider._layoutChartValueLabels(tmpProvider._collectChartValueLabels(tmpChart, labelConfiguration(true), measure), true);
|
|
836
|
+
Expect(tmpLabels.map((pLabel) => `${pLabel.DatasetIndex}:${pLabel.Text}`)).to.deep.equal([ '0:50', '1:51' ]);
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
test('a label already flipped off the top edge has no other side to try', () =>
|
|
840
|
+
{
|
|
841
|
+
const tmpLabels = provider()._collectChartValueLabels(drawnChart([ { data: [ 195 ] } ]), labelConfiguration(true), measure);
|
|
842
|
+
Expect(tmpLabels[0].AlternateTop).to.equal(null);
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
test('HideOverlaps: false keeps every label', () =>
|
|
846
|
+
{
|
|
847
|
+
const tmpChart = drawnChart([ { data: [ 50 ] }, { data: [ 51 ] } ]);
|
|
848
|
+
const tmpProvider = provider();
|
|
849
|
+
const tmpLabels = tmpProvider._layoutChartValueLabels(tmpProvider._collectChartValueLabels(tmpChart, labelConfiguration(true), measure), false);
|
|
850
|
+
Expect(tmpLabels).to.have.length(2);
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
const recordingContext = (pChart, pCalls) => (
|
|
854
|
+
{
|
|
855
|
+
save: () => {},
|
|
856
|
+
restore: () => {},
|
|
857
|
+
measureText: (pText) => ({ width: measure(pText) }),
|
|
858
|
+
strokeText: (pText) => pCalls.push({ Op: 'stroke', Text: pText, Stroke: pChart.ctx.strokeStyle, Width: pChart.ctx.lineWidth }),
|
|
859
|
+
fillText: (pText) => pCalls.push({ Op: 'fill', Text: pText, Font: pChart.ctx.font, Fill: pChart.ctx.fillStyle }),
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
test('drawing writes the kept labels to the canvas in the configured font, each over a halo', () =>
|
|
863
|
+
{
|
|
864
|
+
const tmpCalls = [];
|
|
865
|
+
const tmpChart = drawnChart([ { data: [ 50, 51 ] }, { data: [ 50 ] }, { data: [ 50 ] } ]);
|
|
866
|
+
tmpChart.ctx = recordingContext(tmpChart, tmpCalls);
|
|
867
|
+
const tmpDrawn = provider().drawChartValueLabels(tmpChart, labelConfiguration({ FontWeight: 'bold' }));
|
|
868
|
+
// The third dataset's 50 has both sides of its point taken
|
|
869
|
+
Expect(tmpDrawn).to.equal(3);
|
|
870
|
+
Expect(tmpCalls.map((pCall) => `${pCall.Op}:${pCall.Text}`)).to.deep.equal([ 'stroke:50', 'fill:50', 'stroke:51', 'fill:51', 'stroke:50', 'fill:50' ]);
|
|
871
|
+
Expect(tmpCalls[1].Font).to.equal('bold 11px Arial');
|
|
872
|
+
Expect(tmpCalls[1].Fill).to.equal('rgb(10, 20, 30)');
|
|
873
|
+
// Dark text gets a light halo
|
|
874
|
+
Expect(tmpCalls[0]).to.include({ Stroke: 'rgba(255, 255, 255, 0.85)', Width: 3 });
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
test('light text gets a dark halo, and HaloColor / HaloWidth override it', () =>
|
|
878
|
+
{
|
|
879
|
+
const tmpProvider = provider();
|
|
880
|
+
Expect(tmpProvider._chartValueLabelHaloColor('rgb(230, 230, 230)')).to.equal('rgba(0, 0, 0, 0.75)');
|
|
881
|
+
Expect(tmpProvider._chartValueLabelHaloColor('#333')).to.equal('rgba(255, 255, 255, 0.85)');
|
|
882
|
+
const tmpCalls = [];
|
|
883
|
+
const tmpChart = drawnChart([ { data: [ 50 ] } ]);
|
|
884
|
+
tmpChart.ctx = recordingContext(tmpChart, tmpCalls);
|
|
885
|
+
tmpProvider.drawChartValueLabels(tmpChart, labelConfiguration({ HaloColor: 'navy', HaloWidth: 5 }));
|
|
886
|
+
Expect(tmpCalls[0]).to.include({ Op: 'stroke', Stroke: 'navy', Width: 5 });
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
test('Halo: false draws the text alone', () =>
|
|
890
|
+
{
|
|
891
|
+
const tmpCalls = [];
|
|
892
|
+
const tmpChart = drawnChart([ { data: [ 50 ] } ]);
|
|
893
|
+
tmpChart.ctx = recordingContext(tmpChart, tmpCalls);
|
|
894
|
+
provider().drawChartValueLabels(tmpChart, labelConfiguration({ Halo: false }));
|
|
895
|
+
Expect(tmpCalls.map((pCall) => pCall.Op)).to.deep.equal([ 'fill' ]);
|
|
896
|
+
});
|
|
897
|
+
});
|
|
898
|
+
|
|
652
899
|
suite('template', () =>
|
|
653
900
|
{
|
|
654
901
|
test('the chart canvas container carries the ChartHeight min-height', () =>
|