bcliang / dash-extendable-graph

Graphing component for Dash. Forked from the core Graph component, with modified extend/prepend properties to accept data formats matching `figure["data"]`
MIT License
10 stars 0 forks source link

Feature: Update/remove/replace Trace from Graph #45

Open bcliang opened 4 years ago

bcliang commented 4 years ago

Background

35 requested support for a "sorted" extend

39 asked about updating data within a trace

Unfortunately, the plotly.js graphing library doesn't support this type of "data update". In both cases, the feature could be implemented on the callback side provided there is/was a way to replace (or just delete) an existing trace.

Imagined use-case within a callback:

  1. Grab existing trace data in callback:
    #either 
    [..Input('graph', 'figure')]
    #or
    [..State('graph', 'figure')]
  2. Make updates/modifications to the trace in question:
    ..
    # add new values to x,y and sort by x
    data_unsorted = zip(data[0]['x'].extend(new_x_values), data[0]['y'].extend(new_y_values))
    data_sorted = sorted(data_unsorted, key=lambda x: x[0])
    # map results to independent lists to be used in the callback output
    x_new, y_new = map(list, zip(*data_sorted))
  3. Do something like
    • delete the existing trace, add a new one to the figure (replace a trace)
    • delete the existing trace, add a new one with the same trace index (update existing trace)

In the above case, it may be better to go with update in order to support preserving the trace order for the figure (for future indexing)

Considerations

Requires

hxse commented 4 years ago

Very expecting

bcliang commented 4 years ago

Looks like dash-core-components.Graph will be getting a prependData() method. https://github.com/plotly/dash-core-components/pull/850

Add that to scope here.

bcliang commented 4 years ago

Very expecting

@hxse, which use case do you have in mind? I have not been working on this feature as I have been loaded with work. If you can describe what you need, that would help make a decision on how to implement the above. If it's a small use case it might be easier to do a partial implementation (e.g just creating an additional prop specifically for prependData, deleteTrace or insertTrace)