散景回调不更新图表

问题描述

我创建了一个带有滑块和文本输入框的bokeh图,用户可用来调整值。用户输入所需的值后,还可以使用一个更新按钮来刷新图。一切都很好,但是当他们点击update时,图中的数据并没有更新。

我有一个带有“更新”功能的回调,该回调应该刷新数据。我要去哪里错了?

import numpy as np
import pandas as pd
import bokeh
from os.path import dirname,join

from bokeh.io import curdoc,show
from bokeh.layouts import column,layout
from bokeh.models import ColumnDataSource,Div,Select,Slider,TextInput,Range1d,BasicTickFormatter,NumeralTickFormatter,Button,CustomJS
from bokeh.models.tickers import SingleIntervalTicker
from bokeh.plotting import figure,show
from bokeh.events import ButtonClick

# Description text

desc = Div(text=open(join(dirname(__file__),"description.html")).read(),sizing_mode="stretch_width")

# Defining data input

age_input = Slider(title="Age",value = 30,start = 20,end = 67,step = 1)
salary_input = TextInput(title="Current Salary")
savings_input = TextInput(title = "Current Savings")

# Dummy values to start

age = 30
salary = 60000
salaryatret = (salary)*np.power(1.029,67-age)
savings = 100000

# Loading goal data

goalbyage = pd.read_csv('goalbyage.csv',sep = ',',header = 0,index_col = 0)
specificgoal = goalbyage*salaryatret
specificgoal = specificgoal.round(decimals = 0)
specificgoal = specificgoal.astype(int)

# Update function for inputs

ages = specificgoal.index.tolist()
goals = specificgoal['goal'].tolist()

def update():
    age = age_input.value
    salary = int(salary_input.value or 0)
    savings = int(savings_input.value or 0)
    salaryatret = (salary)*np.power(1.029,67-age)
    specificgoal = goalbyage*salaryatret
    specificgoal = specificgoal.round(decimals = 0)
    specificgoal = specificgoal.astype(int)
    ages = specificgoal.index.tolist()
    goals = specificgoal['goal'].tolist()

button = Button(label = "Update",background = "lightblue")
button.on_event(ButtonClick,update)

controls = [salary_input,age_input,savings_input,button]

plot = figure(plot_height = 500,plot_width = 800)
plot.vbar(x = ages,width = 0.8,bottom = 0,top = goals,color = "mediumseagreen")
plot.circle(x = age,y = savings,size = 15,color = "navy")

plot.xaxis.axis_label = "Age"
plot.xaxis.major_label_text_font_size = "10pt"
plot.xaxis.axis_label_text_font_size = "14pt"
plot.x_range = Range1d(20,68)
plot.xaxis.ticker = SingleIntervalTicker(interval = 5,num_minor_ticks = 4)


plot.yaxis.axis_label = "Savings"
plot.yaxis.major_label_text_font_size = "10pt"
plot.yaxis.axis_label_text_font_size = "14pt"
plot.y_range = Range1d(0,10*salaryatret)
plot.yaxis.formatter = BasicTickFormatter(use_scientific=False)
plot.yaxis.formatter=NumeralTickFormatter(format="$0,0")


percgoal = savings/(specificgoal.loc[age]['goal'])
percgoal = percgoal.round(decimals = 2)

inputs = column(controls,width=320,height=500)
inputs.sizing_mode = "fixed"
l = layout([
    [desc],[inputs,plot]])


curdoc().add_root(l)


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)