|
|
|
|
|
|
|
|
|
import json |
|
from h2o_wave import main, app, Q, ui, data |
|
|
|
|
|
spec_linear_scale = json.dumps(dict( |
|
mark='bar', |
|
encoding=dict( |
|
x=dict(field='a', type='ordinal'), |
|
y=dict(field='b', type='quantitative') |
|
) |
|
)) |
|
|
|
|
|
spec_log_scale = json.dumps(dict( |
|
mark='bar', |
|
encoding=dict( |
|
x=dict(field='a', type='ordinal'), |
|
y=dict(field='b', type='quantitative', scale=dict(type='log')) |
|
) |
|
)) |
|
|
|
|
|
plot_data = data(fields=["a", "b"], rows=[ |
|
["A", 28], ["B", 55], ["C", 43], |
|
["D", 91], ["E", 81], ["F", 53], |
|
["G", 19], ["H", 87], ["I", 52] |
|
]) |
|
|
|
|
|
log_scale_command = ui.command( |
|
name='to_log_scale', |
|
label='Log Scale', |
|
icon='LineChart', |
|
) |
|
linear_scale_command = ui.command( |
|
name='to_linear_scale', |
|
label='Linear Scale', |
|
icon='LineChart', |
|
) |
|
|
|
|
|
@app('/demo') |
|
async def serve(q: Q): |
|
if q.client.plot_added: |
|
example = q.page['example'] |
|
if q.args.to_log_scale: |
|
|
|
example.title = 'Plot (Log Scale)', |
|
example.specification = spec_log_scale |
|
example.commands = [linear_scale_command] |
|
else: |
|
|
|
example.title = 'Plot (Linear Scale)', |
|
example.specification = spec_linear_scale |
|
example.commands = [log_scale_command] |
|
else: |
|
q.page['example'] = ui.vega_card( |
|
box='1 1 2 4', |
|
title='Plot (Linear Scale)', |
|
specification=spec_linear_scale, |
|
data=plot_data, |
|
commands=[log_scale_command], |
|
) |
|
|
|
q.client.plot_added = True |
|
|
|
await q.page.save() |
|
|