@automattic/charts 0.8.2 → 0.8.4
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.
- package/CHANGELOG.md +11 -0
- package/dist/cjs/components/line-chart/line-chart.js +1 -1
- package/dist/cjs/components/line-chart/line-chart.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/mjs/components/line-chart/line-chart.js +1 -1
- package/dist/mjs/components/line-chart/line-chart.js.map +1 -1
- package/package.json +4 -4
- package/src/components/line-chart/line-chart.tsx +35 -14
- package/src/types.ts +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.8.4] - 2025-02-27
|
|
9
|
+
### Fixed
|
|
10
|
+
- Line chart: no need for min x tick num [#42087]
|
|
11
|
+
|
|
12
|
+
## [0.8.3] - 2025-02-25
|
|
13
|
+
### Changed
|
|
14
|
+
- Set options as optional for series [#42047]
|
|
15
|
+
- Update package dependencies. [#41955]
|
|
16
|
+
|
|
8
17
|
## [0.8.2] - 2025-02-19
|
|
9
18
|
### Added
|
|
10
19
|
- Add yScale options support. [#41866]
|
|
@@ -119,6 +128,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
119
128
|
- Fixed lints following ESLint rule changes for TS [#40584]
|
|
120
129
|
- Fixing a bug in Chart storybook data. [#40640]
|
|
121
130
|
|
|
131
|
+
[0.8.4]: https://github.com/Automattic/charts/compare/v0.8.3...v0.8.4
|
|
132
|
+
[0.8.3]: https://github.com/Automattic/charts/compare/v0.8.2...v0.8.3
|
|
122
133
|
[0.8.2]: https://github.com/Automattic/charts/compare/v0.8.1...v0.8.2
|
|
123
134
|
[0.8.1]: https://github.com/Automattic/charts/compare/v0.8.0...v0.8.1
|
|
124
135
|
[0.8.0]: https://github.com/Automattic/charts/compare/v0.7.1...v0.8.0
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("react/jsx-runtime"),t=require("@visx/curve"),a=require("@visx/gradient"),i=require("@visx/xychart");require("@visx/xychart/lib/components/Tooltip");var r=require("clsx"),
|
|
1
|
+
"use strict";var e=require("react/jsx-runtime"),t=require("@visx/curve"),a=require("@visx/gradient"),i=require("@visx/xychart");require("@visx/xychart/lib/components/Tooltip");var r=require("clsx"),o=require("react"),s=require("../../providers/theme/theme-provider.js"),l=require("../legend/base-legend.js"),n=require("../shared/with-responsive.js"),c=require("./line-chart.module.scss.js");const d=({tooltipData:t})=>{const a=t?.nearestDatum?.datum;if(!a)return null;const i=Object.entries(t?.datumByKey||{}).map((([e,{datum:t}])=>({key:e,value:t.value}))).sort(((e,t)=>t.value-e.value));return e.jsxs("div",{className:c["line-chart__tooltip"],children:[e.jsx("div",{className:c["line-chart__tooltip-date"],children:a.date?.toLocaleDateString()}),i.map((t=>e.jsxs("div",{className:c["line-chart__tooltip-row"],children:[e.jsxs("span",{className:c["line-chart__tooltip-label"],children:[t.key,":"]}),e.jsx("span",{className:c["line-chart__tooltip-value"],children:t.value})]},t.key)))]})},m=e=>new Date(e).toLocaleDateString(void 0,{month:"short",day:"numeric"});var u=n.withResponsive((({data:n,width:u,height:h,className:p,margin:v,withTooltips:x=!0,showLegend:g=!1,legendOrientation:j="horizontal",withGradientFill:y=!1,smoothing:T=!0,renderTooltip:b=d,options:N={}})=>{const q=s.useChartTheme(),k=o.useId(),w=o.useMemo((()=>n.map((e=>({...e,data:e.data.sort(((e,t)=>e.date.getTime()-t.date.getTime()))})))),[n]),_=o.useMemo((()=>{const e=w?.map((e=>e.options?.stroke??"")).filter(Boolean)??[];return i.buildChartTheme({...q,colors:[...e,...q.colors]})}),[q,w]);v=o.useMemo((()=>{let e={};return"right"===N.axis?.y?.orientation&&(e={...e,right:40,left:0}),"top"===N.axis?.x?.orientation&&(e={...e,top:20,bottom:10}),{...e,...v}}),[v,N]);const f=o.useMemo((()=>Math.min(w[0]?.data.length,Math.ceil(u/100))),[w,u]),A=(e=>e?.length?e.some((e=>e.data.some((e=>isNaN(e.value)||null===e.value||void 0===e.value||isNaN(e.date.getTime())))))?"Invalid data":null:"No data available")(w);if(A)return e.jsx("div",{className:r("line-chart",c["line-chart"]),children:A});const S=w.map(((e,t)=>({label:e.label,value:"",color:q.colors[t%q.colors.length]}))),D={xAccessor:e=>e?.date,yAccessor:e=>e?.value};return e.jsxs("div",{className:r("line-chart",c["line-chart"],p),"data-testid":"line-chart",role:"img","aria-label":"line chart",children:[e.jsxs(i.XYChart,{theme:_,width:u,height:h,margin:{top:10,right:0,bottom:20,left:40,...v},xScale:{type:"time",...N?.xScale},yScale:{type:"linear",nice:!0,zero:!1,...N?.yScale},children:[e.jsx(i.AnimatedGrid,{columns:!1,numTicks:4}),e.jsx(i.AnimatedAxis,{orientation:"bottom",numTicks:f,tickFormat:m,...N?.axis?.x}),e.jsx(i.AnimatedAxis,{orientation:"left",numTicks:4,...N?.axis?.y}),w.map(((r,o)=>{const s=r.options?.stroke??_.colors[o%_.colors.length];return e.jsxs("g",{children:[y&&e.jsx(a.LinearGradient,{id:`area-gradient-${k}-${o+1}`,from:s,fromOpacity:.4,toOpacity:.1,to:_.backgroundColor,...r.options?.gradient,"data-testid":"line-gradient"}),e.jsx(i.AnimatedAreaSeries,{dataKey:r?.label,data:r.data,...D,fill:y?`url(#area-gradient-${k}-${o+1})`:void 0,renderLine:!0,curve:T?t.curveCatmullRom:t.curveLinear},r?.label)]},r?.label||o)})),x&&e.jsx(i.Tooltip,{snapTooltipToDatumX:!0,snapTooltipToDatumY:!0,showSeriesGlyphs:!0,renderTooltip:b})]}),g&&e.jsx(l.BaseLegend,{items:S,orientation:j,className:c["line-chart-legend"]})]})}));module.exports=u;
|
|
2
2
|
//# sourceMappingURL=line-chart.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"line-chart.js","sources":["/@automattic/charts/../../../../src/components/line-chart/line-chart.tsx"],"sourcesContent":[null],"names":["renderDefaultTooltip","tooltipData","nearestDatum","datum","tooltipPoints","Object","entries","datumByKey","map","key","value","sort","a","b","_jsxs","jsxs","className","styles","children","_jsx","date","toLocaleDateString","point","formatDateTick","Date","undefined","month","day","withResponsive","data","width","height","margin","withTooltips","showLegend","legendOrientation","withGradientFill","smoothing","renderTooltip","options","providerTheme","useChartTheme","
|
|
1
|
+
{"version":3,"file":"line-chart.js","sources":["/@automattic/charts/../../../../src/components/line-chart/line-chart.tsx"],"sourcesContent":[null],"names":["renderDefaultTooltip","tooltipData","nearestDatum","datum","tooltipPoints","Object","entries","datumByKey","map","key","value","sort","a","b","_jsxs","jsxs","className","styles","children","_jsx","date","toLocaleDateString","point","formatDateTick","timestamp","Date","undefined","month","day","withResponsive","data","width","height","margin","withTooltips","showLegend","legendOrientation","withGradientFill","smoothing","renderTooltip","options","providerTheme","useChartTheme","chartId","useId","dataSorted","useMemo","series","getTime","theme","seriesColors","stroke","filter","Boolean","buildChartTheme","colors","defaultMargin","axis","y","orientation","right","left","x","top","bottom","xNumTicks","Math","min","length","ceil","error","some","isNaN","validateData","clsx","legendItems","group","index","label","color","accessors","xAccessor","d","yAccessor","role","XYChart","xScale","type","yScale","nice","zero","AnimatedGrid","columns","numTicks","jsx","AnimatedAxis","tickFormat","seriesData","LinearGradient","id","from","fromOpacity","toOpacity","to","backgroundColor","gradient","AnimatedAreaSeries","dataKey","fill","renderLine","curve","curveCatmullRom","curveLinear","Tooltip","snapTooltipToDatumX","snapTooltipToDatumY","showSeriesGlyphs","Legend","items"],"mappings":"uYAmBA,MAaMA,EAAuB,EAC5BC,kBAUA,MAAMC,EAAeD,GAAaC,cAAcC,MAChD,IAAOD,EAAe,OAAO,KAE7B,MAAME,EAAgCC,OAAOC,QAASL,GAAaM,YAAc,CAAA,GAC/EC,KAAK,EAAIC,GAAON,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;
|
|
22
22
|
label: string;
|
|
23
23
|
data: DataPointDate[] | DataPoint[];
|
|
24
|
-
options
|
|
24
|
+
options?: {
|
|
25
25
|
gradient?: {
|
|
26
26
|
from: string;
|
|
27
27
|
to: string;
|
|
28
|
+
fromOpacity?: number;
|
|
28
29
|
toOpacity?: number;
|
|
29
30
|
};
|
|
30
31
|
stroke?: string;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e}from"react/jsx-runtime";import{curveCatmullRom as a,curveLinear as o}from"@visx/curve";import{LinearGradient as i}from"@visx/gradient";import{buildChartTheme as r,XYChart as l,AnimatedGrid as n,AnimatedAxis as s,AnimatedAreaSeries as c,Tooltip as m}from"@visx/xychart";import"@visx/xychart/lib/components/Tooltip";import d from"clsx";import{useMemo as
|
|
1
|
+
import{jsx as t,jsxs as e}from"react/jsx-runtime";import{curveCatmullRom as a,curveLinear as o}from"@visx/curve";import{LinearGradient as i}from"@visx/gradient";import{buildChartTheme as r,XYChart as l,AnimatedGrid as n,AnimatedAxis as s,AnimatedAreaSeries as c,Tooltip as m}from"@visx/xychart";import"@visx/xychart/lib/components/Tooltip";import d from"clsx";import{useId as h,useMemo as p}from"react";import{useChartTheme as u}from"../../providers/theme/theme-provider.js";import{BaseLegend as v}from"../legend/base-legend.js";import{withResponsive as g}from"../shared/with-responsive.js";import f from"./line-chart.module.scss.js";const y=({tooltipData:a})=>{const o=a?.nearestDatum?.datum;if(!o)return null;const i=Object.entries(a?.datumByKey||{}).map((([t,{datum:e}])=>({key:t,value:e.value}))).sort(((t,e)=>e.value-t.value));return e("div",{className:f["line-chart__tooltip"],children:[t("div",{className:f["line-chart__tooltip-date"],children:o.date?.toLocaleDateString()}),i.map((a=>e("div",{className:f["line-chart__tooltip-row"],children:[e("span",{className:f["line-chart__tooltip-label"],children:[a.key,":"]}),t("span",{className:f["line-chart__tooltip-value"],children:a.value})]},a.key)))]})},x=t=>new Date(t).toLocaleDateString(void 0,{month:"short",day:"numeric"});var b=g((({data:g,width:b,height:N,className:T,margin:k,withTooltips:_=!0,showLegend:w=!1,legendOrientation:D="horizontal",withGradientFill:S=!1,smoothing:j=!0,renderTooltip:L=y,options:O={}})=>{const $=u(),z=h(),A=p((()=>g.map((t=>({...t,data:t.data.sort(((t,e)=>t.date.getTime()-e.date.getTime()))})))),[g]),B=p((()=>{const t=A?.map((t=>t.options?.stroke??"")).filter(Boolean)??[];return r({...$,colors:[...t,...$.colors]})}),[$,A]);k=p((()=>{let t={};return"right"===O.axis?.y?.orientation&&(t={...t,right:40,left:0}),"top"===O.axis?.x?.orientation&&(t={...t,top:20,bottom:10}),{...t,...k}}),[k,O]);const F=p((()=>Math.min(A[0]?.data.length,Math.ceil(b/100))),[A,b]),G=(t=>t?.length?t.some((t=>t.data.some((t=>isNaN(t.value)||null===t.value||void 0===t.value||isNaN(t.date.getTime())))))?"Invalid data":null:"No data available")(A);if(G)return t("div",{className:d("line-chart",f["line-chart"]),children:G});const K=A.map(((t,e)=>({label:t.label,value:"",color:$.colors[e%$.colors.length]}))),M={xAccessor:t=>t?.date,yAccessor:t=>t?.value};return e("div",{className:d("line-chart",f["line-chart"],T),"data-testid":"line-chart",role:"img","aria-label":"line chart",children:[e(l,{theme:B,width:b,height:N,margin:{top:10,right:0,bottom:20,left:40,...k},xScale:{type:"time",...O?.xScale},yScale:{type:"linear",nice:!0,zero:!1,...O?.yScale},children:[t(n,{columns:!1,numTicks:4}),t(s,{orientation:"bottom",numTicks:F,tickFormat:x,...O?.axis?.x}),t(s,{orientation:"left",numTicks:4,...O?.axis?.y}),A.map(((r,l)=>{const n=r.options?.stroke??B.colors[l%B.colors.length];return e("g",{children:[S&&t(i,{id:`area-gradient-${z}-${l+1}`,from:n,fromOpacity:.4,toOpacity:.1,to:B.backgroundColor,...r.options?.gradient,"data-testid":"line-gradient"}),t(c,{dataKey:r?.label,data:r.data,...M,fill:S?`url(#area-gradient-${z}-${l+1})`:void 0,renderLine:!0,curve:j?a:o},r?.label)]},r?.label||l)})),_&&t(m,{snapTooltipToDatumX:!0,snapTooltipToDatumY:!0,showSeriesGlyphs:!0,renderTooltip:L})]}),w&&t(v,{items:K,orientation:D,className:f["line-chart-legend"]})]})}));export{b as default};
|
|
2
2
|
//# sourceMappingURL=line-chart.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"line-chart.js","sources":["../../../../src/components/line-chart/line-chart.tsx"],"sourcesContent":[null],"names":["renderDefaultTooltip","tooltipData","nearestDatum","datum","tooltipPoints","Object","entries","datumByKey","map","key","value","sort","a","b","_jsxs","className","styles","children","_jsx","date","toLocaleDateString","point","formatDateTick","Date","undefined","month","day","lineChart","withResponsive","data","width","height","margin","withTooltips","showLegend","legendOrientation","withGradientFill","smoothing","renderTooltip","options","providerTheme","useChartTheme","
|
|
1
|
+
{"version":3,"file":"line-chart.js","sources":["../../../../src/components/line-chart/line-chart.tsx"],"sourcesContent":[null],"names":["renderDefaultTooltip","tooltipData","nearestDatum","datum","tooltipPoints","Object","entries","datumByKey","map","key","value","sort","a","b","_jsxs","className","styles","children","_jsx","date","toLocaleDateString","point","formatDateTick","timestamp","Date","undefined","month","day","lineChart","withResponsive","data","width","height","margin","withTooltips","showLegend","legendOrientation","withGradientFill","smoothing","renderTooltip","options","providerTheme","useChartTheme","chartId","useId","dataSorted","useMemo","series","getTime","theme","seriesColors","stroke","filter","Boolean","buildChartTheme","colors","defaultMargin","axis","y","orientation","right","left","x","top","bottom","xNumTicks","Math","min","length","ceil","error","some","isNaN","validateData","clsx","legendItems","group","index","label","color","accessors","xAccessor","d","yAccessor","role","XYChart","xScale","type","yScale","nice","zero","AnimatedGrid","columns","numTicks","AnimatedAxis","tickFormat","seriesData","LinearGradient","id","from","fromOpacity","toOpacity","to","backgroundColor","gradient","AnimatedAreaSeries","dataKey","fill","renderLine","curve","curveCatmullRom","curveLinear","Tooltip","snapTooltipToDatumX","snapTooltipToDatumY","showSeriesGlyphs","Legend","items"],"mappings":"0nBAmBA,MAa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
4
|
"description": "Display charts within Automattic products.",
|
|
5
5
|
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/charts/#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -78,8 +78,8 @@
|
|
|
78
78
|
"@rollup/plugin-node-resolve": "15.3.0",
|
|
79
79
|
"@rollup/plugin-terser": "0.4.3",
|
|
80
80
|
"@rollup/plugin-typescript": "12.1.0",
|
|
81
|
-
"@storybook/blocks": "8.
|
|
82
|
-
"@storybook/react": "8.
|
|
81
|
+
"@storybook/blocks": "8.5.8",
|
|
82
|
+
"@storybook/react": "8.5.8",
|
|
83
83
|
"@testing-library/dom": "^10.0.0",
|
|
84
84
|
"@testing-library/jest-dom": "^6.0.0",
|
|
85
85
|
"@testing-library/react": "^16.0.0",
|
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
"sass": "1.64.1",
|
|
111
111
|
"sass-embedded": "1.83.0",
|
|
112
112
|
"sass-loader": "^13.0.0",
|
|
113
|
-
"storybook": "8.
|
|
113
|
+
"storybook": "8.5.8",
|
|
114
114
|
"tsconfig-paths-webpack-plugin": "4.2.0",
|
|
115
115
|
"typescript": "5.7.2",
|
|
116
116
|
"webpack": "^5.88.0",
|
|
@@ -10,13 +10,15 @@ import {
|
|
|
10
10
|
} from '@visx/xychart';
|
|
11
11
|
import { RenderTooltipParams } from '@visx/xychart/lib/components/Tooltip';
|
|
12
12
|
import clsx from 'clsx';
|
|
13
|
-
import { FC, ReactNode, useMemo } from 'react';
|
|
13
|
+
import { FC, ReactNode, useId, useMemo } from 'react';
|
|
14
14
|
import { useChartTheme } from '../../providers/theme/theme-provider';
|
|
15
15
|
import { Legend } from '../legend';
|
|
16
16
|
import { withResponsive } from '../shared/with-responsive';
|
|
17
17
|
import styles from './line-chart.module.scss';
|
|
18
18
|
import type { BaseChartProps, DataPointDate, SeriesData } from '../../types';
|
|
19
19
|
|
|
20
|
+
const X_TICK_WIDTH = 100;
|
|
21
|
+
|
|
20
22
|
interface LineChartProps extends BaseChartProps< SeriesData[] > {
|
|
21
23
|
withGradientFill: boolean;
|
|
22
24
|
smoothing?: boolean;
|
|
@@ -64,8 +66,8 @@ const renderDefaultTooltip = ( {
|
|
|
64
66
|
);
|
|
65
67
|
};
|
|
66
68
|
|
|
67
|
-
const formatDateTick = (
|
|
68
|
-
const date = new Date(
|
|
69
|
+
const formatDateTick = ( timestamp: number ) => {
|
|
70
|
+
const date = new Date( timestamp );
|
|
69
71
|
return date.toLocaleDateString( undefined, {
|
|
70
72
|
month: 'short',
|
|
71
73
|
day: 'numeric',
|
|
@@ -104,15 +106,25 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
104
106
|
options = {},
|
|
105
107
|
} ) => {
|
|
106
108
|
const providerTheme = useChartTheme();
|
|
109
|
+
const chartId = useId(); // Ensure unique ids for gradient fill.
|
|
110
|
+
|
|
111
|
+
const dataSorted = useMemo(
|
|
112
|
+
() =>
|
|
113
|
+
data.map( series => ( {
|
|
114
|
+
...series,
|
|
115
|
+
data: series.data.sort( ( a, b ) => a.date.getTime() - b.date.getTime() ),
|
|
116
|
+
} ) ),
|
|
117
|
+
[ data ]
|
|
118
|
+
);
|
|
107
119
|
|
|
108
120
|
const theme = useMemo( () => {
|
|
109
121
|
const seriesColors =
|
|
110
|
-
|
|
122
|
+
dataSorted?.map( series => series.options?.stroke ?? '' ).filter( Boolean ) ?? [];
|
|
111
123
|
return buildChartTheme( {
|
|
112
124
|
...providerTheme,
|
|
113
125
|
colors: [ ...seriesColors, ...providerTheme.colors ],
|
|
114
126
|
} );
|
|
115
|
-
}, [ providerTheme,
|
|
127
|
+
}, [ providerTheme, dataSorted ] );
|
|
116
128
|
|
|
117
129
|
margin = useMemo( () => {
|
|
118
130
|
// Auto-margin unless specified to make room for axis labels.
|
|
@@ -122,19 +134,24 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
122
134
|
defaultMargin = { ...defaultMargin, right: 40, left: 0 };
|
|
123
135
|
}
|
|
124
136
|
if ( options.axis?.x?.orientation === 'top' ) {
|
|
125
|
-
defaultMargin = { ...defaultMargin, top:
|
|
137
|
+
defaultMargin = { ...defaultMargin, top: 20, bottom: 10 };
|
|
126
138
|
}
|
|
127
139
|
// Merge default margin with user-specified margin.
|
|
128
140
|
return { ...defaultMargin, ...margin };
|
|
129
141
|
}, [ margin, options ] );
|
|
130
142
|
|
|
131
|
-
const
|
|
143
|
+
const xNumTicks = useMemo(
|
|
144
|
+
() => Math.min( dataSorted[ 0 ]?.data.length, Math.ceil( width / X_TICK_WIDTH ) ),
|
|
145
|
+
[ dataSorted, width ]
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const error = validateData( dataSorted );
|
|
132
149
|
if ( error ) {
|
|
133
150
|
return <div className={ clsx( 'line-chart', styles[ 'line-chart' ] ) }>{ error }</div>;
|
|
134
151
|
}
|
|
135
152
|
|
|
136
153
|
// Create legend items from group labels, this iterates over groups rather than data points
|
|
137
|
-
const legendItems =
|
|
154
|
+
const legendItems = dataSorted.map( ( group, index ) => ( {
|
|
138
155
|
label: group.label, // Label for each unique group
|
|
139
156
|
value: '', // Empty string since we don't want to show a specific value
|
|
140
157
|
color: providerTheme.colors[ index % providerTheme.colors.length ],
|
|
@@ -156,29 +173,31 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
156
173
|
theme={ theme }
|
|
157
174
|
width={ width }
|
|
158
175
|
height={ height }
|
|
159
|
-
margin={ { top:
|
|
176
|
+
margin={ { top: 10, right: 0, bottom: 20, left: 40, ...margin } }
|
|
177
|
+
// xScale and yScale could be set in Axis as well, but they are `scale` props there.
|
|
160
178
|
xScale={ { type: 'time', ...options?.xScale } }
|
|
161
179
|
yScale={ { type: 'linear', nice: true, zero: false, ...options?.yScale } }
|
|
162
180
|
>
|
|
163
181
|
<AnimatedGrid columns={ false } numTicks={ 4 } />
|
|
164
182
|
<AnimatedAxis
|
|
165
183
|
orientation="bottom"
|
|
166
|
-
numTicks={
|
|
184
|
+
numTicks={ xNumTicks }
|
|
167
185
|
tickFormat={ formatDateTick }
|
|
168
186
|
{ ...options?.axis?.x }
|
|
169
187
|
/>
|
|
170
188
|
<AnimatedAxis orientation="left" numTicks={ 4 } { ...options?.axis?.y } />
|
|
171
189
|
|
|
172
|
-
{
|
|
190
|
+
{ dataSorted.map( ( seriesData, index ) => {
|
|
173
191
|
const stroke = seriesData.options?.stroke ?? theme.colors[ index % theme.colors.length ];
|
|
174
192
|
return (
|
|
175
193
|
<g key={ seriesData?.label || index }>
|
|
176
194
|
{ withGradientFill && (
|
|
177
195
|
<LinearGradient
|
|
178
|
-
id={ `area-gradient-${ index + 1 }` }
|
|
196
|
+
id={ `area-gradient-${ chartId }-${ index + 1 }` }
|
|
179
197
|
from={ stroke }
|
|
180
|
-
|
|
198
|
+
fromOpacity={ 0.4 }
|
|
181
199
|
toOpacity={ 0.1 }
|
|
200
|
+
to={ theme.backgroundColor }
|
|
182
201
|
{ ...seriesData.options?.gradient }
|
|
183
202
|
data-testid="line-gradient"
|
|
184
203
|
/>
|
|
@@ -188,7 +207,9 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
188
207
|
dataKey={ seriesData?.label }
|
|
189
208
|
data={ seriesData.data as DataPointDate[] }
|
|
190
209
|
{ ...accessors }
|
|
191
|
-
fill={
|
|
210
|
+
fill={
|
|
211
|
+
withGradientFill ? `url(#area-gradient-${ chartId }-${ index + 1 })` : undefined
|
|
212
|
+
}
|
|
192
213
|
renderLine={ true }
|
|
193
214
|
curve={ smoothing ? curveCatmullRom : curveLinear }
|
|
194
215
|
/>
|
package/src/types.ts
CHANGED
|
@@ -24,7 +24,10 @@ export type SeriesData = {
|
|
|
24
24
|
group?: string;
|
|
25
25
|
label: string;
|
|
26
26
|
data: DataPointDate[] | DataPoint[];
|
|
27
|
-
options
|
|
27
|
+
options?: {
|
|
28
|
+
gradient?: { from: string; to: string; fromOpacity?: number; toOpacity?: number };
|
|
29
|
+
stroke?: string;
|
|
30
|
+
};
|
|
28
31
|
};
|
|
29
32
|
|
|
30
33
|
export type MultipleDataPointsDate = {
|