bento-charts 2.4.0 → 2.4.2

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.
Files changed (43) hide show
  1. package/.github/workflows/release.yml +1 -1
  2. package/README.md +1 -1
  3. package/dist/ChartConfigProvider.js +1 -12
  4. package/dist/ChartConfigProvider.js.map +1 -1
  5. package/dist/Components/Charts/BentoBarChart.d.ts +1 -1
  6. package/dist/Components/Charts/BentoBarChart.js +21 -35
  7. package/dist/Components/Charts/BentoBarChart.js.map +1 -1
  8. package/dist/Components/Charts/BentoPie.d.ts +1 -1
  9. package/dist/Components/Charts/BentoPie.js +55 -46
  10. package/dist/Components/Charts/BentoPie.js.map +1 -1
  11. package/dist/Components/Maps/BentoChoroplethMap.d.ts +1 -1
  12. package/dist/Components/Maps/BentoChoroplethMap.js +16 -32
  13. package/dist/Components/Maps/BentoChoroplethMap.js.map +1 -1
  14. package/dist/Components/Maps/BentoMapContainer.js +1 -12
  15. package/dist/Components/Maps/BentoMapContainer.js.map +1 -1
  16. package/dist/Components/Maps/BentoPointMap.js +6 -14
  17. package/dist/Components/Maps/BentoPointMap.js.map +1 -1
  18. package/dist/Components/Maps/controls/MapLegendContinuous.js +1 -12
  19. package/dist/Components/Maps/controls/MapLegendContinuous.js.map +1 -1
  20. package/dist/Components/Maps/controls/MapLegendDiscrete.js +2 -13
  21. package/dist/Components/Maps/controls/MapLegendDiscrete.js.map +1 -1
  22. package/dist/Components/NoData.js +2 -13
  23. package/dist/Components/NoData.js.map +1 -1
  24. package/dist/constants/chartConstants.d.ts +0 -1
  25. package/dist/constants/chartConstants.js +0 -1
  26. package/dist/constants/chartConstants.js.map +1 -1
  27. package/dist/styles.css +48 -0
  28. package/dist/types/chartTypes.d.ts +9 -6
  29. package/dist/types/mapTypes.d.ts +2 -3
  30. package/dist/util/chartUtils.d.ts +2 -0
  31. package/dist/util/chartUtils.js +30 -0
  32. package/dist/util/chartUtils.js.map +1 -1
  33. package/package.json +19 -17
  34. package/src/Components/Charts/BentoBarChart.tsx +15 -25
  35. package/src/Components/Charts/BentoPie.tsx +74 -76
  36. package/src/Components/Maps/BentoChoroplethMap.tsx +6 -13
  37. package/src/Components/Maps/BentoPointMap.tsx +13 -1
  38. package/src/constants/chartConstants.ts +0 -1
  39. package/src/types/chartTypes.ts +10 -7
  40. package/src/types/mapTypes.ts +3 -3
  41. package/src/util/chartUtils.ts +29 -0
  42. package/tsconfig.json +1 -1
  43. package/webpack.config.js +9 -1
@@ -15,16 +15,13 @@ import type { ChoroplethMapProps } from '../../types/mapTypes';
15
15
  import BentoMapContainer from './BentoMapContainer';
16
16
  import MapLegendContinuous from './controls/MapLegendContinuous';
17
17
  import MapLegendDiscrete from './controls/MapLegendDiscrete';
18
+ import { useTransformedChartData } from '../../util/chartUtils';
18
19
 
19
20
  const DEFAULT_CATEGORY = '';
20
21
  const POS_BOTTOM_RIGHT: ControlPosition = 'bottomright';
21
22
 
