-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
748 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/next | ||
/components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/react-charts/src/next/components/Sankey/Sankey.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import * as React from 'react'; | ||
// import * as echarts from 'echarts'; | ||
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 | ||
} | ||
]; | ||
|
||
let spy: any; | ||
|
||
// beforeAll(() => { | ||
// console.log(`*** TEST 1`); | ||
// spy = jest.spyOn(echarts, 'getInstanceByDom').mockImplementation( | ||
// () => | ||
// ({ | ||
// hideLoading: jest.fn(), | ||
// setOption: jest.fn(), | ||
// showLoading: jest.fn() | ||
// }) as any | ||
// ); | ||
// }); | ||
// | ||
// afterAll(() => { | ||
// console.log(`*** TEST 2`); | ||
// spy.mockRestore(); | ||
// }); | ||
|
||
// See https://stackoverflow.com/questions/54921743/testing-echarts-react-component-with-jest-echartelement-is-null | ||
xtest('renders component data', () => { | ||
const { asFragment } = render(<Sankey opts={{ renderer: 'svg' }} series={[{ data, links }]} />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); |
95 changes: 95 additions & 0 deletions
95
packages/react-charts/src/next/components/Sankey/Sankey.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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; | ||
opts?: any; | ||
series: any[]; | ||
title?: any; | ||
tooltip?: any; | ||
width?: number; | ||
} | ||
|
||
export const Sankey: React.FunctionComponent<SankeyProps> = ({ | ||
height, | ||
id, | ||
lineStyle = { | ||
color: 'source', | ||
opacity: 0.6 | ||
}, | ||
opts, | ||
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', opts); // 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, opts, 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'; |
91 changes: 91 additions & 0 deletions
91
packages/react-charts/src/next/components/Sankey/examples/Basic.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'This is a Sankey chart', | ||
left: 'center' | ||
}} | ||
width={width} | ||
/> | ||
</div> | ||
); | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/react-charts/src/next/components/Sankey/examples/Sankey.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
id: Sankey | ||
section: charts | ||
propComponents: [ | ||
'Sankey', | ||
] | ||
hideDarkMode: true | ||
beta: true | ||
--- | ||
|
||
import { Sankey } from '@patternfly/react-charts/next'; | ||
|
||
## 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" | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Sankey'; |
Oops, something went wrong.