@automattic/charts 0.8.3 → 0.9.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 +10 -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 +18 -2
- 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 +1 -1
- package/src/components/line-chart/line-chart.tsx +15 -5
- package/src/types.ts +18 -2
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,14 @@ 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.9.0] - 2025-03-02
|
|
9
|
+
### Added
|
|
10
|
+
- Expose event handling for line chart [#42168]
|
|
11
|
+
|
|
12
|
+
## [0.8.4] - 2025-02-27
|
|
13
|
+
### Fixed
|
|
14
|
+
- Line chart: no need for min x tick num [#42087]
|
|
15
|
+
|
|
8
16
|
## [0.8.3] - 2025-02-25
|
|
9
17
|
### Changed
|
|
10
18
|
- Set options as optional for series [#42047]
|
|
@@ -124,6 +132,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
124
132
|
- Fixed lints following ESLint rule changes for TS [#40584]
|
|
125
133
|
- Fixing a bug in Chart storybook data. [#40640]
|
|
126
134
|
|
|
135
|
+
[0.9.0]: https://github.com/Automattic/charts/compare/v0.8.4...v0.9.0
|
|
136
|
+
[0.8.4]: https://github.com/Automattic/charts/compare/v0.8.3...v0.8.4
|
|
127
137
|
[0.8.3]: https://github.com/Automattic/charts/compare/v0.8.2...v0.8.3
|
|
128
138
|
[0.8.2]: https://github.com/Automattic/charts/compare/v0.8.1...v0.8.2
|
|
129
139
|
[0.8.1]: https://github.com/Automattic/charts/compare/v0.8.0...v0.8.1
|
|
@@ -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","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","
|
|
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,QAEhC,OACCI,EAAKC,KAAA,MAAA,CAAAC,UAAYC,EAAQ,uBACxBC,SAAA,CAAAC,EAAAA,IAAA,MAAA,CAAKH,UAAYC,EAAQ,4BACtBC,SAAAhB,EAAakB,MAAMC,uBAEpBjB,EAAcI,KAAKc,GACpBR,EAAAC,KAAA,MAAA,CAAuBC,UAAYC,EAAQ,2BAC1CC,SAAA,CAAAJ,EAAAC,KAAA,OAAA,CAAMC,UAAYC,EAAQ,6BAA6BC,SAAA,CAAKI,EAAMb,IAAa,OAC/EU,EAAAA,YAAMH,UAAYC,EAAQ,6BAA6BC,SAAKI,EAAMZ,UAFxDY,EAAMb,SAMlB,EAGGc,EAAmBC,GACX,IAAIC,KAAMD,GACXH,wBAAoBK,EAAW,CAC1CC,MAAO,QACPC,IAAK,YAkLP,IAAeC,EAAAA,EAAAA,gBA7JyB,EACvCC,OACAC,QACAC,SACAhB,YACAiB,SACAC,gBAAe,EACfC,cAAa,EACbC,oBAAoB,aACpBC,oBAAmB,EACnBC,aAAY,EACZC,gBAAgBvC,EAChBwC,UAAU,CAAE,EACZC,gBACAC,cACAC,gBACAC,mBAEA,MAAMC,EAAgBC,EAAAA,gBAChBC,EAAUC,EAAAA,QAEVC,EAAaC,EAAOA,SACzB,IACCpB,EAAKtB,KAAK2C,IAAY,IAClBA,EACHrB,KAAMqB,EAAOrB,KAAKnB,MAAM,CAAEC,EAAGC,I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AAuBU,KAAakC,EAAQ,UAAQvD,EAExEwF,YAAa,EACbC,MAAQ7E,EAAY8E,kBAAkBC,EAAAA,aARhCf,GAAYpB,SAbXoB,GAAYpB,OAASD,EAwB7B,IAGD/C,GACDf,EAAC6E,IAAAsB,EAAOA,SACPC,cAAY,EACZC,qBACA,EAAAC,uBACAC,kBAAgB,EAChBnF,cAAgBA,OAKjBJ,GACDhB,MAACwG,EAAMC,WAAA,CACNC,MAAQ9C,EACRhB,YAAc3B,EACdpB,UAAYC,EAAQ,yBAItB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,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
|
*/
|
|
@@ -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","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","
|
|
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","showSeriesGlyphs","Legend","items"],"mapp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BAItB"}
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@ 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 =
|
|
20
|
+
const X_TICK_WIDTH = 100;
|
|
21
21
|
|
|
22
22
|
interface LineChartProps extends BaseChartProps< SeriesData[] > {
|
|
23
23
|
withGradientFill: boolean;
|
|
@@ -66,8 +66,8 @@ const renderDefaultTooltip = ( {
|
|
|
66
66
|
);
|
|
67
67
|
};
|
|
68
68
|
|
|
69
|
-
const formatDateTick = (
|
|
70
|
-
const date = new Date(
|
|
69
|
+
const formatDateTick = ( timestamp: number ) => {
|
|
70
|
+
const date = new Date( timestamp );
|
|
71
71
|
return date.toLocaleDateString( undefined, {
|
|
72
72
|
month: 'short',
|
|
73
73
|
day: 'numeric',
|
|
@@ -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.
|
|
@@ -141,8 +145,7 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
141
145
|
}, [ margin, options ] );
|
|
142
146
|
|
|
143
147
|
const xNumTicks = useMemo(
|
|
144
|
-
() =>
|
|
145
|
-
Math.max( Math.floor( Math.min( dataSorted[ 0 ]?.data.length, width / X_TICK_WIDTH ) ), 5 ),
|
|
148
|
+
() => Math.min( dataSorted[ 0 ]?.data.length, Math.ceil( width / X_TICK_WIDTH ) ),
|
|
146
149
|
[ dataSorted, width ]
|
|
147
150
|
);
|
|
148
151
|
|
|
@@ -175,8 +178,14 @@ const LineChart: FC< LineChartProps > = ( {
|
|
|
175
178
|
width={ width }
|
|
176
179
|
height={ height }
|
|
177
180
|
margin={ { top: 10, right: 0, bottom: 20, left: 40, ...margin } }
|
|
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
|
*/
|