22
23
  const BentoChoroplethMap = ({
23
24
  height,
24
- data: originalData,
25
- preFilter,
26
- dataMap,
27
- postFilter,
28
25
  center,
29
26
  zoom,
30
27
  tileLayer,
@@ -33,14 +30,9 @@ const BentoChoroplethMap = ({
33
30
  categoryProp,
34
31
  onClick,
35
32
  renderPopupBody,
33
+ ...params
36
34
  }: ChoroplethMapProps) => {
37
- const data = useMemo(() => {
38
- let data = [...originalData];
39
- if (preFilter) data = data.filter(preFilter);
40
- if (dataMap) data = data.map(dataMap);
41
- if (postFilter) data = data.filter(postFilter);
42
- return data;
43
- }, [originalData]);
35
+ const data = useTransformedChartData(params);
44
36
 
45
37
  const dataByFeatureCat = useMemo(() => Object.fromEntries(data.map((d) => [d.x, d.y])), [data]);
46
38
 
@@ -87,8 +79,9 @@ const BentoChoroplethMap = ({
87
79
  {onClick ? (
88
80
  <a
89
81
  href="#"
90
- onClick={() => {
82
+ onClick={(e) => {
91
83
  if (onClick) onClick(feature);
84
+ e.preventDefault();
92
85
  }}
93
86
  >
94
87
  {title}
@@ -101,7 +94,7 @@ const BentoChoroplethMap = ({
101
94
  </div>
102
95
  );
103
96
  },
104
- } as LeafletEventHandlerFnMap),
97
+ }) as LeafletEventHandlerFnMap,
105
98
  [onClick, categoryProp, renderPopupBody]
106
99
  );
107
100
 
@@ -16,7 +16,19 @@ const BentoPointMap = ({ height, center, zoom, tileLayer, data, onClick, renderP
16
16
  <Marker key={i} position={coordinatesLatLongOrder}>
17
17
  <Popup>
18
18
  <h4 style={{ marginBottom: renderPopupBody ? 6 : 0 }}>
19
- {onClick ? <a onClick={() => onClick(point)}>{title}</a> : <>{title}</>}
19
+ {onClick ? (
20
+ <a
21
+ href="#"
22
+ onClick={(e) => {
23
+ onClick(point);
24
+ e.preventDefault();
25
+ }}
26
+ >
27
+ {title}
28
+ </a>
29
+ ) : (
30
+ <>{title}</>
31
+ )}
20
32
  </h4>
21
33
  {renderPopupBody ? renderPopupBody(point) : null}
22
34
  </Popup>
@@ -113,7 +113,6 @@ export const TICK_MARGIN = 5; // vertical spacing between tick line and tick lab
113
113
  // pie chart
114
114
  export const CHART_ASPECT_RATIO = 1.4;
115
115
  export const LABEL_THRESHOLD = 0.05;
116
- export const OTHER_THRESHOLD = 0.01;
117
116
 
118
117
  // ################### UTIL CONSTANTS ###################
119
118
  export const RADIAN = Math.PI / 180;
@@ -37,6 +37,7 @@ export type UnitaryMapCallback<T> = (value: T, index: number, array: T[]) => T;
37
37
  // export type BinaryMapCallback<T, U> = (value: T, index: number, array: T[]) => U;
38
38
 
39
39
  export type ChartFilterCallback = FilterCallback<CategoricalChartDataItem>;
40
+ export type ChartDataMapCallback = UnitaryMapCallback<CategoricalChartDataItem>;
40
41
 
41
42
  export type SupportedLng = 'en' | 'fr';
42
43
 
@@ -50,19 +51,21 @@ export type TranslationObject = {
50
51
  [key in SupportedLng]: LngDictionary;
51
52
  };
52
53
 
53
- // ################### COMPONENT PROPS #####################
54
- export interface BaseChartComponentProps {
55
- height: number;
54
+ export interface CategoricalChartDataWithTransforms {
55
+ data: CategoricalChartDataType;
56
56
  preFilter?: ChartFilterCallback;
57
- dataMap?: UnitaryMapCallback<CategoricalChartDataItem>;
57
+ dataMap?: ChartDataMapCallback;
58
58
  postFilter?: ChartFilterCallback;
59
+ removeEmpty?: boolean;
59
60
  }
60
61
 
61
- interface BaseCategoricalChartProps extends BaseChartComponentProps {
62
- data: CategoricalChartDataType;
63
- removeEmpty?: boolean;
62
+ // ################### COMPONENT PROPS #####################
63
+ export interface BaseChartComponentProps {
64
+ height: number;
64
65
  }
65
66
 
67
+ export interface BaseCategoricalChartProps extends BaseChartComponentProps, CategoricalChartDataWithTransforms {}
68
+
66
69
  export interface PieChartProps extends BaseCategoricalChartProps {
67
70
  colorTheme?: keyof ChartTheme['pie'];
68
71
  sort?: boolean;
@@ -1,7 +1,7 @@
1
1
  import { ReactElement, ReactNode } from 'react';
2
2
  import type { Feature as GeoJSONFeatureType } from 'geojson';
3
3
 
4
- import { BaseChartComponentProps, CategoricalChartDataType } from './chartTypes';
4
+ import { BaseCategoricalChartProps, BaseChartComponentProps } from './chartTypes';
5
5
  import type { GeoJSONPolygonOnlyFeatureCollection } from './geoJSONTypes';
6
6
 
7
7
  export interface GeoPointDataItem {
@@ -42,8 +42,8 @@ export interface ChoroplethMapColorModeDiscrete {
42
42
  legendItems: MapDiscreteLegendItem[];
43
43
  }
44
44
 
45
- export interface ChoroplethMapProps extends BaseMapProps {
46
- data: CategoricalChartDataType; // heatmaps are 'categorical' + geographical
45
+ // heatmaps are 'categorical' + geographical:
46
+ export interface ChoroplethMapProps extends BaseCategoricalChartProps, BaseMapProps {
47
47
  features: GeoJSONPolygonOnlyFeatureCollection;
48
48
  colorMode: ChoroplethMapColorModeContinuous | ChoroplethMapColorModeDiscrete;
49
49
  categoryProp: string;
@@ -1,4 +1,6 @@
1
+ import { useMemo } from 'react';
1
2
  import { RADIAN } from '../constants/chartConstants';
3
+ import type { CategoricalChartDataWithTransforms } from '../types/chartTypes';
2
4
 
3
5
  export const polarToCartesian = (cx: number, cy: number, radius: number, angle: number) => {
4
6
  return {
@@ -6,3 +8,30 @@ export const polarToCartesian = (cx: number, cy: number, radius: number, angle:
6
8
  y: cy + Math.sin(-RADIAN * angle) * radius,
7
9
  };
8
10
  };
11
+
12
+ export const useTransformedChartData = (
13
+ {
14
+ data: originalData,
15
+ preFilter,
16
+ dataMap,
17
+ postFilter,
18
+ removeEmpty: origRemoveEmpty,
19
+ }: CategoricalChartDataWithTransforms,
20
+ defaultRemoveEmpty = true,
21
+ sortY = false,
22
+ ) =>
23
+ useMemo(() => {
24
+ const removeEmpty = origRemoveEmpty ?? defaultRemoveEmpty;
25
+
26
+ let data = [...originalData];
27
+
28
+ if (preFilter) data = data.filter(preFilter);
29
+ if (dataMap) data = data.map(dataMap);
30
+ if (postFilter) data = data.filter(postFilter);
31
+
32
+ if (removeEmpty) data = data.filter((e) => e.y !== 0);
33
+
34
+ if (sortY) data.sort((a, b) => a.y - b.y);
35
+
36
+ return data;
37
+ }, [originalData, preFilter, dataMap, postFilter, origRemoveEmpty]);
package/tsconfig.json CHANGED
@@ -23,4 +23,4 @@
23
23
  "exclude": [
24
24
  "node_modules"
25
25
  ],
26
- }
26
+ }
package/webpack.config.js CHANGED
@@ -12,7 +12,14 @@ const config = {
12
12
  },
13
13
  module: {
14
14
  rules: [
15
- { test: /\.[tj](sx|s)?$/, use: { loader: 'ts-loader' }, exclude: /node_modules/ },
15
+ {
16
+ test: /\.[tj](sx|s)?$/,
17
+ loader: 'ts-loader',
18
+ exclude: /node_modules/,
19
+ options: {
20
+ configFile: 'test/tsconfig.json'
21
+ }
22
+ },
16
23
  {
17
24
  test: /\.html$/i,
18
25
  loader: 'html-loader',
@@ -43,6 +50,7 @@ const config = {
43
50
  devtool: 'source-map',
44
51
  devServer: {
45
52
  static: './test/dist',
53
+ historyApiFallback: true,
46
54
  },
47
55
  resolve: {
48
56
  alias: {