@geo2france/api-dashboard 1.14.0 → 1.15.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.
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useDataset } from "../Dataset/hooks";
3
- import { usePalette } from "../Palette/Palette";
3
+ import { usePalette, usePaletteLabels } from "../Palette/Palette";
4
4
  import { from, op } from "arquero";
5
5
  import { ChartEcharts } from "./ChartEcharts";
6
6
  import { merge_others } from "../..";
@@ -23,13 +23,19 @@ export const ChartPie = ({ dataset: dataset_id, nameKey, dataKey, unit, title, d
23
23
  }))
24
24
  : [];
25
25
  const chart_data = merge_others({ dataset: chart_data1 || [], min: other || -1 });
26
+ const colors = usePalette({ nColors: chart_data?.length });
27
+ const colors_labels = usePaletteLabels();
26
28
  const option = {
27
- color: usePalette({ nColors: chart_data?.length }),
28
29
  xAxis: { show: false }, yAxis: { show: false },
29
30
  series: [{
30
31
  type: 'pie',
32
+ color: usePalette({ nColors: chart_data?.length }),
33
+ itemStyle: {
34
+ /* Use label's color if any, otherwise fallback to Echarts calculated color */
35
+ color: (p) => { console.log(p.color, p); return colors_labels.find(i => i.label.toLowerCase() === p.name.toLowerCase())?.color ?? colors?.[p.dataIndex] ?? '#000'; }
36
+ },
31
37
  data: chart_data,
32
- radius: donut ? ['40%', '75%'] : [0, '75%']
38
+ radius: donut ? ['40%', '75%'] : [0, '75%'],
33
39
  }],
34
40
  tooltip: {
35
41
  show: true,
@@ -4,7 +4,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
4
4
  */
5
5
  import { from, op } from "arquero";
6
6
  import { useDataset } from "../Dataset/hooks";
7
- import { usePalette } from "../Palette/Palette";
7
+ import { usePalette, usePaletteLabels } from "../Palette/Palette";
8
8
  import { ChartEcharts } from "./ChartEcharts";
9
9
  import { useBlockConfig } from "../DashboardPage/Block";
10
10
  export const ChartYearSerie = ({ dataset: dataset_id, categoryKey, valueKey, yearKey, yearMark, stack: stack_input, title, type: chart_type = 'bar' }) => {
@@ -31,13 +31,14 @@ export const ChartYearSerie = ({ dataset: dataset_id, categoryKey, valueKey, yea
31
31
  chart_data = full.join_left(grouped_data).objects();
32
32
  }
33
33
  const COLORS = usePalette({ nColors: distinct_cat?.length }) || [];
34
+ const colors_labels = usePaletteLabels();
34
35
  const series = distinct_cat.map((cat, idx) => ({
35
36
  name: cat,
36
37
  type: chart_type === 'area' ? 'line' : chart_type,
37
38
  data: categoryKey ? chart_data?.filter((row) => row[categoryKey] === cat).map((row) => ([String(row[yearKey]), row[valueKey] || 0]))
38
39
  : chart_data?.map((row) => ([String(row[yearKey]), row[valueKey] || 0])),
39
40
  itemStyle: {
40
- color: COLORS && COLORS[idx % COLORS.length],
41
+ color: colors_labels.find(i => i.label.toLowerCase() === cat.toLowerCase())?.color ?? (COLORS && COLORS[idx % COLORS.length]),
41
42
  },
42
43
  stack: stack ? 'total' : undefined,
43
44
  areaStyle: chart_type === 'area' ? {} : undefined,
@@ -6,7 +6,7 @@ import { useDataset } from '../Dataset/hooks';
6
6
  import bbox from '@turf/bbox';
7
7
  import { getType } from '@turf/invariant';
8
8
  import { feature, featureCollection, point } from '@turf/helpers';
9
- import { usePalette } from '../Palette/Palette';
9
+ import { usePalette, usePaletteLabels } from '../Palette/Palette';
10
10
  import { from, op } from 'arquero';
11
11
  import 'maplibre-gl/dist/maplibre-gl.css';
12
12
  import { LegendControl } from '../MapLegend/MapLegend';
@@ -86,9 +86,10 @@ export const MapLayer = ({ dataset, categoryKey, color = 'red', type = 'circle',
86
86
  const values = (type_value === 'string') && categoryKey && data?.data && from(data?.data).rollup({ a: op.array_agg_distinct(categoryKey) }).get('a', 0) || undefined;
87
87
  /** Couleurs de la palette */
88
88
  const colors = usePalette({ nColors: Array.isArray(values) ? values?.length : 1 });
89
+ const colors_labels = usePaletteLabels();
89
90
  const match = Array.isArray(values) ? values?.map((v, i) => ({
90
91
  val: v,
91
- color: colors?.[i],
92
+ color: colors_labels.find(i => i.label.toLowerCase() == v.toLowerCase())?.color ?? colors?.[i],
92
93
  })) : undefined;
93
94
  /** Expression mapLibre qui permet de mapper les valeurs et les couleurs de la palette */
94
95
  const expression = match && categoryKey
@@ -3,6 +3,11 @@ type PaletteModeType = 'rgb' | 'hsl' | 'lab' | 'lrgb' | 'lch';
3
3
  export interface PaletteType {
4
4
  steps?: string[];
5
5
  mode?: PaletteModeType;
6
+ /**
7
+ * Mapping optionnel de libellés vers des couleurs.
8
+ * Exemple : `labels={{'oui':'#0f0', 'non':'#f00'}}
9
+ * */
10
+ labels?: Record<string, string>;
6
11
  }
7
12
  type PaletteContextType = {
8
13
  palette: PaletteType;
@@ -13,6 +18,14 @@ interface UsePaletteProps {
13
18
  nColors?: number;
14
19
  }
15
20
  export declare const usePalette: ({ nColors }: UsePaletteProps) => string[] | undefined;
21
+ /**
22
+ * Normalise les labels de la palette en tableau `{ label, color }`.
23
+ * Retourne un tableau vide si aucun label n’est défini.
24
+ */
25
+ export declare const usePaletteLabels: () => {
26
+ label: string;
27
+ color: string | undefined;
28
+ }[];
16
29
  export declare const Palette: React.FC<PaletteType>;
17
30
  export declare const PalettePreview: React.FC;
18
31
  export {};
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Tag } from "antd";
3
3
  import chroma from "chroma-js";
4
4
  import { createContext, useContext, useEffect } from "react";
@@ -12,11 +12,23 @@ export const usePalette = ({ nColors }) => {
12
12
  }
13
13
  return palette_info?.steps && chroma.scale(palette_info.steps).mode(palette_info.mode || 'hsl').colors(nColors);
14
14
  };
15
+ /**
16
+ * Normalise les labels de la palette en tableau `{ label, color }`.
17
+ * Retourne un tableau vide si aucun label n’est défini.
18
+ */
19
+ export const usePaletteLabels = () => {
20
+ const palette_context = useContext(PaletteContext);
21
+ const palette_info = palette_context?.palette;
22
+ return Object.keys(palette_info?.labels || {}).map(key => ({
23
+ label: key,
24
+ color: palette_info?.labels?.[key]
25
+ }));
26
+ };
15
27
  /*
16
28
  * Composant permettant à l'utilisateur de définir une palette pour sa page
17
29
  */
18
- export const Palette = ({ steps, mode }) => {
19
- const palette = { steps, mode };
30
+ export const Palette = ({ steps, mode, labels }) => {
31
+ const palette = { steps, mode, labels };
20
32
  const palette_context = useContext(PaletteContext);
21
33
  useEffect(() => {
22
34
  palette_context?.setPalette(palette);
@@ -25,5 +37,6 @@ export const Palette = ({ steps, mode }) => {
25
37
  };
26
38
  export const PalettePreview = ({}) => {
27
39
  const colors = usePalette({ nColors: 10 }) || [];
28
- return (_jsx(_Fragment, { children: colors.map((color) => (_jsx(Tag, { color: color, children: color }, color))) }));
40
+ const colors_labels = usePaletteLabels();
41
+ return (_jsxs(_Fragment, { children: [colors.map((color) => (_jsx(Tag, { color: color, children: color }, color))), _jsx("br", {}), "Labels : ", colors_labels.map(e => _jsx(Tag, { color: e.color, children: e.label }, e.label))] }));
29
42
  };
@@ -12,7 +12,7 @@ import { DSL_Control as Control, useAllControls, useControl } from "../component
12
12
  import { Radio } from "../components/Control/Radio";
13
13
  import { Select } from "../components/Control/Select";
14
14
  import { Input } from "antd";
15
- import { Palette, usePalette, PalettePreview } from "../components/Palette/Palette";
15
+ import { Palette, usePalette, PalettePreview, usePaletteLabels } from "../components/Palette/Palette";
16
16
  import { Debug } from "../components/Debug/Debug";
17
17
  import { Join } from "../components/Dataset/Join";
18
18
  import { ChartEcharts } from "../components/Charts/ChartEcharts";
@@ -20,4 +20,4 @@ import { useBlockConfig } from "../components/DashboardPage/Block";
20
20
  import { Statistics, StatisticsCollection } from "../components/Charts/Statistics";
21
21
  import { MapLayer, Map } from "../components/Map/Map";
22
22
  import { Section } from "../components/DashboardPage/Section";
23
- export { Dashboard, Dataset, Provider, Transform, Join, Filter, Section, DataPreview, ChartEcharts, ChartPie, ChartYearSerie, Statistics, StatisticsCollection, useDataset, useDatasets, useAllDatasets, useBlockConfig, Producer, Control, useControl, useAllControls, Radio, Select, Input, Palette, usePalette, PalettePreview, Debug, Map, MapLayer };
23
+ export { Dashboard, Dataset, Provider, Transform, Join, Filter, Section, DataPreview, ChartEcharts, ChartPie, ChartYearSerie, Statistics, StatisticsCollection, useDataset, useDatasets, useAllDatasets, useBlockConfig, Producer, Control, useControl, useAllControls, Radio, Select, Input, Palette, usePalette, usePaletteLabels, PalettePreview, Debug, Map, MapLayer };
package/dist/dsl/index.js CHANGED
@@ -12,7 +12,7 @@ import { DSL_Control as Control, useAllControls, useControl } from "../component
12
12
  import { Radio } from "../components/Control/Radio";
13
13
  import { Select } from "../components/Control/Select";
14
14
  import { Input } from "antd";
15
- import { Palette, usePalette, PalettePreview } from "../components/Palette/Palette";
15
+ import { Palette, usePalette, PalettePreview, usePaletteLabels } from "../components/Palette/Palette";
16
16
  import { Debug } from "../components/Debug/Debug";
17
17
  import { Join } from "../components/Dataset/Join";
18
18
  import { ChartEcharts } from "../components/Charts/ChartEcharts";
@@ -20,4 +20,4 @@ import { useBlockConfig } from "../components/DashboardPage/Block";
20
20
  import { Statistics, StatisticsCollection } from "../components/Charts/Statistics";
21
21
  import { MapLayer, Map } from "../components/Map/Map";
22
22
  import { Section } from "../components/DashboardPage/Section";
23
- export { Dashboard, Dataset, Provider, Transform, Join, Filter, Section, DataPreview, ChartEcharts, ChartPie, ChartYearSerie, Statistics, StatisticsCollection, useDataset, useDatasets, useAllDatasets, useBlockConfig, Producer, Control, useControl, useAllControls, Radio, Select, Input, Palette, usePalette, PalettePreview, Debug, Map, MapLayer };
23
+ export { Dashboard, Dataset, Provider, Transform, Join, Filter, Section, DataPreview, ChartEcharts, ChartPie, ChartYearSerie, Statistics, StatisticsCollection, useDataset, useDatasets, useAllDatasets, useBlockConfig, Producer, Control, useControl, useAllControls, Radio, Select, Input, Palette, usePalette, usePaletteLabels, PalettePreview, Debug, Map, MapLayer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geo2france/api-dashboard",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "private": false,
5
5
  "description": "Build dashboards with JSX/TSX",
6
6
  "main": "dist/index.js",