|
1 | | -import plotly.graph_objects as go |
2 | | -from plotly.graph_objs import Layout |
| 1 | +import altair as alt |
3 | 2 |
|
4 | 3 | from dashipy import (Component, Input, Output) |
5 | 4 | from dashipy.components import (Plot, Box, Dropdown) |
|
14 | 13 | def render_panel(ctx: Context) -> Component: |
15 | 14 | selected_dataset: int = 0 |
16 | 15 | plot = Plot( |
17 | | - id="plot", figure=make_figure(ctx, selected_dataset), style={"flexGrow": 1} |
| 16 | + id="plot", chart=make_figure(ctx, selected_dataset), style={"flexGrow": 1} |
18 | 17 | ) |
19 | 18 | dropdown = Dropdown( |
20 | 19 | id="selected_dataset", |
@@ -46,16 +45,33 @@ def render_panel(ctx: Context) -> Component: |
46 | 45 |
|
47 | 46 | @panel.callback( |
48 | 47 | Input("selected_dataset"), |
49 | | - Output("plot", "figure"), |
| 48 | + Output("plot", "chart"), |
50 | 49 | ) |
51 | | -def make_figure(ctx: Context, selected_dataset: int = 0) -> go.Figure: |
| 50 | +def make_figure(ctx: Context, selected_dataset: int = 0) -> alt.Chart: |
52 | 51 | dataset = ctx.datasets[selected_dataset] |
53 | | - fig = go.Figure( |
54 | | - layout=Layout( |
55 | | - title=f"DS #{selected_dataset + 1}", |
56 | | - margin=dict(t=40, r=4, b=4, l=4), |
57 | | - autosize=True, |
58 | | - ) |
59 | | - ) |
60 | | - fig.add_trace(go.Bar(x=["A", "B", "C"], y=dataset)) |
61 | | - return fig |
| 52 | + |
| 53 | + # Create a slider |
| 54 | + corner_slider = alt.binding_range(min=0, max=50, step=1) |
| 55 | + # Create a parameter and bind that to the slider |
| 56 | + corner_var = alt.param(bind=corner_slider, value=0, name="cornerRadius") |
| 57 | + # Create another parameter to handle the click events and send the data as |
| 58 | + # specified in the fields |
| 59 | + click_param = alt.selection_point(on="click", name="onClick", |
| 60 | + fields=["a", "b"]) |
| 61 | + # Create a chart type using mark_* where * could be any kind of chart |
| 62 | + # supported by Vega. We can add properties and parameters as shown below. |
| 63 | + chart = alt.Chart(dataset).mark_bar(cornerRadius=corner_var).encode( |
| 64 | + x=alt.X('a:N', title='a'), |
| 65 | + y=alt.Y('b:Q', title='b'), |
| 66 | + tooltip=[ |
| 67 | + alt.Tooltip('a:N'), |
| 68 | + alt.Tooltip('b:Q'), |
| 69 | + ], |
| 70 | + color='b:Q', |
| 71 | + ).properties( |
| 72 | + width=300, |
| 73 | + height=300, |
| 74 | + title="Vega charts" |
| 75 | + ).add_params(corner_var, click_param) |
| 76 | + |
| 77 | + return chart |
0 commit comments