Skip to content

Commit

Permalink
Support having no Series (#312)
Browse files Browse the repository at this point in the history
Allow charts to exist which don't have any Series. This can happen if
Griff is used in an application where the user can dynamically build a
chart, for example.
  • Loading branch information
Evan Charlton authored May 9, 2019
1 parent 7ca395e commit 2cf0d8b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/components/ContextChart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const ContextChart = ({
xAxisPlacement,
zoomable,
}) => {
if (series.length === 0) {
return null;
}

const getYScale = (s, height) => {
const domain =
firstResolvedDomain(
Expand Down
5 changes: 5 additions & 0 deletions src/components/InteractionLayer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ class InteractionLayer extends React.Component {
width,
zoomAxes,
} = this.props;

if (series.length === 0) {
return null;
}

const {
crosshair: { x, y },
points,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Scaler/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Scaler extends React.Component<Props, State> {

return updates;
},
{ domainsByItemId: {}, subDomainsByItemId: {} }
{ domainsByItemId, subDomainsByItemId }
);
return updated ? stateUpdates : null;
}
Expand Down
48 changes: 48 additions & 0 deletions stories/LineChart.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,54 @@ storiesOf('LineChart', module)
{story()}
</div>
))
.add('Empty', () => (
<DataProvider defaultLoader={staticLoader} timeDomain={staticXDomain}>
<LineChart height={CHART_HEIGHT} />
</DataProvider>
))
.add('Dynamic series', () => {
const randomColor = () =>
`rgb(${Math.round(Math.random() * 255)},${Math.round(
Math.random() * 255
)},${Math.round(Math.random() * 255)},1)`;

class DynamicSeries extends React.Component {
state = {
series: [],
};

addSeries = () =>
this.setState(({ series }) => ({
series: [...series, { id: series.length + 1, color: randomColor() }],
}));

clearSeries = () => this.setState({ series: [] });

render() {
const { series } = this.state;
return (
<div>
<button type="button" onClick={this.addSeries}>
Add series
</button>
<button type="button" onClick={this.clearSeries}>
Remove all series
</button>
<DataProvider
defaultLoader={staticLoader}
timeDomain={staticXDomain}
>
{series.map(s => (
<Series key={`series-${s.id}`} {...s} />
))}
<LineChart height={CHART_HEIGHT} />
</DataProvider>
</div>
);
}
}
return <DynamicSeries />;
})
.add('Basic', () => (
<DataProvider defaultLoader={staticLoader} timeDomain={staticXDomain}>
<Series id="1" color="steelblue" />
Expand Down

0 comments on commit 2cf0d8b

Please sign in to comment.