Saving dc.js filters in a URI and restoring them

Here I selected 3 filters 1 from each diagram and inserted this encoded url into the param url. but when I click the decrypt url button, it redraws only the middle filters of the diagram, but not the remaining one. What should I do? thanks

function encodeFunction() { var filters = []; for (var i = 0; i < dc.chartRegistry.list().length; i++) { var chart = dc.chartRegistry.list()[i]; for (var j = 0; j < chart.filters().length; j++) { filters.push({ChartID: chart.chartID(), Filter: chart.filters()[j]}); } } var urlParam = encodeURIComponent(JSON.stringify(filters)); alert(urlParam); } function decodeFunction() { //encoded url here var urlParam="%5B%7B%22ChartID%22%3A1%2C%22Filter%22%3A2012%7D%2C%7B%22ChartID%22%3A2%2C%22Filter%22%3A%5B1.0454545454545454%2C4.045454545454545%5D%7D%2C%7B%22ChartID%22%3A3%2C%22Filter%22%3A%22Mr%20B%22%7D%5D "; var filterObjects = JSON.parse(decodeURIComponent(urlParam)); for (var i = 0; i< filterObjects.length; i++) { dc.chartRegistry.list()[filterObjects[i].ChartID-1].filter(filterObjects[i].Filter); } // dc.renderAll(); dc.redrawAll(); } 

here is the fiddle: js fiddle link

+1
source share
2 answers

It looks like your code is correct for the general case, but due to some quirks about how dc.js handles filters, you cannot just restore a range filter from its serialized form.

I managed to get it working by adding a special case for arrays:

  for (var i = 0; i< filterObjects.length; i++) { var filter = filterObjects[i].Filter; if(filter instanceof Array) filter = dc.filters.RangedFilter(filter[0], filter[1]); dc.chartRegistry.list()[filterObjects[i].ChartID-1].filter(filter); } 

Here is my fork of your violin: http://jsfiddle.net/gordonwoodhull/4dv93aht/10/

I do not think such special cases will be needed, so I discovered the problem here: https://github.com/dc-js/dc.js/issues/819

+1
source

All Articles