@automattic/charts 0.8.4 → 0.10.0
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 +12 -0
- package/dist/cjs/components/bar-chart/bar-chart.js +1 -1
- package/dist/cjs/components/bar-chart/bar-chart.js.map +1 -1
- 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 +25 -2
- package/dist/mjs/components/bar-chart/bar-chart.js +1 -1
- package/dist/mjs/components/bar-chart/bar-chart.js.map +1 -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/bar-chart/bar-chart.tsx +68 -38
- package/src/components/line-chart/line-chart.tsx +10 -0
- package/src/types.ts +18 -2
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,16 @@ 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.10.0] - 2025-03-17
|
|
9
|
+
### Changed
|
|
10
|
+
- Bar chart: align options with line chart [#42448]
|
|
11
|
+
- Update package dependencies. [#42163]
|
|
12
|
+
- Update package dependencies. [#42384]
|
|
13
|
+
|
|
14
|
+
## [0.9.0] - 2025-03-02
|
|
15
|
+
### Added
|
|
16
|
+
- Expose event handling for line chart [#42168]
|
|
17
|
+
|
|
8
18
|
## [0.8.4] - 2025-02-27
|
|
9
19
|
### Fixed
|
|
10
20
|
- Line chart: no need for min x tick num [#42087]
|
|
@@ -128,6 +138,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
128
138
|
- Fixed lints following ESLint rule changes for TS [#40584]
|
|
129
139
|
- Fixing a bug in Chart storybook data. [#40640]
|
|
130
140
|
|
|
141
|
+
[0.10.0]: https://github.com/Automattic/charts/compare/v0.9.0...v0.10.0
|
|
142
|
+
[0.9.0]: https://github.com/Automattic/charts/compare/v0.8.4...v0.9.0
|
|
131
143
|
[0.8.4]: https://github.com/Automattic/charts/compare/v0.8.3...v0.8.4
|
|
132
144
|
[0.8.3]: https://github.com/Automattic/charts/compare/v0.8.2...v0.8.3
|
|
133
145
|
[0.8.2]: https://github.com/Automattic/charts/compare/v0.8.1...v0.8.2
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("react/jsx-runtime"),a=require("@visx/axis"),t=require("@visx/event"),i=require("@visx/group"),r=require("@visx/scale"),
|
|
1
|
+
"use strict";var e=require("react/jsx-runtime"),a=require("@visx/axis"),t=require("@visx/event"),i=require("@visx/group"),r=require("@visx/scale"),o=require("@visx/shape"),l=require("@visx/tooltip"),s=require("clsx"),n=require("react"),d=require("../../providers/theme/theme-provider.js"),c=require("../grid-control/grid-control.js"),h=require("../legend/base-legend.js"),p=require("../shared/with-responsive.js"),u=require("../tooltip/base-tooltip.js"),x=require("./bar-chart.module.scss.js");const m=e=>new Date(e).toLocaleDateString(void 0,{month:"short",day:"numeric"}),v=a=>a?e.jsxs("div",{className:x["bar-chart__tooltip"],children:[e.jsx("div",{className:x["bar-chart__tooltip-header"],children:a.yLabel}),e.jsxs("div",{className:x["bar-chart__tooltip-row"],children:[e.jsxs("span",{className:x["bar-chart__tooltip-label"],children:[a.xLabel,":"]}),e.jsx("span",{className:x["bar-chart__tooltip-value"],children:a.value})]})]}):null;var b=p.withResponsive((({data:p,width:b,height:g=400,className:j,margin:q={top:20,right:20,bottom:40,left:40},withTooltips:N=!1,showLegend:w=!1,legendOrientation:L="horizontal",gridVisibility:_="x",renderTooltip:f=v,options:T={}})=>{const y=d.useChartTheme(),{tooltipOpen:B,tooltipLeft:k,tooltipTop:M,tooltipData:S,hideTooltip:D,showTooltip:A}=l.useTooltip(),C=T.axis?.x?.tickFormat??m;delete T.axis?.x?.tickFormat;const F=n.useCallback(((e,a,i,r,o)=>{const l=t.localPoint(e);l&&A({tooltipData:{value:a,xLabel:i,yLabel:r,seriesIndex:o},tooltipLeft:l.x,tooltipTop:l.y-10})}),[A]),G=(e=>e?.length?e.some((e=>e.data.some((e=>null===e.value||void 0===e.value||isNaN(e.value)||!e.label&&(!e.date||isNaN(e.date.getTime()))))))?"Invalid data":null:"No data available")(p);if(G)return e.jsx("div",{className:s("bar-chart",x["bar-chart"]),children:G});const I=q,O=b-I.left-I.right,V=g-I.top-I.bottom,$=p[0].data?.map((e=>e?.label||C(e?.date))),z=r.scaleBand({range:[0,O],domain:$,padding:.2}),P=r.scaleBand({range:[0,z.bandwidth()],domain:p.map(((e,a)=>a.toString())),padding:.1}),R=r.scaleLinear({range:[V,0],domain:[0,Math.max(...p.map((e=>Math.max(...e.data.map((e=>e?.value||0))))))]}),E=p.map(((e,a)=>({label:e.label,value:"",color:e.options?.stroke||y.colors[a%y.colors.length]})));return e.jsxs("div",{className:s("bar-chart",x["bar-chart"],j),"data-testid":"bar-chart",role:"img","aria-label":"bar chart",children:[e.jsx("svg",{width:b,height:g,children:e.jsxs(i.Group,{left:I.left,top:I.top,children:[e.jsx(c,{width:O,height:V,xScale:z,yScale:R,gridVisibility:_}),p.map(((a,t)=>e.jsx(i.Group,{children:a.data.map((i=>{const r=i?.label||C(i?.date),l=z(r);if(void 0===l)return null;const s=P.bandwidth(),n=l+(P(t.toString())??0),d=a.options?.stroke||y.colors[t%y.colors.length];return e.jsx(o.Bar,{x:n,y:R(i.value),width:s,height:V-(R(i.value)??0),fill:d,onMouseMove:N?e=>F(e,i.value,r,a.label,t):void 0,onMouseLeave:N?D:void 0},`bar-${t}-${r}`)}))},t))),e.jsx(a.AxisLeft,{scale:R}),e.jsx(a.AxisBottom,{scale:z,top:V})]})}),N&&B&&S&&e.jsx(u.BaseTooltip,{top:M||0,left:k||0,children:f(S)}),w&&e.jsx(h.BaseLegend,{items:E,orientation:L,className:x["bar-chart__legend"]})]})}));module.exports=b;
|
|
2
2
|
//# sourceMappingURL=bar-chart.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bar-chart.js","sources":["/@automattic/charts/../../../../src/components/bar-chart/bar-chart.tsx"],"sourcesContent":[null],"names":["
|
|
1
|
+
{"version":3,"file":"bar-chart.js","sources":["/@automattic/charts/../../../../src/components/bar-chart/bar-chart.tsx"],"sourcesContent":[null],"names":["formatDateTick","timestamp","Date","toLocaleDateString","undefined","month","day","renderDefaultTooltip","tooltipData","_jsxs","className","styles","children","_jsx","yLabel","jsxs","xLabel","value","withResponsive","data","width","height","margin","top","right","bottom","left","withTooltips","showLegend","legendOrientation","gridVisibility","renderTooltip","options","theme","useChartTheme","tooltipOpen","tooltipLeft","tooltipTop","hideTooltip","showTooltip","useTooltip","tickFormat","axis","x","handleMouseMove","useCallback","event","seriesIndex","coords","localPoint","y","error","length","some","series","d","isNaN","label","date","getTime","validateData","clsx","margins","xMax","yMax","labels","map","xScale","scaleBand","range","domain","padding","innerScale","bandwidth","_","i","toString","yScale","scaleLinear","Math","max","legendItems","group","index","color","stroke","colors","role","Group","GridControl","xPos","barWidth","barX","barColor","Bar","fill","onMouseMove","onMouseLeave","jsx","AxisLeft","scale","AxisBottom","BaseTooltip","Legend","BaseLegend","items","orientation"],"mappings":"8eA2BA,MAAMA,EAAmBC,GACX,IAAIC,KAAMD,GACXE,wBAAoBC,EAAW,CAC1CC,MAAO,QACPC,IAAK,YAKDC,EAAyBC,GACvBA,EAGNC,OAAK,MAAA,CAAAC,UAAYC,EAAQ,sBAAsBC,SAAA,CAC9CC,EAAAA,IAAK,MAAA,CAAAH,UAAYC,EAAQ,6BAA6BC,SAAKJ,EAAYM,SACvEL,EAAAA,KAAA,MAAA,CAAKC,UAAYC,EAAQ,oCACxBF,EAAMM,KAAA,OAAA,CAAAL,UAAYC,EAAQ,4BAA4BC,SAAA,CAAKJ,EAAYQ,OAAM,OAC7EH,EAAAA,I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AAOF,GAASlB,EAAYzB,EAAY6B,aAAgB,GACxDiB,EACLvC,EAAOtB,SAASqD,QAAUpD,EAAMqD,OAAQvC,EAAcd,EAAMqD,OAAOlC,QAIpE,OACCvC,EAAAA,IAACiF,EAAAA,IAAG,CAEHnD,EAAIiD,EACJ1C,EAAI2B,EAAQtB,EAAEtC,OACdG,MAAQuE,EACRtE,OAAS2C,GAASa,EAAQtB,EAAEtC,QAAW,GACvC8E,KAAOF,EACPG,YAAcrE,EAXamB,GAC5BF,EAAiBE,EAAOS,EAAEtC,MAAOD,EAAQsC,EAAOG,MAAOV,QAUJ3C,EAClD6F,aAAetE,EAAeW,OAAclC,GAPtC,OAAQ2C,KAAiB/B,IAS/B,KAxBS+B,KA4BdlC,EAAAqF,IAACC,WAAQ,CAACC,MAAQvB,IAClBhE,EAAAqF,IAACG,EAAUA,WAAC,CAAAD,MAAQjC,EAAS5C,IAAMyC,SAInCrC,GAAgBQ,GAAe3B,GAChCK,EAACqF,IAAAI,EAAWA,aAAC/E,IAAMc,GAAc,EAAIX,KAAOU,GAAe,EAACxB,SACzDmB,EAAevB,KAIjBoB,GACDf,EAAAA,IAAC0F,EAAMC,WAAA,CACNC,MAAQxB,EACRyB,YAAc7E,EACdnB,UAAYC,EAAQ,yBAItB"}
|
|
@@ -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"),o=require("react"),
|
|
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"),n=require("../../providers/theme/theme-provider.js"),s=require("../legend/base-legend.js"),l=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=l.withResponsive((({data:l,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={},onPointerDown:w,onPointerUp:q,onPointerMove:k,onPointerOut:D})=>{const _=n.useChartTheme(),f=o.useId(),A=o.useMemo((()=>l.map((e=>({...e,data:e.data.sort(((e,t)=>e.date.getTime()-t.date.getTime()))})))),[l]),M=o.useMemo((()=>{const e=A?.map((e=>e.options?.stroke??"")).filter(Boolean)??[];return i.buildChartTheme({..._,colors:[...e,..._.colors]})}),[_,A]);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 P=o.useMemo((()=>Math.min(A[0]?.data.length,Math.ceil(u/100))),[A,u]),S=(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")(A);if(S)return e.jsx("div",{className:r("line-chart",c["line-chart"]),children:S});const L=A.map(((e,t)=>({label:e.label,value:"",color:_.colors[t%_.colors.length]}))),O={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:M,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},onPointerDown:w,onPointerUp:q,onPointerMove:k,onPointerOut:D,pointerEventsDataKey:"nearest",children:[e.jsx(i.AnimatedGrid,{columns:!1,numTicks:4}),e.jsx(i.AnimatedAxis,{orientation:"bottom",numTicks:P,tickFormat:m,...N?.axis?.x}),e.jsx(i.AnimatedAxis,{orientation:"left",numTicks:4,...N?.axis?.y}),A.map(((r,o)=>{const n=r.options?.stroke??M.colors[o%M.colors.length];return e.jsxs("g",{children:[y&&e.jsx(a.LinearGradient,{id:`area-gradient-${f}-${o+1}`,from:n,fromOpacity:.4,toOpacity:.1,to:M.backgroundColor,...r.options?.gradient,"data-testid":"line-gradient"}),e.jsx(i.AnimatedAreaSeries,{dataKey:r?.label,data:r.data,...O,fill:y?`url(#area-gradient-${f}-${o+1})`:void 0,renderLine:!0,curve:T?t.curveCatmullRom:t.curveLinear},r?.label)]},r?.label||o)})),x&&e.jsx(i.Tooltip,{detectBounds:!0,snapTooltipToDatumX:!0,snapTooltipToDatumY:!0,showSeriesGlyphs:!0,renderTooltip:b})]}),g&&e.jsx(s.BaseLegend,{items:L,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","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","
|
|
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","onPointerDown","onPointerUp","onPointerMove","onPointerOut","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","pointerEventsDataKey","jsx","AnimatedGrid","columns","numTicks","AnimatedAxis","tickFormat","seriesData","LinearGradient","id","from","fromOpacity","toOpacity","to","backgroundColor","gradient","AnimatedAreaSeries","dataKey","fill","renderLine","curve","curveCatmullRom","curveLinear","Tooltip","detectBounds","snapTooltipToDatumX","snapTooltipToDatumY","showSeriesGlyphs","Legend","BaseLegend","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,QAE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,8 +1,8 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { Orientation } from '@visx/axis';
|
|
3
3
|
import { ScaleType, scaleOrdinal } from '@visx/scale';
|
|
4
|
-
import { LineStyles } from '@visx/xychart';
|
|
5
|
-
import { CSSProperties, ReactNode, ComponentType, FC } from 'react';
|
|
4
|
+
import { LineStyles, EventHandlerParams } from '@visx/xychart';
|
|
5
|
+
import { CSSProperties, PointerEvent, ReactNode, ComponentType, FC } from 'react';
|
|
6
6
|
import { RenderTooltipParams } from '@visx/xychart/lib/components/Tooltip';
|
|
7
7
|
|
|
8
8
|
type ValueOf<T> = T[keyof T];
|
|
@@ -118,6 +118,22 @@ type BaseChartProps<T = DataPoint | DataPointDate> = {
|
|
|
118
118
|
bottom?: number;
|
|
119
119
|
left?: number;
|
|
120
120
|
};
|
|
121
|
+
/**
|
|
122
|
+
* Callback function for pointer down event
|
|
123
|
+
*/
|
|
124
|
+
onPointerDown?: (event: EventHandlerParams<object>) => void;
|
|
125
|
+
/**
|
|
126
|
+
* Callback function for pointer down event
|
|
127
|
+
*/
|
|
128
|
+
onPointerUp?: (event: EventHandlerParams<object>) => void;
|
|
129
|
+
/**
|
|
130
|
+
* Callback function for pointer down event
|
|
131
|
+
*/
|
|
132
|
+
onPointerMove?: (event: EventHandlerParams<object>) => void;
|
|
133
|
+
/**
|
|
134
|
+
* Callback function for pointer up event
|
|
135
|
+
*/
|
|
136
|
+
onPointerOut?: (event: PointerEvent<Element>) => void;
|
|
121
137
|
/**
|
|
122
138
|
* Whether to show tooltips on hover. False by default.
|
|
123
139
|
*/
|
|
@@ -154,7 +170,14 @@ type BaseChartProps<T = DataPoint | DataPointDate> = {
|
|
|
154
170
|
};
|
|
155
171
|
};
|
|
156
172
|
|
|
173
|
+
type BarChartTooltipData = {
|
|
174
|
+
value: number;
|
|
175
|
+
xLabel: string;
|
|
176
|
+
yLabel: string;
|
|
177
|
+
seriesIndex: number;
|
|
178
|
+
};
|
|
157
179
|
interface BarChartProps extends BaseChartProps<SeriesData[]> {
|
|
180
|
+
renderTooltip?: (params: BarChartTooltipData) => ReactNode;
|
|
158
181
|
}
|
|
159
182
|
declare const _default$3: (props: Optional<BarChartProps, "height" | "width" | "size">) => react_jsx_runtime.JSX.Element;
|
|
160
183
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsx as t,jsxs as a}from"react/jsx-runtime";import{AxisLeft as
|
|
1
|
+
import{jsx as t,jsxs as a}from"react/jsx-runtime";import{AxisLeft as o,AxisBottom as e}from"@visx/axis";import{localPoint as i}from"@visx/event";import{Group as r}from"@visx/group";import{scaleBand as l,scaleLinear as s}from"@visx/scale";import{Bar as n}from"@visx/shape";import{useTooltip as d}from"@visx/tooltip";import m from"clsx";import{useCallback as p}from"react";import{useChartTheme as c}from"../../providers/theme/theme-provider.js";import h from"../grid-control/grid-control.js";import{BaseLegend as v}from"../legend/base-legend.js";import{withResponsive as b}from"../shared/with-responsive.js";import{BaseTooltip as g}from"../tooltip/base-tooltip.js";import f from"./bar-chart.module.scss.js";const u=t=>new Date(t).toLocaleDateString(void 0,{month:"short",day:"numeric"}),x=o=>o?a("div",{className:f["bar-chart__tooltip"],children:[t("div",{className:f["bar-chart__tooltip-header"],children:o.yLabel}),a("div",{className:f["bar-chart__tooltip-row"],children:[a("span",{className:f["bar-chart__tooltip-label"],children:[o.xLabel,":"]}),t("span",{className:f["bar-chart__tooltip-value"],children:o.value})]})]}):null;var N=b((({data:b,width:N,height:w=400,className:_,margin:L={top:20,right:20,bottom:40,left:40},withTooltips:y=!1,showLegend:j=!1,legendOrientation:T="horizontal",gridVisibility:M="x",renderTooltip:S=x,options:k={}})=>{const D=c(),{tooltipOpen:F,tooltipLeft:I,tooltipTop:O,tooltipData:V,hideTooltip:$,showTooltip:z}=d(),q=k.axis?.x?.tickFormat??u;delete k.axis?.x?.tickFormat;const A=p(((t,a,o,e,r)=>{const l=i(t);l&&z({tooltipData:{value:a,xLabel:o,yLabel:e,seriesIndex:r},tooltipLeft:l.x,tooltipTop:l.y-10})}),[z]),B=(t=>t?.length?t.some((t=>t.data.some((t=>null===t.value||void 0===t.value||isNaN(t.value)||!t.label&&(!t.date||isNaN(t.date.getTime()))))))?"Invalid data":null:"No data available")(b);if(B)return t("div",{className:m("bar-chart",f["bar-chart"]),children:B});const C=L,E=N-C.left-C.right,G=w-C.top-C.bottom,H=b[0].data?.map((t=>t?.label||q(t?.date))),J=l({range:[0,E],domain:H,padding:.2}),K=l({range:[0,J.bandwidth()],domain:b.map(((t,a)=>a.toString())),padding:.1}),P=s({range:[G,0],domain:[0,Math.max(...b.map((t=>Math.max(...t.data.map((t=>t?.value||0))))))]}),Q=b.map(((t,a)=>({label:t.label,value:"",color:t.options?.stroke||D.colors[a%D.colors.length]})));return a("div",{className:m("bar-chart",f["bar-chart"],_),"data-testid":"bar-chart",role:"img","aria-label":"bar chart",children:[t("svg",{width:N,height:w,children:a(r,{left:C.left,top:C.top,children:[t(h,{width:E,height:G,xScale:J,yScale:P,gridVisibility:M}),b.map(((a,o)=>t(r,{children:a.data.map((e=>{const i=e?.label||q(e?.date),r=J(i);if(void 0===r)return null;const l=K.bandwidth(),s=r+(K(o.toString())??0),d=a.options?.stroke||D.colors[o%D.colors.length];return t(n,{x:s,y:P(e.value),width:l,height:G-(P(e.value)??0),fill:d,onMouseMove:y?t=>A(t,e.value,i,a.label,o):void 0,onMouseLeave:y?$:void 0},`bar-${o}-${i}`)}))},o))),t(o,{scale:P}),t(e,{scale:J,top:G})]})}),y&&F&&V&&t(g,{top:O||0,left:I||0,children:S(V)}),j&&t(v,{items:Q,orientation:T,className:f["bar-chart__legend"]})]})}));export{N as default};
|
|
2
2
|
//# sourceMappingURL=bar-chart.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bar-chart.js","sources":["../../../../src/components/bar-chart/bar-chart.tsx"],"sourcesContent":[null],"names":["
|
|
1
|
+
{"version":3,"file":"bar-chart.js","sources":["../../../../src/components/bar-chart/bar-chart.tsx"],"sourcesContent":[null],"names":["formatDateTick","timestamp","Date","toLocaleDateString","undefined","month","day","renderDefaultTooltip","tooltipData","_jsxs","className","styles","children","_jsx","yLabel","xLabel","value","barChart","withResponsive","data","width","height","margin","top","right","bottom","left","withTooltips","showLegend","legendOrientation","gridVisibility","renderTooltip","options","theme","useChartTheme","tooltipOpen","tooltipLeft","tooltipTop","hideTooltip","showTooltip","useTooltip","tickFormat","axis","x","handleMouseMove","useCallback","event","seriesIndex","coords","localPoint","y","error","length","some","series","d","isNaN","label","date","getTime","validateData","clsx","margins","xMax","yMax","labels","map","xScale","scaleBand","range","domain","padding","innerScale","bandwidth","_","i","toString","yScale","scaleLinear","Math","max","legendItems","group","index","color","stroke","colors","role","Group","GridControl","xPos","barWidth","barX","barColor","Bar","fill","onMouseMove","onMouseLeave","AxisLeft","scale","AxisBottom","BaseTooltip","Legend","items","orientation"],"mappings":"isBA2BA,MAAMA,EAAmBC,GACX,IAAIC,KAAMD,GACXE,wBAAoBC,EAAW,CAC1CC,MAAO,QACPC,IAAK,YAKDC,EAAyBC,GACvBA,EAGNC,EAAK,MAAA,CAAAC,UAAYC,EAAQ,sBAAsBC,SAAA,CAC9CC,EAAK,MAAA,CAAAH,UAAYC,EAAQ,6BAA6BC,SAAKJ,EAAYM,SACvEL,EAAA,MAAA,CAAKC,UAAYC,EAAQ,oCACxBF,EAAM,OAAA,CAAAC,UAAYC,EAAQ,4BAA4BC,SAAA,CAAKJ,EAAYO,OAAM,OAC7EF,EAAA,OAAA,CAAMH,UAAYC,EAAQ,4BAAiCC,SAAAJ,EAAYQ,cAP9C,KAmL7B,IAAeC,EAAAC,GApJuB,EACrCC,OACAC,QACAC,SAAS,IACTX,YACAY,SAAS,CAAEC,IAAK,GAAIC,MAAO,GAAIC,OAAQ,GAAIC,KAAM,IACjDC,gBAAe,EACfC,cAAa,EACbC,oBAAoB,aACpBC,iBAAiB,IACjBC,gBAAgBxB,EAChByB,U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3C,EAClD6F,aAAetE,EAAeW,OAAclC,GAPtC,OAAQ2C,KAAiBhC,IAS/B,KAxBSgC,KA4BdlC,EAACqF,EAAQ,CAACC,MAAQtB,IAClBhE,EAACuF,EAAW,CAAAD,MAAQhC,EAAS5C,IAAMyC,SAInCrC,GAAgBQ,GAAe3B,GAChCK,EAACwF,GAAY9E,IAAMc,GAAc,EAAIX,KAAOU,GAAe,EAACxB,SACzDmB,EAAevB,KAIjBoB,GACDf,EAACyF,EAAM,CACNC,MAAQtB,EACRuB,YAAc3E,EACdnB,UAAYC,EAAQ,yBAItB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e}from"react/jsx-runtime";import{curveCatmullRom as
|
|
1
|
+
import{jsx as t,jsxs as e}from"react/jsx-runtime";import{curveCatmullRom as o,curveLinear as a}from"@visx/curve";import{LinearGradient as i}from"@visx/gradient";import{buildChartTheme as r,XYChart as n,AnimatedGrid as l,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:o})=>{const a=o?.nearestDatum?.datum;if(!a)return null;const i=Object.entries(o?.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:a.date?.toLocaleDateString()}),i.map((o=>e("div",{className:f["line-chart__tooltip-row"],children:[e("span",{className:f["line-chart__tooltip-label"],children:[o.key,":"]}),t("span",{className:f["line-chart__tooltip-value"],children:o.value})]},o.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:w,withTooltips:k=!0,showLegend:D=!1,legendOrientation:_="horizontal",withGradientFill:P=!1,smoothing:S=!0,renderTooltip:j=y,options:O={},onPointerDown:L,onPointerUp:M,onPointerMove:$,onPointerOut:B})=>{const K=u(),z=h(),A=p((()=>g.map((t=>({...t,data:t.data.sort(((t,e)=>t.date.getTime()-e.date.getTime()))})))),[g]),F=p((()=>{const t=A?.map((t=>t.options?.stroke??"")).filter(Boolean)??[];return r({...K,colors:[...t,...K.colors]})}),[K,A]);w=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,...w}}),[w,O]);const G=p((()=>Math.min(A[0]?.data.length,Math.ceil(b/100))),[A,b]),U=(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(U)return t("div",{className:d("line-chart",f["line-chart"]),children:U});const C=A.map(((t,e)=>({label:t.label,value:"",color:K.colors[e%K.colors.length]}))),E={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(n,{theme:F,width:b,height:N,margin:{top:10,right:0,bottom:20,left:40,...w},xScale:{type:"time",...O?.xScale},yScale:{type:"linear",nice:!0,zero:!1,...O?.yScale},onPointerDown:L,onPointerUp:M,onPointerMove:$,onPointerOut:B,pointerEventsDataKey:"nearest",children:[t(l,{columns:!1,numTicks:4}),t(s,{orientation:"bottom",numTicks:G,tickFormat:x,...O?.axis?.x}),t(s,{orientation:"left",numTicks:4,...O?.axis?.y}),A.map(((r,n)=>{const l=r.options?.stroke??F.colors[n%F.colors.length];return e("g",{children:[P&&t(i,{id:`area-gradient-${z}-${n+1}`,from:l,fromOpacity:.4,toOpacity:.1,to:F.backgroundColor,...r.options?.gradient,"data-testid":"line-gradient"}),t(c,{dataKey:r?.label,data:r.data,...E,fill:P?`url(#area-gradient-${z}-${n+1})`:void 0,renderLine:!0,curve:S?o:a},r?.label)]},r?.label||n)})),k&&t(m,{detectBounds:!0,snapTooltipToDatumX:!0,snapTooltipToDatumY:!0,showSeriesGlyphs:!0,renderTooltip:j})]}),D&&t(v,{items:C,orientation:_,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","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,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,EAAK,MAAA,CAAAC,UAAYC,EAAQ,uBACxBC,SAAA,CAAAC,EAAA,MAAA,CAAKH,UAAYC,EAAQ,4BACtBC,SAAAf,EAAaiB,MAAMC,uBAEpBhB,EAAcI,KAAKa,GACpBP,EAAA,MAAA,CAAuBC,UAAYC,EAAQ,2BAC1CC,SAAA,CAAAH,EAAA,OAAA,CAAMC,UAAYC,EAAQ,6BAA6BC,SAAA,CAAKI,EAAMZ,IAAa,OAC/ES,UAAMH,UAAYC,EAAQ,6BAA6BC,SAAKI,EAAMX,UAFxDW,EAAMZ,SAMlB,EAGGa,EAAmBC,GACX,IAAIC,KAAMD,GACXH,wBAAoBK,EAAW,CAC1CC,MAAO,QACPC,IAAK,
|
|
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","onPointerDown","onPointerUp","onPointerMove","onPointerOut","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","pointerEventsDataKey","AnimatedGrid","columns","numTicks","AnimatedAxis","tickFormat","seriesData","LinearGradient","id","from","fromOpacity","toOpacity","to","backgroundColor","gradient","AnimatedAreaSeries","dataKey","fill","renderLine","curve","curveCatmullRom","curveLinear","Tooltip","detectBounds","snapTooltipToDatumX","snapTooltipToDatumY","showSeri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drB,UAAYC,EAAQ,yBAItB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automattic/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
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": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"./style.css": "./dist/mjs/style.css"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@babel/runtime": "7.26.
|
|
49
|
+
"@babel/runtime": "7.26.10",
|
|
50
50
|
"@react-spring/web": "9.7.3",
|
|
51
51
|
"@visx/axis": "^3.12.0",
|
|
52
52
|
"@visx/curve": "3.12.0",
|
|
@@ -87,8 +87,8 @@
|
|
|
87
87
|
"@types/jest": "^29.0.0",
|
|
88
88
|
"@types/react": "18.3.18",
|
|
89
89
|
"@types/react-dom": "18.3.5",
|
|
90
|
-
"@wordpress/babel-plugin-import-jsx-pragma": "5.
|
|
91
|
-
"@wordpress/element": "6.
|
|
90
|
+
"@wordpress/babel-plugin-import-jsx-pragma": "5.19.0",
|
|
91
|
+
"@wordpress/element": "6.19.0",
|
|
92
92
|
"babel-jest": "^29.7.0",
|
|
93
93
|
"babel-loader": "9.1.2",
|
|
94
94
|
"clean-webpack-plugin": "^4.0.0",
|
|
@@ -5,7 +5,7 @@ import { scaleBand, scaleLinear } from '@visx/scale';
|
|
|
5
5
|
import { Bar } from '@visx/shape';
|
|
6
6
|
import { useTooltip } from '@visx/tooltip';
|
|
7
7
|
import clsx from 'clsx';
|
|
8
|
-
import { FC, useCallback, type MouseEvent } from 'react';
|
|
8
|
+
import { FC, useCallback, type MouseEvent, ReactNode } from 'react';
|
|
9
9
|
import { useChartTheme } from '../../providers/theme';
|
|
10
10
|
import { GridControl } from '../grid-control';
|
|
11
11
|
import { Legend } from '../legend';
|
|
@@ -21,22 +21,69 @@ type BarChartTooltipData = {
|
|
|
21
21
|
seriesIndex: number;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
interface BarChartProps extends BaseChartProps< SeriesData[] > {
|
|
24
|
+
interface BarChartProps extends BaseChartProps< SeriesData[] > {
|
|
25
|
+
renderTooltip?: ( params: BarChartTooltipData ) => ReactNode;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const formatDateTick = ( timestamp: number ) => {
|
|
29
|
+
const date = new Date( timestamp );
|
|
30
|
+
return date.toLocaleDateString( undefined, {
|
|
31
|
+
month: 'short',
|
|
32
|
+
day: 'numeric',
|
|
33
|
+
} );
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Default tooltip renderer
|
|
37
|
+
const renderDefaultTooltip = ( tooltipData: BarChartTooltipData ) => {
|
|
38
|
+
if ( ! tooltipData ) return null;
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div className={ styles[ 'bar-chart__tooltip' ] }>
|
|
42
|
+
<div className={ styles[ 'bar-chart__tooltip-header' ] }>{ tooltipData.yLabel }</div>
|
|
43
|
+
<div className={ styles[ 'bar-chart__tooltip-row' ] }>
|
|
44
|
+
<span className={ styles[ 'bar-chart__tooltip-label' ] }>{ tooltipData.xLabel }:</span>
|
|
45
|
+
<span className={ styles[ 'bar-chart__tooltip-value' ] }>{ tooltipData.value }</span>
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Validation function similar to LineChart
|
|
52
|
+
const validateData = ( data: SeriesData[] ) => {
|
|
53
|
+
if ( ! data?.length ) return 'No data available';
|
|
54
|
+
|
|
55
|
+
const hasInvalidData = data.some( series =>
|
|
56
|
+
series.data.some(
|
|
57
|
+
d =>
|
|
58
|
+
d.value === null ||
|
|
59
|
+
d.value === undefined ||
|
|
60
|
+
isNaN( d.value ) ||
|
|
61
|
+
( ! d.label && ( ! d.date || isNaN( d.date.getTime() ) ) )
|
|
62
|
+
)
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if ( hasInvalidData ) return 'Invalid data';
|
|
66
|
+
return null;
|
|
67
|
+
};
|
|
25
68
|
|
|
26
69
|
const BarChart: FC< BarChartProps > = ( {
|
|
27
70
|
data,
|
|
71
|
+
width,
|
|
72
|
+
height = 400,
|
|
73
|
+
className,
|
|
28
74
|
margin = { top: 20, right: 20, bottom: 40, left: 40 },
|
|
29
75
|
withTooltips = false,
|
|
30
76
|
showLegend = false,
|
|
31
77
|
legendOrientation = 'horizontal',
|
|
32
|
-
className,
|
|
33
78
|
gridVisibility = 'x',
|
|
34
|
-
|
|
35
|
-
|
|
79
|
+
renderTooltip = renderDefaultTooltip,
|
|
80
|
+
options = {},
|
|
36
81
|
} ) => {
|
|
37
82
|
const theme = useChartTheme();
|
|
38
83
|
const { tooltipOpen, tooltipLeft, tooltipTop, tooltipData, hideTooltip, showTooltip } =
|
|
39
84
|
useTooltip< BarChartTooltipData >();
|
|
85
|
+
const tickFormat = options.axis?.x?.tickFormat ?? formatDateTick;
|
|
86
|
+
delete options.axis?.x?.tickFormat;
|
|
40
87
|
|
|
41
88
|
const handleMouseMove = useCallback(
|
|
42
89
|
(
|
|
@@ -57,25 +104,10 @@ const BarChart: FC< BarChartProps > = ( {
|
|
|
57
104
|
[ showTooltip ]
|
|
58
105
|
);
|
|
59
106
|
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
// Add date validation to hasInvalidData check
|
|
66
|
-
const hasInvalidData = data.some( series =>
|
|
67
|
-
series.data.some(
|
|
68
|
-
d =>
|
|
69
|
-
d.value === null ||
|
|
70
|
-
d.value === undefined ||
|
|
71
|
-
isNaN( d.value ) ||
|
|
72
|
-
! d.label ||
|
|
73
|
-
( d.date && isNaN( d.date.getTime() ) ) // Add date validation
|
|
74
|
-
)
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
if ( hasInvalidData ) {
|
|
78
|
-
return <div className={ clsx( styles[ 'bar-chart-error' ] ) }>Invalid data</div>;
|
|
107
|
+
// Validate data using the same pattern as LineChart
|
|
108
|
+
const error = validateData( data );
|
|
109
|
+
if ( error ) {
|
|
110
|
+
return <div className={ clsx( 'bar-chart', styles[ 'bar-chart' ] ) }>{ error }</div>;
|
|
79
111
|
}
|
|
80
112
|
|
|
81
113
|
const margins = margin;
|
|
@@ -83,7 +115,9 @@ const BarChart: FC< BarChartProps > = ( {
|
|
|
83
115
|
const yMax = height - margins.top - margins.bottom;
|
|
84
116
|
|
|
85
117
|
// Get labels for x-axis from the first series (assuming all series have same labels)
|
|
86
|
-
const labels = data[ 0 ].data?.map( d =>
|
|
118
|
+
const labels = data[ 0 ].data?.map( d => {
|
|
119
|
+
return d?.label || tickFormat( d?.date );
|
|
120
|
+
} );
|
|
87
121
|
|
|
88
122
|
// Create scales
|
|
89
123
|
const xScale = scaleBand< string >( {
|
|
@@ -110,7 +144,7 @@ const BarChart: FC< BarChartProps > = ( {
|
|
|
110
144
|
const legendItems = data.map( ( group, index ) => ( {
|
|
111
145
|
label: group.label, // Label for each unique group
|
|
112
146
|
value: '', // Empty string since we don't want to show a specific value
|
|
113
|
-
color: theme.colors[ index % theme.colors.length ],
|
|
147
|
+
color: group.options?.stroke || theme.colors[ index % theme.colors.length ],
|
|
114
148
|
} ) );
|
|
115
149
|
|
|
116
150
|
return (
|
|
@@ -132,23 +166,25 @@ const BarChart: FC< BarChartProps > = ( {
|
|
|
132
166
|
{ data.map( ( series, seriesIndex ) => (
|
|
133
167
|
<Group key={ seriesIndex }>
|
|
134
168
|
{ series.data.map( d => {
|
|
135
|
-
const
|
|
169
|
+
const xLabel = d?.label || tickFormat( d?.date );
|
|
170
|
+
const xPos = xScale( xLabel );
|
|
136
171
|
if ( xPos === undefined ) return null;
|
|
137
172
|
|
|
138
173
|
const barWidth = innerScale.bandwidth();
|
|
139
174
|
const barX = xPos + ( innerScale( seriesIndex.toString() ) ?? 0 );
|
|
140
|
-
|
|
175
|
+
const barColor =
|
|
176
|
+
series.options?.stroke || theme.colors[ seriesIndex % theme.colors.length ];
|
|
141
177
|
const handleBarMouseMove = ( event: MouseEvent< SVGRectElement > ) =>
|
|
142
|
-
handleMouseMove( event, d.value,
|
|
178
|
+
handleMouseMove( event, d.value, xLabel, series.label, seriesIndex );
|
|
143
179
|
|
|
144
180
|
return (
|
|
145
181
|
<Bar
|
|
146
|
-
key={ `bar-${ seriesIndex }-${
|
|
182
|
+
key={ `bar-${ seriesIndex }-${ xLabel }` }
|
|
147
183
|
x={ barX }
|
|
148
184
|
y={ yScale( d.value ) }
|
|
149
185
|
width={ barWidth }
|
|
150
186
|
height={ yMax - ( yScale( d.value ) ?? 0 ) }
|
|
151
|
-
fill={
|
|
187
|
+
fill={ barColor }
|
|
152
188
|
onMouseMove={ withTooltips ? handleBarMouseMove : undefined }
|
|
153
189
|
onMouseLeave={ withTooltips ? hideTooltip : undefined }
|
|
154
190
|
/>
|
|
@@ -163,12 +199,7 @@ const BarChart: FC< BarChartProps > = ( {
|
|
|
163
199
|
|
|
164
200
|
{ withTooltips && tooltipOpen && tooltipData && (
|
|
165
201
|
<BaseTooltip top={ tooltipTop || 0 } left={ tooltipLeft || 0 }>
|
|
166
|
-
|
|
167
|
-
<div>{ tooltipData.yLabel }</div>
|
|
168
|
-
<div>
|
|
169
|
-
{ tooltipData.xLabel }: { tooltipData.value }
|
|
170
|
-
</div>
|
|
171
|
-
</div>
|
|
202
|
+
{ renderTooltip( tooltipData ) }
|
|
172
203
|
</BaseTooltip>
|
|
173
204
|
) }
|
|
174
205
|
|
|
@@ -183,5 +214,4 @@ const BarChart: FC< BarChartProps > = ( {
|
|
|
183
214
|
);
|
|
184
215
|
};
|
|
185
216
|
|
|
186
|
-
BarChart.displayName = 'BarChart';
|
|
187
217
|
export default withResponsive< BarChartProps >( BarChart );
|
|
@@ -104,6 +104,10 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
104
104
|
smoothing = true,
|
|
105
105
|
renderTooltip = renderDefaultTooltip,
|
|
106
106
|
options = {},
|
|
107
|
+
onPointerDown = undefined,
|
|
108
|
+
onPointerUp = undefined,
|
|
109
|
+
onPointerMove = undefined,
|
|
110
|
+
onPointerOut = undefined,
|
|
107
111
|
} ) => {
|
|
108
112
|
const providerTheme = useChartTheme();
|
|
109
113
|
const chartId = useId(); // Ensure unique ids for gradient fill.
|
|
@@ -177,6 +181,11 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
177
181
|
// xScale and yScale could be set in Axis as well, but they are `scale` props there.
|
|
178
182
|
xScale={ { type: 'time', ...options?.xScale } }
|
|
179
183
|
yScale={ { type: 'linear', nice: true, zero: false, ...options?.yScale } }
|
|
184
|
+
onPointerDown={ onPointerDown }
|
|
185
|
+
onPointerUp={ onPointerUp }
|
|
186
|
+
onPointerMove={ onPointerMove }
|
|
187
|
+
onPointerOut={ onPointerOut }
|
|
188
|
+
pointerEventsDataKey="nearest"
|
|
180
189
|
>
|
|
181
190
|
<AnimatedGrid columns={ false } numTicks={ 4 } />
|
|
182
191
|
<AnimatedAxis
|
|
@@ -219,6 +228,7 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
219
228
|
|
|
220
229
|
{ withTooltips && (
|
|
221
230
|
<Tooltip
|
|
231
|
+
detectBounds
|
|
222
232
|
snapTooltipToDatumX
|
|
223
233
|
snapTooltipToDatumY
|
|
224
234
|
showSeriesGlyphs
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Orientation } from '@visx/axis';
|
|
2
2
|
import { ScaleType } from '@visx/scale';
|
|
3
|
-
import { LineStyles } from '@visx/xychart';
|
|
4
|
-
import type { CSSProperties } from 'react';
|
|
3
|
+
import { EventHandlerParams, LineStyles } from '@visx/xychart';
|
|
4
|
+
import type { CSSProperties, PointerEvent } from 'react';
|
|
5
5
|
|
|
6
6
|
type ValueOf< T > = T[ keyof T ];
|
|
7
7
|
|
|
@@ -125,6 +125,22 @@ export type BaseChartProps< T = DataPoint | DataPointDate > = {
|
|
|
125
125
|
bottom?: number;
|
|
126
126
|
left?: number;
|
|
127
127
|
};
|
|
128
|
+
/**
|
|
129
|
+
* Callback function for pointer down event
|
|
130
|
+
*/
|
|
131
|
+
onPointerDown?: ( event: EventHandlerParams< object > ) => void;
|
|
132
|
+
/**
|
|
133
|
+
* Callback function for pointer down event
|
|
134
|
+
*/
|
|
135
|
+
onPointerUp?: ( event: EventHandlerParams< object > ) => void;
|
|
136
|
+
/**
|
|
137
|
+
* Callback function for pointer down event
|
|
138
|
+
*/
|
|
139
|
+
onPointerMove?: ( event: EventHandlerParams< object > ) => void;
|
|
140
|
+
/**
|
|
141
|
+
* Callback function for pointer up event
|
|
142
|
+
*/
|
|
143
|
+
onPointerOut?: ( event: PointerEvent< Element > ) => void;
|
|
128
144
|
/**
|
|
129
145
|
* Whether to show tooltips on hover. False by default.
|
|
130
146
|
*/
|