Skip to content

Commit 74ad8c0

Browse files
committed
Merge branch 'next' of github.com:appbaseio/reactivesearch into next
2 parents 6b81d66 + 8cc1871 commit 74ad8c0

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

packages/web/examples/ReactiveChart/StackedBarChart/src/index.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Main = () => (
2525
<ReactiveChart
2626
componentId="stackedBarChart"
2727
dataField="release_year"
28-
chartType="bar"
28+
chartType="custom"
2929
filterLabel="Language"
3030
URLParams
3131
title="Languages"
@@ -58,18 +58,23 @@ const Main = () => (
5858
genres.key.split(',').forEach((genre) => {
5959
// genres might be a comma separated value
6060
if (releaseYearGenresTable[releaseYear][genre]) {
61-
releaseYearGenresTable[releaseYear][genre] += genres.doc_count;
61+
releaseYearGenresTable[releaseYear][genre]
62+
+= genres.doc_count;
6263
} else {
6364
releaseYearGenresTable[releaseYear][genre] = 0;
64-
releaseYearGenresTable[releaseYear][genre] = genres.doc_count;
65+
releaseYearGenresTable[releaseYear][genre]
66+
= genres.doc_count;
6567
}
6668
});
6769
});
6870
});
6971
Object.keys(releaseYearGenresTable).forEach((year) => {
7072
Object.keys(releaseYearGenresTable[year]).forEach((genre) => {
71-
if (!genresReleaseYearTable[genre]) genresReleaseYearTable[genre] = {};
72-
genresReleaseYearTable[genre][year] = releaseYearGenresTable[year][genre];
73+
if (!genresReleaseYearTable[genre]) {
74+
genresReleaseYearTable[genre] = {};
75+
}
76+
genresReleaseYearTable[genre][year]
77+
= releaseYearGenresTable[year][genre];
7378
});
7479
});
7580
}
@@ -89,6 +94,32 @@ const Main = () => (
8994
})),
9095
};
9196
}}
97+
customQuery={value =>
98+
(value && value.secondaryLabel && value.mainLabel
99+
? {
100+
query: {
101+
bool: {
102+
must: [
103+
{
104+
term: {
105+
release_year: value.mainLabel,
106+
},
107+
},
108+
{
109+
term: {
110+
'genres_data.keyword': value.secondaryLabel,
111+
},
112+
},
113+
],
114+
},
115+
},
116+
}
117+
: {
118+
query: {
119+
match_all: {},
120+
},
121+
})
122+
}
92123
/>
93124
</div>
94125

packages/web/src/components/basic/SelectedFilters.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ class SelectedFilters extends Component {
8282
if (value.location) {
8383
label = `${value.location} - ${label}`;
8484
}
85+
// Detect if value is from a chart with chartType as custom
86+
if (value && typeof value === 'object') {
87+
if (value.mainLabel || value.secondaryLabel) {
88+
let data = '';
89+
try {
90+
if (value.data) {
91+
data = JSON.stringify(value.data);
92+
}
93+
} catch (e) {
94+
data = String(value.data);
95+
}
96+
if (value.mainLabel && value.secondaryLabel) {
97+
label = `${value.mainLabel}-${value.secondaryLabel}`;
98+
} else if (value.mainLabel) {
99+
label = `${value.mainLabel}`;
100+
} else {
101+
label = `${value.secondaryLabel}`;
102+
}
103+
if (data) {
104+
label += `-${data}`;
105+
}
106+
}
107+
}
85108
return label;
86109
}
87110
return value;

packages/web/src/components/basic/URLParamsProvider.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ class URLParamsProvider extends Component {
201201
// TODO: support for NestedList
202202
if (value.location) return value;
203203
if (value.category) return value;
204+
if (Object.hasOwn(value, 'mainLabel') || Object.hasOwn(value, 'secondaryLabel')) {
205+
return value;
206+
}
204207
return value.label || value.key || null;
205208
}
206209
return value;

packages/web/src/components/chart/ReactiveChart.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ReactiveChartProps extends CommonProps {
1717
onGlobalOut?: (...args: any[]) => any;
1818
onContextMenu?: (...args: any[]) => any;
1919
onDataZoom?: (...args: any[]) => any;
20+
beforeValueChange?: (...args: any[]) => any;
2021
// ---- user props ---
2122
// props to configure query
2223
dataField: types.dataFieldArray;

packages/web/src/components/chart/ReactiveChart.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const ChartTypes = {
4141
Histogram: 'histogram',
4242
Line: 'line',
4343
Bar: 'bar',
44+
Custom: 'custom',
4445
};
4546

4647
class ReactiveChart extends React.Component {
@@ -66,6 +67,15 @@ class ReactiveChart extends React.Component {
6667
}
6768
this.setReact(props, this.internalComponent);
6869
this.handleRange = debounce(this.handleRange, 100);
70+
71+
if (props.chartType === ChartTypes.Custom) {
72+
if (typeof props.defaultQuery !== 'function' || typeof props.setOption !== 'function') {
73+
throw new Error('defaultQuery and setOption should be defined when chartType is custom');
74+
}
75+
if (props.useAsFilter && typeof props.customQuery !== 'function') {
76+
throw new Error('customQuery should be defined when chartType is custom and useAsFilter is true');
77+
}
78+
}
6979
}
7080
componentDidUpdate(prevProps) {
7181
if (!isEqual(prevProps.options, this.props.options)) {
@@ -182,6 +192,13 @@ class ReactiveChart extends React.Component {
182192
if (!Number.isNaN(parseInt(value, 10))) {
183193
value = parseInt(value, 10);
184194
}
195+
if (chartType === ChartTypes.Custom) {
196+
value = {
197+
mainLabel: item.name,
198+
secondaryLabel: item.seriesName,
199+
data: item.data,
200+
};
201+
}
185202
this.setValue(value);
186203
}
187204
};
@@ -343,8 +360,7 @@ class ReactiveChart extends React.Component {
343360

344361
ReactiveChart.generateQueryOptions = (props, after, value = {}) => {
345362
const queryOptions = getQueryOptions(props);
346-
const valueArray = Array.isArray(value) ? value : [value];
347-
return getAggsQuery(valueArray, queryOptions, props);
363+
return getAggsQuery(value, queryOptions, props);
348364
};
349365

350366
ReactiveChart.defaultQuery = (value, props) => {
@@ -639,6 +655,7 @@ ReactiveChart.propTypes = {
639655
onMouseOut: func,
640656
onGlobalOut: func,
641657
onContextMenu: func,
658+
beforeValueChange: func,
642659
onDataZoom: func,
643660
// ---- user props ---
644661
// props to configure query

0 commit comments

Comments
 (0)