Skip to content

Commit

Permalink
feat(echarts): sankey
Browse files Browse the repository at this point in the history
  • Loading branch information
dlabrecq committed Aug 13, 2024
1 parent 7b64b05 commit e3e26f8
Show file tree
Hide file tree
Showing 16 changed files with 707 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/react-charts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/next
/components
14 changes: 12 additions & 2 deletions packages/react-charts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"main": "dist/js/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"typesVersions": {
"*": {
"next": [
"dist/esm/next/index.d.ts"
]
}
},
"patternfly:src": "src/",
"sideEffects": [
"*.css",
Expand All @@ -30,6 +37,7 @@
"dependencies": {
"@patternfly/react-styles": "workspace:^",
"@patternfly/react-tokens": "workspace:^",
"echarts": "^5.5.1",
"hoist-non-react-statics": "^3.3.2",
"lodash": "^4.17.21",
"tslib": "^2.6.3",
Expand All @@ -56,10 +64,12 @@
"react-dom": "^17 || ^18"
},
"scripts": {
"clean": "rimraf dist",
"build:single:packages": "node ../../scripts/build-single-packages.js --config single-packages.config.json"
"build:single:packages": "node ../../scripts/build-single-packages.js --config single-packages.config.json",
"clean": "rimraf dist components next",
"subpaths": "node ../../scripts/exportSubpaths.js --config subpaths.config.json"
},
"devDependencies": {
"@types/echarts": "^4.9.22",
"@types/lodash": "^4.17.6",
"fs-extra": "^11.2.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-charts/single-packages.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"packageName": "@patternfly/react-charts",
"exclude": ["dist/esm/deprecated/index.js", "dist/esm/next/index.js"]
"exclude": ["dist/esm/deprecated/index.js"]
}
62 changes: 62 additions & 0 deletions packages/react-charts/src/next/components/Sankey/Sankey.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as React from 'react';
import { render } from '@testing-library/react';
import { Sankey } from './Sankey';

const data = [
{
name: 'a'
},
{
name: 'b'
},
{
name: 'a1'
},
{
name: 'a2'
},
{
name: 'b1'
},
{
name: 'c'
}
];

const links = [
{
source: 'a',
target: 'a1',
value: 5
},
{
source: 'a',
target: 'a2',
value: 3
},
{
source: 'b',
target: 'b1',
value: 8
},
{
source: 'a',
target: 'b1',
value: 3
},
{
source: 'b1',
target: 'a1',
value: 1
},
{
source: 'b1',
target: 'c',
value: 2
}
];

test('renders component data', () => {
const { asFragment } = render(<Sankey series={[{ data, links }]} />);
expect(asFragment()).toMatchSnapshot();
});
93 changes: 93 additions & 0 deletions packages/react-charts/src/next/components/Sankey/Sankey.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as React from 'react';
import * as echarts from 'echarts';
import { useRef } from 'react';
// import { BarChart, SankeyChart } from 'echarts/charts';
// import { CanvasRenderer } from 'echarts/renderers';

// import {
// TitleComponent,
// TooltipComponent,
// GridComponent,
// DatasetComponent,
// TransformComponent
// } from 'echarts/components';

// Register the required components
// echarts.use([
// BarChart,
// SankeyChart,
// TitleComponent,
// TooltipComponent,
// GridComponent,
// DatasetComponent,
// TransformComponent,
// LabelLayout,
// UniversalTransition,
// CanvasRenderer
// ]);

import { theme } from './theme';

/**
*/
export interface SankeyProps {
height?: number;
id?: string;
lineStyle?: any;
series: any[];
title?: any;
tooltip?: any;
width?: number;
}

export const Sankey: React.FunctionComponent<SankeyProps> = ({
height,
id,
lineStyle = {
color: 'source',
opacity: 0.6
},
series,
title,
tooltip = {
trigger: 'item',
triggerOn: 'mousemove'
},
width
}: SankeyProps) => {
const containerRef = useRef<HTMLDivElement>();
const echart = useRef<echarts.ECharts>();

React.useEffect(() => {
echarts.registerTheme('pf-v5-sankey', theme);
echart.current = echarts.init(containerRef.current, 'pf-v5-sankey'); // renderer: 'svg'

const newSeries = series.map((serie: any) => ({
emphasis: {
focus: 'adjacency'
},
layout: 'none',
lineStyle,
type: 'sankey',
...serie
}));

echart.current.setOption({
series: newSeries,
title,
tooltip
});
}, [containerRef, lineStyle, series, title, tooltip]);

React.useEffect(() => {
echart?.current.resize();
}, [height, width]);

const getSize = () => ({
...(height && { height: `${height}px` }),
...(width && { width: `${width}px` })
});

return <div id={id} ref={containerRef} style={getSize()} />;
};
Sankey.displayName = 'Sankey';
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from 'react';
import { Sankey } from '@patternfly/react-charts/next';
import { getResizeObserver } from '@patternfly/react-core';

export const FormBasic: React.FunctionComponent = () => {
const data = [
{
name: 'a'
},
{
name: 'b'
},
{
name: 'a1'
},
{
name: 'a2'
},
{
name: 'b1'
},
{
name: 'c'
}
];

const links = [
{
source: 'a',
target: 'a1',
value: 5
},
{
source: 'a',
target: 'a2',
value: 3
},
{
source: 'b',
target: 'b1',
value: 8
},
{
source: 'a',
target: 'b1',
value: 3
},
{
source: 'b1',
target: 'a1',
value: 1
},
{
source: 'b1',
target: 'c',
value: 2
}
];

// let observer = () => {};
const containerRef = React.useRef<HTMLDivElement>();
const [width, setWidth] = React.useState(0);

React.useEffect(() => {
const handleResize = () => {
if (containerRef.current && containerRef.current.clientWidth) {
setWidth(containerRef.current.clientWidth);
}
};
let observer = () => {};
observer = getResizeObserver(containerRef.current, handleResize);

return () => {
observer();
};
}, [containerRef, width]);

return (
<div ref={containerRef}>
<Sankey
height={400}
series={[{ data, links }]}
title={{
subtext: 'Data From lisachristina1234 on GitHub',
left: 'center'
}}
width={width}
/>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
id: Sankey
section: charts
propComponents: [
'Sankey',
]
hideDarkMode: true
beta: true
---

## Introduction
Note: PatternFly React charts live in its own package at [@patternfly/react-charts](https://www.npmjs.com/package/@patternfly/react-charts)!

PatternFly React charts are based on the [Apache ECharts](https://echarts.apache.org/) chart library, along with additional functionality, custom components, and theming for PatternFly. This provides a collection of React based components you can use to build PatternFly patterns with consistent markup, styling, and behavior.

## Examples
### Basic

```ts file="./Basic.tsx"

```
1 change: 1 addition & 0 deletions packages/react-charts/src/next/components/Sankey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Sankey';
Loading

0 comments on commit e3e26f8

Please sign in to comment.