-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature/charts package #1648
Open
natanfernandes
wants to merge
23
commits into
main
Choose a base branch
from
feature/charts-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/charts package #1648
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2a42d22
feat: setup with echarts for react component and tests
natanfernandes 3bd8c56
fix: add default value to loading and lint
natanfernandes f0ed931
Merge branch 'main' of https://github.com/vtex/shoreline into feature…
natanfernandes 30150af
fix: biomejs on vscode
natanfernandes 414b773
fix: color and add props export
natanfernandes 4546750
fix: canUseDom and array check
natanfernandes 8b38b74
feat: add chartConfig props to validate variant and type
natanfernandes 4218d1b
fix: wrong colors on the chart labels and lines
natanfernandes e417a65
Merge branch 'main' of https://github.com/vtex/shoreline into feature…
natanfernandes 4ef950c
feat: use cloneDeep from lodash, bar default styles
natanfernandes a45b6bb
feat: add sunrise colors, story with controls and fix somes styles in…
natanfernandes 87a20b7
Merge branch 'main' of https://github.com/vtex/shoreline into feature…
natanfernandes b00e7f0
fix: pnpm lock for chart lib
natanfernandes c3db54f
fix: types and spacing grid
natanfernandes 6d6e0a9
fix: legends spacing
natanfernandes 72344e0
feat: add tooltip on charts
natanfernandes 1e6e2ba
fix: lock pnpm
natanfernandes 95a80a6
chore: add chart docs
natanfernandes 581abae
feat: export css chart bundle and import on docs
natanfernandes 4abd5f8
chore: start to add doc to chart variants
natanfernandes 1589a97
docs: improve doc props
natanfernandes e905a0f
Merge branch 'main' of https://github.com/vtex/shoreline into feature…
natanfernandes 87ce98c
fix: pnpm lock
natanfernandes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not using it, right?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, i'll remove it |
||||
"vitest-canvas-mock": "^0.3.3" | ||||
} | ||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { | ||
useRef, | ||
useEffect, | ||
useMemo, | ||
forwardRef, | ||
useImperativeHandle, | ||
type ComponentPropsWithRef, | ||
useCallback, | ||
} from 'react' | ||
import type { EChartsOption, SetOptionOpts } from 'echarts' | ||
import ReactECharts, { type EChartsInstance } from 'echarts-for-react' | ||
import type * as echarts from 'echarts' | ||
|
||
import { defaultTheme } from '../../theme/themes' | ||
import type { ChartConfig } from '../../types/chart' | ||
import { getChartOptions } from '../../utils/chart' | ||
import { canUseDOM } from '@vtex/shoreline-utils' | ||
import { DEFAULT_LOADING_SPINNER } from '../../theme/chartStyles' | ||
|
||
/** | ||
* Render a Shoreline Chart with echarts | ||
* @see https://echarts.apache.org/en/index.html | ||
*/ | ||
export const Chart = forwardRef<echarts.EChartsType | undefined, ChartProps>( | ||
function Charts(props, ref) { | ||
const { | ||
option, | ||
settings, | ||
loading = false, | ||
loadingConfig = DEFAULT_LOADING_SPINNER, | ||
chartConfig, | ||
style, | ||
...otherProps | ||
} = props | ||
|
||
const chartRef = useRef<ReactECharts>(null) | ||
|
||
useImperativeHandle(ref, () => { | ||
if (chartRef.current) { | ||
return chartRef.current.getEchartsInstance() | ||
} | ||
return undefined | ||
}) | ||
|
||
const chartOptions: EChartsOption = useMemo(() => { | ||
const { type, variant } = chartConfig | ||
return getChartOptions(option, type, variant) || option | ||
}, [option, chartConfig]) | ||
|
||
const handleResize = useCallback(() => { | ||
if (chartRef.current) { | ||
chartRef.current.getEchartsInstance().resize() | ||
} | ||
}, [chartRef]) | ||
|
||
useEffect(() => { | ||
if (!canUseDOM) return | ||
|
||
window.addEventListener('resize', handleResize) | ||
return () => { | ||
window.removeEventListener('resize', handleResize) | ||
} | ||
}, [handleResize, canUseDOM]) | ||
|
||
return ( | ||
<div data-sl-chart> | ||
<ReactECharts | ||
ref={chartRef} | ||
theme={defaultTheme} | ||
option={chartOptions} | ||
style={{ minWidth: 290, ...style }} | ||
opts={{ | ||
renderer: 'svg', | ||
}} | ||
showLoading={loading} | ||
loadingOption={loadingConfig} | ||
onChartReady={(instance) => instance.resize()} | ||
{...otherProps} | ||
/> | ||
</div> | ||
) | ||
} | ||
) | ||
|
||
export interface ChartsOptions { | ||
/** | ||
* Echarts options for the chart | ||
*/ | ||
option: EChartsOption | ||
/** | ||
* Echarts settings | ||
*/ | ||
settings?: SetOptionOpts | ||
/** | ||
* Wether is loading | ||
* @default false | ||
*/ | ||
loading?: boolean | ||
/** | ||
* Options for customize the chart loading | ||
* @default false | ||
*/ | ||
loadingConfig?: EChartsInstance['showLoading'] | ||
/** | ||
* Configs containing type of chart and its variants, each variant is a pre-defined chart style for each type | ||
* @default default | ||
*/ | ||
chartConfig: ChartConfig | ||
} | ||
|
||
export type ChartProps = ChartsOptions & ComponentPropsWithRef<'div'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './chart' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './tooltip' | ||
export * from './chart' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './tooltip' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { TooltipComponentFormatterCallbackParams } from 'echarts' | ||
import { renderToStaticMarkup } from 'react-dom/server' | ||
import '../../theme/components/tooltip.css' | ||
|
||
export default function ChartTooltip({ params }: ChartTooltipProps) { | ||
return ( | ||
<div data-sl-chart-tooltip> | ||
{Array.isArray(params) ? ( | ||
params.map((param) => ( | ||
<ChartTooltipBase key={param.dataIndex} params={param} /> | ||
)) | ||
) : ( | ||
<ChartTooltipBase params={params} /> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export function ChartTooltipBase({ params }: { params: any }) { | ||
return ( | ||
<> | ||
<h4 data-sl-chart-tooltip-title>{params.name}</h4> | ||
<div data-sl-chart-tooltip-data-container> | ||
<div data-sl-chart-tooltip-data-serie-container> | ||
<span | ||
data-sl-chart-tooltip-data-serie-label-box | ||
style={{ | ||
backgroundColor: params.color, | ||
}} | ||
/> | ||
<span data-sl-chart-tooltip-data-serie-name>{params.seriesName}</span> | ||
</div> | ||
<span data-sl-chart-tooltip-data-serie-value>{params.value}</span> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export const getTooltipStaticString = ( | ||
params: TooltipComponentFormatterCallbackParams | ||
) => renderToStaticMarkup(<ChartTooltip params={params} />) | ||
|
||
export interface ChartTooltipProps { | ||
params: TooltipComponentFormatterCallbackParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// biome-ignore lint/correctness/noUnusedImports: <explanation> | ||
import React from 'react' | ||
import { Chart } from '../index' | ||
import { compactNumber } from '../utils/format' | ||
import type { StoryObj } from '@storybook/react' | ||
|
||
const Bar = { | ||
title: 'Charts/bar', | ||
component: Chart, | ||
} | ||
export default Bar | ||
|
||
type Story = StoryObj<typeof Chart> | ||
|
||
export const Basic: Story = { | ||
args: { | ||
option: { | ||
xAxis: { | ||
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], | ||
}, | ||
series: [{ data: [1, 2, 3, 4, 5, 6, 7] }], | ||
}, | ||
chartConfig: { type: 'bar' }, | ||
style: { height: 550 }, | ||
}, | ||
} | ||
|
||
export const Loading: Story = { | ||
render: (args) => { | ||
const { option, chartConfig, loading } = args | ||
|
||
return ( | ||
<Chart | ||
option={option} | ||
chartConfig={chartConfig} | ||
style={{ height: 550 }} | ||
loading={loading} | ||
/> | ||
) | ||
}, | ||
args: { | ||
option: { | ||
xAxis: { | ||
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], | ||
}, | ||
series: [{ data: [1, 2, 3, 4, 5, 6, 7] }], | ||
}, | ||
chartConfig: { type: 'bar' }, | ||
style: { height: 550 }, | ||
loading: true, | ||
}, | ||
} | ||
export const MultiSeries: Story = { | ||
args: { | ||
option: { | ||
xAxis: { | ||
data: ['Mon', 'Tue', 'Wed'], | ||
}, | ||
series: [ | ||
{ data: [3, 2, 3, 4], name: 'Series 1' }, | ||
{ data: [1, 4, 2, 3], name: 'Series 2' }, | ||
{ data: [2, 1, 4, 1], name: 'Series 3' }, | ||
], | ||
}, | ||
chartConfig: { type: 'bar' }, | ||
style: { height: 550 }, | ||
}, | ||
} | ||
export const WithHugeNumbers: Story = { | ||
args: { | ||
option: { | ||
xAxis: { | ||
data: ['Mon', 'Tue', 'Wed'], | ||
}, | ||
yAxis: { | ||
axisLabel: { | ||
formatter: (value: number) => compactNumber(value), | ||
}, | ||
}, | ||
series: [ | ||
{ | ||
data: [12344441, 62346346, 97346346], | ||
name: 'Series 1', | ||
}, | ||
], | ||
}, | ||
chartConfig: { type: 'bar' }, | ||
style: { height: 550 }, | ||
}, | ||
} | ||
|
||
export const Horizontal: Story = { | ||
args: { | ||
option: { | ||
xAxis: { | ||
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Test'], | ||
}, | ||
series: [ | ||
{ data: [1, 2, 3, 4, 5, 6, 7, 8] }, | ||
{ data: [1, 4, 2, 1, 4, 3, 5, 9] }, | ||
], | ||
}, | ||
chartConfig: { type: 'bar', variant: 'horizontal' }, | ||
style: { height: 550 }, | ||
}, | ||
} | ||
|
||
export const MultiType: Story = { | ||
args: { | ||
option: { | ||
xAxis: { | ||
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], | ||
}, | ||
series: [ | ||
{ data: [1, 2, 3, 4, 5, 6, 7] }, | ||
{ data: [1, 4, 2, 1, 4, 3, 5], type: 'line' }, | ||
], | ||
}, | ||
chartConfig: { type: 'bar', variant: 'default' }, | ||
style: { height: 550 }, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const BAR_CHART_DATA = { | ||
xAxis: { | ||
weekdays: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], | ||
}, | ||
series: { | ||
dayNumbers: [1, 2, 3, 4, 5, 6, 7], | ||
}, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.