@cratis/scene.primereact 4.4.0 → 4.6.0
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/README.md +25 -3
- package/chart/ChartGallery.stories.tsx +63 -0
- package/chart/ChartType.ts +14 -0
- package/chart/ChartTypeResolution.ts +14 -0
- package/chart/PrimeChart.tsx +62 -0
- package/chart/chartHasData.ts +22 -0
- package/chart/chartTypeChoices.ts +32 -0
- package/chart/index.ts +7 -0
- package/chart/resolveChartType.ts +61 -0
- package/chart/useChart.ts +61 -0
- package/dist/cjs/chart/ChartType.js +18 -0
- package/dist/cjs/chart/ChartType.js.map +1 -0
- package/dist/cjs/chart/PrimeChart.js +92 -0
- package/dist/cjs/chart/PrimeChart.js.map +1 -0
- package/dist/cjs/chart/chartHasData.js +24 -0
- package/dist/cjs/chart/chartHasData.js.map +1 -0
- package/dist/cjs/chart/chartTypeChoices.js +43 -0
- package/dist/cjs/chart/chartTypeChoices.js.map +1 -0
- package/dist/cjs/chart/resolveChartType.js +69 -0
- package/dist/cjs/chart/resolveChartType.js.map +1 -0
- package/dist/cjs/chart/useChart.js +62 -0
- package/dist/cjs/chart/useChart.js.map +1 -0
- package/dist/cjs/form/MultiStateGlyph.js +59 -0
- package/dist/cjs/form/MultiStateGlyph.js.map +1 -0
- package/dist/cjs/form/PrimeMultiStateCheckbox.js +123 -0
- package/dist/cjs/form/PrimeMultiStateCheckbox.js.map +1 -0
- package/dist/cjs/form/multiStateOptions.js +77 -0
- package/dist/cjs/form/multiStateOptions.js.map +1 -0
- package/dist/cjs/form/multiStateStates.js +32 -0
- package/dist/cjs/form/multiStateStates.js.map +1 -0
- package/dist/cjs/form/useMultiStateSelection.js +51 -0
- package/dist/cjs/form/useMultiStateSelection.js.map +1 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/primeReactComponents.js +5 -0
- package/dist/cjs/primeReactComponents.js.map +1 -1
- package/dist/cjs/primeReactDescriptors.js +157 -0
- package/dist/cjs/primeReactDescriptors.js.map +1 -0
- package/dist/cjs/primeReactPackage.js +4 -0
- package/dist/cjs/primeReactPackage.js.map +1 -1
- package/dist/cjs/properties.js +11 -0
- package/dist/cjs/properties.js.map +1 -1
- package/dist/cjs/structurallyEqual.js +27 -0
- package/dist/cjs/structurallyEqual.js.map +1 -0
- package/dist/cjs/useStructuralValue.js +20 -0
- package/dist/cjs/useStructuralValue.js.map +1 -0
- package/dist/esm/chart/ChartGallery.stories.d.ts +14 -0
- package/dist/esm/chart/ChartGallery.stories.d.ts.map +1 -0
- package/dist/esm/chart/ChartGallery.stories.js +47 -0
- package/dist/esm/chart/ChartGallery.stories.js.map +1 -0
- package/dist/esm/chart/ChartType.d.ts +11 -0
- package/dist/esm/chart/ChartType.d.ts.map +1 -0
- package/dist/esm/chart/ChartType.js +16 -0
- package/dist/esm/chart/ChartType.js.map +1 -0
- package/dist/esm/chart/ChartTypeResolution.d.ts +10 -0
- package/dist/esm/chart/ChartTypeResolution.d.ts.map +1 -0
- package/dist/esm/chart/ChartTypeResolution.js +2 -0
- package/dist/esm/chart/ChartTypeResolution.js.map +1 -0
- package/dist/esm/chart/PrimeChart.d.ts +3 -0
- package/dist/esm/chart/PrimeChart.d.ts.map +1 -0
- package/dist/esm/chart/PrimeChart.js +90 -0
- package/dist/esm/chart/PrimeChart.js.map +1 -0
- package/dist/esm/chart/chartHasData.d.ts +2 -0
- package/dist/esm/chart/chartHasData.d.ts.map +1 -0
- package/dist/esm/chart/chartHasData.js +22 -0
- package/dist/esm/chart/chartHasData.js.map +1 -0
- package/dist/esm/chart/chartTypeChoices.d.ts +3 -0
- package/dist/esm/chart/chartTypeChoices.d.ts.map +1 -0
- package/dist/esm/chart/chartTypeChoices.js +41 -0
- package/dist/esm/chart/chartTypeChoices.js.map +1 -0
- package/dist/esm/chart/index.d.ts +5 -0
- package/dist/esm/chart/index.d.ts.map +1 -0
- package/dist/esm/chart/index.js +5 -0
- package/dist/esm/chart/index.js.map +1 -0
- package/dist/esm/chart/resolveChartType.d.ts +6 -0
- package/dist/esm/chart/resolveChartType.d.ts.map +1 -0
- package/dist/esm/chart/resolveChartType.js +66 -0
- package/dist/esm/chart/resolveChartType.js.map +1 -0
- package/dist/esm/chart/useChart.d.ts +4 -0
- package/dist/esm/chart/useChart.d.ts.map +1 -0
- package/dist/esm/chart/useChart.js +60 -0
- package/dist/esm/chart/useChart.js.map +1 -0
- package/dist/esm/form/FormGallery.stories.d.ts.map +1 -1
- package/dist/esm/form/FormGallery.stories.js +8 -0
- package/dist/esm/form/FormGallery.stories.js.map +1 -1
- package/dist/esm/form/MultiStateGlyph.d.ts +8 -0
- package/dist/esm/form/MultiStateGlyph.d.ts.map +1 -0
- package/dist/esm/form/MultiStateGlyph.js +57 -0
- package/dist/esm/form/MultiStateGlyph.js.map +1 -0
- package/dist/esm/form/MultiStateOption.d.ts +7 -0
- package/dist/esm/form/MultiStateOption.d.ts.map +1 -0
- package/dist/esm/form/MultiStateOption.js +2 -0
- package/dist/esm/form/MultiStateOption.js.map +1 -0
- package/dist/esm/form/PrimeMultiStateCheckbox.d.ts +3 -0
- package/dist/esm/form/PrimeMultiStateCheckbox.d.ts.map +1 -0
- package/dist/esm/form/PrimeMultiStateCheckbox.js +121 -0
- package/dist/esm/form/PrimeMultiStateCheckbox.js.map +1 -0
- package/dist/esm/form/index.d.ts +1 -0
- package/dist/esm/form/index.d.ts.map +1 -1
- package/dist/esm/form/index.js +1 -0
- package/dist/esm/form/index.js.map +1 -1
- package/dist/esm/form/multiStateOptions.d.ts +4 -0
- package/dist/esm/form/multiStateOptions.d.ts.map +1 -0
- package/dist/esm/form/multiStateOptions.js +75 -0
- package/dist/esm/form/multiStateOptions.js.map +1 -0
- package/dist/esm/form/multiStateStates.d.ts +4 -0
- package/dist/esm/form/multiStateStates.d.ts.map +1 -0
- package/dist/esm/form/multiStateStates.js +30 -0
- package/dist/esm/form/multiStateStates.js.map +1 -0
- package/dist/esm/form/useMultiStateSelection.d.ts +6 -0
- package/dist/esm/form/useMultiStateSelection.d.ts.map +1 -0
- package/dist/esm/form/useMultiStateSelection.js +49 -0
- package/dist/esm/form/useMultiStateSelection.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/primeReactComponents.d.ts.map +1 -1
- package/dist/esm/primeReactComponents.js +5 -0
- package/dist/esm/primeReactComponents.js.map +1 -1
- package/dist/esm/primeReactDescriptors.d.ts +3 -0
- package/dist/esm/primeReactDescriptors.d.ts.map +1 -0
- package/dist/esm/primeReactDescriptors.js +155 -0
- package/dist/esm/primeReactDescriptors.js.map +1 -0
- package/dist/esm/primeReactPackage.d.ts.map +1 -1
- package/dist/esm/primeReactPackage.js +4 -0
- package/dist/esm/primeReactPackage.js.map +1 -1
- package/dist/esm/properties.d.ts +1 -0
- package/dist/esm/properties.d.ts.map +1 -1
- package/dist/esm/properties.js +11 -1
- package/dist/esm/properties.js.map +1 -1
- package/dist/esm/structurallyEqual.d.ts +2 -0
- package/dist/esm/structurallyEqual.d.ts.map +1 -0
- package/dist/esm/structurallyEqual.js +25 -0
- package/dist/esm/structurallyEqual.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/useStructuralValue.d.ts +2 -0
- package/dist/esm/useStructuralValue.d.ts.map +1 -0
- package/dist/esm/useStructuralValue.js +18 -0
- package/dist/esm/useStructuralValue.js.map +1 -0
- package/form/FormGallery.stories.tsx +8 -0
- package/form/MultiStateGlyph.tsx +55 -0
- package/form/MultiStateOption.ts +17 -0
- package/form/PrimeMultiStateCheckbox.tsx +101 -0
- package/form/index.ts +1 -0
- package/form/multiStateOptions.ts +78 -0
- package/form/multiStateStates.ts +23 -0
- package/form/useMultiStateSelection.ts +48 -0
- package/index.ts +2 -0
- package/package.json +17 -7
- package/primeReactComponents.ts +4 -0
- package/primeReactDescriptors.ts +48 -0
- package/primeReactPackage.ts +4 -2
- package/properties.ts +14 -0
- package/structurallyEqual.ts +29 -0
- package/useStructuralValue.ts +17 -0
package/README.md
CHANGED
|
@@ -113,11 +113,33 @@ not through the `--p-*` properties, which only PrimeReact's own components read.
|
|
|
113
113
|
|
|
114
114
|
| Component | Why |
|
|
115
115
|
| --- | --- |
|
|
116
|
-
| `chart` | PrimeReact's `Chart` is a thin wrapper over Chart.js and does nothing without `chart.js` installed and a full Chart.js configuration object. Adding a charting library as a dependency of a component-mapping package is out of scope; charting deserves its own Scene package with its own vocabulary. |
|
|
117
116
|
| `editor` | `Editor` wraps Quill and needs `quill` installed. Same reasoning — a rich-text editor is a product decision, not a mapping. |
|
|
118
117
|
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
The editor is a deliberate omission, not an oversight. A profile needing it should activate a package
|
|
119
|
+
that owns its dependency.
|
|
120
|
+
|
|
121
|
+
## Chart.js peer dependency
|
|
122
|
+
|
|
123
|
+
PrimeReact 11 removed its former Chart.js wrapper, so the Scene `chart` adapter mounts a Chart.js canvas
|
|
124
|
+
itself. `chart.js@^4.5.1` is an optional peer dependency of `@cratis/scene.primereact`, optional only in that
|
|
125
|
+
a host that never renders a chart can omit it. **A host that renders charts must install `chart.js`**,
|
|
126
|
+
including when a bundler resolves imports statically: without it the build can fail to resolve
|
|
127
|
+
`chart.js/auto`, or the chart reports that it could not be drawn.
|
|
128
|
+
|
|
129
|
+
The adapter only imports Chart.js after its canvas mounts, which keeps SSR safe. It rebuilds the chart when
|
|
130
|
+
the type, data or options change in content, not when a document edit hands it equal data in a new object.
|
|
131
|
+
|
|
132
|
+
`chart` accepts canonical string types (`bar`, `line`, `pie`, `doughnut`, `polarArea`, `radar`, `bubble`,
|
|
133
|
+
`scatter`), the numeric values `0` to `7` of the old .NET enum, and that enum's Pascal case names, without
|
|
134
|
+
rewriting the stored element. Any other type is reported instead of drawn as a bar chart. A chart without data
|
|
135
|
+
points shows its `emptyLabel`.
|
|
136
|
+
|
|
137
|
+
## Browser specifications
|
|
138
|
+
|
|
139
|
+
Most specifications run under jsdom with `yarn test`. Painted pixels, keyboard behavior and computed theme
|
|
140
|
+
styles need a real browser, so `yarn test:browser` bundles the controls and drives headless Chromium through
|
|
141
|
+
`playwright-core`. Install the browser once with `yarn playwright-core install chromium`. These specifications
|
|
142
|
+
are not part of `yarn test` and fail, rather than skip, when Chromium is missing.
|
|
121
143
|
|
|
122
144
|
Also worth knowing: `column` renders nothing on its own, and since PrimeReact 11 that is *our* semantics
|
|
123
145
|
rather than PrimeReact's. v11 removed `primereact/column`, so `data/Column.tsx` is a Cratis-owned
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
5
|
+
import { SceneElementView } from '@cratis/scene.react';
|
|
6
|
+
import { primeReactComponents } from '../primeReactComponents';
|
|
7
|
+
import { sceneComponent } from '../storyElements';
|
|
8
|
+
|
|
9
|
+
const meta = {
|
|
10
|
+
title: 'PrimeReact/Chart',
|
|
11
|
+
component: SceneElementView,
|
|
12
|
+
tags: ['autodocs'],
|
|
13
|
+
} satisfies Meta<typeof SceneElementView>;
|
|
14
|
+
|
|
15
|
+
export default meta;
|
|
16
|
+
|
|
17
|
+
type Story = StoryObj<typeof meta>;
|
|
18
|
+
|
|
19
|
+
/** A Chart.js chart rendered through the Scene element and package registry. */
|
|
20
|
+
const salesData = {
|
|
21
|
+
labels: ['January', 'February', 'March'],
|
|
22
|
+
datasets: [{ label: 'Sales', data: [12, 19, 8], backgroundColor: '#6366f1' }],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const Bar: Story = {
|
|
26
|
+
args: {
|
|
27
|
+
element: sceneComponent('sales', 'chart', {
|
|
28
|
+
type: 'bar', data: salesData, options: { maintainAspectRatio: false }, style: { height: 240 }, ariaLabel: 'Monthly sales',
|
|
29
|
+
}),
|
|
30
|
+
registry: primeReactComponents,
|
|
31
|
+
resolveBinding: () => undefined,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** A migrated chart that has no data yet. It says so instead of drawing a blank canvas or inventing values. */
|
|
36
|
+
export const MigratedWithoutData: Story = {
|
|
37
|
+
args: {
|
|
38
|
+
element: sceneComponent('legacy-empty', 'chart', { type: 0, style: { height: 72 }, ariaLabel: 'Migrated chart without data', legacyType: 'PrimeReact.Chart' }),
|
|
39
|
+
registry: primeReactComponents,
|
|
40
|
+
resolveBinding: () => undefined,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** A chart type the renderer does not know is reported rather than drawn as a bar chart. */
|
|
45
|
+
export const UnsupportedType: Story = {
|
|
46
|
+
args: {
|
|
47
|
+
element: sceneComponent('unsupported', 'chart', { type: 8, data: salesData }),
|
|
48
|
+
registry: primeReactComponents,
|
|
49
|
+
resolveBinding: () => undefined,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** A migrated element that retains the original numeric ChartType enum value in its own properties. */
|
|
54
|
+
export const MigratedLegacyBar: Story = {
|
|
55
|
+
args: {
|
|
56
|
+
element: sceneComponent('legacy-sales', 'chart', {
|
|
57
|
+
type: 0, data: salesData, options: { maintainAspectRatio: false }, style: { height: 240 }, ariaLabel: 'Migrated monthly sales',
|
|
58
|
+
legacyType: 'PrimeReact.Chart', legacyExtras: { canvas: { x: 256, y: 184 } },
|
|
59
|
+
}),
|
|
60
|
+
registry: primeReactComponents,
|
|
61
|
+
resolveBinding: () => undefined,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/** The canonical names of the Chart.js chart types Scene's `chart` component supports. */
|
|
5
|
+
export enum ChartType {
|
|
6
|
+
Bar = 'bar',
|
|
7
|
+
Line = 'line',
|
|
8
|
+
Pie = 'pie',
|
|
9
|
+
Doughnut = 'doughnut',
|
|
10
|
+
PolarArea = 'polarArea',
|
|
11
|
+
Radar = 'radar',
|
|
12
|
+
Bubble = 'bubble',
|
|
13
|
+
Scatter = 'scatter',
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ChartType } from './ChartType';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What an authored chart `type` resolved to: a Chart.js type, or the reason it names none.
|
|
8
|
+
*
|
|
9
|
+
* A value that is not a known type is never replaced by a default. Drawing a bar chart for a document that
|
|
10
|
+
* asked for something else hides a data problem behind a plausible picture.
|
|
11
|
+
*/
|
|
12
|
+
export type ChartTypeResolution =
|
|
13
|
+
| { isValid: true; type: ChartType }
|
|
14
|
+
| { isValid: false; value: unknown; message: string };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { CSSProperties, useMemo, useRef } from 'react';
|
|
5
|
+
import type { ChartConfiguration, ChartData, ChartOptions } from 'chart.js';
|
|
6
|
+
import { RegisteredComponentProps } from '@cratis/scene.react';
|
|
7
|
+
import { booleanProperty, recordProperty, stringProperty } from '../properties';
|
|
8
|
+
import { useStructuralValue } from '../useStructuralValue';
|
|
9
|
+
import { chartHasData } from './chartHasData';
|
|
10
|
+
import { resolveChartType } from './resolveChartType';
|
|
11
|
+
import { useChart } from './useChart';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The `PrimeReact:chart` component, rendered by Chart.js because PrimeReact 11 no longer ships its
|
|
15
|
+
* former Chart.js wrapper.
|
|
16
|
+
*
|
|
17
|
+
* Three states are explicit rather than a blank canvas: an unsupported `type` is reported (the right chart
|
|
18
|
+
* cannot be guessed), a chart with no data points shows its `emptyLabel` (data is never invented), and a
|
|
19
|
+
* chart Chart.js could not draw says why. Only a chart with data and a supported type reaches Chart.js.
|
|
20
|
+
*
|
|
21
|
+
* Chart.js is an optional peer dependency of this package: a host that renders charts must install
|
|
22
|
+
* `chart.js` itself. The chart rebuilds only when its resolved type, data or options change in content -
|
|
23
|
+
* every edit to a Scene document clones the whole document, and a clone with equal content is not a change.
|
|
24
|
+
*
|
|
25
|
+
* The chart fills the authored size: `maintainAspectRatio` defaults to `false` here (Chart.js defaults to
|
|
26
|
+
* `true`, which shrinks the chart inside its box) and an authored `options.maintainAspectRatio` wins. The
|
|
27
|
+
* `responsive` property is authoritative over `options.responsive` when both are set.
|
|
28
|
+
*/
|
|
29
|
+
export function PrimeChart({ element, interactions }: RegisteredComponentProps) {
|
|
30
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
31
|
+
const resolution = resolveChartType(element);
|
|
32
|
+
const data = useStructuralValue(recordProperty(element, 'data') as ChartData | undefined);
|
|
33
|
+
const options = useStructuralValue(recordProperty(element, 'options') as ChartOptions | undefined);
|
|
34
|
+
const style = recordProperty(element, 'style') as CSSProperties | undefined;
|
|
35
|
+
const responsive = booleanProperty(element, 'responsive', options?.responsive ?? true);
|
|
36
|
+
const ariaLabel = stringProperty(element, 'ariaLabel', element.name || 'Chart');
|
|
37
|
+
const emptyLabel = stringProperty(element, 'emptyLabel', 'No chart data');
|
|
38
|
+
const hasData = chartHasData(data);
|
|
39
|
+
const type = resolution.isValid ? resolution.type : undefined;
|
|
40
|
+
const enabled = type !== undefined && hasData;
|
|
41
|
+
|
|
42
|
+
const configuration = useMemo(() => ({
|
|
43
|
+
type,
|
|
44
|
+
data: data ?? { datasets: [] },
|
|
45
|
+
options: { maintainAspectRatio: false, ...options, responsive },
|
|
46
|
+
}) as unknown as ChartConfiguration, [type, data, options, responsive]);
|
|
47
|
+
|
|
48
|
+
const failure = useChart(canvas, configuration, enabled);
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div
|
|
52
|
+
data-scene-id={element.id}
|
|
53
|
+
data-scene-component='chart'
|
|
54
|
+
style={{ position: 'relative', width: element.size.width ?? '100%', height: element.size.height ?? '100%', ...style }}
|
|
55
|
+
{...interactions}>
|
|
56
|
+
{!resolution.isValid && <div role='alert' data-scene-state='unsupported-type'>{resolution.message}</div>}
|
|
57
|
+
{resolution.isValid && !hasData && <div role='status' data-scene-state='empty'>{emptyLabel}</div>}
|
|
58
|
+
{failure !== undefined && <div role='alert' data-scene-state='unavailable'>{`The chart could not be drawn: ${failure}`}</div>}
|
|
59
|
+
{enabled && <canvas ref={canvas} role='img' aria-label={ariaLabel} hidden={failure !== undefined} />}
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Whether chart data holds at least one data point to draw.
|
|
6
|
+
*
|
|
7
|
+
* Datasets whose `data` is empty (or absent) draw nothing but axes, so the chart reports it instead.
|
|
8
|
+
* A dataset may also give its points as keyed records (Chart.js `parsing` over an object), which counts
|
|
9
|
+
* when it has any keys.
|
|
10
|
+
*/
|
|
11
|
+
export function chartHasData(data: unknown): boolean {
|
|
12
|
+
if (data === null || typeof data !== 'object') return false;
|
|
13
|
+
const datasets = (data as Record<string, unknown>).datasets;
|
|
14
|
+
if (!Array.isArray(datasets)) return false;
|
|
15
|
+
|
|
16
|
+
return datasets.some(dataset => {
|
|
17
|
+
if (dataset === null || typeof dataset !== 'object') return false;
|
|
18
|
+
const points = (dataset as Record<string, unknown>).data;
|
|
19
|
+
if (Array.isArray(points)) return points.length > 0;
|
|
20
|
+
return points !== null && typeof points === 'object' && Object.keys(points).length > 0;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { PropertyChoice } from '@cratis/scene.model';
|
|
5
|
+
import { ChartType } from './ChartType';
|
|
6
|
+
import { legacyChartTypes } from './resolveChartType';
|
|
7
|
+
|
|
8
|
+
const chartTypeLabels: Record<ChartType, string> = {
|
|
9
|
+
[ChartType.Bar]: 'Bar',
|
|
10
|
+
[ChartType.Line]: 'Line',
|
|
11
|
+
[ChartType.Pie]: 'Pie',
|
|
12
|
+
[ChartType.Doughnut]: 'Doughnut',
|
|
13
|
+
[ChartType.PolarArea]: 'Polar area',
|
|
14
|
+
[ChartType.Radar]: 'Radar',
|
|
15
|
+
[ChartType.Bubble]: 'Bubble',
|
|
16
|
+
[ChartType.Scatter]: 'Scatter',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The chart type choices: the stable names new documents use, then the ordinals migrated documents carry,
|
|
21
|
+
* then the Pascal case member names of the original .NET enum that a serialized enum produces.
|
|
22
|
+
* A migrated chart stores its original `type`; offering every form the renderer accepts keeps that stored
|
|
23
|
+
* value valid (and its type visible and re-selectable) without rewriting it to another form behind the
|
|
24
|
+
* author's back. The choices are the same set `resolveChartType` accepts, so what the renderer draws is
|
|
25
|
+
* exactly what the descriptor allows.
|
|
26
|
+
*/
|
|
27
|
+
export const chartTypeChoices: PropertyChoice[] = [
|
|
28
|
+
...Object.values(ChartType).map(type => ({ value: type, label: chartTypeLabels[type] })),
|
|
29
|
+
...legacyChartTypes.map((type, ordinal) => ({ value: ordinal, label: `${chartTypeLabels[type]} (legacy ${ordinal})`, description: `The ordinal ${ordinal} of the original chart type enum, as stored by migrated documents.` })),
|
|
30
|
+
...(Object.keys(ChartType) as (keyof typeof ChartType)[]).map(name => ({ value: name, label: `${chartTypeLabels[ChartType[name]]} (legacy name ${name})`, description: `The member name ${name} of the original chart type enum, as a serialized enum stores it.` })),
|
|
31
|
+
];
|
|
32
|
+
|
package/chart/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
export * from './ChartType';
|
|
5
|
+
export * from './ChartTypeResolution';
|
|
6
|
+
export * from './PrimeChart';
|
|
7
|
+
export * from './resolveChartType';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SceneElement } from '@cratis/scene.model';
|
|
5
|
+
import { ChartType } from './ChartType';
|
|
6
|
+
import { ChartTypeResolution } from './ChartTypeResolution';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The ordinals of the original .NET `PrimeReact.Charts.ChartType` enum, in declaration order
|
|
10
|
+
* (`Studio/Source/PrimeReact/Charts/Chart.cs`). Migrated documents keep the ordinal, so the position of
|
|
11
|
+
* each entry here is part of the stored-data contract and must never be reordered.
|
|
12
|
+
*/
|
|
13
|
+
export const legacyChartTypes: readonly ChartType[] = [
|
|
14
|
+
ChartType.Bar,
|
|
15
|
+
ChartType.Line,
|
|
16
|
+
ChartType.Pie,
|
|
17
|
+
ChartType.Doughnut,
|
|
18
|
+
ChartType.PolarArea,
|
|
19
|
+
ChartType.Radar,
|
|
20
|
+
ChartType.Bubble,
|
|
21
|
+
ChartType.Scatter,
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolves an authored chart type to the Chart.js vocabulary.
|
|
26
|
+
*
|
|
27
|
+
* Three forms are accepted, and the stored value is never rewritten:
|
|
28
|
+
* - a canonical Chart.js name from {@link ChartType} (`'polarArea'`), which new documents use;
|
|
29
|
+
* - the ordinal of the original .NET enum (`4`), which migrated documents carry;
|
|
30
|
+
* - the .NET member name in its original Pascal case (`'PolarArea'`), which a JSON serialization of that
|
|
31
|
+
* enum produces.
|
|
32
|
+
*
|
|
33
|
+
* Anything else - an unknown or fractional ordinal, a numeric string, a differently cased name, `null` -
|
|
34
|
+
* resolves to a finding with a message, because the right chart cannot be known. An absent `type` is the
|
|
35
|
+
* one default: the original control defaulted to `Bar`.
|
|
36
|
+
*/
|
|
37
|
+
export function resolveChartType(element: SceneElement): ChartTypeResolution {
|
|
38
|
+
const value = element.properties.type;
|
|
39
|
+
if (value === undefined) return { isValid: true, type: ChartType.Bar };
|
|
40
|
+
|
|
41
|
+
if (typeof value === 'number') {
|
|
42
|
+
const type = Number.isInteger(value) ? legacyChartTypes[value] : undefined;
|
|
43
|
+
return type === undefined ? unsupported(value) : { isValid: true, type };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof value === 'string') {
|
|
47
|
+
if (Object.values(ChartType).includes(value as ChartType)) return { isValid: true, type: value as ChartType };
|
|
48
|
+
if (Object.hasOwn(ChartType, value)) return { isValid: true, type: ChartType[value as keyof typeof ChartType] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return unsupported(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function unsupported(value: unknown): ChartTypeResolution {
|
|
55
|
+
const shown = typeof value === 'string' ? `'${value}'` : JSON.stringify(value) ?? String(value);
|
|
56
|
+
return {
|
|
57
|
+
isValid: false,
|
|
58
|
+
value,
|
|
59
|
+
message: `Unsupported chart type ${shown}. Use one of ${Object.values(ChartType).join(', ')}, or a legacy ordinal from 0 to ${legacyChartTypes.length - 1}.`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { RefObject, useEffect, useState } from 'react';
|
|
5
|
+
import type { ChartConfiguration } from 'chart.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Draws a Chart.js chart on a canvas and owns its lifetime.
|
|
9
|
+
*
|
|
10
|
+
* Chart.js loads only after the canvas has mounted, which keeps server rendering independent of browser
|
|
11
|
+
* globals. The chart is destroyed before it is rebuilt and when the component unmounts - Chart.js owns
|
|
12
|
+
* listeners and resize observers, so a retained instance would draw twice and leak them.
|
|
13
|
+
*
|
|
14
|
+
* A chart that cannot be drawn is reported rather than left as a blank canvas: `chart.js` not being
|
|
15
|
+
* installed (it is an optional peer dependency), a failed dynamic import and a rejected configuration all
|
|
16
|
+
* come back as the returned failure message. A rejected configuration is released from the canvas as well,
|
|
17
|
+
* so correcting it draws again on the same canvas.
|
|
18
|
+
*
|
|
19
|
+
* @param canvas The canvas to draw on. It must be rendered whenever `enabled` is true.
|
|
20
|
+
* @param configuration The Chart.js configuration. Pass a structurally stable value (see
|
|
21
|
+
* {@link useStructuralValue}); a new identity rebuilds the chart.
|
|
22
|
+
* @param enabled Whether there is something to draw.
|
|
23
|
+
* @returns The reason the chart could not be drawn, or `undefined`.
|
|
24
|
+
*/
|
|
25
|
+
export function useChart(canvas: RefObject<HTMLCanvasElement | null>, configuration: ChartConfiguration, enabled: boolean): string | undefined {
|
|
26
|
+
const [failure, setFailure] = useState<string | undefined>(undefined);
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
setFailure(undefined);
|
|
30
|
+
const target = canvas.current;
|
|
31
|
+
if (!enabled || target === null) return undefined;
|
|
32
|
+
|
|
33
|
+
let disposed = false;
|
|
34
|
+
let instance: { destroy(): void } | undefined;
|
|
35
|
+
|
|
36
|
+
const create = async () => {
|
|
37
|
+
let chartType: typeof import('chart.js/auto').default | undefined;
|
|
38
|
+
try {
|
|
39
|
+
chartType = (await import('chart.js/auto')).default;
|
|
40
|
+
if (disposed) return;
|
|
41
|
+
instance = new chartType(target, structuredClone(configuration));
|
|
42
|
+
} catch (error) {
|
|
43
|
+
// Chart.js registers a chart on its canvas before the constructor finishes, so a constructor
|
|
44
|
+
// that throws still leaves a registered instance behind - one `instance` never points at. Left
|
|
45
|
+
// alone it makes every later attempt fail with "Canvas is already in use" and hides the real
|
|
46
|
+
// reason, so release whatever is bound to this canvas before reporting.
|
|
47
|
+
chartType?.getChart(target)?.destroy();
|
|
48
|
+
if (!disposed) setFailure(error instanceof Error ? error.message : String(error));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
void create();
|
|
53
|
+
return () => {
|
|
54
|
+
disposed = true;
|
|
55
|
+
instance?.destroy();
|
|
56
|
+
instance = undefined;
|
|
57
|
+
};
|
|
58
|
+
}, [canvas, configuration, enabled]);
|
|
59
|
+
|
|
60
|
+
return failure;
|
|
61
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
4
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
5
|
+
/** The canonical names of the Chart.js chart types Scene's `chart` component supports. */ var ChartType = /*#__PURE__*/ function(ChartType) {
|
|
6
|
+
ChartType["Bar"] = "bar";
|
|
7
|
+
ChartType["Line"] = "line";
|
|
8
|
+
ChartType["Pie"] = "pie";
|
|
9
|
+
ChartType["Doughnut"] = "doughnut";
|
|
10
|
+
ChartType["PolarArea"] = "polarArea";
|
|
11
|
+
ChartType["Radar"] = "radar";
|
|
12
|
+
ChartType["Bubble"] = "bubble";
|
|
13
|
+
ChartType["Scatter"] = "scatter";
|
|
14
|
+
return ChartType;
|
|
15
|
+
}({});
|
|
16
|
+
|
|
17
|
+
exports.ChartType = ChartType;
|
|
18
|
+
//# sourceMappingURL=ChartType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChartType.js","sources":["../../../chart/ChartType.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\n/** The canonical names of the Chart.js chart types Scene's `chart` component supports. */\nexport enum ChartType {\n Bar = 'bar',\n Line = 'line',\n Pie = 'pie',\n Doughnut = 'doughnut',\n PolarArea = 'polarArea',\n Radar = 'radar',\n Bubble = 'bubble',\n Scatter = 'scatter',\n}\n"],"names":["ChartType"],"mappings":";;AAAA;AACA;AAEA,2FACO,IAAKA,SAAAA,iBAAAA,SAAAA,SAAAA,EAAAA;;;;;;;;;AAAAA,IAAAA,OAAAA,SAAAA;AASX,CAAA,CAAA,EAAA;;"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var properties = require('../properties.js');
|
|
6
|
+
var useStructuralValue = require('../useStructuralValue.js');
|
|
7
|
+
var chartHasData = require('./chartHasData.js');
|
|
8
|
+
var resolveChartType = require('./resolveChartType.js');
|
|
9
|
+
var useChart = require('./useChart.js');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The `PrimeReact:chart` component, rendered by Chart.js because PrimeReact 11 no longer ships its
|
|
13
|
+
* former Chart.js wrapper.
|
|
14
|
+
*
|
|
15
|
+
* Three states are explicit rather than a blank canvas: an unsupported `type` is reported (the right chart
|
|
16
|
+
* cannot be guessed), a chart with no data points shows its `emptyLabel` (data is never invented), and a
|
|
17
|
+
* chart Chart.js could not draw says why. Only a chart with data and a supported type reaches Chart.js.
|
|
18
|
+
*
|
|
19
|
+
* Chart.js is an optional peer dependency of this package: a host that renders charts must install
|
|
20
|
+
* `chart.js` itself. The chart rebuilds only when its resolved type, data or options change in content -
|
|
21
|
+
* every edit to a Scene document clones the whole document, and a clone with equal content is not a change.
|
|
22
|
+
*
|
|
23
|
+
* The chart fills the authored size: `maintainAspectRatio` defaults to `false` here (Chart.js defaults to
|
|
24
|
+
* `true`, which shrinks the chart inside its box) and an authored `options.maintainAspectRatio` wins. The
|
|
25
|
+
* `responsive` property is authoritative over `options.responsive` when both are set.
|
|
26
|
+
*/ function PrimeChart({ element, interactions }) {
|
|
27
|
+
const canvas = react.useRef(null);
|
|
28
|
+
const resolution = resolveChartType.resolveChartType(element);
|
|
29
|
+
const data = useStructuralValue.useStructuralValue(properties.recordProperty(element, 'data'));
|
|
30
|
+
const options = useStructuralValue.useStructuralValue(properties.recordProperty(element, 'options'));
|
|
31
|
+
const style = properties.recordProperty(element, 'style');
|
|
32
|
+
const responsive = properties.booleanProperty(element, 'responsive', options?.responsive ?? true);
|
|
33
|
+
const ariaLabel = properties.stringProperty(element, 'ariaLabel', element.name || 'Chart');
|
|
34
|
+
const emptyLabel = properties.stringProperty(element, 'emptyLabel', 'No chart data');
|
|
35
|
+
const hasData = chartHasData.chartHasData(data);
|
|
36
|
+
const type = resolution.isValid ? resolution.type : undefined;
|
|
37
|
+
const enabled = type !== undefined && hasData;
|
|
38
|
+
const configuration = react.useMemo(()=>({
|
|
39
|
+
type,
|
|
40
|
+
data: data ?? {
|
|
41
|
+
datasets: []
|
|
42
|
+
},
|
|
43
|
+
options: {
|
|
44
|
+
maintainAspectRatio: false,
|
|
45
|
+
...options,
|
|
46
|
+
responsive
|
|
47
|
+
}
|
|
48
|
+
}), [
|
|
49
|
+
type,
|
|
50
|
+
data,
|
|
51
|
+
options,
|
|
52
|
+
responsive
|
|
53
|
+
]);
|
|
54
|
+
const failure = useChart.useChart(canvas, configuration, enabled);
|
|
55
|
+
return /*#__PURE__*/ jsxRuntime.jsxs("div", {
|
|
56
|
+
"data-scene-id": element.id,
|
|
57
|
+
"data-scene-component": "chart",
|
|
58
|
+
style: {
|
|
59
|
+
position: 'relative',
|
|
60
|
+
width: element.size.width ?? '100%',
|
|
61
|
+
height: element.size.height ?? '100%',
|
|
62
|
+
...style
|
|
63
|
+
},
|
|
64
|
+
...interactions,
|
|
65
|
+
children: [
|
|
66
|
+
!resolution.isValid && /*#__PURE__*/ jsxRuntime.jsx("div", {
|
|
67
|
+
role: "alert",
|
|
68
|
+
"data-scene-state": "unsupported-type",
|
|
69
|
+
children: resolution.message
|
|
70
|
+
}),
|
|
71
|
+
resolution.isValid && !hasData && /*#__PURE__*/ jsxRuntime.jsx("div", {
|
|
72
|
+
role: "status",
|
|
73
|
+
"data-scene-state": "empty",
|
|
74
|
+
children: emptyLabel
|
|
75
|
+
}),
|
|
76
|
+
failure !== undefined && /*#__PURE__*/ jsxRuntime.jsx("div", {
|
|
77
|
+
role: "alert",
|
|
78
|
+
"data-scene-state": "unavailable",
|
|
79
|
+
children: `The chart could not be drawn: ${failure}`
|
|
80
|
+
}),
|
|
81
|
+
enabled && /*#__PURE__*/ jsxRuntime.jsx("canvas", {
|
|
82
|
+
ref: canvas,
|
|
83
|
+
role: "img",
|
|
84
|
+
"aria-label": ariaLabel,
|
|
85
|
+
hidden: failure !== undefined
|
|
86
|
+
})
|
|
87
|
+
]
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
exports.PrimeChart = PrimeChart;
|
|
92
|
+
//# sourceMappingURL=PrimeChart.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrimeChart.js","sources":["../../../chart/PrimeChart.tsx"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { CSSProperties, useMemo, useRef } from 'react';\nimport type { ChartConfiguration, ChartData, ChartOptions } from 'chart.js';\nimport { RegisteredComponentProps } from '@cratis/scene.react';\nimport { booleanProperty, recordProperty, stringProperty } from '../properties';\nimport { useStructuralValue } from '../useStructuralValue';\nimport { chartHasData } from './chartHasData';\nimport { resolveChartType } from './resolveChartType';\nimport { useChart } from './useChart';\n\n/**\n * The `PrimeReact:chart` component, rendered by Chart.js because PrimeReact 11 no longer ships its\n * former Chart.js wrapper.\n *\n * Three states are explicit rather than a blank canvas: an unsupported `type` is reported (the right chart\n * cannot be guessed), a chart with no data points shows its `emptyLabel` (data is never invented), and a\n * chart Chart.js could not draw says why. Only a chart with data and a supported type reaches Chart.js.\n *\n * Chart.js is an optional peer dependency of this package: a host that renders charts must install\n * `chart.js` itself. The chart rebuilds only when its resolved type, data or options change in content -\n * every edit to a Scene document clones the whole document, and a clone with equal content is not a change.\n *\n * The chart fills the authored size: `maintainAspectRatio` defaults to `false` here (Chart.js defaults to\n * `true`, which shrinks the chart inside its box) and an authored `options.maintainAspectRatio` wins. The\n * `responsive` property is authoritative over `options.responsive` when both are set.\n */\nexport function PrimeChart({ element, interactions }: RegisteredComponentProps) {\n const canvas = useRef<HTMLCanvasElement>(null);\n const resolution = resolveChartType(element);\n const data = useStructuralValue(recordProperty(element, 'data') as ChartData | undefined);\n const options = useStructuralValue(recordProperty(element, 'options') as ChartOptions | undefined);\n const style = recordProperty(element, 'style') as CSSProperties | undefined;\n const responsive = booleanProperty(element, 'responsive', options?.responsive ?? true);\n const ariaLabel = stringProperty(element, 'ariaLabel', element.name || 'Chart');\n const emptyLabel = stringProperty(element, 'emptyLabel', 'No chart data');\n const hasData = chartHasData(data);\n const type = resolution.isValid ? resolution.type : undefined;\n const enabled = type !== undefined && hasData;\n\n const configuration = useMemo(() => ({\n type,\n data: data ?? { datasets: [] },\n options: { maintainAspectRatio: false, ...options, responsive },\n }) as unknown as ChartConfiguration, [type, data, options, responsive]);\n\n const failure = useChart(canvas, configuration, enabled);\n\n return (\n <div\n data-scene-id={element.id}\n data-scene-component='chart'\n style={{ position: 'relative', width: element.size.width ?? '100%', height: element.size.height ?? '100%', ...style }}\n {...interactions}>\n {!resolution.isValid && <div role='alert' data-scene-state='unsupported-type'>{resolution.message}</div>}\n {resolution.isValid && !hasData && <div role='status' data-scene-state='empty'>{emptyLabel}</div>}\n {failure !== undefined && <div role='alert' data-scene-state='unavailable'>{`The chart could not be drawn: ${failure}`}</div>}\n {enabled && <canvas ref={canvas} role='img' aria-label={ariaLabel} hidden={failure !== undefined} />}\n </div>\n );\n}\n"],"names":["PrimeChart","element","interactions","canvas","useRef","resolution","resolveChartType","data","useStructuralValue","recordProperty","options","style","responsive","booleanProperty","ariaLabel","stringProperty","name","emptyLabel","hasData","chartHasData","type","isValid","undefined","enabled","configuration","useMemo","datasets","maintainAspectRatio","failure","useChart","_jsxs","div","data-scene-id","id","data-scene-component","position","width","size","height","_jsx","role","data-scene-state","message","ref","aria-label","hidden"],"mappings":"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"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
4
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
5
|
+
/**
|
|
6
|
+
* Whether chart data holds at least one data point to draw.
|
|
7
|
+
*
|
|
8
|
+
* Datasets whose `data` is empty (or absent) draw nothing but axes, so the chart reports it instead.
|
|
9
|
+
* A dataset may also give its points as keyed records (Chart.js `parsing` over an object), which counts
|
|
10
|
+
* when it has any keys.
|
|
11
|
+
*/ function chartHasData(data) {
|
|
12
|
+
if (data === null || typeof data !== 'object') return false;
|
|
13
|
+
const datasets = data.datasets;
|
|
14
|
+
if (!Array.isArray(datasets)) return false;
|
|
15
|
+
return datasets.some((dataset)=>{
|
|
16
|
+
if (dataset === null || typeof dataset !== 'object') return false;
|
|
17
|
+
const points = dataset.data;
|
|
18
|
+
if (Array.isArray(points)) return points.length > 0;
|
|
19
|
+
return points !== null && typeof points === 'object' && Object.keys(points).length > 0;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.chartHasData = chartHasData;
|
|
24
|
+
//# sourceMappingURL=chartHasData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chartHasData.js","sources":["../../../chart/chartHasData.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\n/**\n * Whether chart data holds at least one data point to draw.\n *\n * Datasets whose `data` is empty (or absent) draw nothing but axes, so the chart reports it instead.\n * A dataset may also give its points as keyed records (Chart.js `parsing` over an object), which counts\n * when it has any keys.\n */\nexport function chartHasData(data: unknown): boolean {\n if (data === null || typeof data !== 'object') return false;\n const datasets = (data as Record<string, unknown>).datasets;\n if (!Array.isArray(datasets)) return false;\n\n return datasets.some(dataset => {\n if (dataset === null || typeof dataset !== 'object') return false;\n const points = (dataset as Record<string, unknown>).data;\n if (Array.isArray(points)) return points.length > 0;\n return points !== null && typeof points === 'object' && Object.keys(points).length > 0;\n });\n}\n"],"names":["chartHasData","data","datasets","Array","isArray","some","dataset","points","length","Object","keys"],"mappings":";;AAAA;AACA;AAEA;;;;;;IAOO,SAASA,YAAAA,CAAaC,IAAa,EAAA;AACtC,IAAA,IAAIA,IAAAA,KAAS,IAAA,IAAQ,OAAOA,IAAAA,KAAS,UAAU,OAAO,KAAA;IACtD,MAAMC,QAAAA,GAAW,IAACD,CAAiCC,QAAQ;AAC3D,IAAA,IAAI,CAACC,KAAAA,CAAMC,OAAO,CAACF,WAAW,OAAO,KAAA;IAErC,OAAOA,QAAAA,CAASG,IAAI,CAACC,CAAAA,OAAAA,GAAAA;AACjB,QAAA,IAAIA,OAAAA,KAAY,IAAA,IAAQ,OAAOA,OAAAA,KAAY,UAAU,OAAO,KAAA;QAC5D,MAAMC,MAAAA,GAAS,OAACD,CAAoCL,IAAI;AACxD,QAAA,IAAIE,MAAMC,OAAO,CAACG,SAAS,OAAOA,MAAAA,CAAOC,MAAM,GAAG,CAAA;QAClD,OAAOD,MAAAA,KAAW,IAAA,IAAQ,OAAOA,MAAAA,KAAW,QAAA,IAAYE,OAAOC,IAAI,CAACH,MAAAA,CAAAA,CAAQC,MAAM,GAAG,CAAA;AACzF,IAAA,CAAA,CAAA;AACJ;;"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ChartType = require('./ChartType.js');
|
|
4
|
+
var resolveChartType = require('./resolveChartType.js');
|
|
5
|
+
|
|
6
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
7
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
8
|
+
const chartTypeLabels = {
|
|
9
|
+
[ChartType.ChartType.Bar]: 'Bar',
|
|
10
|
+
[ChartType.ChartType.Line]: 'Line',
|
|
11
|
+
[ChartType.ChartType.Pie]: 'Pie',
|
|
12
|
+
[ChartType.ChartType.Doughnut]: 'Doughnut',
|
|
13
|
+
[ChartType.ChartType.PolarArea]: 'Polar area',
|
|
14
|
+
[ChartType.ChartType.Radar]: 'Radar',
|
|
15
|
+
[ChartType.ChartType.Bubble]: 'Bubble',
|
|
16
|
+
[ChartType.ChartType.Scatter]: 'Scatter'
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The chart type choices: the stable names new documents use, then the ordinals migrated documents carry,
|
|
20
|
+
* then the Pascal case member names of the original .NET enum that a serialized enum produces.
|
|
21
|
+
* A migrated chart stores its original `type`; offering every form the renderer accepts keeps that stored
|
|
22
|
+
* value valid (and its type visible and re-selectable) without rewriting it to another form behind the
|
|
23
|
+
* author's back. The choices are the same set `resolveChartType` accepts, so what the renderer draws is
|
|
24
|
+
* exactly what the descriptor allows.
|
|
25
|
+
*/ const chartTypeChoices = [
|
|
26
|
+
...Object.values(ChartType.ChartType).map((type)=>({
|
|
27
|
+
value: type,
|
|
28
|
+
label: chartTypeLabels[type]
|
|
29
|
+
})),
|
|
30
|
+
...resolveChartType.legacyChartTypes.map((type, ordinal)=>({
|
|
31
|
+
value: ordinal,
|
|
32
|
+
label: `${chartTypeLabels[type]} (legacy ${ordinal})`,
|
|
33
|
+
description: `The ordinal ${ordinal} of the original chart type enum, as stored by migrated documents.`
|
|
34
|
+
})),
|
|
35
|
+
...Object.keys(ChartType.ChartType).map((name)=>({
|
|
36
|
+
value: name,
|
|
37
|
+
label: `${chartTypeLabels[ChartType.ChartType[name]]} (legacy name ${name})`,
|
|
38
|
+
description: `The member name ${name} of the original chart type enum, as a serialized enum stores it.`
|
|
39
|
+
}))
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
exports.chartTypeChoices = chartTypeChoices;
|
|
43
|
+
//# sourceMappingURL=chartTypeChoices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chartTypeChoices.js","sources":["../../../chart/chartTypeChoices.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { PropertyChoice } from '@cratis/scene.model';\nimport { ChartType } from './ChartType';\nimport { legacyChartTypes } from './resolveChartType';\n\nconst chartTypeLabels: Record<ChartType, string> = {\n [ChartType.Bar]: 'Bar',\n [ChartType.Line]: 'Line',\n [ChartType.Pie]: 'Pie',\n [ChartType.Doughnut]: 'Doughnut',\n [ChartType.PolarArea]: 'Polar area',\n [ChartType.Radar]: 'Radar',\n [ChartType.Bubble]: 'Bubble',\n [ChartType.Scatter]: 'Scatter',\n};\n\n/**\n * The chart type choices: the stable names new documents use, then the ordinals migrated documents carry,\n * then the Pascal case member names of the original .NET enum that a serialized enum produces.\n * A migrated chart stores its original `type`; offering every form the renderer accepts keeps that stored\n * value valid (and its type visible and re-selectable) without rewriting it to another form behind the\n * author's back. The choices are the same set `resolveChartType` accepts, so what the renderer draws is\n * exactly what the descriptor allows.\n */\nexport const chartTypeChoices: PropertyChoice[] = [\n ...Object.values(ChartType).map(type => ({ value: type, label: chartTypeLabels[type] })),\n ...legacyChartTypes.map((type, ordinal) => ({ value: ordinal, label: `${chartTypeLabels[type]} (legacy ${ordinal})`, description: `The ordinal ${ordinal} of the original chart type enum, as stored by migrated documents.` })),\n ...(Object.keys(ChartType) as (keyof typeof ChartType)[]).map(name => ({ value: name, label: `${chartTypeLabels[ChartType[name]]} (legacy name ${name})`, description: `The member name ${name} of the original chart type enum, as a serialized enum stores it.` })),\n];\n\n"],"names":["chartTypeLabels","ChartType","Bar","Line","Pie","Doughnut","PolarArea","Radar","Bubble","Scatter","chartTypeChoices","Object","values","map","type","value","label","legacyChartTypes","ordinal","description","keys","name"],"mappings":";;;;;AAAA;AACA;AAMA,MAAMA,eAAAA,GAA6C;IAC/C,CAACC,mBAAAA,CAAUC,GAAG,GAAG,KAAA;IACjB,CAACD,mBAAAA,CAAUE,IAAI,GAAG,MAAA;IAClB,CAACF,mBAAAA,CAAUG,GAAG,GAAG,KAAA;IACjB,CAACH,mBAAAA,CAAUI,QAAQ,GAAG,UAAA;IACtB,CAACJ,mBAAAA,CAAUK,SAAS,GAAG,YAAA;IACvB,CAACL,mBAAAA,CAAUM,KAAK,GAAG,OAAA;IACnB,CAACN,mBAAAA,CAAUO,MAAM,GAAG,QAAA;IACpB,CAACP,mBAAAA,CAAUQ,OAAO,GAAG;AACzB,CAAA;AAEA;;;;;;;UAQaC,gBAAAA,GAAqC;AAC3CC,IAAAA,GAAAA,MAAAA,CAAOC,MAAM,CAACX,mBAAAA,CAAAA,CAAWY,GAAG,CAACC,CAAAA,QAAS;YAAEC,KAAAA,EAAOD,IAAAA;YAAME,KAAAA,EAAOhB,eAAe,CAACc,IAAAA;SAAM,CAAA,CAAA;AAClFG,IAAAA,GAAAA,iCAAAA,CAAiBJ,GAAG,CAAC,CAACC,IAAAA,EAAMI,WAAa;YAAEH,KAAAA,EAAOG,OAAAA;YAASF,KAAAA,EAAO,CAAA,EAAGhB,eAAe,CAACc,IAAAA,CAAK,CAAC,SAAS,EAAEI,OAAAA,CAAQ,CAAC,CAAC;AAAEC,YAAAA,WAAAA,EAAa,CAAC,YAAY,EAAED,OAAAA,CAAQ,kEAAkE;SAAE,CAAA,CAAA;OAC1N,MAACP,CAAOS,IAAI,CAACnB,mBAAAA,CAAAA,CAA0CY,GAAG,CAACQ,CAAAA,QAAS;YAAEN,KAAAA,EAAOM,IAAAA;AAAML,YAAAA,KAAAA,EAAO,CAAA,EAAGhB,eAAe,CAACC,mBAAS,CAACoB,IAAAA,CAAK,CAAC,CAAC,cAAc,EAAEA,IAAAA,CAAK,CAAC,CAAC;AAAEF,YAAAA,WAAAA,EAAa,CAAC,gBAAgB,EAAEE,IAAAA,CAAK,iEAAiE;SAAE,CAAA;;;"}
|