How to create new chart after tabulator data is clicked?

how to create new chart after tabulator data is clicked?

for example, tabulator data is simple pandas dataframe.

I want to create new chart when select dataframe cell.

Hi @jaewoo-so,

A nice example from @Marc below, is this what your looking for

import hvplot.pandas # Could be any plotting library
import pandas as pd
import panel as pn

pn.extension(sizing_mode="stretch_width")

wide_data = pd.DataFrame({
    "CODE": ["A", "B", "C"],
    "vPoint1_R4": [357, 247, 232],
    "vPoint2_R4": [430, 365, 285],
    "vPoint3_R4": [502, 448, 360],
    "vPoint4_R4": [539, 477, 398],
    "step1": [15, 15, 15],
    "step2": [30, 30, 30],
    "step3": [45, 45, 45],
    "step4": [60, 60, 60],
})

def to_tidy_data(wide_data):
    ydata = pd.melt(wide_data, id_vars=["CODE"], value_vars=["vPoint1_R4", "vPoint2_R4", "vPoint3_R4", "vPoint4_R4"], value_name="y", var_name="item")
    ydata["item"]=ydata["item"].str[-4]
    xdata = pd.melt(wide_data, id_vars=["CODE"], value_vars=["step1", "step2", "step3", "step4"], value_name="x", var_name="item")
    xdata["item"]=xdata["item"].str[-1]
    data = pd.merge(xdata, ydata, on=["CODE", "item"])
    return data

tidy_data = to_tidy_data(wide_data)

def plot(index):
    if not index:
        return "No row selected"
    
    code = wide_data.iloc[index].CODE.iloc[0]
    filtered_data = tidy_data[tidy_data["CODE"]==code]
    return filtered_data.hvplot.scatter(x="x", y="y")

table = pn.widgets.Tabulator(wide_data, height=125, theme="fast", selectable=1, )
interactive_plot = pn.bind(plot, index=table.param.selection)

pn.template.FastListTemplate(
    site="Awesome Panel", title="Table selections",
    main=[table, pn.panel(interactive_plot, height=350)]
).show()

1 Like

That’s a perfect answer. Thank you very much:)