@geo2france/api-dashboard 1.6.1 → 1.7.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.
@@ -5,6 +5,7 @@ interface IChartPieProps {
5
5
  unit?: string;
6
6
  title?: string;
7
7
  donut?: boolean;
8
+ other?: number | null;
8
9
  }
9
10
  export declare const ChartPie: React.FC<IChartPieProps>;
10
11
  export {};
@@ -5,7 +5,8 @@ import { ChartBlockContext } from "../DashboardPage/Block";
5
5
  import { usePalette } from "../Palette/Palette";
6
6
  import { from, op } from "arquero";
7
7
  import { ChartEcharts } from "./ChartEcharts";
8
- export const ChartPie = ({ dataset: dataset_id, nameKey, dataKey, unit, title, donut = false }) => {
8
+ import { merge_others } from "../..";
9
+ export const ChartPie = ({ dataset: dataset_id, nameKey, dataKey, unit, title, donut = false, other = 5 }) => {
9
10
  const dataset = useDataset(dataset_id);
10
11
  const blockConfig = useContext(ChartBlockContext);
11
12
  const data = dataset?.data;
@@ -14,10 +15,17 @@ export const ChartPie = ({ dataset: dataset_id, nameKey, dataKey, unit, title, d
14
15
  dataExport: data
15
16
  };
16
17
  useEffect(() => blockConfig?.setConfig(block_config), [data]);
17
- const chart_data = data && from(data).groupby(nameKey).rollup({ value: op.sum(dataKey) }).objects().map((d) => ({
18
- name: d[nameKey],
19
- value: d.value,
20
- }));
18
+ const chart_data1 = data && data.length > 0
19
+ ? from(data)
20
+ .groupby(nameKey)
21
+ .rollup({ value: op.sum(dataKey) })
22
+ .objects()
23
+ .map((d) => ({
24
+ name: d[nameKey],
25
+ value: d.value,
26
+ }))
27
+ : [];
28
+ const chart_data = merge_others({ dataset: chart_data1 || [], min: other || -1 });
21
29
  const option = {
22
30
  color: usePalette({ nColors: chart_data?.length }),
23
31
  xAxis: { show: false }, yAxis: { show: false },
@@ -4,7 +4,6 @@
4
4
  interface IYearSerieProps {
5
5
  dataset: string;
6
6
  title?: string;
7
- yearControl?: string;
8
7
  yearKey: string;
9
8
  valueKey: string;
10
9
  categoryKey?: string;
@@ -6,27 +6,31 @@ import { from, op } from "arquero";
6
6
  import { useDataset } from "../Dataset/hooks";
7
7
  import { usePalette } from "../Palette/Palette";
8
8
  import { ChartEcharts } from "./ChartEcharts";
9
- export const ChartYearSerie = ({ dataset: dataset_id, categoryKey, valueKey, yearKey, yearMark, stack: stack_input, type: chart_type = 'bar', yearControl = 'year' }) => {
9
+ export const ChartYearSerie = ({ dataset: dataset_id, categoryKey, valueKey, yearKey, yearMark, stack: stack_input, type: chart_type = 'bar' }) => {
10
10
  const stack = stack_input || chart_type == 'line' ? false : true; // Pas de stack par défaut pour le type line
11
11
  const dataset = useDataset(dataset_id);
12
12
  const data = dataset?.data;
13
- const chart_data = categoryKey ? data && from(data).groupby(yearKey, categoryKey) //Somme par année et categorykey
14
- .rollup({ [valueKey]: op.sum(valueKey) })
15
- .groupby(yearKey).orderby(yearKey).objects()
16
- :
17
- data && from(data).groupby(yearKey) //Somme par année seulement
18
- .rollup({ [valueKey]: op.sum(valueKey) }).orderby(yearKey)
19
- .objects();
20
- const distinct_cat = categoryKey ?
21
- data && from(data).rollup({ cat: op.array_agg_distinct(categoryKey) }).object().cat
22
- :
23
- [valueKey];
13
+ let chart_data = [];
14
+ let distinct_cat = [];
15
+ if (data && data.length > 0) {
16
+ const grouped_data = categoryKey ? from(data).groupby(yearKey, categoryKey) //Somme par année et categorykey
17
+ .rollup({ [valueKey]: op.sum(valueKey) })
18
+ .groupby(yearKey).orderby(yearKey)
19
+ :
20
+ from(data).groupby(yearKey) //Somme par année seulement
21
+ .rollup({ [valueKey]: op.sum(valueKey) }).orderby(yearKey);
22
+ const all_years = from(data).groupby(yearKey).rollup({ [yearKey]: op.any(yearKey) });
23
+ const all_cats = categoryKey ? (from(data).groupby(categoryKey).rollup({ [categoryKey]: op.any(categoryKey) })) : from([{ 'cat': valueKey }]);
24
+ const full = all_years.cross(all_cats); // Contient chaque annee x catégorie (pour éviter les trous)
25
+ distinct_cat = all_cats.array(categoryKey || 'cat'); // Pour générer chaque serie
26
+ chart_data = full.join_left(grouped_data).objects();
27
+ }
24
28
  const COLORS = usePalette({ nColors: distinct_cat?.length }) || [];
25
- const series = distinct_cat ? distinct_cat.map((cat, idx) => ({
29
+ const series = distinct_cat.map((cat, idx) => ({
26
30
  name: cat,
27
31
  type: chart_type === 'area' ? 'line' : chart_type,
28
- data: categoryKey ? chart_data?.filter((row) => row[categoryKey] === cat).map((row) => ([String(row[yearKey]), row[valueKey]]))
29
- : chart_data?.map((row) => ([String(row[yearKey]), row[valueKey]])),
32
+ data: categoryKey ? chart_data?.filter((row) => row[categoryKey] === cat).map((row) => ([String(row[yearKey]), row[valueKey] || 0]))
33
+ : chart_data?.map((row) => ([String(row[yearKey]), row[valueKey] || 0])),
30
34
  itemStyle: {
31
35
  color: COLORS && COLORS[idx % COLORS.length],
32
36
  },
@@ -38,7 +42,7 @@ export const ChartYearSerie = ({ dataset: dataset_id, categoryKey, valueKey, yea
38
42
  { xAxis: String(yearMark) }
39
43
  ]
40
44
  } : undefined
41
- })) : [];
45
+ }));
42
46
  function tooltipFormatter(params) {
43
47
  if (!params || params.length === 0)
44
48
  return '';
@@ -39,5 +39,6 @@ interface IDSLDashboardPageProps {
39
39
  children: React.ReactNode;
40
40
  name?: string;
41
41
  columns?: number;
42
+ debug?: boolean;
42
43
  }
43
44
  export declare const DSL_DashboardPage: React.FC<IDSLDashboardPageProps>;
@@ -40,7 +40,7 @@ export default DashboardPage;
40
40
  export const DatasetContext = createContext({});
41
41
  export const DatasetRegistryContext = createContext(() => { }); // A modifier, utiliser un seul context
42
42
  export const ControlContext = createContext(undefined);
43
- export const DSL_DashboardPage = ({ name = 'Tableau de bord', columns = 2, children }) => {
43
+ export const DSL_DashboardPage = ({ name = 'Tableau de bord', columns = 2, children, debug = false }) => {
44
44
  const [datasets, setdatasets] = useState({});
45
45
  const [palette, setPalette] = useState(DEFAULT_PALETTE);
46
46
  //const allDatasetLoaded = Object.values(datasets).every(d => !d.isFetching);
@@ -68,6 +68,9 @@ export const DSL_DashboardPage = ({ name = 'Tableau de bord', columns = 2, child
68
68
  const visible_components = childrenArray.filter((c) => c && getComponentKind(c) == 'other');
69
69
  const logic_components = childrenArray.filter((c) => getComponentKind(c) == 'logical');
70
70
  const control_components = childrenArray.filter((c) => getComponentKind(c) == 'control');
71
+ if (debug && !logic_components.some((c) => typeof c.type !== "string" && c.type.name === Debug.name)) {
72
+ logic_components.push(_jsx(Debug, {}, "debug_property"));
73
+ }
71
74
  return (_jsxs(_Fragment, { children: [_jsx(Helmet, { children: _jsx("title", { children: name }) }), _jsx(DatasetRegistryContext.Provider, { value: pushDataset, children: _jsx(DatasetContext.Provider, { value: datasets, children: _jsxs(PaletteContext.Provider, { value: { palette, setPalette }, children: [control_components.length > 0 && _jsx(Header, { style: {
72
75
  padding: 12,
73
76
  position: "sticky",
@@ -62,7 +62,7 @@ export const DSL_Dataset = ({ children, id, provider: provider_input, type: prov
62
62
  React.Children.toArray(children)
63
63
  .filter((c) => React.isValidElement(c))
64
64
  .filter((c) => typeof c.type != 'string' && c.type.name == Filter.name).forEach((c) => {
65
- const value = String(c.props.children).trim() || controls?.[c.props.control];
65
+ const value = (typeof c.props.children === "string" ? c.props.children.trim() : c.props.children) || controls?.[c.props.control];
66
66
  filters.push({
67
67
  operator: c.props.operator || 'eq',
68
68
  value: value,
@@ -1,7 +1,7 @@
1
1
  import { CrudOperators } from "../../data_providers/types";
2
2
  interface IFilterProps {
3
3
  field: string;
4
- children?: string;
4
+ children?: string | string[];
5
5
  control?: string;
6
6
  operator?: Exclude<CrudOperators, "or" | "and">;
7
7
  }
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { useChartData, useDashboardElement, useNoData } from "./components/Dashb
6
6
  export { useMapControl } from "./utils/useMapControl";
7
7
  export { BaseRecordToGeojsonPoint } from "./utils/baserecordtogeojsonpoint";
8
8
  export { cardStyles } from "./utils/cardStyles";
9
+ export { merge_others } from "./utils/merge_others";
9
10
  import KeyFigure from "./components/KeyFigure/KeyFigure";
10
11
  import LoadingContainer from "./components/LoadingContainer/LoadingContainer";
11
12
  import FlipCard from "./components/FlipCard/FlipCard";
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ export { useMapControl } from "./utils/useMapControl";
8
8
  // Helpers
9
9
  export { BaseRecordToGeojsonPoint } from "./utils/baserecordtogeojsonpoint";
10
10
  export { cardStyles } from "./utils/cardStyles";
11
+ export { merge_others } from "./utils/merge_others";
11
12
  // Components
12
13
  import KeyFigure from "./components/KeyFigure/KeyFigure";
13
14
  import LoadingContainer from "./components/LoadingContainer/LoadingContainer";
@@ -0,0 +1,8 @@
1
+ import { SimpleRecord } from "../types";
2
+ interface MergeOthersOpts {
3
+ dataset: SimpleRecord[];
4
+ min: number;
5
+ other_cat_name?: string;
6
+ }
7
+ export declare const merge_others: ({ dataset, min, other_cat_name }: MergeOthersOpts) => SimpleRecord[];
8
+ export {};
@@ -0,0 +1,14 @@
1
+ import { from, op } from "arquero";
2
+ export const merge_others = ({ dataset, min, other_cat_name = 'Autres' }) => {
3
+ const total = dataset.reduce((acc, d) => acc + d.value, 0);
4
+ const small = dataset.filter(d => (d.value / total) * 100 < min);
5
+ const large = dataset.filter(d => (d.value / total) * 100 >= min);
6
+ if (small.length > 1) {
7
+ const others_row = from(small).rollup({ value: op.sum('value') }).objects().map((d) => ({
8
+ name: other_cat_name,
9
+ value: d.value
10
+ }));
11
+ return [...large, ...others_row];
12
+ }
13
+ return dataset;
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geo2france/api-dashboard",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "private": false,
5
5
  "description": "Build dashboards with JSX/TSX",
6
6
  "main": "dist/index.js",