-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Charts package continued #2104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 107 commits
860ed12
8861f70
0802182
c1659b1
7f70690
ea4cc71
730d4ee
2cf8a79
c4d24d5
c8835a2
3967709
a380de5
44e71e2
3ae45fa
1534b6a
7928d1b
34503a6
794f7bf
d26993a
0d618ce
4fce027
a71cc20
76cc04b
dd355a8
3655dd8
c795c79
bdaa2c8
f9c4081
0e903e7
7b20069
d296094
7b198d3
cf2980c
ca927e9
ea58035
561c7e9
81b67d9
95f4092
53336e1
3201588
367070c
1ccc761
bca7083
1ebd5a9
9526d9b
dee60a9
2568466
2f016e3
9d75fe4
5baf678
011da2c
c9f74b0
23bd595
14f818b
8be3119
e84152e
b34ba27
ee1dc8e
139ae17
536d7d8
6575c15
7b240bf
5c4d6f8
f2167c6
534e18f
249a9c9
f96b1dc
c8a9de6
d582176
7aa3a1c
abe62ad
f8b90a3
7c18c31
7cf11e3
183dc05
e5acd8a
cdee02c
cb641e6
e1eed11
ea28435
0720422
94b78a0
f814186
d85f355
2486277
52d0e20
3a8430a
ec922a8
6d7666b
dc489e7
3e6481c
22522dc
d1fcc3f
456abf3
4cbf6c0
90a2f8b
e6b2be4
c370ffd
9ddc002
af48b9e
dda71e3
35b9d3c
ccd2ed1
037768e
58fb93c
9f37ff7
578d0b9
2f497d3
b58c5c0
4f96094
b86bf7a
b9eb2a8
2553826
a6bf3d5
c122d51
ef2d10f
bed8689
d72cd1e
8dabd8a
96215b9
0375ee0
03432c1
0e9a337
e2b6929
5679e24
1d656f7
877f9b9
466f00c
b367d29
cff1f1a
48e72f5
9141293
e34d33d
5ab525b
34a1b7b
ae0d33d
0ba56c0
d3fc5c0
910e759
14521c8
5d2bc8d
2103af9
c2c62fd
9b38684
9059acf
89e4611
dc23cb4
b55e090
2afb231
de470a1
19ee878
86988de
f512d7e
1ea5a35
fbb7cfe
10e76fc
fc08e2c
dafd903
257cc00
5a410ac
0de3d4e
88bcc82
d8328f3
1d9704b
68dba39
6c85f46
18d7281
b26d135
2254025
aec0037
4f227dc
eb25ead
fa7dacb
0db71d8
7b8ec72
23b052c
f95c351
245d484
bbd9baa
b3ef35d
ea08de1
7d653c4
10d0bc7
81ba03e
340a011
be90242
2eb229d
752bae3
522341a
16ad678
b40db13
5276e1f
b379a44
dff8a69
9a0a3de
819818a
f48f0cb
b7a7323
0214989
e4b101e
7a0bc66
3e0e846
4840444
0ed319a
6beb347
8878589
549ab0c
5574879
f0516de
80dc46f
2cab8f4
2779360
3ee2fe5
3499d4f
05cc047
f3162df
8a65bf6
8d5f849
aacb9f6
015475f
a76e0b3
233eebb
f4f973e
5312ec3
d332178
eadd143
d499e44
816435a
a777bc9
e083235
032e815
99e19b7
8ffe811
72824bf
b946092
25f60dc
4e73efe
89cace1
b8fa9d6
528534e
63685eb
1cd3c43
187a599
2dde456
14ece17
54940fc
3e88c4e
d9bd815
d6e894a
3bfc894
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "onlyChanged": true, | ||
| "projectId": "Project:67dafea9025400b3251fa8ef", | ||
| "zip": true, | ||
| "buildScriptName": "build:storybook" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Shoreline Charts | ||
|
|
||
| `shoreline-components` and `echarts` are peer dependencies of `shoreline-charts` | ||
|
||
|
|
||
| ```sh | ||
| pnpm add @vtex/shoreline echarts @vtex/shoreline-charts | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| { | ||
| "name": "@vtex/shoreline-charts", | ||
| "description": "Shoreline datavis library", | ||
| "version": "0.0.0", | ||
| "main": "./dist/index.js", | ||
| "module": "./dist/index.mjs", | ||
| "types": "./dist/index.d.ts", | ||
| "publishConfig": { | ||
| "access": "public", | ||
| "registry": "https://registry.npmjs.org" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "exports": { | ||
| ".": { | ||
| "require": "./dist/index.js", | ||
| "import": "./dist/index.mjs", | ||
| "types": "./dist/index.d.ts" | ||
| }, | ||
| "./css": "./dist/index.css" | ||
| }, | ||
| "engines": { | ||
| "node": ">=16" | ||
| }, | ||
| "scripts": { | ||
| "prebuild": "rm -rf dist", | ||
| "dev": "tsup --watch", | ||
| "build": "tsup" | ||
| }, | ||
| "repository": { | ||
| "directory": "packages/charts", | ||
| "type": "git", | ||
| "url": "git+https://github.com/vtex/shoreline.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/vtex/shoreline/issues" | ||
| }, | ||
| "peerDependencies": { | ||
| "@vtex/shoreline": "1.x", | ||
| "echarts": "5.x", | ||
| "react": "18.x", | ||
| "react-dom": "18.x" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/lodash": "^4.17.4", | ||
| "@vtex/shoreline": "workspace:*", | ||
| "echarts": "5.5.0" | ||
| }, | ||
| "dependencies": { | ||
| "@vtex/shoreline-utils": "workspace:*", | ||
| "echarts-for-react": "^3.0.2", | ||
| "lodash": "^4.17.21", | ||
|
||
| "vitest-canvas-mock": "^0.3.3" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import type { EChartsOption } from 'echarts' | ||
| import { type ComponentPropsWithRef, forwardRef, useMemo } from 'react' | ||
| import type { ChartConfig, ChartUnit } from '../../types/chart' | ||
| import { Chart, type ChartOptions } from '../chart/chart' | ||
| import { | ||
| applySeriesHook, | ||
| defaultHooks, | ||
| getDataToChartCompositor, | ||
| getTooltipChartCompositor, | ||
| } from '../../utils/chart' | ||
| import { merge } from '@vtex/shoreline-utils' | ||
| import { | ||
| DATAZOOM_DEFAULT_STYLE, | ||
| GRID_DEFAULT_STYLE, | ||
| LEGEND_DEFAULT_STYLE, | ||
| } from '../../theme/chartStyles' | ||
|
|
||
| /** | ||
| * Used to make charts with multiple different types. | ||
| * @status stable | ||
| * @example | ||
| * <ChartCompositor | ||
| * charts={[ | ||
| * { serie: { data: [1, 2, 3, 4, 5] }, config: { type: 'bar' } }, | ||
| * { serie: { data: [1, 3, 2, 5, 4] }, config: { type: 'line' } }, | ||
| * ]} | ||
| * background={{ type: 'bar' }} | ||
| * tooltip={{ type: 'line' }} | ||
| * /> | ||
| */ | ||
| export const ChartCompositor = forwardRef< | ||
| echarts.EChartsType | undefined, | ||
| ChartCompositorProps | ||
| >(function ChartCompositor(props, ref) { | ||
| const { | ||
| charts, | ||
| xAxis = { type: 'category' }, | ||
| yAxis = { type: 'value' }, | ||
| tooltip, | ||
| zoom = false, | ||
| options, | ||
| ...otherProps | ||
| } = props | ||
|
|
||
| const hookedUnits: ChartUnit[] = useMemo(() => { | ||
| return charts.map((chart) => { | ||
| const { type, variant = 'default' } = chart.chartConfig | ||
| const seriesHooks: CallableFunction[] = defaultHooks[type][variant] | ||
| if (chart.hooks === undefined) { | ||
| return { | ||
| ...chart, | ||
| series: seriesHooks.reduce( | ||
| (out, fn) => applySeriesHook(out, fn), | ||
| chart.series | ||
| ), | ||
| } | ||
| } | ||
| if (chart.hooks === null) { | ||
| return chart | ||
| } | ||
| seriesHooks.push(...chart.hooks) | ||
| return { | ||
| ...chart, | ||
| series: seriesHooks.reduce( | ||
| (out, fn) => applySeriesHook(out, fn), | ||
| chart.series | ||
| ), | ||
| } | ||
| }) | ||
| }, []) | ||
|
|
||
| const seriesOptions: EChartsOption['series'] = useMemo(() => { | ||
| return hookedUnits.map((u) => getDataToChartCompositor(u)) | ||
| }, [hookedUnits]) | ||
|
|
||
| const tooltipOptions: EChartsOption['tooltip'] = useMemo(() => { | ||
| return getTooltipChartCompositor(tooltip) | ||
| }, [tooltip]) | ||
|
|
||
| const chartOptions: EChartsOption = useMemo(() => { | ||
| const finalOptions: EChartsOption = {} | ||
|
|
||
| finalOptions.legend = LEGEND_DEFAULT_STYLE | ||
| finalOptions.grid = GRID_DEFAULT_STYLE | ||
| if (zoom) finalOptions.dataZoom = DATAZOOM_DEFAULT_STYLE | ||
|
|
||
| finalOptions.series = seriesOptions | ||
| finalOptions.tooltip = tooltipOptions | ||
| finalOptions.yAxis = yAxis | ||
| finalOptions.xAxis = xAxis | ||
|
|
||
| return options ? merge(options, finalOptions) : finalOptions | ||
| }, [charts, tooltip, xAxis, yAxis, options]) | ||
|
|
||
| return ( | ||
| <Chart | ||
| chartConfig={null} | ||
| option={chartOptions} | ||
| style={{ height: 550 }} | ||
| ref={ref} | ||
| seriesHooks={null} | ||
| {...otherProps} | ||
| /> | ||
| ) | ||
| }) | ||
|
|
||
| export interface ChartCompositorOptions { | ||
| /** | ||
| * The data that will be rendered by the Compostior. Each unit contains | ||
| * a SeriesOption from Echarts, the ChartConfig which determines the type of chart and, optionally, | ||
| * an array of hook functions that will be applied to that series data. | ||
| * | ||
| * By default certain hooks will always be applied to certain chart types. This behaviour can be disabled by explicitly passing **null** to hooks. | ||
| * @example { series: { data: [1,2,3] }, config: { type: "bar", variant: "horizontal" } } | ||
| */ | ||
| charts: ChartUnit[] | ||
| /** | ||
| * Defines the yAxis setup for the chart, using the ECharts props. | ||
| * By default it just returns the value passed in data to the axis. | ||
| * | ||
| * @default {type:'value'} | ||
| */ | ||
| yAxis?: EChartsOption['yAxis'] | ||
| /** | ||
| * Defines the xAxis setup for the chart, using the ECharts props. | ||
| * By default it just returns the value passed in data to the axis. | ||
| * | ||
| * @default {type:'category'} | ||
| */ | ||
| xAxis?: EChartsOption['xAxis'] | ||
| /** | ||
| * Defines which type of tooltip is going to be used by the chart. | ||
| * @example { type: "bar", variant: "horizontal" } | ||
| */ | ||
| tooltip: ChartConfig | ||
| /** | ||
| * Whether to enable zoom. | ||
| * @default false | ||
| * @type boolean | ||
| */ | ||
| zoom?: boolean | ||
| /** | ||
| * Merges the passed options to the final options. | ||
| * | ||
| * **series**, **xAxis**, **yAxis**, and **tooltip** | ||
| * should be configured using other props from this component. | ||
| */ | ||
| options?: EChartsOption | ||
| } | ||
|
|
||
| export type ChartCompositorProps = ChartCompositorOptions & | ||
| Omit<ChartOptions, 'chartConfig' | 'option'> & | ||
| ComponentPropsWithRef<'div'> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './chart-compositor' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you change it for old modules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wrote an issue about this, but i believe the mixing of ESM and commonJS syntax in this file leads to it being read as a different kind of script in different node versions, being the only thing stopping shoreline from running in newer versions. We had a few surprise compilation errors in the lab when we didn't realize our node version was different (besides at some point this would need to be fixed anyway unless we plan to stick to node 20 forever).
As for why i changed it to just CommonJs instead of ESM, i just couldn't get it to work with just ESM for some reason, i could try again after all these months of experience.