@recharts/devtools 0.0.3 → 0.0.5
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/README.md +50 -21
- package/dist/components/RechartsDevtools.js +58 -0
- package/dist/components/useEventDispatch.js +11 -0
- package/dist/constants.js +1 -0
- package/dist/context/RechartsDevtoolsContext.d.ts +8 -0
- package/dist/context/RechartsDevtoolsContext.js +19 -0
- package/dist/generateMockData.js +14 -0
- package/dist/hooks/useSessionStorageState.d.ts +7 -0
- package/dist/hooks/useSessionStorageState.js +28 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/{src/inspectors → inspectors}/ActiveTooltipLabelInspector.d.ts +1 -1
- package/dist/inspectors/ActiveTooltipLabelInspector.js +12 -0
- package/dist/{src/inspectors → inspectors}/ChartDimensionInspector.d.ts +1 -1
- package/dist/inspectors/ChartDimensionInspector.js +18 -0
- package/dist/{src/inspectors → inspectors}/MarginInspector.d.ts +1 -1
- package/dist/inspectors/MarginInspector.js +37 -0
- package/dist/{src/inspectors → inspectors}/OffsetInspector.d.ts +1 -1
- package/dist/inspectors/OffsetInspector.js +39 -0
- package/dist/{src/inspectors → inspectors}/PlotAreaInspector.d.ts +1 -1
- package/dist/inspectors/PlotAreaInspector.js +30 -0
- package/dist/{src/inspectors → inspectors}/UseActiveTooltipDataPointsInspector.d.ts +1 -1
- package/dist/inspectors/UseActiveTooltipDataPointsInspector.js +12 -0
- package/dist/{src/inspectors → inspectors}/XAxisDomainInspector.d.ts +1 -1
- package/dist/inspectors/XAxisDomainInspector.js +12 -0
- package/dist/{src/inspectors → inspectors}/YAxisDomainInspector.d.ts +1 -1
- package/dist/inspectors/YAxisDomainInspector.js +12 -0
- package/dist/inspectors/generic/ArrayInspector.js +27 -0
- package/dist/inspectors/generic/ObjectInspector.js +43 -0
- package/dist/inspectors/generic/PrimitiveInspector.js +8 -0
- package/dist/inspectors/utils/SvgDimensionShower.js +60 -0
- package/dist/{src/random.js → random.js} +2 -6
- package/package.json +11 -8
- package/dist/src/components/RechartsDevtools.js +0 -80
- package/dist/src/components/useEventDispatch.js +0 -14
- package/dist/src/constants.js +0 -4
- package/dist/src/generateMockData.js +0 -17
- package/dist/src/index.d.ts +0 -4
- package/dist/src/index.js +0 -20
- package/dist/src/inspectors/ActiveTooltipLabelInspector.js +0 -18
- package/dist/src/inspectors/ChartDimensionInspector.js +0 -24
- package/dist/src/inspectors/MarginInspector.js +0 -43
- package/dist/src/inspectors/OffsetInspector.js +0 -45
- package/dist/src/inspectors/PlotAreaInspector.js +0 -36
- package/dist/src/inspectors/UseActiveTooltipDataPointsInspector.js +0 -18
- package/dist/src/inspectors/XAxisDomainInspector.js +0 -18
- package/dist/src/inspectors/YAxisDomainInspector.js +0 -18
- package/dist/src/inspectors/generic/ArrayInspector.js +0 -63
- package/dist/src/inspectors/generic/ObjectInspector.js +0 -77
- package/dist/src/inspectors/generic/PrimitiveInspector.js +0 -15
- package/dist/src/inspectors/utils/SvgDimensionShower.js +0 -66
- package/dist/src/types.js +0 -2
- package/dist/test/generateMockData.spec.js +0 -27
- package/dist/test/random.spec.d.ts +0 -1
- package/dist/test/random.spec.js +0 -30
- /package/dist/{src/components → components}/RechartsDevtools.d.ts +0 -0
- /package/dist/{src/components → components}/useEventDispatch.d.ts +0 -0
- /package/dist/{src/constants.d.ts → constants.d.ts} +0 -0
- /package/dist/{src/generateMockData.d.ts → generateMockData.d.ts} +0 -0
- /package/dist/{src/inspectors → inspectors}/generic/ArrayInspector.d.ts +0 -0
- /package/dist/{src/inspectors → inspectors}/generic/ObjectInspector.d.ts +0 -0
- /package/dist/{src/inspectors → inspectors}/generic/PrimitiveInspector.d.ts +0 -0
- /package/dist/{src/inspectors → inspectors}/utils/SvgDimensionShower.d.ts +0 -0
- /package/dist/{src/random.d.ts → random.d.ts} +0 -0
- /package/dist/{src/types.d.ts → types.d.ts} +0 -0
- /package/dist/{test/generateMockData.spec.d.ts → types.js} +0 -0
package/README.md
CHANGED
|
@@ -1,39 +1,68 @@
|
|
|
1
1
|
# @recharts/devtools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Devtools for Recharts.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://badge.fury.io/js/@recharts%2Fdevtools)
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
npm install @recharts/devtools
|
|
10
|
+
npm install @recharts/devtools --save-dev
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
import { random, generateMockData } from '@recharts/devtools';
|
|
15
|
+
### Basic Usage
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
Import the `RechartsDevtools` component and render it inside of your chart.
|
|
18
|
+
|
|
19
|
+
Add a div with specific ID where the devtools will render a portal.
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { RechartsDevtools, RECHARTS_DEVTOOLS_PORTAL_ID } from '@recharts/devtools';
|
|
23
|
+
import { AreaChart } from 'recharts';
|
|
24
|
+
|
|
25
|
+
function App() {
|
|
26
|
+
return (
|
|
27
|
+
<article>
|
|
28
|
+
<AreaChart>
|
|
29
|
+
{/* The DevTools component reads state from inside of the chart */}
|
|
30
|
+
<RechartsDevtools />
|
|
31
|
+
</AreaChart>
|
|
32
|
+
{/* The Portal component renders the devtools UI, debugging information, hooks and other stuff */}
|
|
33
|
+
<div id={RECHARTS_DEVTOOLS_PORTAL_ID} />
|
|
34
|
+
</article>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
20
37
|
```
|
|
21
38
|
|
|
22
|
-
|
|
39
|
+
### Multiple Instances (Context Mode)
|
|
23
40
|
|
|
24
|
-
|
|
25
|
-
- `npm run test`: Run tests with Vitest
|
|
26
|
-
- `npm run lint`: Lint code
|
|
41
|
+
If you have multiple charts or devtools instances on the same page (e.g., in a documentation site with live editors), you should use the `RechartsDevtoolsContext` and `RechartsDevtoolsPortal` to ensure each devtools instance targets the correct location without ID conflicts.
|
|
27
42
|
|
|
28
|
-
|
|
43
|
+
1. Wrap your chart and editor/devtools area with `RechartsDevtoolsContext`. One context for one devtools.
|
|
44
|
+
2. Place `RechartsDevtoolsPortal` where you want the devtools UI to be rendered.
|
|
45
|
+
3. `RechartsDevtools` will automatically detect the context and render into the associated portal.
|
|
29
46
|
|
|
30
|
-
|
|
47
|
+
```tsx
|
|
48
|
+
import { RechartsDevtools, RechartsDevtoolsContext, RechartsDevtoolsPortal } from '@recharts/devtools';
|
|
49
|
+
import { AreaChart, LineChart } from 'recharts';
|
|
31
50
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
function EditorWithPreview() {
|
|
52
|
+
return (
|
|
53
|
+
<RechartsDevtoolsContext>
|
|
54
|
+
<AreaChart>
|
|
55
|
+
<RechartsDevtools />
|
|
56
|
+
</AreaChart>
|
|
57
|
+
<RechartsDevtoolsPortal />
|
|
58
|
+
</RechartsDevtoolsContext>
|
|
59
|
+
|
|
60
|
+
<RechartsDevtoolsContext>
|
|
61
|
+
<LineChart>
|
|
62
|
+
<RechartsDevtools />
|
|
63
|
+
</LineChart>
|
|
64
|
+
<RechartsDevtoolsPortal />
|
|
65
|
+
</RechartsDevtoolsContext>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { RECHARTS_DEVTOOLS_PORTAL_ID } from '../constants.js';
|
|
4
|
+
import { ChartDimensionInspector } from '../inspectors/ChartDimensionInspector.js';
|
|
5
|
+
import { MarginInspector } from '../inspectors/MarginInspector.js';
|
|
6
|
+
import { OffsetInspector } from '../inspectors/OffsetInspector.js';
|
|
7
|
+
import { PlotAreaInspector } from '../inspectors/PlotAreaInspector.js';
|
|
8
|
+
import { UseActiveTooltipDataPointsInspector } from '../inspectors/UseActiveTooltipDataPointsInspector.js';
|
|
9
|
+
import { XAxisDomainInspector } from '../inspectors/XAxisDomainInspector.js';
|
|
10
|
+
import { YAxisDomainInspector } from '../inspectors/YAxisDomainInspector.js';
|
|
11
|
+
import { ActiveTooltipLabelInspector } from '../inspectors/ActiveTooltipLabelInspector.js';
|
|
12
|
+
import { useSessionStorageState } from '../hooks/useSessionStorageState.js';
|
|
13
|
+
import { useRechartsDevtoolsContext } from '../context/RechartsDevtoolsContext.js';
|
|
14
|
+
const INSPECTORS = {
|
|
15
|
+
'useChartWidth | useChartHeight': ChartDimensionInspector,
|
|
16
|
+
useMargin: MarginInspector,
|
|
17
|
+
useOffset: OffsetInspector,
|
|
18
|
+
usePlotArea: PlotAreaInspector,
|
|
19
|
+
useActiveTooltipDataPoints: UseActiveTooltipDataPointsInspector,
|
|
20
|
+
useXAxisDomain: XAxisDomainInspector,
|
|
21
|
+
useYAxisDomain: YAxisDomainInspector,
|
|
22
|
+
useActiveTooltipLabel: ActiveTooltipLabelInspector,
|
|
23
|
+
};
|
|
24
|
+
function useSelectedInspector() {
|
|
25
|
+
const [selectedInspectorId, setSelectedInspectorId] = useSessionStorageState('useChartWidth | useChartHeight');
|
|
26
|
+
const selectedInspector = INSPECTORS[selectedInspectorId];
|
|
27
|
+
if (!selectedInspector) {
|
|
28
|
+
setSelectedInspectorId('useChartWidth | useChartHeight');
|
|
29
|
+
}
|
|
30
|
+
return { selectedInspectorId, setSelectedInspectorId, selectedInspector };
|
|
31
|
+
}
|
|
32
|
+
export const RechartsDevtools = () => {
|
|
33
|
+
const contextId = useRechartsDevtoolsContext();
|
|
34
|
+
const portalId = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
|
|
35
|
+
const [container, setContainer] = React.useState(null);
|
|
36
|
+
React.useEffect(() => {
|
|
37
|
+
setContainer(document.getElementById(portalId));
|
|
38
|
+
}, [portalId]);
|
|
39
|
+
const { selectedInspectorId, setSelectedInspectorId, selectedInspector } = useSelectedInspector();
|
|
40
|
+
const [isOverlayEnabled, setIsOverlayEnabled] = useSessionStorageState(false);
|
|
41
|
+
if (!container || !selectedInspector) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const { Inspector, Overlay } = selectedInspector;
|
|
45
|
+
return (React.createElement(React.Fragment, null,
|
|
46
|
+
Overlay && isOverlayEnabled && React.createElement(Overlay, null),
|
|
47
|
+
createPortal(React.createElement("div", { className: "recharts-devtools", style: { fontFamily: 'monospace', fontSize: '12px' } },
|
|
48
|
+
React.createElement("div", { style: { marginBottom: '10px', paddingBottom: '10px', borderBottom: '1px solid #ccc' } },
|
|
49
|
+
React.createElement("div", { style: { display: 'flex', alignItems: 'center', marginBottom: '4px', gap: '8px' } },
|
|
50
|
+
React.createElement("label", null, "Inspect Hook:"),
|
|
51
|
+
React.createElement("select", { value: selectedInspectorId, onChange: (e) => setSelectedInspectorId(e.target.value), style: { padding: '4px' } }, Object.keys(INSPECTORS).map((key) => (React.createElement("option", { key: key, value: key }, key)))),
|
|
52
|
+
Overlay && (React.createElement("div", { style: { marginTop: '4px' } },
|
|
53
|
+
React.createElement("label", { style: { cursor: 'pointer', display: 'flex', alignItems: 'center' } },
|
|
54
|
+
React.createElement("input", { type: "checkbox", checked: isOverlayEnabled, onChange: (e) => setIsOverlayEnabled(e.target.checked), style: { marginRight: '4px' } }),
|
|
55
|
+
"Show Overlay on chart"))))),
|
|
56
|
+
React.createElement("div", { style: { padding: '10px 0' } },
|
|
57
|
+
React.createElement(Inspector, null))), container)));
|
|
58
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { RECHARTS_DEVTOOLS_PORTAL_ID } from "../constants.js";
|
|
3
|
+
export function useEventDispatch(detail) {
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
const container = document.getElementById(RECHARTS_DEVTOOLS_PORTAL_ID);
|
|
6
|
+
if (!container)
|
|
7
|
+
return;
|
|
8
|
+
const event = new CustomEvent('recharts-devtools-change', { detail });
|
|
9
|
+
container.dispatchEvent(event);
|
|
10
|
+
}, [detail]);
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const RECHARTS_DEVTOOLS_PORTAL_ID = 'recharts-devtools-portal';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
interface RechartsDevtoolsContextProps {
|
|
3
|
+
children: React.ReactNode;
|
|
4
|
+
}
|
|
5
|
+
export declare const RechartsDevtoolsContext: React.FC<RechartsDevtoolsContextProps>;
|
|
6
|
+
export declare const useRechartsDevtoolsContext: () => string | null;
|
|
7
|
+
export declare const RechartsDevtoolsPortal: React.FC<React.HTMLAttributes<HTMLDivElement>>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React, { createContext, useContext, useId } from 'react';
|
|
2
|
+
import { RECHARTS_DEVTOOLS_PORTAL_ID } from '../constants.js';
|
|
3
|
+
const Context = createContext(null);
|
|
4
|
+
export const RechartsDevtoolsContext = ({ children }) => {
|
|
5
|
+
// Generate a unique ID for this instance.
|
|
6
|
+
// React.useId returns a string that includes colons, e.g., ":r0:"
|
|
7
|
+
// We sanitize it to make it a valid HTML ID and safer for CSS selectors if needed.
|
|
8
|
+
const rawId = useId();
|
|
9
|
+
const portalId = `recharts-devtools-portal-${rawId.replace(/:/g, '')}`;
|
|
10
|
+
return React.createElement(Context.Provider, { value: portalId }, children);
|
|
11
|
+
};
|
|
12
|
+
export const useRechartsDevtoolsContext = () => {
|
|
13
|
+
return useContext(Context);
|
|
14
|
+
};
|
|
15
|
+
export const RechartsDevtoolsPortal = (props) => {
|
|
16
|
+
const contextId = useRechartsDevtoolsContext();
|
|
17
|
+
const id = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
|
|
18
|
+
return React.createElement("div", { id: id, ...props });
|
|
19
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { between, random } from './random.js';
|
|
2
|
+
export function generateMockData(length, seed) {
|
|
3
|
+
const result = [];
|
|
4
|
+
const gen = random(seed);
|
|
5
|
+
for (let i = 0; i < length; i++) {
|
|
6
|
+
result.push({
|
|
7
|
+
label: `Iter: ${i}`,
|
|
8
|
+
x: between(gen, 100, 300),
|
|
9
|
+
y: between(gen, 400, 800),
|
|
10
|
+
z: between(gen, 1000, 2000),
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This hook works just the same as `useState`, but it also stores the state in the session storage.
|
|
3
|
+
* It uses `useId` from React to generate a unique key for the state.
|
|
4
|
+
*
|
|
5
|
+
* In browsers without session storage support, it will fallback to `useState`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const useSessionStorageState: <T>(initialState: T) => readonly [any, import("react").Dispatch<any>];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useId, useState, useEffect } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* This hook works just the same as `useState`, but it also stores the state in the session storage.
|
|
4
|
+
* It uses `useId` from React to generate a unique key for the state.
|
|
5
|
+
*
|
|
6
|
+
* In browsers without session storage support, it will fallback to `useState`.
|
|
7
|
+
*/
|
|
8
|
+
export const useSessionStorageState = (initialState) => {
|
|
9
|
+
const id = useId();
|
|
10
|
+
const [state, setState] = useState(() => {
|
|
11
|
+
try {
|
|
12
|
+
const storedState = sessionStorage.getItem(id);
|
|
13
|
+
return storedState ? JSON.parse(storedState) : initialState;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return initialState;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
try {
|
|
21
|
+
sessionStorage.setItem(id, JSON.stringify(state));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
/* Ignored intentionally */
|
|
25
|
+
}
|
|
26
|
+
}, [state]);
|
|
27
|
+
return [state, setState];
|
|
28
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { between, random } from './random.js';
|
|
2
|
+
export { generateMockData } from './generateMockData.js';
|
|
3
|
+
export { RechartsDevtools } from './components/RechartsDevtools.js';
|
|
4
|
+
export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
|
|
5
|
+
export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { between, random } from './random.js';
|
|
2
|
+
export { generateMockData } from './generateMockData.js';
|
|
3
|
+
export { RechartsDevtools } from './components/RechartsDevtools.js';
|
|
4
|
+
export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
|
|
5
|
+
export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const ActiveTooltipLabelInspector: InspectorDef;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useActiveTooltipLabel } from 'recharts';
|
|
3
|
+
import { PrimitiveInspector } from './generic/PrimitiveInspector.js';
|
|
4
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
5
|
+
const Inspector = () => {
|
|
6
|
+
const activeLabel = useActiveTooltipLabel();
|
|
7
|
+
useEventDispatch(activeLabel);
|
|
8
|
+
return React.createElement(PrimitiveInspector, { value: activeLabel });
|
|
9
|
+
};
|
|
10
|
+
export const ActiveTooltipLabelInspector = {
|
|
11
|
+
Inspector,
|
|
12
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const ChartDimensionInspector: InspectorDef;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useChartHeight, useChartWidth } from 'recharts';
|
|
3
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
4
|
+
const Inspector = () => {
|
|
5
|
+
const chartWidth = useChartWidth();
|
|
6
|
+
const chartHeight = useChartHeight();
|
|
7
|
+
useEventDispatch({ useChartWidth: chartWidth, useChartHeight: chartHeight });
|
|
8
|
+
return (React.createElement("div", null,
|
|
9
|
+
React.createElement("div", null,
|
|
10
|
+
"useChartWidth: ",
|
|
11
|
+
chartWidth),
|
|
12
|
+
React.createElement("div", null,
|
|
13
|
+
"useChartHeight: ",
|
|
14
|
+
chartHeight)));
|
|
15
|
+
};
|
|
16
|
+
export const ChartDimensionInspector = {
|
|
17
|
+
Inspector,
|
|
18
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const MarginInspector: InspectorDef;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useChartHeight, useChartWidth, useMargin } from 'recharts';
|
|
3
|
+
import { SvgDimensionShower } from './utils/SvgDimensionShower.js';
|
|
4
|
+
import { ObjectInspector } from './generic/ObjectInspector.js';
|
|
5
|
+
const Overlay = () => {
|
|
6
|
+
const margin = useMargin();
|
|
7
|
+
const height = useChartHeight();
|
|
8
|
+
const width = useChartWidth();
|
|
9
|
+
if (margin == null || width == null || height == null) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return (React.createElement(React.Fragment, null,
|
|
13
|
+
React.createElement(SvgDimensionShower, { width: margin.left, height: height, labels: {
|
|
14
|
+
background: 'Left',
|
|
15
|
+
horizontal: 'useMargin.left',
|
|
16
|
+
} }),
|
|
17
|
+
React.createElement(SvgDimensionShower, { width: margin.right, x: width - margin.right, height: height, labels: {
|
|
18
|
+
background: 'Right',
|
|
19
|
+
horizontal: 'useMargin.right',
|
|
20
|
+
} }),
|
|
21
|
+
React.createElement(SvgDimensionShower, { width: width, height: margin.top, labels: {
|
|
22
|
+
background: 'Top',
|
|
23
|
+
vertical: 'useMargin.top',
|
|
24
|
+
} }),
|
|
25
|
+
React.createElement(SvgDimensionShower, { width: width, height: margin.bottom, y: height - margin.bottom, labels: {
|
|
26
|
+
background: 'Bottom',
|
|
27
|
+
vertical: 'useMargin.bottom',
|
|
28
|
+
} })));
|
|
29
|
+
};
|
|
30
|
+
const Inspector = () => {
|
|
31
|
+
const margin = useMargin();
|
|
32
|
+
return React.createElement(ObjectInspector, { obj: margin });
|
|
33
|
+
};
|
|
34
|
+
export const MarginInspector = {
|
|
35
|
+
Overlay,
|
|
36
|
+
Inspector,
|
|
37
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const OffsetInspector: InspectorDef;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useChartHeight, useChartWidth, useOffset } from 'recharts';
|
|
3
|
+
import { SvgDimensionShower } from './utils/SvgDimensionShower.js';
|
|
4
|
+
import { ObjectInspector } from './generic/ObjectInspector.js';
|
|
5
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
6
|
+
const Overlay = () => {
|
|
7
|
+
const offset = useOffset();
|
|
8
|
+
const height = useChartHeight();
|
|
9
|
+
const width = useChartWidth();
|
|
10
|
+
if (offset == null || width == null || height == null) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return (React.createElement(React.Fragment, null,
|
|
14
|
+
React.createElement(SvgDimensionShower, { width: offset.left, height: height, labels: {
|
|
15
|
+
background: 'Left',
|
|
16
|
+
horizontal: 'useOffset.left',
|
|
17
|
+
} }),
|
|
18
|
+
React.createElement(SvgDimensionShower, { width: offset.right, x: width - offset.right, height: height, labels: {
|
|
19
|
+
background: 'Right',
|
|
20
|
+
horizontal: 'useOffset.right',
|
|
21
|
+
} }),
|
|
22
|
+
React.createElement(SvgDimensionShower, { width: width, height: offset.top, labels: {
|
|
23
|
+
background: 'Top',
|
|
24
|
+
vertical: 'useOffset.top',
|
|
25
|
+
} }),
|
|
26
|
+
React.createElement(SvgDimensionShower, { width: width, height: offset.bottom, y: height - offset.bottom, labels: {
|
|
27
|
+
background: 'Bottom',
|
|
28
|
+
vertical: 'useOffset.bottom',
|
|
29
|
+
} })));
|
|
30
|
+
};
|
|
31
|
+
const Inspector = () => {
|
|
32
|
+
const offset = useOffset();
|
|
33
|
+
useEventDispatch(offset);
|
|
34
|
+
return React.createElement(ObjectInspector, { obj: offset });
|
|
35
|
+
};
|
|
36
|
+
export const OffsetInspector = {
|
|
37
|
+
Overlay,
|
|
38
|
+
Inspector,
|
|
39
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const PlotAreaInspector: InspectorDef;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useChartHeight, useChartWidth, usePlotArea } from 'recharts';
|
|
3
|
+
import { SvgDimensionShower } from './utils/SvgDimensionShower.js';
|
|
4
|
+
import { ObjectInspector } from './generic/ObjectInspector.js';
|
|
5
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
6
|
+
const Overlay = () => {
|
|
7
|
+
const plotArea = usePlotArea();
|
|
8
|
+
const width = useChartWidth();
|
|
9
|
+
const height = useChartHeight();
|
|
10
|
+
if (plotArea == null || width == null || height == null) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return (React.createElement(React.Fragment, null,
|
|
14
|
+
React.createElement(SvgDimensionShower, { width: width, height: plotArea.y, labels: { background: 'y', vertical: 'usePlotArea.y' } }),
|
|
15
|
+
React.createElement(SvgDimensionShower, { width: plotArea.x, height: height, labels: { background: 'x', horizontal: 'usePlotArea.x' } }),
|
|
16
|
+
React.createElement(SvgDimensionShower, { x: plotArea.x, y: plotArea.y, width: plotArea.width, height: plotArea.height, labels: {
|
|
17
|
+
background: 'Plot area',
|
|
18
|
+
horizontal: 'usePlotArea.width',
|
|
19
|
+
vertical: 'usePlotArea.height',
|
|
20
|
+
} })));
|
|
21
|
+
};
|
|
22
|
+
const Inspector = () => {
|
|
23
|
+
const plotArea = usePlotArea();
|
|
24
|
+
useEventDispatch(plotArea);
|
|
25
|
+
return React.createElement(ObjectInspector, { obj: plotArea });
|
|
26
|
+
};
|
|
27
|
+
export const PlotAreaInspector = {
|
|
28
|
+
Overlay,
|
|
29
|
+
Inspector,
|
|
30
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const UseActiveTooltipDataPointsInspector: InspectorDef;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useActiveTooltipDataPoints } from 'recharts';
|
|
3
|
+
import { ArrayInspector } from './generic/ArrayInspector.js';
|
|
4
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
5
|
+
const Inspector = () => {
|
|
6
|
+
const dataPoints = useActiveTooltipDataPoints();
|
|
7
|
+
useEventDispatch(dataPoints);
|
|
8
|
+
return React.createElement(ArrayInspector, { arr: dataPoints, expandByDefault: true });
|
|
9
|
+
};
|
|
10
|
+
export const UseActiveTooltipDataPointsInspector = {
|
|
11
|
+
Inspector,
|
|
12
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const XAxisDomainInspector: InspectorDef;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useXAxisDomain } from 'recharts';
|
|
3
|
+
import { ArrayInspector } from './generic/ArrayInspector.js';
|
|
4
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
5
|
+
const Inspector = () => {
|
|
6
|
+
const xAxisDomain = useXAxisDomain();
|
|
7
|
+
useEventDispatch(xAxisDomain);
|
|
8
|
+
return React.createElement(ArrayInspector, { arr: xAxisDomain });
|
|
9
|
+
};
|
|
10
|
+
export const XAxisDomainInspector = {
|
|
11
|
+
Inspector,
|
|
12
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InspectorDef } from '../types';
|
|
1
|
+
import { InspectorDef } from '../types.js';
|
|
2
2
|
export declare const YAxisDomainInspector: InspectorDef;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useYAxisDomain } from 'recharts';
|
|
3
|
+
import { ArrayInspector } from './generic/ArrayInspector.js';
|
|
4
|
+
import { useEventDispatch } from '../components/useEventDispatch.js';
|
|
5
|
+
const Inspector = () => {
|
|
6
|
+
const yAxisDomain = useYAxisDomain();
|
|
7
|
+
useEventDispatch(yAxisDomain);
|
|
8
|
+
return React.createElement(ArrayInspector, { arr: yAxisDomain });
|
|
9
|
+
};
|
|
10
|
+
export const YAxisDomainInspector = {
|
|
11
|
+
Inspector,
|
|
12
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { PrimitiveInspector, serializePrimitive } from './PrimitiveInspector.js';
|
|
3
|
+
function ArrayOfObjectsInspector({ arr, expand }) {
|
|
4
|
+
if (!expand) {
|
|
5
|
+
return React.createElement("code", null,
|
|
6
|
+
"Array(",
|
|
7
|
+
arr.length,
|
|
8
|
+
")");
|
|
9
|
+
}
|
|
10
|
+
return (React.createElement("pre", null,
|
|
11
|
+
React.createElement("code", null,
|
|
12
|
+
"[",
|
|
13
|
+
arr.map(serializePrimitive).join(',\n'),
|
|
14
|
+
"]")));
|
|
15
|
+
}
|
|
16
|
+
export function ArrayInspector({ arr, expandByDefault, }) {
|
|
17
|
+
if (arr == null) {
|
|
18
|
+
return React.createElement(PrimitiveInspector, { value: arr });
|
|
19
|
+
}
|
|
20
|
+
const length = arr.length ?? 0;
|
|
21
|
+
const typeofArr = typeof arr[0];
|
|
22
|
+
const shouldExpandByDefault = expandByDefault || length <= 1 || (typeofArr !== 'object' && typeofArr !== 'function');
|
|
23
|
+
const [expand, setExpand] = useState(shouldExpandByDefault);
|
|
24
|
+
return (React.createElement(React.Fragment, null,
|
|
25
|
+
expand ? (React.createElement("button", { type: "button", onClick: () => setExpand(false) }, "Collapse")) : (React.createElement("button", { type: "button", onClick: () => setExpand(true) }, "Expand")),
|
|
26
|
+
React.createElement(ArrayOfObjectsInspector, { arr: arr, expand: expand })));
|
|
27
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { PrimitiveInspector } from './PrimitiveInspector.js';
|
|
3
|
+
/*
|
|
4
|
+
* Values that are expanded by default:
|
|
5
|
+
* - primitives
|
|
6
|
+
* - objects with 0 or 1 or 2 keys
|
|
7
|
+
* - arrays with 0 or 1 elements
|
|
8
|
+
*/
|
|
9
|
+
function shouldExpandByDefault(value) {
|
|
10
|
+
if (Array.isArray(value)) {
|
|
11
|
+
return value.length <= 1;
|
|
12
|
+
}
|
|
13
|
+
if (value != null && typeof value === 'object') {
|
|
14
|
+
return Object.keys(value).length <= 2;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
function NotPrimitiveInspector({ value }) {
|
|
19
|
+
const [expand, setExpand] = useState(shouldExpandByDefault(value));
|
|
20
|
+
return (React.createElement(React.Fragment, null,
|
|
21
|
+
expand ? (React.createElement("button", { type: "button", onClick: () => setExpand(false) }, "Collapse")) : (React.createElement("button", { type: "button", onClick: () => setExpand(true) }, "Expand")),
|
|
22
|
+
expand ? (React.createElement("pre", null,
|
|
23
|
+
React.createElement("code", null, JSON.stringify(value, null, 2)))) : (React.createElement("code", null, typeof value))));
|
|
24
|
+
}
|
|
25
|
+
export function ValueInspector({ value }) {
|
|
26
|
+
if (value == null ||
|
|
27
|
+
typeof value === 'string' ||
|
|
28
|
+
typeof value === 'number' ||
|
|
29
|
+
typeof value === 'boolean') {
|
|
30
|
+
return React.createElement(PrimitiveInspector, { value: value });
|
|
31
|
+
}
|
|
32
|
+
return React.createElement(NotPrimitiveInspector, { value: value });
|
|
33
|
+
}
|
|
34
|
+
export function ObjectInspector({ obj }) {
|
|
35
|
+
if (obj == null) {
|
|
36
|
+
return React.createElement("code", null, JSON.stringify(obj));
|
|
37
|
+
}
|
|
38
|
+
const keys = Object.keys(obj);
|
|
39
|
+
return (React.createElement("div", null, keys.map((key) => (React.createElement("div", { key: key },
|
|
40
|
+
React.createElement("strong", null, key),
|
|
41
|
+
": ",
|
|
42
|
+
React.createElement(ValueInspector, { value: obj[key] }))))));
|
|
43
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export function serializePrimitive(value) {
|
|
3
|
+
return value === undefined ? 'undefined' : JSON.stringify(value, null, 2);
|
|
4
|
+
}
|
|
5
|
+
export function PrimitiveInspector({ value }) {
|
|
6
|
+
const str = serializePrimitive(value);
|
|
7
|
+
return React.createElement("code", null, str);
|
|
8
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Renders a line with arrows on left and right.
|
|
4
|
+
* @constructor
|
|
5
|
+
*/
|
|
6
|
+
function HorizontalLineWithArrows({ x1, x2, y, label, stroke = 'black', }) {
|
|
7
|
+
const strokeWidth = 2;
|
|
8
|
+
const arrowSize = 7;
|
|
9
|
+
const width = x2 - x1;
|
|
10
|
+
return (React.createElement(React.Fragment, null,
|
|
11
|
+
React.createElement("line", { x1: x1, y1: y, x2: x2, y2: y, stroke: stroke, strokeWidth: strokeWidth }),
|
|
12
|
+
React.createElement("line", { x1: x1, y1: y, x2: x1 + arrowSize, y2: y - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
13
|
+
React.createElement("line", { x1: x1, y1: y, x2: x1 + arrowSize, y2: y + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
14
|
+
React.createElement("line", { x1: x2, y1: y, x2: x2 - arrowSize, y2: y - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
15
|
+
React.createElement("line", { x1: x2, y1: y, x2: x2 - arrowSize, y2: y + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
16
|
+
React.createElement("text", { x: width * 0.9, y: y + 8, textAnchor: "end", dominantBaseline: "hanging", stroke: "red" }, label)));
|
|
17
|
+
}
|
|
18
|
+
function Background({ width, height, label, }) {
|
|
19
|
+
const patternId = `pattern+${label.replace(/\s+/g, '-')}`;
|
|
20
|
+
return (React.createElement("svg", { width: width, height: height, style: { position: 'absolute', top: 0, left: 0 } },
|
|
21
|
+
React.createElement("defs", null,
|
|
22
|
+
React.createElement("pattern", { id: patternId, x: 0, y: 0, width: 120, height: 50, patternUnits: "userSpaceOnUse", patternTransform: "rotate(30)" },
|
|
23
|
+
React.createElement("text", { x: 0, y: 10, fontSize: 24, stroke: "#eee", fill: "#eee", dominantBaseline: "hanging" }, label.toUpperCase()))),
|
|
24
|
+
React.createElement("rect", { x: 0, y: 0, width: width, height: height, fill: `url(#${patternId})` })));
|
|
25
|
+
}
|
|
26
|
+
function VerticalLineWithArrows({ x, y1, y2, label, stroke = 'black', }) {
|
|
27
|
+
const strokeWidth = 2;
|
|
28
|
+
const arrowSize = 7;
|
|
29
|
+
const height = y2 - y1;
|
|
30
|
+
return (React.createElement(React.Fragment, null,
|
|
31
|
+
React.createElement("line", { x1: x, y1: y1, x2: x, y2: y2, stroke: stroke, strokeWidth: strokeWidth }),
|
|
32
|
+
React.createElement("line", { x1: x, y1: y1, x2: x - arrowSize, y2: y1 + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
33
|
+
React.createElement("line", { x1: x, y1: y1, x2: x + arrowSize, y2: y1 + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
34
|
+
React.createElement("line", { x1: x, y1: y2, x2: x - arrowSize, y2: y2 - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
35
|
+
React.createElement("line", { x1: x, y1: y2, x2: x + arrowSize, y2: y2 - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
|
|
36
|
+
React.createElement("text", { x: x + 8, y: height * 0.7, textAnchor: "start", dominantBaseline: "middle", stroke: "blue" }, label)));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a label for the horizontal line.
|
|
40
|
+
* It will trim information to save space if the width is too small.
|
|
41
|
+
* @param width number of pixels to display
|
|
42
|
+
* @param label description of the width
|
|
43
|
+
* @return the label to display
|
|
44
|
+
*/
|
|
45
|
+
function makeHorizontalLabel(width, label) {
|
|
46
|
+
if (width < 50) {
|
|
47
|
+
return String(width);
|
|
48
|
+
}
|
|
49
|
+
if (width < 100) {
|
|
50
|
+
return `${width}px`;
|
|
51
|
+
}
|
|
52
|
+
return `${label}: ${width}px`;
|
|
53
|
+
}
|
|
54
|
+
export function SvgDimensionShower({ x = 0, y = 0, width, height, labels, }) {
|
|
55
|
+
return (React.createElement("svg", { width: width, height: height, x: x, y: y, style: { pointerEvents: 'none' } },
|
|
56
|
+
React.createElement(Background, { width: width, height: height, label: labels.background }),
|
|
57
|
+
React.createElement("rect", { x: 0, y: 0, width: width, height: height, fill: "none", stroke: "green", strokeWidth: 5, strokeDasharray: "3 3" }),
|
|
58
|
+
labels.horizontal && (React.createElement(HorizontalLineWithArrows, { x1: 0, y: height * 0.15, x2: width, stroke: "red", label: makeHorizontalLabel(width, labels.horizontal) })),
|
|
59
|
+
labels.vertical && (React.createElement(VerticalLineWithArrows, { x: width * 0.1, y1: 0, y2: height, stroke: "blue", label: `${labels.vertical}: ${height}px` }))));
|
|
60
|
+
}
|