Filter dashboard clicking on chart
Make sure these boxes are checked before submitting your issue - thank you!
- [X] I have checked the superset logs for python stacktraces and included it here as text if any
- [X] I have reproduced the issue with at least the latest released version of superset
- [X] I have checked the issue tracker for the same issue and I haven't found one similar
Superset version
0.28
Expected results
I'd like to click on a chart item and filter the dashboard based with the item's value. For example, if I have a bar chart with population per country, if I click on the US bar, I want to filter the whole dashboard with the US value.
Actual results
There's no on click event.
Steps to reproduce
I would like to know if this is going to be implemented or if this is not hard to do without changing much of the core of the app, so we can implement it ourselves. We need this feature as soon as possible.
I think there may be a little clumsy way to implement it.
You can use the function addFilter() in slice, and add a click event to each chart.
But It need to change many files in visualizations.
I have used this way and I also want a better way.
Hope this is helpful.
#4366 may be a similar issue
Yes! That would work for now, thanks! I hope it gets better once the filtering is refactored to the dashboard itself.
Hi Narcissus7,
Could you give detail about your solution?
Hey, Please give more details. How you implemented it. I am also working on the same kind of thing. Someone suggested me to make custom d3 charts for this functionality but If you can help, it will save a lot of time.
In visualizations , every chart file has transformProps.js
You can find chartProps which contains onAddFilter() function already.
The function is src/dashboard/actions/dashboardState.changeFilter()
Take Sankey as an example:
transformProps.js :
export default function transformProps(chartProps) {
const { ... onAddFilter } = chartProps;
const { ... groupby } = formData;
return {
...
onAddFilter, // add. filter function
groupby, // add. filter field
};
}
Sankey.js:
// ... receive onAddFilter and groupby
let centered; // add
const link = svg.append('g').selectAll('.link')
......
.on('click', function (d) { // add filter on link
if(d && centered !== d){
addFilter(groupby[0], d.source.name, false, false); // d.source.name: filter value
addFilter(groupby[1], d.target.name, false, true);
centered = d;
} else { // second click to clear filter
addFilter(groupby[0], [], false, false);
addFilter(groupby[1], [], false, true);
centered = null;
}
d3.event.stopPropagation();
})
This method may apply only to a part of the charts.And the way to get filtered fields/values is not the same.
The implementation of each chart is quite different.
(e.g. click event of Pie is chart.pie.dispatch.on("elementClick", function(e) {}))
Thank you Narcissus7 for the reply. I have only confusion here. What is Sankey.js file here. To my Knowledge, it is charts/ChartRenderer.jsx Component that uses Superchart.js file to render the charts. Where to add this code that you have mentioned in Sankey.js file.
Hi @ankit6373,
I implemented the filtering in some visualizations based on what Narcissus7 told me. Basically you need to implement the onClick function for every visualization. For example, that Sankey.js file, Sunburst.js, nvd3.js...
Here you have an example, based on an older version of superset, but it should work either way. Check out the filterData function, it calls 'this.props.addFilter' which is the function that Narcissus7 told you about.
I'm so sorry that the method is invalid now because the code has changed a lot. I guess the specific implementation of each chart is packaged in SuperChart. I don't know how to change it too.
But it should work in version0.28,0.29 or 0.30 which has all charts in visualizations.
Also, there is a branch Embeddable Charts related issue #6858. It may solve your problem.
@algorri94 @Narcissus7 I too think so, The code has changed a lot. I had to figure it out a lot and I need this functionality in all the charts that I am making in my application. I have total 10-12 charts to make,few of them are quite complex. As Further I need more functionalities like: I have 4 charts in single page: 1 is pie chart,1 is bar graph, 1 is line chart and 1 other. If I click on an element of pie chart or bar graph ,other 3 charts on that page should rerender with the data related to that specific element that is selected. What is your opinion on this. Should I stick with superset for this kind of funcationality or I need to do it in other way like using d3 or highcharts ? I just want to know your opinion guys.
i alsao need the same feature request in the chart and i want it
According to ChartRenderer.jsx, it provides BLANK callback to addFilter.
// dashboard callbacks
addFilter: PropTypes.func,
onFilterMenuOpen: PropTypes.func,
onFilterMenuClose: PropTypes.func,
const defaultProps = {
addFilter: () => BLANK,
onFilterMenuOpen: () => BLANK,
onFilterMenuClose: () => BLANK,
initialValues: BLANK,
setControlValue() {},
triggerRender: false,
};
In order to achieve this, I think need to:
- Implement the addFilter callback to viz type. it will broadcast the filter to the dashboard level.
- Other charts on the dashboard will receive the change and reflect the filter by checking shouldComponentUpdate in the same renderer.
Not sure if it is enough though
Do we have a workaround now on how to filter clicking on the dashboard?
@Narcissus7 if i want step to add On Click event on the charts. if i click on pie chart and select some sector of pie chart it will allow me to render some other charts is it possible. if yes then How?
Any update on this?