Skip to content

Commit 937d223

Browse files
authored
add no data available prompt (#234)
In NIST interface:If no features is available for any stations then display "No data Available" inside the chart canvas.
2 parents cf0083e + 3d724a9 commit 937d223

File tree

2 files changed

+68
-23
lines changed

2 files changed

+68
-23
lines changed

nist-interface/src/components/chart/config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ Chart.register(annotationPlugin);
99

1010
export const ghgBlue = "#082A63";
1111

12+
export const noDataPlugin = {
13+
id: "noDataPlugin",
14+
beforeDraw: (chart) => {
15+
const { data } = chart;
16+
const hasData = data?.labels?.length && data.datasets?.[0]?.data?.length;
17+
const noData =
18+
chart.config?.options?.plugins?.noDataMessage?.enabled && !hasData;
19+
20+
if (noData) {
21+
const ctx = chart.ctx;
22+
const { width, height } = chart;
23+
ctx.save();
24+
ctx.textAlign = "center";
25+
ctx.textBaseline = "middle";
26+
ctx.font = "16px Arial";
27+
ctx.fillStyle = "#999";
28+
ctx.clearRect(0, 0, width, height);
29+
ctx.fillText("No data available", width / 2, height / 2);
30+
ctx.restore();
31+
}
32+
},
33+
};
34+
1235
export const plugin = {
1336
id: "corsair",
1437
defaults: {
@@ -23,6 +46,14 @@ export const plugin = {
2346
};
2447
},
2548
afterEvent: (chart, args) => {
49+
const isEnabled =
50+
chart?.config?.options?.plugins?.corsair?.enabled !== false &&
51+
chart?.config?.options?.plugins?.noDataMessage?.enabled !== true;
52+
53+
if (!isEnabled) {
54+
chart.corsair.draw = false;
55+
return;
56+
}
2657
const { inChartArea } = args;
2758
const { x, y } = args.event;
2859

@@ -103,6 +134,9 @@ export const options = {
103134
corsair: {
104135
// color: 'black',
105136
},
137+
noDataMessage: {
138+
enabled: false,
139+
},
106140
zoom: {
107141
zoom: {
108142
wheel: {

nist-interface/src/components/chart/index.jsx

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { faXmark, faRotateLeft, faCircleInfo } from '@fortawesome/free-solid-svg
66
import { LoadingSpinner } from '../loading';
77
import { fetchAllFromFeaturesAPI } from "../../services/api";
88

9-
import { plugin, options } from './config';
9+
import { plugin, options,noDataPlugin } from './config';
1010
import { dataPreprocess, getYAxisLabel, getChangedGHGStationId, getStationCode, isChartZoomed } from "./helper";
1111
import './index.css';
1212

@@ -23,6 +23,7 @@ export class ConcentrationChart extends Component {
2323
showChartInstructions: true,
2424
chartDataIsLoading: false,
2525
dataAccessLink: "",
26+
noData: false,
2627
};
2728
this.dataPreprocess = dataPreprocess;
2829
this.getYAxisLabel = getYAxisLabel;
@@ -72,8 +73,14 @@ export class ConcentrationChart extends Component {
7273
this.chart = new Chart(this.chartCanvas, {
7374
type: 'line',
7475
data: dataset,
75-
options: options,
76-
plugins: [plugin]
76+
options: {
77+
...options,
78+
plugins: {
79+
...options.plugins,
80+
noDataMessage: { enabled: this.state.noData },
81+
},
82+
},
83+
plugins: [plugin, noDataPlugin],
7784
});
7885

7986
this.chart.options.scales.y.title.text = dataPointLabel;
@@ -86,7 +93,7 @@ export class ConcentrationChart extends Component {
8693
}
8794

8895
this.prepareChart();
89-
}
96+
};
9097

9198
prepareChart = () => {
9299
let currentStationId = this.getChangedGHGStationId(this.props.selectedStationId, this.props.ghg);
@@ -97,19 +104,23 @@ export class ConcentrationChart extends Component {
97104
this.fetchStationData(currentStationId).then(data => {
98105
const { time, concentration } = data;
99106
// Post data fetch, check if the chart title needs to be modified (for race conditions when multiple stations are clicked).
100-
this.changeTitle(currentStationId);
101-
this.updateChart(concentration, time);
102-
this.setInitialScaleLimits();
107+
const noData = !time.length;
108+
this.setState({ noData }, () => {
109+
this.changeTitle(currentStationId);
110+
this.updateChart(concentration, time, noData);
111+
});
112+
if (!noData) this.setInitialScaleLimits();
103113
});
104-
}
114+
};
105115

106-
updateChart = (data, label) => {
116+
updateChart = (data, label, noData) => {
107117
if (this.chart) {
108118
// first reset the zoom
109119
this.chart.resetZoom();
110120
this.chart.options.plugins.tooltip.enabled = false;
111121

112122
let labelY = this.getYAxisLabel(this.props.ghg);
123+
this.chart.options.plugins.noDataMessage.enabled = noData;
113124
this.chart.data.datasets[0].label = labelY;
114125
this.chart.options.scales.y.title.text = labelY;
115126

@@ -188,28 +199,28 @@ export class ConcentrationChart extends Component {
188199
return (
189200
<Box id="chart-box" style={this.props.style}>
190201
<div id="chart-container" style={{width: "100%", height:"100%"}}>
191-
<div id="chart-tools">
192-
<div id="chart-tools-left">
193-
<div id="chart-instructions-container">
194-
<div className="icon-and-instructions">
195-
<FontAwesomeIcon
196-
icon={faCircleInfo}
197-
// style={{margin: "12px"}}
202+
<div id="chart-tools">
203+
<div id="chart-tools-left">
204+
<div id="chart-instructions-container">
205+
<div className="icon-and-instructions">
206+
<FontAwesomeIcon
207+
icon={faCircleInfo}
208+
// style={{margin: "12px"}}
198209
onMouseEnter={() => this.setState({showChartInstructions: true})}
199210
onMouseLeave={() => this.setState({showChartInstructions: false})}
200-
/>
211+
/>
201212
{this.state.showChartInstructions && <div id="chart-instructions">
202213
<p>1. Click and drag, scroll or pinch on the chart to zoom in.</p>
203214
<p>2. Hover over data points when zoomed in to see the values.</p>
204215
<p>3. Click on the rectangle boxes on the side to toggle chart.</p>
205216
</div>
206217
}
207-
</div>
208218
</div>
209219
</div>
210-
<div id="chart-tools-right">
220+
</div>
221+
<div id="chart-tools-right">
211222
{ this.state.dataAccessLink && <a id="data-access-link" href={this.state.dataAccessLink} target="_blank" rel='noreferrer'>Data Access Link ↗</a> }
212-
<div id="chart-controls">
223+
<div id="chart-controls">
213224
<FontAwesomeIcon id="zoom-reset-button" icon={faRotateLeft} title="Reset Zoom" onClick={this.handleRefresh}/>
214225
<FontAwesomeIcon id="chart-close-button" icon={faXmark} title="Close" onClick={this.handleClose}/>
215226
</div>
@@ -218,13 +229,13 @@ export class ConcentrationChart extends Component {
218229
{
219230
this.state.chartDataIsLoading && <LoadingSpinner />
220231
}
221-
<canvas
232+
<canvas
222233
id = "chart"
223234
className='fullWidth'
224235
style={{width: "100%", height: "100%"}}
225236
ref={chartCanvas => (this.chartCanvas = chartCanvas)}
226-
/>
227-
</div>
237+
/>
238+
</div>
228239
</Box>
229240
);
230241
}

0 commit comments

Comments
 (0)