@oicl/openbridge-webcomponents-full-bundle 2.1.0 → 2.1.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/bundle/openbridge-webcomponents.bundle.js +46 -37
- package/bundle/openbridge-webcomponents.bundle.js.map +1 -1
- package/custom-elements.json +5 -5
- package/dist/bars-graphs/donut-chart/donut-chart.d.ts.map +1 -1
- package/dist/bars-graphs/donut-chart/donut-chart.js +3 -4
- package/dist/bars-graphs/donut-chart/donut-chart.js.map +1 -1
- package/dist/bars-graphs/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/bars-graphs/pie-chart/pie-chart.js +3 -4
- package/dist/bars-graphs/pie-chart/pie-chart.js.map +1 -1
- package/dist/bars-graphs/polar-chart/polar-chart.d.ts.map +1 -1
- package/dist/bars-graphs/polar-chart/polar-chart.js +3 -4
- package/dist/bars-graphs/polar-chart/polar-chart.js.map +1 -1
- package/dist/bars-graphs/radial-bar-chart/radial-bar-chart.d.ts.map +1 -1
- package/dist/bars-graphs/radial-bar-chart/radial-bar-chart.js +3 -4
- package/dist/bars-graphs/radial-bar-chart/radial-bar-chart.js.map +1 -1
- package/dist/building-blocks/chart-line/chart-line-base.d.ts.map +1 -1
- package/dist/building-blocks/chart-line/chart-line-base.js +3 -6
- package/dist/building-blocks/chart-line/chart-line-base.js.map +1 -1
- package/dist/charthelpers/index.js +2 -2
- package/dist/charthelpers/legend.d.ts +24 -7
- package/dist/charthelpers/legend.d.ts.map +1 -1
- package/dist/charthelpers/legend.js +40 -16
- package/dist/charthelpers/legend.js.map +1 -1
- package/package.json +1 -1
- package/src/bars-graphs/donut-chart/donut-chart.ts +3 -4
- package/src/bars-graphs/pie-chart/pie-chart.ts +3 -4
- package/src/bars-graphs/polar-chart/polar-chart.ts +3 -4
- package/src/bars-graphs/radial-bar-chart/radial-bar-chart.ts +3 -4
- package/src/building-blocks/chart-line/chart-line-base.ts +3 -4
- package/src/charthelpers/legend.spec.ts +261 -0
- package/src/charthelpers/legend.ts +63 -27
|
@@ -5,8 +5,8 @@ import { ObcPalette, PaletteController, currentPalette, observeThemeChanges } fr
|
|
|
5
5
|
import { calculateSumTotal, createArcOuterLabelPlugin } from "./outer-labels.js";
|
|
6
6
|
import { calculateRectangularChartLayout } from "./rectangular-chart-layout.js";
|
|
7
7
|
import { getChartTooltipOptions } from "./tooltip.js";
|
|
8
|
-
import { generateLegendHTML } from "./legend.js";
|
|
8
|
+
import { generateLegendHTML, renderLegend } from "./legend.js";
|
|
9
9
|
import { XValueMode, formatXValue, normalizeXValue } from "./x-value.js";
|
|
10
10
|
import { observeLabelThreshold } from "./label-threshold.js";
|
|
11
11
|
import { formatRangeValue, measureRangeLabelGutters, rangeLabelFontString, rangeLabelLineHeight, yRangeLabelValues } from "./range-labels.js";
|
|
12
|
-
export { CENTER_READOUT_CONFIG, CHART_AREA_BACKGROUND_COLOR_VAR, CHART_DIMENSIONS, CHART_SECTOR_DEFAULT_COLORS, CHART_SECTOR_ENHANCED_COLORS, LINE_GRAPH_GRID_CONFIG, LINE_GRAPH_LABEL_CONFIG, OUTER_LABEL_CONFIG, ObcPalette, PaletteController, RECTANGULAR_CHART_DIMENSIONS, TOOLTIP_CONFIG, XValueMode, applyAlphaToColor, calculateChartPaddingAndLabels, calculateFixedHeightChartLayout, calculateRectangularChartLayout, calculateSumTotal, createArcOuterLabelPlugin, currentPalette, formatChartNumber, formatRangeValue, formatSingleLabel, formatXValue, generateLegendHTML, getChartColorsOrDefault, getChartTooltipOptions, getCssVariableValue, measureRangeLabelGutters, normalizeXValue, observeLabelThreshold, observeThemeChanges, rangeLabelFontString, rangeLabelLineHeight, yRangeLabelValues };
|
|
12
|
+
export { CENTER_READOUT_CONFIG, CHART_AREA_BACKGROUND_COLOR_VAR, CHART_DIMENSIONS, CHART_SECTOR_DEFAULT_COLORS, CHART_SECTOR_ENHANCED_COLORS, LINE_GRAPH_GRID_CONFIG, LINE_GRAPH_LABEL_CONFIG, OUTER_LABEL_CONFIG, ObcPalette, PaletteController, RECTANGULAR_CHART_DIMENSIONS, TOOLTIP_CONFIG, XValueMode, applyAlphaToColor, calculateChartPaddingAndLabels, calculateFixedHeightChartLayout, calculateRectangularChartLayout, calculateSumTotal, createArcOuterLabelPlugin, currentPalette, formatChartNumber, formatRangeValue, formatSingleLabel, formatXValue, generateLegendHTML, getChartColorsOrDefault, getChartTooltipOptions, getCssVariableValue, measureRangeLabelGutters, normalizeXValue, observeLabelThreshold, observeThemeChanges, rangeLabelFontString, rangeLabelLineHeight, renderLegend, yRangeLabelValues };
|
|
@@ -1,12 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @returns HTML string for the legend
|
|
5
|
-
*/
|
|
6
|
-
export declare function generateLegendHTML(legendItems: Array<{
|
|
1
|
+
/** One row of a chart legend. */
|
|
2
|
+
export interface LegendItem {
|
|
3
|
+
/** Swatch colour. A value that is not a CSS colour leaves the swatch unpainted. */
|
|
7
4
|
fillStyle: string;
|
|
8
5
|
label: string;
|
|
9
6
|
value: string;
|
|
10
7
|
unit: string;
|
|
11
|
-
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Fills a chart's legend with one row per item, replacing what was there.
|
|
11
|
+
*
|
|
12
|
+
* Labels, values, units and colours come from the consumer's data, which an
|
|
13
|
+
* application often takes from a backend or from what a user typed. They are
|
|
14
|
+
* written as text and as a single style value, never as markup: a label
|
|
15
|
+
* containing HTML shows up as that literal text.
|
|
16
|
+
*
|
|
17
|
+
* @param container - The legend element to fill
|
|
18
|
+
* @param items - The rows, in display order; an empty list clears the legend
|
|
19
|
+
*/
|
|
20
|
+
export declare function renderLegend(container: HTMLElement, items: readonly LegendItem[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* The legend rows as an HTML string, with every value escaped.
|
|
23
|
+
*
|
|
24
|
+
* @deprecated Use `renderLegend()`, which fills the legend element itself.
|
|
25
|
+
* @param legendItems - The rows, in display order
|
|
26
|
+
* @returns The rows' markup
|
|
27
|
+
*/
|
|
28
|
+
export declare function generateLegendHTML(legendItems: readonly LegendItem[]): string;
|
|
12
29
|
//# sourceMappingURL=legend.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"legend.d.ts","sourceRoot":"","sources":["../../src/charthelpers/legend.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"legend.d.ts","sourceRoot":"","sources":["../../src/charthelpers/legend.ts"],"names":[],"mappings":"AAAA,iCAAiC;AACjC,MAAM,WAAW,UAAU;IACzB,mFAAmF;IACnF,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd;AASD;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,WAAW,EACtB,KAAK,EAAE,SAAS,UAAU,EAAE,GAC3B,IAAI,CAqBN;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,CAK7E"}
|
|
@@ -1,24 +1,48 @@
|
|
|
1
1
|
//#region src/charthelpers/legend.ts
|
|
2
|
+
function span(className, text = "") {
|
|
3
|
+
const el = document.createElement("span");
|
|
4
|
+
el.className = className;
|
|
5
|
+
el.textContent = text;
|
|
6
|
+
return el;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Fills a chart's legend with one row per item, replacing what was there.
|
|
10
|
+
*
|
|
11
|
+
* Labels, values, units and colours come from the consumer's data, which an
|
|
12
|
+
* application often takes from a backend or from what a user typed. They are
|
|
13
|
+
* written as text and as a single style value, never as markup: a label
|
|
14
|
+
* containing HTML shows up as that literal text.
|
|
15
|
+
*
|
|
16
|
+
* @param container - The legend element to fill
|
|
17
|
+
* @param items - The rows, in display order; an empty list clears the legend
|
|
18
|
+
*/
|
|
19
|
+
function renderLegend(container, items) {
|
|
20
|
+
container.replaceChildren(...items.map((item) => {
|
|
21
|
+
const swatch = span("legend-color");
|
|
22
|
+
swatch.style.backgroundColor = item.fillStyle;
|
|
23
|
+
const label = span("legend-group legend-group--label");
|
|
24
|
+
label.append(swatch, span("legend-label", item.label));
|
|
25
|
+
const valueUnit = span("legend-group legend-group--value-unit");
|
|
26
|
+
valueUnit.append(span("legend-value", item.value), span("legend-unit", item.unit));
|
|
27
|
+
const row = document.createElement("div");
|
|
28
|
+
row.className = "legend-item";
|
|
29
|
+
row.append(label, valueUnit);
|
|
30
|
+
return row;
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
2
33
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @
|
|
34
|
+
* The legend rows as an HTML string, with every value escaped.
|
|
35
|
+
*
|
|
36
|
+
* @deprecated Use `renderLegend()`, which fills the legend element itself.
|
|
37
|
+
* @param legendItems - The rows, in display order
|
|
38
|
+
* @returns The rows' markup
|
|
6
39
|
*/
|
|
7
40
|
function generateLegendHTML(legendItems) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
<span class="legend-color" style="background-color: ${item.fillStyle};"></span>
|
|
12
|
-
<span class="legend-label">${item.label}</span>
|
|
13
|
-
</span>
|
|
14
|
-
<span class="legend-group legend-group--value-unit">
|
|
15
|
-
<span class="legend-value">${item.value}</span>
|
|
16
|
-
<span class="legend-unit">${item.unit}</span>
|
|
17
|
-
</span>
|
|
18
|
-
</div>
|
|
19
|
-
`).join("");
|
|
41
|
+
const holder = document.createElement("div");
|
|
42
|
+
renderLegend(holder, legendItems);
|
|
43
|
+
return holder.innerHTML;
|
|
20
44
|
}
|
|
21
45
|
//#endregion
|
|
22
|
-
export { generateLegendHTML };
|
|
46
|
+
export { generateLegendHTML, renderLegend };
|
|
23
47
|
|
|
24
48
|
//# sourceMappingURL=legend.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"legend.js","names":[],"sources":["../../src/charthelpers/legend.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"legend.js","names":[],"sources":["../../src/charthelpers/legend.ts"],"sourcesContent":["/** One row of a chart legend. */\nexport interface LegendItem {\n /** Swatch colour. A value that is not a CSS colour leaves the swatch unpainted. */\n fillStyle: string;\n label: string;\n value: string;\n unit: string;\n}\n\nfunction span(className: string, text = ''): HTMLSpanElement {\n const el = document.createElement('span');\n el.className = className;\n el.textContent = text;\n return el;\n}\n\n/**\n * Fills a chart's legend with one row per item, replacing what was there.\n *\n * Labels, values, units and colours come from the consumer's data, which an\n * application often takes from a backend or from what a user typed. They are\n * written as text and as a single style value, never as markup: a label\n * containing HTML shows up as that literal text.\n *\n * @param container - The legend element to fill\n * @param items - The rows, in display order; an empty list clears the legend\n */\nexport function renderLegend(\n container: HTMLElement,\n items: readonly LegendItem[]\n): void {\n container.replaceChildren(\n ...items.map((item) => {\n const swatch = span('legend-color');\n swatch.style.backgroundColor = item.fillStyle;\n\n const label = span('legend-group legend-group--label');\n label.append(swatch, span('legend-label', item.label));\n\n const valueUnit = span('legend-group legend-group--value-unit');\n valueUnit.append(\n span('legend-value', item.value),\n span('legend-unit', item.unit)\n );\n\n const row = document.createElement('div');\n row.className = 'legend-item';\n row.append(label, valueUnit);\n return row;\n })\n );\n}\n\n/**\n * The legend rows as an HTML string, with every value escaped.\n *\n * @deprecated Use `renderLegend()`, which fills the legend element itself.\n * @param legendItems - The rows, in display order\n * @returns The rows' markup\n */\nexport function generateLegendHTML(legendItems: readonly LegendItem[]): string {\n const holder = document.createElement('div');\n renderLegend(holder, legendItems);\n // A read, not a write: serialising the nodes escapes the text they hold.\n return holder.innerHTML;\n}\n"],"mappings":";AASA,SAAS,KAAK,WAAmB,OAAO,IAAqB;CAC3D,MAAM,KAAK,SAAS,cAAc,MAAM;CACxC,GAAG,YAAY;CACf,GAAG,cAAc;CACjB,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,aACd,WACA,OACM;CACN,UAAU,gBACR,GAAG,MAAM,KAAK,SAAS;EACrB,MAAM,SAAS,KAAK,cAAc;EAClC,OAAO,MAAM,kBAAkB,KAAK;EAEpC,MAAM,QAAQ,KAAK,kCAAkC;EACrD,MAAM,OAAO,QAAQ,KAAK,gBAAgB,KAAK,KAAK,CAAC;EAErD,MAAM,YAAY,KAAK,uCAAuC;EAC9D,UAAU,OACR,KAAK,gBAAgB,KAAK,KAAK,GAC/B,KAAK,eAAe,KAAK,IAAI,CAC/B;EAEA,MAAM,MAAM,SAAS,cAAc,KAAK;EACxC,IAAI,YAAY;EAChB,IAAI,OAAO,OAAO,SAAS;EAC3B,OAAO;CACT,CAAC,CACH;AACF;;;;;;;;AASA,SAAgB,mBAAmB,aAA4C;CAC7E,MAAM,SAAS,SAAS,cAAc,KAAK;CAC3C,aAAa,QAAQ,WAAW;CAEhC,OAAO,OAAO;AAChB"}
|
package/package.json
CHANGED
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
createArcOuterLabelPlugin,
|
|
23
23
|
calculateFixedHeightChartLayout,
|
|
24
24
|
getChartTooltipOptions,
|
|
25
|
-
|
|
25
|
+
renderLegend,
|
|
26
26
|
observeLabelThreshold,
|
|
27
27
|
} from '../../charthelpers/index.js';
|
|
28
28
|
import type {FixedHeightChartDimensions} from '../../charthelpers/canvas-layout.js';
|
|
@@ -802,7 +802,7 @@ export class ObcDonutChart extends LitElement {
|
|
|
802
802
|
|
|
803
803
|
// Guard: Check if dataset has data
|
|
804
804
|
if (!this.data || this.data.length === 0) {
|
|
805
|
-
this.legendDiv
|
|
805
|
+
renderLegend(this.legendDiv, []);
|
|
806
806
|
return;
|
|
807
807
|
}
|
|
808
808
|
|
|
@@ -832,8 +832,7 @@ export class ObcDonutChart extends LitElement {
|
|
|
832
832
|
};
|
|
833
833
|
});
|
|
834
834
|
|
|
835
|
-
|
|
836
|
-
this.legendDiv.innerHTML = legendHTML;
|
|
835
|
+
renderLegend(this.legendDiv, legendItems);
|
|
837
836
|
} catch (error) {
|
|
838
837
|
console.debug(
|
|
839
838
|
'[obc-donut-chart] updateLegend: error generating legend HTML',
|
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
formatSingleLabel,
|
|
29
29
|
formatChartNumber,
|
|
30
30
|
getChartTooltipOptions,
|
|
31
|
-
|
|
31
|
+
renderLegend,
|
|
32
32
|
observeLabelThreshold,
|
|
33
33
|
} from '../../charthelpers/index.js';
|
|
34
34
|
import type {FixedHeightChartDimensions} from '../../charthelpers/canvas-layout.js';
|
|
@@ -818,7 +818,7 @@ export class ObcPieChart extends LitElement {
|
|
|
818
818
|
|
|
819
819
|
// Guard: Check if dataset has data
|
|
820
820
|
if (!this.data || this.data.length === 0) {
|
|
821
|
-
this.legendDiv
|
|
821
|
+
renderLegend(this.legendDiv, []);
|
|
822
822
|
return;
|
|
823
823
|
}
|
|
824
824
|
|
|
@@ -847,8 +847,7 @@ export class ObcPieChart extends LitElement {
|
|
|
847
847
|
};
|
|
848
848
|
});
|
|
849
849
|
|
|
850
|
-
|
|
851
|
-
this.legendDiv.innerHTML = legendHTML;
|
|
850
|
+
renderLegend(this.legendDiv, legendItems);
|
|
852
851
|
} catch (error) {
|
|
853
852
|
console.debug(
|
|
854
853
|
'[obc-pie-chart] updateLegend: error generating legend HTML',
|
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
calculateFixedHeightChartLayout,
|
|
27
27
|
getChartTooltipOptions,
|
|
28
28
|
formatChartNumber,
|
|
29
|
-
|
|
29
|
+
renderLegend,
|
|
30
30
|
observeLabelThreshold,
|
|
31
31
|
} from '../../charthelpers/index.js';
|
|
32
32
|
import type {FixedHeightChartDimensions} from '../../charthelpers/canvas-layout.js';
|
|
@@ -690,7 +690,7 @@ export class ObcPolarChart extends LitElement {
|
|
|
690
690
|
|
|
691
691
|
// Guard: Check if dataset has data
|
|
692
692
|
if (!this.data || this.data.length === 0) {
|
|
693
|
-
this.legendDiv
|
|
693
|
+
renderLegend(this.legendDiv, []);
|
|
694
694
|
return;
|
|
695
695
|
}
|
|
696
696
|
|
|
@@ -733,8 +733,7 @@ export class ObcPolarChart extends LitElement {
|
|
|
733
733
|
};
|
|
734
734
|
});
|
|
735
735
|
|
|
736
|
-
|
|
737
|
-
this.legendDiv.innerHTML = legendHTML;
|
|
736
|
+
renderLegend(this.legendDiv, legendItems);
|
|
738
737
|
} catch (error) {
|
|
739
738
|
console.debug(
|
|
740
739
|
'[obc-polar-chart] updateLegend: error generating legend HTML',
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
observeThemeChanges,
|
|
19
19
|
calculateFixedHeightChartLayout,
|
|
20
20
|
getChartTooltipOptions,
|
|
21
|
-
|
|
21
|
+
renderLegend,
|
|
22
22
|
observeLabelThreshold,
|
|
23
23
|
} from '../../charthelpers/index.js';
|
|
24
24
|
import type {FixedHeightChartDimensions} from '../../charthelpers/canvas-layout.js';
|
|
@@ -534,7 +534,7 @@ export class ObcRadialBarChart extends LitElement {
|
|
|
534
534
|
|
|
535
535
|
// Guard: Check if dataset has data
|
|
536
536
|
if (!this.data || this.data.length === 0) {
|
|
537
|
-
this.legendDiv
|
|
537
|
+
renderLegend(this.legendDiv, []);
|
|
538
538
|
return;
|
|
539
539
|
}
|
|
540
540
|
|
|
@@ -562,8 +562,7 @@ export class ObcRadialBarChart extends LitElement {
|
|
|
562
562
|
};
|
|
563
563
|
});
|
|
564
564
|
|
|
565
|
-
|
|
566
|
-
this.legendDiv.innerHTML = legendHTML;
|
|
565
|
+
renderLegend(this.legendDiv, legendItems);
|
|
567
566
|
} catch (error) {
|
|
568
567
|
console.debug(
|
|
569
568
|
'[obc-radial-bar-chart] updateLegend: error generating legend HTML',
|
|
@@ -36,7 +36,7 @@ import {
|
|
|
36
36
|
observeThemeChanges,
|
|
37
37
|
formatChartNumber,
|
|
38
38
|
getChartTooltipOptions,
|
|
39
|
-
|
|
39
|
+
renderLegend,
|
|
40
40
|
applyAlphaToColor,
|
|
41
41
|
normalizeXValue,
|
|
42
42
|
formatXValue,
|
|
@@ -3281,7 +3281,7 @@ export class ObcChartLineBase extends LitElement {
|
|
|
3281
3281
|
|
|
3282
3282
|
// Guard: Check if chart has datasets
|
|
3283
3283
|
if (!this.chart.data.datasets || this.chart.data.datasets.length === 0) {
|
|
3284
|
-
this.legendDiv
|
|
3284
|
+
renderLegend(this.legendDiv, []);
|
|
3285
3285
|
return;
|
|
3286
3286
|
}
|
|
3287
3287
|
|
|
@@ -3314,8 +3314,7 @@ export class ObcChartLineBase extends LitElement {
|
|
|
3314
3314
|
|
|
3315
3315
|
// Only update legend if we have valid items
|
|
3316
3316
|
if (legendItems.length > 0) {
|
|
3317
|
-
|
|
3318
|
-
this.legendDiv.innerHTML = legendHTML;
|
|
3317
|
+
renderLegend(this.legendDiv, legendItems);
|
|
3319
3318
|
}
|
|
3320
3319
|
} catch (error) {
|
|
3321
3320
|
console.debug(
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import {describe, it, expect, afterEach, vi} from 'vitest';
|
|
2
|
+
import '../main.css';
|
|
3
|
+
import '../bars-graphs/donut-chart/donut-chart.js';
|
|
4
|
+
import '../bars-graphs/pie-chart/pie-chart.js';
|
|
5
|
+
import '../bars-graphs/polar-chart/polar-chart.js';
|
|
6
|
+
import '../bars-graphs/radial-bar-chart/radial-bar-chart.js';
|
|
7
|
+
import '../bars-graphs/line-graph/line-graph.js';
|
|
8
|
+
import '../bars-graphs/area-graph/area-graph.js';
|
|
9
|
+
import type {ChartLineDataset} from '../building-blocks/chart-line/chart-line-base.js';
|
|
10
|
+
import {generateLegendHTML} from './legend.js';
|
|
11
|
+
|
|
12
|
+
const PROBE = '__obcLegendProbe';
|
|
13
|
+
const probe = () => (window as unknown as Record<string, unknown>)[PROBE];
|
|
14
|
+
|
|
15
|
+
/** Markup that sets the probe when the browser parses it as HTML. */
|
|
16
|
+
const markup = (tag: string) =>
|
|
17
|
+
`<img src="x" onerror="window.${PROBE} = '${tag}'">`;
|
|
18
|
+
|
|
19
|
+
/** A colour that closes a quoted `style` attribute and adds a handler. */
|
|
20
|
+
const HOSTILE_COLOR = `red" onmouseover="window.${PROBE} = 'color'`;
|
|
21
|
+
|
|
22
|
+
/** A colour that carries further declarations after the colour itself. */
|
|
23
|
+
const OVERLAY_COLOR = 'red; position: fixed; inset: 0';
|
|
24
|
+
|
|
25
|
+
const series = (overrides: Partial<ChartLineDataset>): ChartLineDataset[] => [
|
|
26
|
+
{label: 'Series', data: [1, 2], ...overrides},
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const mounted: HTMLElement[] = [];
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
mounted.splice(0).forEach((el) => el.remove());
|
|
33
|
+
delete (window as unknown as Record<string, unknown>)[PROBE];
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** Mounts a chart with its legend on and waits for the legend rows. */
|
|
37
|
+
async function legendOf<K extends keyof HTMLElementTagNameMap>(
|
|
38
|
+
tag: K,
|
|
39
|
+
configure: (chart: HTMLElementTagNameMap[K]) => void
|
|
40
|
+
): Promise<HTMLElement> {
|
|
41
|
+
const host = document.createElement('div');
|
|
42
|
+
host.style.cssText = 'width: 600px; height: 400px';
|
|
43
|
+
document.body.appendChild(host);
|
|
44
|
+
mounted.push(host);
|
|
45
|
+
|
|
46
|
+
const chart = document.createElement(tag);
|
|
47
|
+
configure(chart);
|
|
48
|
+
(chart as unknown as {legend: boolean}).legend = true;
|
|
49
|
+
host.appendChild(chart);
|
|
50
|
+
|
|
51
|
+
let legend: HTMLElement | null = null;
|
|
52
|
+
await vi.waitFor(() => {
|
|
53
|
+
legend = chart.shadowRoot!.querySelector<HTMLElement>('.legend');
|
|
54
|
+
expect(legend?.querySelector('.legend-item')).toBeTruthy();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// An injected image reports its failed load asynchronously; wait for it so
|
|
58
|
+
// the probe is read after any handler had its chance to run.
|
|
59
|
+
await Promise.all(
|
|
60
|
+
[...legend!.querySelectorAll('img')].map(
|
|
61
|
+
(img) =>
|
|
62
|
+
img.complete ||
|
|
63
|
+
new Promise((resolve) => {
|
|
64
|
+
img.addEventListener('error', resolve, {once: true});
|
|
65
|
+
img.addEventListener('load', resolve, {once: true});
|
|
66
|
+
})
|
|
67
|
+
)
|
|
68
|
+
);
|
|
69
|
+
return legend!;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Every element in the legend is one the legend itself builds. */
|
|
73
|
+
function expectOnlyLegendMarkup(legend: HTMLElement) {
|
|
74
|
+
for (const el of legend.querySelectorAll('*')) {
|
|
75
|
+
expect(['DIV', 'SPAN']).toContain(el.tagName);
|
|
76
|
+
for (const attr of el.getAttributeNames()) {
|
|
77
|
+
expect(['class', 'style']).toContain(attr);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
expect(probe()).toBeUndefined();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
describe('chart legend treats consumer strings as text', () => {
|
|
84
|
+
it('donut: label and unit', async () => {
|
|
85
|
+
const legend = await legendOf('obc-donut-chart', (chart) => {
|
|
86
|
+
chart.data = [
|
|
87
|
+
{label: markup('label'), value: 60},
|
|
88
|
+
{label: 'B', value: 40},
|
|
89
|
+
];
|
|
90
|
+
chart.showUnit = true;
|
|
91
|
+
chart.outerLabelUnit = markup('unit');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expectOnlyLegendMarkup(legend);
|
|
95
|
+
expect(legend.querySelector('.legend-label')!.textContent).toBe(
|
|
96
|
+
markup('label')
|
|
97
|
+
);
|
|
98
|
+
expect(legend.querySelector('.legend-unit')!.textContent).toBe(
|
|
99
|
+
markup('unit')
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('pie: label and unit', async () => {
|
|
104
|
+
const legend = await legendOf('obc-pie-chart', (chart) => {
|
|
105
|
+
chart.data = [
|
|
106
|
+
{label: markup('label'), value: 60},
|
|
107
|
+
{label: 'B', value: 40},
|
|
108
|
+
];
|
|
109
|
+
chart.showUnit = true;
|
|
110
|
+
chart.outerLabelUnit = markup('unit');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expectOnlyLegendMarkup(legend);
|
|
114
|
+
expect(legend.querySelector('.legend-label')!.textContent).toBe(
|
|
115
|
+
markup('label')
|
|
116
|
+
);
|
|
117
|
+
expect(legend.querySelector('.legend-unit')!.textContent).toBe(
|
|
118
|
+
markup('unit')
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('polar: sector label', async () => {
|
|
123
|
+
const legend = await legendOf('obc-polar-chart', (chart) => {
|
|
124
|
+
chart.data = [
|
|
125
|
+
{label: markup('label'), value: 60},
|
|
126
|
+
{label: 'B', value: 40},
|
|
127
|
+
];
|
|
128
|
+
chart.showSectorLabels = true;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expectOnlyLegendMarkup(legend);
|
|
132
|
+
expect(legend.querySelector('.legend-label')!.textContent).toBe(
|
|
133
|
+
markup('label')
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('polar: unit appended to the angle label', async () => {
|
|
138
|
+
const legend = await legendOf('obc-polar-chart', (chart) => {
|
|
139
|
+
chart.data = [
|
|
140
|
+
{label: 'A', value: 60},
|
|
141
|
+
{label: 'B', value: 40},
|
|
142
|
+
];
|
|
143
|
+
chart.outerLabelUnit = markup('unit');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
expectOnlyLegendMarkup(legend);
|
|
147
|
+
expect(legend.querySelector('.legend-label')!.textContent).toBe(
|
|
148
|
+
`0${markup('unit')}`
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('radial bar: a value that arrives as a string', async () => {
|
|
153
|
+
const legend = await legendOf('obc-radial-bar-chart', (chart) => {
|
|
154
|
+
chart.data = [markup('value') as unknown as number, 40];
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
expectOnlyLegendMarkup(legend);
|
|
158
|
+
expect(legend.querySelector('.legend-value')!.textContent).toBe(
|
|
159
|
+
markup('value')
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('line graph: dataset label', async () => {
|
|
164
|
+
const legend = await legendOf('obc-line-graph', (chart) => {
|
|
165
|
+
chart.datasets = series({label: markup('label')});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
expectOnlyLegendMarkup(legend);
|
|
169
|
+
expect(legend.querySelector('.legend-label')!.textContent).toBe(
|
|
170
|
+
markup('label')
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('area graph: dataset label', async () => {
|
|
175
|
+
const legend = await legendOf('obc-area-graph', (chart) => {
|
|
176
|
+
chart.datasets = series({label: markup('label')});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
expectOnlyLegendMarkup(legend);
|
|
180
|
+
expect(legend.querySelector('.legend-label')!.textContent).toBe(
|
|
181
|
+
markup('label')
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('chart legend swatch takes a colour and nothing else', () => {
|
|
187
|
+
it('line graph: a colour cannot add an attribute', async () => {
|
|
188
|
+
const legend = await legendOf('obc-line-graph', (chart) => {
|
|
189
|
+
chart.datasets = series({borderColor: HOSTILE_COLOR});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
expectOnlyLegendMarkup(legend);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('line graph: a colour cannot add a declaration', async () => {
|
|
196
|
+
const legend = await legendOf('obc-line-graph', (chart) => {
|
|
197
|
+
chart.datasets = series({borderColor: OVERLAY_COLOR});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const swatch = legend.querySelector<HTMLElement>('.legend-color')!;
|
|
201
|
+
expect(swatch.style.position).toBe('');
|
|
202
|
+
expect(getComputedStyle(swatch).position).toBe('static');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('donut: a colour cannot add an attribute', async () => {
|
|
206
|
+
const legend = await legendOf('obc-donut-chart', (chart) => {
|
|
207
|
+
chart.data = [
|
|
208
|
+
{label: 'A', value: 60},
|
|
209
|
+
{label: 'B', value: 40},
|
|
210
|
+
];
|
|
211
|
+
chart.colors = [HOSTILE_COLOR, 'blue'];
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
expectOnlyLegendMarkup(legend);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('a valid colour still paints the swatch', async () => {
|
|
218
|
+
const legend = await legendOf('obc-line-graph', (chart) => {
|
|
219
|
+
chart.datasets = series({borderColor: 'rgb(1, 2, 3)'});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const swatch = legend.querySelector<HTMLElement>('.legend-color')!;
|
|
223
|
+
expect(getComputedStyle(swatch).backgroundColor).toBe('rgb(1, 2, 3)');
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('generateLegendHTML', () => {
|
|
228
|
+
it('returns markup that parses back to the values as text', () => {
|
|
229
|
+
const holder = document.createElement('div');
|
|
230
|
+
holder.innerHTML = generateLegendHTML([
|
|
231
|
+
{
|
|
232
|
+
fillStyle: HOSTILE_COLOR,
|
|
233
|
+
label: markup('label'),
|
|
234
|
+
value: markup('value'),
|
|
235
|
+
unit: markup('unit'),
|
|
236
|
+
},
|
|
237
|
+
]);
|
|
238
|
+
|
|
239
|
+
expectOnlyLegendMarkup(holder);
|
|
240
|
+
expect(holder.querySelector('.legend-label')!.textContent).toBe(
|
|
241
|
+
markup('label')
|
|
242
|
+
);
|
|
243
|
+
expect(holder.querySelector('.legend-value')!.textContent).toBe(
|
|
244
|
+
markup('value')
|
|
245
|
+
);
|
|
246
|
+
expect(holder.querySelector('.legend-unit')!.textContent).toBe(
|
|
247
|
+
markup('unit')
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('keeps a valid colour on the swatch', () => {
|
|
252
|
+
const holder = document.createElement('div');
|
|
253
|
+
holder.innerHTML = generateLegendHTML([
|
|
254
|
+
{fillStyle: 'rgb(1, 2, 3)', label: 'A', value: '1', unit: ''},
|
|
255
|
+
]);
|
|
256
|
+
|
|
257
|
+
expect(
|
|
258
|
+
holder.querySelector<HTMLElement>('.legend-color')!.style.backgroundColor
|
|
259
|
+
).toBe('rgb(1, 2, 3)');
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -1,30 +1,66 @@
|
|
|
1
|
+
/** One row of a chart legend. */
|
|
2
|
+
export interface LegendItem {
|
|
3
|
+
/** Swatch colour. A value that is not a CSS colour leaves the swatch unpainted. */
|
|
4
|
+
fillStyle: string;
|
|
5
|
+
label: string;
|
|
6
|
+
value: string;
|
|
7
|
+
unit: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function span(className: string, text = ''): HTMLSpanElement {
|
|
11
|
+
const el = document.createElement('span');
|
|
12
|
+
el.className = className;
|
|
13
|
+
el.textContent = text;
|
|
14
|
+
return el;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fills a chart's legend with one row per item, replacing what was there.
|
|
19
|
+
*
|
|
20
|
+
* Labels, values, units and colours come from the consumer's data, which an
|
|
21
|
+
* application often takes from a backend or from what a user typed. They are
|
|
22
|
+
* written as text and as a single style value, never as markup: a label
|
|
23
|
+
* containing HTML shows up as that literal text.
|
|
24
|
+
*
|
|
25
|
+
* @param container - The legend element to fill
|
|
26
|
+
* @param items - The rows, in display order; an empty list clears the legend
|
|
27
|
+
*/
|
|
28
|
+
export function renderLegend(
|
|
29
|
+
container: HTMLElement,
|
|
30
|
+
items: readonly LegendItem[]
|
|
31
|
+
): void {
|
|
32
|
+
container.replaceChildren(
|
|
33
|
+
...items.map((item) => {
|
|
34
|
+
const swatch = span('legend-color');
|
|
35
|
+
swatch.style.backgroundColor = item.fillStyle;
|
|
36
|
+
|
|
37
|
+
const label = span('legend-group legend-group--label');
|
|
38
|
+
label.append(swatch, span('legend-label', item.label));
|
|
39
|
+
|
|
40
|
+
const valueUnit = span('legend-group legend-group--value-unit');
|
|
41
|
+
valueUnit.append(
|
|
42
|
+
span('legend-value', item.value),
|
|
43
|
+
span('legend-unit', item.unit)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const row = document.createElement('div');
|
|
47
|
+
row.className = 'legend-item';
|
|
48
|
+
row.append(label, valueUnit);
|
|
49
|
+
return row;
|
|
50
|
+
})
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
1
54
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
55
|
+
* The legend rows as an HTML string, with every value escaped.
|
|
56
|
+
*
|
|
57
|
+
* @deprecated Use `renderLegend()`, which fills the legend element itself.
|
|
58
|
+
* @param legendItems - The rows, in display order
|
|
59
|
+
* @returns The rows' markup
|
|
5
60
|
*/
|
|
6
|
-
export function generateLegendHTML(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
unit: string;
|
|
12
|
-
}>
|
|
13
|
-
): string {
|
|
14
|
-
return legendItems
|
|
15
|
-
.map(
|
|
16
|
-
(item) => `
|
|
17
|
-
<div class="legend-item">
|
|
18
|
-
<span class="legend-group legend-group--label">
|
|
19
|
-
<span class="legend-color" style="background-color: ${item.fillStyle};"></span>
|
|
20
|
-
<span class="legend-label">${item.label}</span>
|
|
21
|
-
</span>
|
|
22
|
-
<span class="legend-group legend-group--value-unit">
|
|
23
|
-
<span class="legend-value">${item.value}</span>
|
|
24
|
-
<span class="legend-unit">${item.unit}</span>
|
|
25
|
-
</span>
|
|
26
|
-
</div>
|
|
27
|
-
`
|
|
28
|
-
)
|
|
29
|
-
.join('');
|
|
61
|
+
export function generateLegendHTML(legendItems: readonly LegendItem[]): string {
|
|
62
|
+
const holder = document.createElement('div');
|
|
63
|
+
renderLegend(holder, legendItems);
|
|
64
|
+
// A read, not a write: serialising the nodes escapes the text they hold.
|
|
65
|
+
return holder.innerHTML;
|
|
30
66
|
}
|