Skip to content

Commit 4011c98

Browse files
authored
Fixed bug coming from a race condition in record objects for bar/pie charts (#233)
1 parent 4bffb70 commit 4011c98

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

src/chart/bar/BarChart.tsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,32 @@ const NeoBarChart = (props: ChartProps) => {
3636

3737
const keys = {};
3838
const data: Record<string, any>[] = records.reduce((data: Record<string, any>[], row: Record<string, any>) => {
39-
if (!selection || !selection['index'] || !selection['value']) {
40-
return data;
41-
}
42-
const index = convertRecordObjectToString(row.get(selection['index']));
43-
const idx = data.findIndex(item => item.index === index)
39+
try {
40+
if (!selection || !selection['index'] || !selection['value']) {
41+
return data;
42+
}
43+
const index = convertRecordObjectToString(row.get(selection['index']));
44+
const idx = data.findIndex(item => item.index === index)
4445

45-
const key = selection['key'] !== "(none)" ? recordToNative(row.get(selection['key'])) : selection['value'];
46-
const value = recordToNative(row.get(selection['value']));
46+
const key = selection['key'] !== "(none)" ? recordToNative(row.get(selection['key'])) : selection['value'];
47+
const value = recordToNative(row.get(selection['value']));
4748

48-
if (isNaN(value)) {
49-
return data;
50-
}
51-
keys[key] = true;
49+
if (isNaN(value)) {
50+
return data;
51+
}
52+
keys[key] = true;
5253

53-
if (idx > -1) {
54-
data[idx][key] = value
55-
}
56-
else {
57-
data.push({ index, [key]: value })
54+
if (idx > -1) {
55+
data[idx][key] = value
56+
}
57+
else {
58+
data.push({ index, [key]: value })
59+
}
60+
return data
61+
} catch (e) {
62+
console.error(e);
63+
return [];
5864
}
59-
60-
return data
6165
}, [])
6266
.map(row => {
6367
Object.keys(keys).forEach(key => {
@@ -104,7 +108,7 @@ const NeoBarChart = (props: ChartProps) => {
104108
}
105109

106110
// TODO: Get rid of duplicate pie slice names...
107-
111+
108112
return <ResponsiveBar
109113
layout={settings.layout == "horizontal" ? 'horizontal' : 'vertical'}
110114
groupMode={groupMode == "stacked" ? 'stacked' : 'grouped'}

src/chart/pie/PieChart.tsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,33 @@ const NeoPieChart = (props: ChartProps) => {
2020

2121
const keys = {};
2222
const data: Record<string, any>[] = records.reduce((data: Record<string, any>[], row: Record<string, any>) => {
23-
if (!selection || !selection['index'] || !selection['value']) {
24-
return data;
25-
}
23+
try {
24+
if (!selection || !selection['index'] || !selection['value']) {
25+
return data;
26+
}
2627

27-
const index = convertRecordObjectToString(row.get(selection['index']));
28-
const idx = data.findIndex(item => item.index === index)
28+
const index = convertRecordObjectToString(row.get(selection['index']));
29+
const idx = data.findIndex(item => item.index === index)
30+
const key = selection['key'] !== "(none)" ? recordToNative(row.get(selection['key'])) : selection['value'];
31+
const value = recordToNative(row.get(selection['value']));
2932

30-
const key = selection['key'] !== "(none)" ? recordToNative(row.get(selection['key'])) : selection['value'];
31-
const value = recordToNative(row.get(selection['value']));
33+
if (isNaN(value)) {
34+
return data;
35+
}
36+
keys[key] = true;
3237

33-
if (isNaN(value)) {
34-
return data;
35-
}
36-
keys[key] = true;
38+
if (idx > -1) {
39+
data[idx][key] = value
40+
}
41+
else {
42+
data.push({ id: index, label: index, value: value })
43+
}
3744

38-
if (idx > -1) {
39-
data[idx][key] = value
40-
}
41-
else {
42-
data.push({ id: index, label: index, value: value })
45+
return data
46+
} catch (e) {
47+
console.error(e);
48+
return [];
4349
}
44-
45-
return data
4650
}, [])
4751
.map(row => {
4852
Object.keys(keys).forEach(key => {
@@ -99,7 +103,7 @@ const NeoPieChart = (props: ChartProps) => {
99103
if (data.length == 0) {
100104
return <NoDrawableDataErrorMessage />
101105
}
102-
106+
103107
return <ResponsivePie
104108
data={data}
105109
sortByValue={sortByValue}

0 commit comments

Comments
 (0)