Skip to content

Commit

Permalink
feat(chart): SKFP-692 add pie and chart graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
lflangis committed May 17, 2023
1 parent 6d77a73 commit eba0812
Show file tree
Hide file tree
Showing 9 changed files with 807 additions and 4 deletions.
592 changes: 590 additions & 2 deletions packages/ui/package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ferlab/ui",
"version": "7.3.0",
"version": "7.4.0",
"description": "Core components for scientific research data portals",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -85,6 +85,10 @@
"@dnd-kit/core": "^4.0.3",
"@dnd-kit/sortable": "^5.1.0",
"@dnd-kit/utilities": "^3.2.0",
"@nivo/bar": "^0.80.0",
"@nivo/core": "^0.80.0",
"@nivo/pie": "^0.80.0",
"@nivo/tooltip": "^0.80.0",
"classnames": "^2.2.6",
"command-line-args": "^5.1.1",
"cross-spawn": "^7.0.3",
Expand Down
20 changes: 20 additions & 0 deletions packages/ui/src/components/Charts/Bar/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.barChartWrapper {
height: 100%;
position: relative;;
width: 100%;
text-align: center;

.barChartContent {
position: absolute;
width: 100%;
height: 100%;
text-align: center;

}

*[class$="-typography"] {
margin: 0;
}
}


38 changes: 38 additions & 0 deletions packages/ui/src/components/Charts/Bar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { BarDatum, BarSvgProps, ResponsiveBar } from '@nivo/bar';
import { Typography } from 'antd';

import styles from './index.module.scss';

type TBarChart = Omit<BarSvgProps<BarDatum>, 'width' | 'height'> & {
title?: string;
};

const { Title } = Typography;

const BarChart = ({
margin = { bottom: 12, left: 24, right: 24, top: 12 },
onMouseEnter,
colorBy = 'indexValue',
title,
...rest
}: TBarChart): JSX.Element => (
<div className={styles.barChartWrapper}>
<div className={styles.barChartContent}>
{title && <Title level={5}>{title}</Title>}
<ResponsiveBar
colorBy={colorBy}
margin={margin}
onMouseEnter={(_: any, e: any) => {
if (onMouseEnter) {
onMouseEnter(_, e);
}
e.target.style.cursor = 'pointer';
}}
{...rest}
/>
</div>
</div>
);

export default BarChart;
17 changes: 17 additions & 0 deletions packages/ui/src/components/Charts/Pie/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.pieChartWrapper {
height: 100%;
position: relative;;
width: 100%;
text-align: center;

.pieChartContent {
position: absolute;
width: 100%;
height: 100%;
}

*[class$="-typography"] {
margin: 0;
}
}

48 changes: 48 additions & 0 deletions packages/ui/src/components/Charts/Pie/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { DefaultRawDatum, PieSvgProps, ResponsivePie } from '@nivo/pie';
import { BasicTooltip } from '@nivo/tooltip';
import { Typography } from 'antd';

import styles from './index.module.scss';

export type TPieChart = Omit<PieSvgProps<DefaultRawDatum>, 'width' | 'height'> & {
title?: string;
margin?: {
top: number;
bottom: number;
left: number;
right: number;
};
};

const { Title } = Typography;

const PieChart = ({
margin = { bottom: 12, left: 24, right: 24, top: 12 },
onMouseEnter,
title,
...rest
}: TPieChart): JSX.Element => (
<div className={styles.pieChartWrapper}>
<div className={styles.pieChartContent}>
{title && <Title level={5}>{title}</Title>}
<ResponsivePie
enableArcLabels={false}
enableArcLinkLabels={false}
margin={margin}
onMouseEnter={(_: any, e: any) => {
if (onMouseEnter) {
onMouseEnter(_, e);
}
e.target.style.cursor = 'pointer';
}}
tooltip={(value) => (
<BasicTooltip color={value.datum.color} id={value.datum.id.toString()} value={value.datum.value} />
)}
{...rest}
/>
</div>
</div>
);

export default PieChart;
2 changes: 1 addition & 1 deletion packages/ui/src/layout/ResizableGridLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { Layout, Layouts, Responsive as ResponsiveGridLayout, ResponsiveProps } from 'react-grid-layout';
import { SizeMe } from 'react-sizeme';
import { Space } from 'antd';
import { isEqual, xorWith } from 'lodash';
import { isEqual } from 'lodash';

import ResizableItemSelector from './ResizableItemSelector';

Expand Down
44 changes: 44 additions & 0 deletions storybook/stories/Components/Chart/bar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import BarChart from '@ferlab/ui/components/Charts/Bar';
import { Meta } from "@storybook/react/types-6-0";
import React from "react";

const data = [
{
"id": "1",
"label": "label 1",
"value": 6560
},
{
"id": "2",
"label": "label 2",
"value": 2966
},
{
"id": "3",
"label": "label 3",
"value": 2096
},
{
"id": "4",
"label": "label 4",
"value": 1681
}
]

export default {
title: "@ferlab/Components/Charts/Bar",
component: BarChart,
decorators: [
(Story) => (
<>
<h2>{Story}</h2>
<Story />
</>
),
],
} as Meta;


export const BarChartStory = () => (
<BarChart title="Bar Chart Example" data={data} />
);
44 changes: 44 additions & 0 deletions storybook/stories/Components/Chart/pie.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import PieChart from '@ferlab/ui/components/Charts/Pie';
import { Meta } from "@storybook/react/types-6-0";
import React from "react";

const data = [
{
"id": "1",
"label": "label 1",
"value": 6560
},
{
"id": "2",
"label": "label 2",
"value": 2966
},
{
"id": "3",
"label": "label 3",
"value": 2096
},
{
"id": "4",
"label": "label 4",
"value": 1681
}
]

export default {
title: "@ferlab/Components/Charts/Pie",
component: PieChart,
decorators: [
(Story) => (
<>
<h2>{Story}</h2>
<Story />
</>
),
],
} as Meta;


export const PieChartBaseStory = () => (
<PieChart title="Pie Chart Example" data={data} />
);

0 comments on commit eba0812

Please sign in to comment.