我是Dash的新手,我正在尝试根据下拉菜单中的选项创建2个图表。
这是一个带有我数据示例的csv,其中
这是我迄今为止制作的代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output
df = pd.read_csv('C:/Users/WILDFIRE/Desktop/dash/datahere.csv')
# this is only for reference
def generate_table(dataframe, max_rows=100):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([html.Td(dataframe.iloc[i][col]) for col in dataframe.columns]) for i in range(min(len(dataframe), max_rows))]
)
available_indicators = df['Barangay']
app = dash.Dash()
first_row = []
brgy_name = 'x'
for i in available_indicators:
if brgy_name == i:
continue
if not pd.isnull(i):
first_row.append(i)
brgy_name = i
else:
continue
app.layout = html.Div(children=[
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in first_row]
),
html.H4(children='Data Visualization'),
generate_table(df),
html.Div(id='pb_output'),
html.Div(id='kgd_output')
])
@app.callback(Output('pb_output', 'children'), [Input('dropdown', 'value')])
def display_graphs(pb_barangay):
# row of brgy names
brgy_candidate = []
brgy_candidate_skip = 3
for row in df.iterrows():
brgy_candidate_skip = brgy_candidate_skip + 1
if brgy_candidate_skip % 4 == 0:
index, data = row
brgy_candidate.append(data.tolist())
else:
continue
# next row
brgy_candidate_data = []
brgy_candidate_data_skip = 3
for row in df.iloc[1:].iterrows():
brgy_candidate_data_skip = brgy_candidate_data_skip + 1
if brgy_candidate_data_skip % 4 == 0:
index, data = row
brgy_candidate_data.append(data.tolist())
else:
continue
return {
'data': [
{'x': i,
'y': j,
'type': 'bar',
'name': 'bg1'} for i in brgy_candidate for j in brgy_candidate_data
],
'layout': {
'title': 'Punong Barangay'
}
}
if __name__ == '__main__':
app.run_server(debug=True)
我还有两个列表:
我在csv文件中循环,只获取每个唯一bg中的pb行并将它们放在相应的列表中: 第一个列表对应于键,第二个列表对应于值。
所以(现在)在用户从下拉列表中选择一个选项后,如何输出条形图,其中第一个列表作为刻度/标签,第二个列表作为这些刻度的值?