TargetProcess / tauCharts

D3 based data-focused charting library. Designed with passion. Flexible.
https://www.taucharts.com
Other
1.91k stars 118 forks source link

Pre-select series in URL #539

Closed OddBloke closed 6 years ago

OddBloke commented 6 years ago

I have a line graph with a whole bunch of different lines plotted on to it, and the most common way people will use it is to select a few of those lines that they are interested in, using the legend. Once people have selected some lines, it would be great if they could just copy-paste the URL and have other people see the same chart as them.

Mavrin commented 6 years ago

It is possible in taucharts@2.5.0. I made some example. Code of example.

OddBloke commented 6 years ago

Wow, thanks! This example is almost perfect, the one wrinkle it has is that when a selection is reset, every category is added to the URL, rather than the selections being entirely cleared from the URL.

I do have a list of all the categories (to define an order) which I could compare with to check if that's the situation, but I didn't know if there was a more generic way I could do it from the handler?

Mavrin commented 6 years ago

It's a good solution to compare ordered categories and selected categories. Maybe I'll add event type for handler ('reset', 'leave-others', 'focus-single') in the next version Taucharts.

Mavrin commented 6 years ago

@OddBloke I added event type param in Taucharts 2.6.0.

Taucharts.api.plugins.get('legend')({
            onSelect({type, selectedCategories}) {
                if(type !== 'reset') {
                    location.hash = 'filter=' + JSON.stringify(selectedCategories);
                } else {
                    location.hash = '';
                }
            },
            selectedCategories: selectedCategories
        })