From bd5b9f3982c7168386632b517f01ca18b6d33b01 Mon Sep 17 00:00:00 2001 From: DIonysos Dajka Date: Wed, 21 Aug 2019 12:25:25 +0200 Subject: [PATCH] feat: Add empty state to SimpleChart --- src/charts/README.mdx | 2 ++ src/charts/SimpleChart/index.js | 46 ++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/charts/README.mdx b/src/charts/README.mdx index 610bb07a..1e16f3c0 100644 --- a/src/charts/README.mdx +++ b/src/charts/README.mdx @@ -33,6 +33,8 @@ You can also provide a `name` prop for the tooltip value and an array of `labels height={125} color="highlight" /> +
+ ## SimpleGauge diff --git a/src/charts/SimpleChart/index.js b/src/charts/SimpleChart/index.js index ccb8a993..aefa45d4 100644 --- a/src/charts/SimpleChart/index.js +++ b/src/charts/SimpleChart/index.js @@ -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'; @@ -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 = { @@ -52,6 +66,12 @@ const defaultOptions = { }, }; +function getRandomPoints(length = 7) { + return Array(length) + .fill(1) + .map(() => Math.random()); +} + function SimpleChart({ labels, data, @@ -60,6 +80,8 @@ function SimpleChart({ color = 'links', tooltipRenderer, getTooltipReadout, + isEmpty, + emptyContent = 'No data to display', }) { const hostRef = useRef(null); @@ -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 ( - - + + {hasData ? ( + + ) : ( + {emptyContent} + )} ); }