2

I need help in how to access and "update" states in an nvd3 multiBarChart (and others like stackedAreaChart, multiChart). I am using nvd3 1.7.x and have a running multiBarChart with some sources (states) from a JSON file. Additionally I have multiple JSON files for different time sets with the same sources (states).

I searched a lot for answers and tipps regarding this question and found only 2 hits in a old questions (old nvd3 version) nvd3 passing wrong event on stateChange (when clicking legend) after chart is reloaded and How to disable some streams by default in a nvd3 Simple Line Chart? regarding nvd3 1.1 or older for which they discussed an workaround because of an bug. As we got updates in 2015 to 1.7 and there have been some changes to the state functions (getter, setter and so on) I want to know if this bug still exists and how to use the new functions in the right way. The annotation and the new documentation did not help me out (only for deprecated defaultState its told: use dispatch.stateChange(state); instead).


OK, what do i do/need?: I want to save changes/choices on the states a user made as a "config"-variable and then I want to restore these states to the same chartobject wich was updated with different data (of another json file) having the same states available. My problem is, that the json files have stored default state settings that I need to overwrite in cases a user cycles through data sets - otherwise he needs to select the stream config every change again.

Example: Lets say we have the chart running with stacked and grouped available and some streams: stream1, stream2, stream3. By default the chart loads stacked view and all 3 streams are visible in json file for e.g. "2015".

chart.stacked(): {true}
chart.state.disabled: {"disabled":[false,false,false]}

So if a user only wants only to compare stream 1 and 3 in different time series, he deactivates/klicks on stream 2:

chart.stacked(): {true}
chart.state.disabled: {"disabled":[false,true,false]}

and after that this setting is stored (this is working!) and needs to be restored for the next data set (different json file e.g. "2014") (this is not working right now).


What i have is a working chart, that is loading and displaying different json files. The selection is implemented by dropdown list and events are fired by jquery. I am able to save the states by getting values of:

chart.stacked();
chart.state.disabled;
chart.reduceXTicks();

When I overwrite the saved states i can see the new values in chart.state.disabled but after an chart.update(); they are gone and i see the default states from the json file /from the initial chart object.

I already played around with different functions like the dispatch functions (changeState, stateChange) but I had no success to keep the values in after a chart update.

//oldStates is a copy of the last states before switching to a new time set (json file) in the chart.
chart.state.disabled = oldStates; 
// works but chart.update overwrites it with default values!

chart.defaultState(oldStates);
// NO changes. marked as DEPRECATED in documentation saying: Use chart.dispatch.changeState(...) instead.

chart.dispatch.changeState(oldStates); 
// No change, not working?

I would be glad in getting any hints/tipps how to change states by overwriting/changing single states and update the chart with them, rendering only the new selection. THX!

Community
  • 1
  • 1
Alex.
  • 43
  • 6

1 Answers1

0

Maybe this helps: http://plnkr.co/edit/CoGZkNoe4COEtjHMWtk6?p=preview

    data = data.map(function(series){
      if (series.key === 'Disabled Series Key'){
        series.disabled = true;
      }
      return series;
    });

You can set the individual series disabled attributes in the array of series before you pass it to be charted.

Lucas
  • 1,359
  • 7
  • 16
  • hey there, thanks - even so so long after your post. First I did not get it to work and after some time working hard on other topics we finally got back to this in the last half year and there we found our mistake and understood your solution (very late but not too late :-D ). so thanks for your help we finally got it working by your function hint. – Alex. Nov 05 '17 at 02:13