There are two dropdowns, that show the columns of the dataset and two radio buttons to select the type of chart, bar or pie. If the radio button is selected the chart should render, but unable to render the graph. It shows just the layout with lines, no plot.
‘’’’
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as go
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
app = dash.Dash(name, external_stylesheets=external_stylesheets)
df = pd.read_csv(r’data.csv’)
columns=list(df)
app.layout = html.Div([
html.Div([
html.Div([
html.P('X axis'),
dcc.Dropdown(
id='xaxis-column',
options=[{'label': i, 'value': i} for i in columns],
value=columns[0]
),
html.P('Y axis'),
dcc.Dropdown(
id='yaxis-column',
options=[{'label': i, 'value': i} for i in columns],
value=columns[1]
),
],
style={'width': '48%', 'display': 'inline-block'}),
html.Div([
dcc.RadioItems(
id='chart-type',
options=[{'label': i, 'value': i} for i in ['Bar plot', 'Pie plot']],
value='',
labelStyle={'display': 'inline-block'}
),
])
]),dcc.Graph(id='graph-output')
])
app.config[‘suppress_callback_exceptions’] = True
@app.callback(Output(‘graph-output’, ‘figure’),
[Input(‘xaxis-column’, ‘value’),
Input(‘yaxis-column’, ‘value’),
Input(‘chart-type’, ‘value’)])
def update_graph(xaxis_column_name, yaxis_column_name, chart_type):
x=df[xaxis_column_name]
y=df[yaxis_column_name]
if chart_type=='Bar plot':
fig = go.Figure(
data=[go.Bar(
x=df[xaxis_column_name],
y=df[yaxis_column_name])
],
layout=go.Layout(
xaxis=dict(
tickangle=-45,
tickfont=dict(family='Rockwell', color='crimson', size=14)
),
yaxis=dict(
showticklabels=True
),
)
)
return fig
if chart_type=='Pie plot':
trace=go.Pie(labels=df[xaxis_column_name], values=df[yaxis_column_name])
return {'data':[trace]}
if name == ‘main’:
app.run_server(debug=False)