@automattic/charts 0.8.2 → 0.8.3
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 +6 -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 +33 -12
- package/src/types.ts +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,11 @@ 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.3] - 2025-02-25
|
|
9
|
+
### Changed
|
|
10
|
+
- Set options as optional for series [#42047]
|
|
11
|
+
- Update package dependencies. [#41955]
|
|
12
|
+
|
|
8
13
|
## [0.8.2] - 2025-02-19
|
|
9
14
|
### Added
|
|
10
15
|
- Add yScale options support. [#41866]
|
|
@@ -119,6 +124,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
119
124
|
- Fixed lints following ESLint rule changes for TS [#40584]
|
|
120
125
|
- Fixing a bug in Chart storybook data. [#40640]
|
|
121
126
|
|
|
127
|
+
[0.8.3]: https://github.com/Automattic/charts/compare/v0.8.2...v0.8.3
|
|
122
128
|
[0.8.2]: https://github.com/Automattic/charts/compare/v0.8.1...v0.8.2
|
|
123
129
|
[0.8.1]: https://github.com/Automattic/charts/compare/v0.8.0...v0.8.1
|
|
124
130
|
[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:x,withTooltips:v=!0,showLegend:g=!1,legendOrientation:j="horizontal",withGradientFill:y=!1,smoothing:T=!0,renderTooltip:b=d,options:N={}})=>{const q=s.useChartTheme(),f=o.useId(),k=o.useMemo((()=>n.map((e=>({...e,data:e.data.sort(((e,t)=>e.date.getTime()-t.date.getTime()))})))),[n]),w=o.useMemo((()=>{const e=k?.map((e=>e.options?.stroke??"")).filter(Boolean)??[];return i.buildChartTheme({...q,colors:[...e,...q.colors]})}),[q,k]);x=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,...x}}),[x,N]);const _=o.useMemo((()=>Math.max(Math.floor(Math.min(k[0]?.data.length,u/60)),5)),[k,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")(k);if(A)return e.jsx("div",{className:r("line-chart",c["line-chart"]),children:A});const S=k.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:w,width:u,height:h,margin:{top:10,right:0,bottom:20,left:40,...x},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:_,tickFormat:m,...N?.axis?.x}),e.jsx(i.AnimatedAxis,{orientation:"left",numTicks:4,...N?.axis?.y}),k.map(((r,o)=>{const s=r.options?.stroke??w.colors[o%w.colors.length];return e.jsxs("g",{children:[y&&e.jsx(a.LinearGradient,{id:`area-gradient-${f}-${o+1}`,from:s,fromOpacity:.4,toOpacity:.1,to:w.backgroundColor,...r.options?.gradient,"data-testid":"line-gradient"}),e.jsx(i.AnimatedAreaSeries,{dataKey:r?.label,data:r.data,...D,fill:y?`url(#area-gradient-${f}-${o+1})`:void 0,renderLine:!0,curve:T?t.curveCatmullRom:t.curveLinear},r?.label)]},r?.label||o)})),v&&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","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","max","floor","min","length","error","some","isNaN","validateData","clsx","legendItems","group","index","label","color","accessors","xAccessor","d","yAccessor","role","XYChart","xScale","type","yScale","nice","zero","jsx","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":"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,aAAiB,CACjCM,MACAC,MAAOP,EAAMO,UAEbC,MAAM,CAAEC,EAAGC,IAAOA,EAAEH,MAAQE,EAAEF,QAEhC,OACCI,EAAKC,KAAA,MAAA,CAAAC,UAAYC,EAAQ,uBACxBC,S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SAAW,IAC3C3E,EAACwE,IAAAI,gBACArC,YAAY,SACZoC,SAAW9B,EACXgC,WAAazE,KACRgB,GAASiB,MAAMK,IAErB1C,MAAC4E,eAAY,CAACrC,YAAY,OAAOoC,SAAW,KAASvD,GAASiB,MAAMC,IAElEb,EAAWpC,KAAK,CAAEyF,EAAYpB,KAC/B,MAAM3B,EAAS+C,EAAW1D,SAASW,QAAUF,EAAMM,OAAQuB,EAAQ7B,EAAMM,OAAOe,QAChF,OACCvD,EAAAA,KACG,IAAA,CAAAI,SAAA,CAAAkB,GACDjB,EAAAA,IAAC+E,EAAAA,eAAc,CACdC,GAAK,iBAAkBzD,KAAamC,EAAQ,IAC5CuB,KAAOlD,EACPmD,YAAc,GACdC,UAAY,GACZC,GAAKvD,EAAMwD,mBACNP,EAAW1D,SAASkE,SACb,cAAA,kBAGdtF,EAAAA,IAACuF,EAAAA,mBAEA,CAAAC,QAAUV,GAAYnB,MACtBjD,KAAOoE,EAAWpE,QACbmD,EACL4B,KACCxE,EAAmB,sBAAuBM,KAAamC,EAAQ,UAAQpD,EAExEoF,YAAa,EACbC,MAAQzE,EAAY0E,kBAAkBC,EAAAA,aARhCf,GAAYnB,SAbXmB,GAAYnB,OAASD,EAwB7B,IAGD5C,GACDd,EAAAA,IAAC8F,EAAAA,SACAC,qBAAmB,EACnBC,qBACA,EAAAC,oBACA9E,cAAgBA,OAKjBJ,GACDf,EAAAA,IAACkG,EAAAA,WACA,CAAAC,MAAQ3C,EACRjB,YAAcvB,EACdnB,UAAYC,EAAQ,yBAItB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -21,10 +21,11 @@ type SeriesData = {
|
|
|
21
21
|
group?: string;
|
|
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 x=({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)))]})},y=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=x,options:O={}})=>{const $=u(),M=h(),z=p((()=>g.map((t=>({...t,data:t.data.sort(((t,e)=>t.date.getTime()-e.date.getTime()))})))),[g]),A=p((()=>{const t=z?.map((t=>t.options?.stroke??"")).filter(Boolean)??[];return r({...$,colors:[...t,...$.colors]})}),[$,z]);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 B=p((()=>Math.max(Math.floor(Math.min(z[0]?.data.length,b/60)),5)),[z,b]),F=(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")(z);if(F)return t("div",{className:d("line-chart",f["line-chart"]),children:F});const G=z.map(((t,e)=>({label:t.label,value:"",color:$.colors[e%$.colors.length]}))),K={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:A,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:B,tickFormat:y,...O?.axis?.x}),t(s,{orientation:"left",numTicks:4,...O?.axis?.y}),z.map(((r,l)=>{const n=r.options?.stroke??A.colors[l%A.colors.length];return e("g",{children:[S&&t(i,{id:`area-gradient-${M}-${l+1}`,from:n,fromOpacity:.4,toOpacity:.1,to:A.backgroundColor,...r.options?.gradient,"data-testid":"line-gradient"}),t(c,{dataKey:r?.label,data:r.data,...K,fill:S?`url(#area-gradient-${M}-${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:G,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","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","max","floor","min","length","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,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,aAAiB,CACjCM,MACAC,MAAOP,E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,CAAAC,SAAU,EAAQC,SAAW,IAC3C3E,EAAC4E,GACApC,YAAY,SACZmC,SAAW7B,EACX+B,WAAazE,KACRiB,GAASiB,MAAMK,IAErB3C,EAAC4E,EAAY,CAACpC,YAAY,OAAOmC,SAAW,KAAStD,GAASiB,MAAMC,IAElEb,EAAWpC,KAAK,CAAEwF,EAAYnB,KAC/B,MAAM3B,EAAS8C,EAAWzD,SAASW,QAAUF,EAAMM,OAAQuB,EAAQ7B,EAAMM,OAAOe,QAChF,OACCvD,EACG,IAAA,CAAAG,SAAA,CAAAmB,GACDlB,EAAC+E,EAAc,CACdC,GAAK,iBAAkBxD,KAAamC,EAAQ,IAC5CsB,KAAOjD,EACPkD,YAAc,GACdC,UAAY,GACZC,GAAKtD,EAAMuD,mBACNP,EAAWzD,SAASiE,SACb,cAAA,kBAGdtF,EAACuF,EAEA,CAAAC,QAAUV,GAAYlB,MACtBjD,KAAOmE,EAAWnE,QACbmD,EACL2B,KACCvE,EAAmB,sBAAuBM,KAAamC,EAAQ,UAAQrD,EAExEoF,YAAa,EACbC,MAAQxE,EAAYyE,EAAkBC,GARhCf,GAAYlB,SAbXkB,GAAYlB,OAASD,EAwB7B,IAGD5C,GACDf,EAAC8F,GACAC,qBAAmB,EACnBC,qBACA,EAAAC,oBACA7E,cAAgBA,OAKjBJ,GACDhB,EAACkG,EACA,CAAAC,MAAQ1C,EACRjB,YAAcvB,EACdpB,UAAYC,EAAQ,yBAItB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automattic/charts",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
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 = 60;
|
|
21
|
+
|
|
20
22
|
interface LineChartProps extends BaseChartProps< SeriesData[] > {
|
|
21
23
|
withGradientFill: boolean;
|
|
22
24
|
smoothing?: boolean;
|
|
@@ -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,25 @@ 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
|
+
() =>
|
|
145
|
+
Math.max( Math.floor( Math.min( dataSorted[ 0 ]?.data.length, width / X_TICK_WIDTH ) ), 5 ),
|
|
146
|
+
[ dataSorted, width ]
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const error = validateData( dataSorted );
|
|
132
150
|
if ( error ) {
|
|
133
151
|
return <div className={ clsx( 'line-chart', styles[ 'line-chart' ] ) }>{ error }</div>;
|
|
134
152
|
}
|
|
135
153
|
|
|
136
154
|
// Create legend items from group labels, this iterates over groups rather than data points
|
|
137
|
-
const legendItems =
|
|
155
|
+
const legendItems = dataSorted.map( ( group, index ) => ( {
|
|
138
156
|
label: group.label, // Label for each unique group
|
|
139
157
|
value: '', // Empty string since we don't want to show a specific value
|
|
140
158
|
color: providerTheme.colors[ index % providerTheme.colors.length ],
|
|
@@ -156,29 +174,30 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
156
174
|
theme={ theme }
|
|
157
175
|
width={ width }
|
|
158
176
|
height={ height }
|
|
159
|
-
margin={ { top:
|
|
177
|
+
margin={ { top: 10, right: 0, bottom: 20, left: 40, ...margin } }
|
|
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 = {
|