Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
feat: Add empty state to SimpleChart
Browse files Browse the repository at this point in the history
  • Loading branch information
diondiondion committed Aug 21, 2019
1 parent 69a8caf commit bd5b9f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/charts/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ You can also provide a `name` prop for the tooltip value and an array of `labels
height={125}
color="highlight"
/>
<br />
<SimpleChart isEmpty height={125} color="highlight" />
</Playground>

## SimpleGauge
Expand Down
46 changes: 37 additions & 9 deletions src/charts/SimpleChart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import styled from 'styled-components';

import {getColorBlock} from '../../utils/colors';

import CenterContent from '../../CenterContent';

import useChartist from '../useChartist';
import ChartTooltips from './ChartTooltips';

Expand All @@ -26,6 +28,18 @@ const ChartWrapper = styled.div`
stroke: black;
opacity: 0.1;
}
${p =>
p.isEmpty &&
`
.ct-line {
opacity: 0.2;
stroke-dasharray: 10;
}
.ct-area {
opacity: 0;
}
`}
`;

const defaultOptions = {
Expand All @@ -52,6 +66,12 @@ const defaultOptions = {
},
};

function getRandomPoints(length = 7) {
return Array(length)
.fill(1)
.map(() => Math.random());
}

function SimpleChart({
labels,
data,
Expand All @@ -60,6 +80,8 @@ function SimpleChart({
color = 'links',
tooltipRenderer,
getTooltipReadout,
isEmpty,
emptyContent = 'No data to display',
}) {
const hostRef = useRef(null);

Expand All @@ -68,25 +90,31 @@ function SimpleChart({
height,
};

const hasData = !isEmpty && data && data.length !== 0;

useChartist(hostRef, {
type: 'Line',
data: {
labels,
series: [data],
series: [hasData ? data : getRandomPoints()],
},
options,
preserveAspectRatio: 'none',
});

return (
<ChartWrapper ref={hostRef} color={color}>
<ChartTooltips
data={data}
labels={labels}
name={name}
tooltipRenderer={tooltipRenderer}
getReadout={getTooltipReadout}
/>
<ChartWrapper ref={hostRef} color={color} isEmpty={!hasData}>
{hasData ? (
<ChartTooltips
data={data}
labels={labels}
name={name}
tooltipRenderer={tooltipRenderer}
getReadout={getTooltipReadout}
/>
) : (
<CenterContent fillParent>{emptyContent}</CenterContent>
)}
</ChartWrapper>
);
}
Expand Down

0 comments on commit bd5b9f3

Please sign in to comment.