Skip to content

Commit ba1a67b

Browse files
authored
fix(specs): shows a chart message without series specified (#506)
This commit fix the use case when no series spec is configured. Instead of throwing an error, it will just display the "No data to display" message.
1 parent d294b77 commit ba1a67b

File tree

7 files changed

+44
-18
lines changed

7 files changed

+44
-18
lines changed

.playground/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
background: white;
2626
display: inline-block;
2727
position: relative;
28-
width: 1000px;
28+
width: 100%;
2929
height: 350px;
3030
margin: 0px;
3131
}

.playground/playgroud.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ export class Playground extends React.Component {
4747
showLegend
4848
onPointerUpdate={this.onPointerUpdate}
4949
/>
50-
<AreaSeries
51-
id="lines"
52-
xAccessor={0}
53-
yAccessors={[1]}
54-
stackAccessors={[0]}
55-
data={KIBANA_METRICS.metrics.kibana_os_load[0].data.slice(0, 5)}
56-
/>
5750
</Chart>
5851
</div>
5952
<div className="chart">

src/components/chart.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
// Using node env because JSDOM environment throws warnings:
5+
// Jest doesn't work well with the environment detection hack that react-redux uses internally.
6+
// https://github.com/reduxjs/react-redux/issues/1373
7+
8+
import React from 'react';
9+
import { Chart } from './chart';
10+
import { render } from 'enzyme';
11+
import { Settings } from '../specs';
12+
13+
describe('Chart', () => {
14+
it("should render 'No data to display' without series", () => {
15+
const wrapper = render(<Chart />);
16+
expect(wrapper.text()).toContain('No data to display');
17+
});
18+
19+
it("should render 'No data to display' with settings but without series", () => {
20+
const wrapper = render(
21+
<Chart>
22+
<Settings debug={true} rendering={'svg'} />
23+
</Chart>,
24+
);
25+
expect(wrapper.text()).toContain('No data to display');
26+
});
27+
});

src/components/chart.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ export class Chart extends React.Component<ChartProps, ChartState> {
6565

6666
const id = uuid.v4();
6767
const storeReducer = chartStoreReducer(id);
68-
const enhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
69-
? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ trace: true, name: `@elastic/charts (id: ${id})` })()
70-
: undefined;
68+
const enhancers =
69+
typeof window !== 'undefined' && (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
70+
? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ trace: true, name: `@elastic/charts (id: ${id})` })()
71+
: undefined;
7172

7273
this.chartStore = createStore(storeReducer, enhancers);
7374
this.state = {

src/components/chart_container.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ class ChartContainerComponent extends React.Component<ReactiveChartProps> {
115115
render() {
116116
const { initialized } = this.props;
117117
if (!initialized) {
118-
return null;
118+
return (
119+
<div className="echReactiveChart_unavailable">
120+
<p>No data to display</p>
121+
</div>
122+
);
119123
}
120124
const { pointerCursor, internalChartRenderer, getChartContainerRef, forwardStageRef } = this.props;
121125
return (
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import { GlobalChartState } from '../chart_state';
2+
import { getSeriesSpecsSelector } from '../../chart_types/xy_chart/state/selectors/get_specs';
23

3-
export const isInitialized = (state: GlobalChartState) => state.specsInitialized;
4+
export const isInitialized = (state: GlobalChartState) => {
5+
return state.specsInitialized && getSeriesSpecsSelector(state).length > 0;
6+
};

src/state/utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ export function getSpecsFromStore<U extends Spec>(specs: SpecList, chartType: Ch
66
return Object.keys(specs)
77
.filter((specId) => {
88
const currentSpec = specs[specId];
9-
if (specType) {
10-
return currentSpec.specType === specType && currentSpec.chartType === chartType;
11-
} else {
12-
return currentSpec.chartType === chartType;
13-
}
9+
const sameChartType = currentSpec.chartType === chartType;
10+
const sameSpecType = specType ? currentSpec.specType === specType : true;
11+
return sameChartType && sameSpecType;
1412
})
1513
.map((specId) => {
1614
return specs[specId] as U;

0 commit comments

Comments
 (0)