WIP: line-pod react component #5
Closed
rozetko
wants to merge 5 commits from yarn-workspaces
into main
27 changed files with 3494 additions and 78 deletions
@ -0,0 +1,33 @@
|
||||
{ |
||||
"name": "line-pod", |
||||
"version": "0.6.9", |
||||
"description": "Chartwerk line chart", |
||||
"main": "dist/index.js", |
||||
"files": [ |
||||
"/dist" |
||||
], |
||||
"scripts": { |
||||
"build": "webpack --config build/webpack.prod.conf.js && webpack --config build/webpack.dev.conf.js", |
||||
"dev": "webpack --watch --config build/webpack.dev.conf.js", |
||||
"test": "echo \"Error: no test specified\" && exit 1", |
||||
"update-core": "yarn up @chartwerk/core && yarn up @chartwerk/core@latest" |
||||
}, |
||||
"repository": { |
||||
"type": "git", |
||||
"url": "https://gitlab.com/chartwerk/line-pod.git" |
||||
}, |
||||
"author": "CorpGlory", |
||||
"license": "ISC", |
||||
"dependencies": { |
||||
"@chartwerk/core": "latest" |
||||
}, |
||||
"devDependencies": { |
||||
"css-loader": "^6.8.1", |
||||
"style-loader": "^3.3.3", |
||||
"ts-loader": "^9.4.3", |
||||
"typescript": "^5.1.3", |
||||
"webpack": "^5.87.0", |
||||
"webpack-cli": "^5.1.4" |
||||
}, |
||||
"packageManager": "yarn@3.2.1" |
||||
} |
@ -0,0 +1,27 @@
|
||||
# React + TypeScript + Vite |
||||
|
||||
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. |
||||
|
||||
Currently, two official plugins are available: |
||||
|
||||
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh |
||||
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh |
||||
|
||||
## Expanding the ESLint configuration |
||||
|
||||
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: |
||||
|
||||
- Configure the top-level `parserOptions` property like this: |
||||
|
||||
```js |
||||
parserOptions: { |
||||
ecmaVersion: 'latest', |
||||
sourceType: 'module', |
||||
project: ['./tsconfig.json', './tsconfig.node.json'], |
||||
tsconfigRootDir: __dirname, |
||||
}, |
||||
``` |
||||
|
||||
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` |
||||
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` |
||||
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list |
@ -0,0 +1,13 @@
|
||||
<!doctype html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8" /> |
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
||||
<title>Line-Pod Demo</title> |
||||
</head> |
||||
<body> |
||||
<div id="root"></div> |
||||
<script type="module" src="/src/main.tsx"></script> |
||||
</body> |
||||
</html> |
@ -0,0 +1,126 @@
|
||||
import type { LineTimeSerie, LineOptions } from 'line-pod'; |
||||
import { LinePod } from 'line-pod'; |
||||
// import { AxisRange } from '@chartwerk/core/dist/types';
|
||||
|
||||
import { useEffect, useRef, useState, PropsWithChildren, forwardRef } from 'react'; |
||||
|
||||
import * as _ from 'lodash'; |
||||
|
||||
export type AxisRange = [number, number] | undefined; |
||||
export type ChartwerkLinePodProps = { |
||||
id: string; |
||||
series: LineTimeSerie[]; |
||||
options: LineOptions; |
||||
className?: string; |
||||
// TODO: callback types should be exported from chartwerk
|
||||
onZoomIn?: (ranges: AxisRange[]) => void; |
||||
onZoomOut?: (centers: { x: number, y: number }) => void; |
||||
onMouseMove?: (event: any) => void; |
||||
onMouseOut?: () => void; |
||||
onLegendClick?: (idx: number) => void, |
||||
onPanning?: (event: { ranges: AxisRange[], d3Event: any }) => void; |
||||
onPanningEnd?: (ranges: AxisRange[]) => void; |
||||
onContextMenu?: (evt: any) => void; |
||||
onSharedCrosshairMove?: (evt: any) => void; |
||||
onRenderStart?: () => void, |
||||
onRenderEnd?: () => void, |
||||
} |
||||
|
||||
export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<ChartwerkLinePodProps>>((props, ref) => { |
||||
const [pod, setPod] = useState<LinePod | null>(null); |
||||
const [hack, setHack] = useState<number | null>(null); |
||||
|
||||
const chartRef = useRef(null); |
||||
const chart = chartRef.current; |
||||
|
||||
useEffect(() => { |
||||
// this function will be called on component unmount
|
||||
return () => { |
||||
if (pod === null) { |
||||
return; |
||||
} |
||||
|
||||
console.log('remove chart'); |
||||
|
||||
// @ts-ignore
|
||||
pod.removeEventListeners(); |
||||
} |
||||
}, []); |
||||
|
||||
useEffect(() => { |
||||
if (chart === null) { |
||||
return; |
||||
} |
||||
|
||||
let eventsCallbacks = _.cloneDeep(props.options.eventsCallbacks || {}); |
||||
if (props.onZoomIn) { |
||||
eventsCallbacks.zoomIn = props.onZoomIn; |
||||
} |
||||
if (props.onZoomOut) { |
||||
eventsCallbacks.zoomOut = props.onZoomOut; |
||||
} |
||||
if (props.onMouseMove) { |
||||
eventsCallbacks.mouseMove = props.onMouseMove; |
||||
} |
||||
if (props.onMouseOut) { |
||||
eventsCallbacks.mouseOut = props.onMouseOut; |
||||
} |
||||
if (props.onLegendClick) { |
||||
eventsCallbacks.onLegendClick = props.onLegendClick; |
||||
} |
||||
if (props.onPanning) { |
||||
eventsCallbacks.panning = props.onPanning; |
||||
} |
||||
if (props.onPanningEnd) { |
||||
eventsCallbacks.panningEnd = props.onPanningEnd; |
||||
} |
||||
if (props.onContextMenu) { |
||||
eventsCallbacks.contextMenu = props.onContextMenu; |
||||
} |
||||
if (props.onSharedCrosshairMove) { |
||||
eventsCallbacks.sharedCrosshairMove = props.onSharedCrosshairMove; |
||||
} |
||||
if (props.onRenderStart) { |
||||
eventsCallbacks.renderStart = props.onRenderStart; |
||||
} |
||||
|
||||
if (pod === null) { |
||||
console.log('create chart'); |
||||
|
||||
const newPod = new LinePod( |
||||
// @ts-ignore
|
||||
chart, |
||||
props.series, |
||||
{ |
||||
...props.options, |
||||
eventsCallbacks, |
||||
} |
||||
); |
||||
setPod( |
||||
newPod |
||||
); |
||||
console.log('initial chart render'); |
||||
newPod.render(); |
||||
} else { |
||||
console.log('update chart'); |
||||
pod.updateData(props.series, { |
||||
...props.options, |
||||
eventsCallbacks, |
||||
}); |
||||
} |
||||
}, [chart, pod, props.id, props.series, props.options]); |
||||
|
||||
// TODO: it's a hack to render the LinePod right after the div appears in DOM
|
||||
setTimeout(() => { |
||||
if (hack === null) { |
||||
setHack(1); |
||||
} |
||||
}, 1); |
||||
|
||||
return ( |
||||
<div ref={ref}> |
||||
<div id={props.id} className={props.className} ref={chartRef}></div> |
||||
{props.children} |
||||
</div> |
||||
); |
||||
}); |
@ -0,0 +1 @@
|
||||
export { ChartwerkLinePod } from './components/ChartwerkLinePod'; |
@ -0,0 +1,36 @@
|
||||
{ |
||||
"name": "react-line-pod", |
||||
"version": "0.0.0", |
||||
"type": "module", |
||||
"main": "dist/react-line-pod.js", |
||||
"types": "dist/main.d.ts", |
||||
"files": [ |
||||
"dist" |
||||
], |
||||
"scripts": { |
||||
"dev": "vite", |
||||
"build": "tsc --p ./tsconfig-build.json && vite build", |
||||
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", |
||||
"preview": "vite preview" |
||||
}, |
||||
"dependencies": { |
||||
"react": "^18.2.0", |
||||
"react-dom": "^18.2.0" |
||||
}, |
||||
"devDependencies": { |
||||
"@types/lodash": "^4.14.201", |
||||
"@types/react": "^18.2.15", |
||||
"@types/react-dom": "^18.2.7", |
||||
"@typescript-eslint/eslint-plugin": "^6.0.0", |
||||
"@typescript-eslint/parser": "^6.0.0", |
||||
"@vitejs/plugin-react": "^4.0.3", |
||||
"eslint": "^8.45.0", |
||||
"eslint-plugin-react-hooks": "^4.6.0", |
||||
"eslint-plugin-react-refresh": "^0.4.3", |
||||
"line-pod": "workspace:^", |
||||
"lodash": "^4.17.21", |
||||
"typescript": "^5.0.2", |
||||
"vite": "^4.4.5", |
||||
"vite-plugin-dts": "^3.6.3" |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
import { ChartwerkLinePod } from '../lib/main'; |
||||
|
||||
function App() { |
||||
const startTime = 1590590148; |
||||
const arrayLength = 20; |
||||
const data1 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 40)]); |
||||
const data2 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 10)]); |
||||
const data3 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 20) + 90]); |
||||
|
||||
const options = { |
||||
renderLegend: false, usePanning: false, |
||||
axis: { |
||||
y: { invert: false, range: [0, 350] }, |
||||
y1: { isActive: true, range: [0, 10], ticksCount: 8 }, |
||||
x: { format: 'time' } |
||||
}, |
||||
zoomEvents: { |
||||
mouse: { zoom: { isActive: true, orientation: 'horizontal' } }, |
||||
scroll: { zoom: { isActive: true, orientation: 'horizontal' } } |
||||
} |
||||
}; |
||||
const series = [ |
||||
{ target: 'test1', datapoints: data1, color: 'green', dashArray: '5,3', class: 'first', renderArea: true }, |
||||
{ target: 'test2', datapoints: data2, color: 'blue', yOrientation: 'right' }, |
||||
{ target: 'test3', datapoints: data3, color: 'orange' }, |
||||
]; |
||||
|
||||
return ( |
||||
<> |
||||
{/* @ts-ignore */} |
||||
<ChartwerkLinePod id="demo" series={series} options={options}></ChartwerkLinePod> |
||||
</> |
||||
); |
||||
} |
||||
|
||||
export default App; |
@ -0,0 +1,10 @@
|
||||
import App from './App.tsx'; |
||||
|
||||
import React from 'react'; |
||||
import ReactDOM from 'react-dom/client'; |
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render( |
||||
<React.StrictMode> |
||||
<App /> |
||||
</React.StrictMode> |
||||
); |
@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
@ -0,0 +1,6 @@
|
||||
{ |
||||
"extends": "./tsconfig.json", |
||||
"include": [ |
||||
"lib" |
||||
] |
||||
} |
@ -0,0 +1,25 @@
|
||||
{ |
||||
"compilerOptions": { |
||||
"target": "ES2020", |
||||
"useDefineForClassFields": true, |
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"], |
||||
"module": "ESNext", |
||||
"skipLibCheck": true, |
||||
|
||||
/* Bundler mode */ |
||||
"moduleResolution": "bundler", |
||||
"allowImportingTsExtensions": true, |
||||
"resolveJsonModule": true, |
||||
"isolatedModules": true, |
||||
"noEmit": true, |
||||
"jsx": "react-jsx", |
||||
|
||||
/* Linting */ |
||||
"strict": true, |
||||
"noUnusedLocals": true, |
||||
"noUnusedParameters": true, |
||||
"noFallthroughCasesInSwitch": true |
||||
}, |
||||
"include": ["src", "lib"], |
||||
"references": [{ "path": "./tsconfig.node.json" }] |
||||
} |
@ -0,0 +1,10 @@
|
||||
{ |
||||
"compilerOptions": { |
||||
"composite": true, |
||||
"skipLibCheck": true, |
||||
"module": "ESNext", |
||||
"moduleResolution": "bundler", |
||||
"allowSyntheticDefaultImports": true |
||||
}, |
||||
"include": ["vite.config.ts"] |
||||
} |
@ -0,0 +1,29 @@
|
||||
import { defineConfig } from 'vite'; |
||||
import react from '@vitejs/plugin-react'; |
||||
import dts from 'vite-plugin-dts'; |
||||
|
||||
import { resolve } from 'path'; |
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({ |
||||
plugins: [ |
||||
react(), |
||||
dts({ include: ['lib'] }), |
||||
], |
||||
optimizeDeps: { |
||||
include: ['line-pod'], |
||||
}, |
||||
build: { |
||||
copyPublicDir: false, |
||||
lib: { |
||||
entry: resolve(__dirname, 'lib/main.ts'), |
||||
formats: ['es'] |
||||
}, |
||||
rollupOptions: { |
||||
external: ['react', 'react/jsx-runtime'], |
||||
}, |
||||
commonjsOptions: { |
||||
include: [/line-pod/], |
||||
}, |
||||
} |
||||
}); |
Loading…
Reference in new issue