我正嘗試使用帶有散景(python)的java腳本回調來在給定選定小部件值時更改堆疊條形圖。我正在使用矩形字形來構建堆疊的條形圖。每個小節都是一個年齡段,我的小工具應該選擇一個年齡段來顯示。我需要做的就是告訴圖表停止渲染其他矩形字形,並將一個年齡段的y值更改爲等於其高度(從而使其成爲一個簡單的條形圖)。散景Python;在ColumnDataSource上使用回調來更改帶有選擇小部件的堆疊條形圖
我有我所有的數據在數組中,但然後我把它變成列表爲了傳遞給回調。我的Java腳本警告告訴我,我的for語句工作,它正在讀取y值罰款。我的回調邏輯可能很時髦/不正確,但我更擔心字形根本不會響應回調而改變。也許這是我在回調中推送數據的方式?謝謝!!
from bokeh.models import Callback, ColumnDataSource, Rect, Select
from bokeh.plotting import figure, output_file, show, VBox, gridplot, HBox
import pandas as pd
#Color Dictionary
redcolor5 = {u'All ages': "#720017", u'70+ years': "#bd0026", u'50-69 years': "#f03b20", u'15-49 years': "#fd8d3c", u'Under 5 years': "#f4cc63", u'gridline': '#b2ada6', u'background': '#e3e0db', u'axis' : '#aba9a7'}
#Just a sample of my data
country_both = ['China', 'India', 'United States', 'Russia', 'Japan', 'Indonesia', 'Germany', 'United Kingdom', 'Italy', 'Brazil']
ages_gen = ['Under 5 years', '15-49 years', '50-69 years', '70+ years', 'All ages', 'Age-standardized']
height70yr = [919470, 421922, 321125, 193960, 148946, 107822, 97529, 90198, 81107, 76782]
height50to69 = [640496, 626995, 182338, 195472, 40422, 109242, 44161, 33333, 24964, 64429]
height15to49 = [126094, 139420, 26159, 43239, 5480, 39040, 6829, 4163, 3571, 16152]
heightUnder5 = [10210, 43338, 82, 714, 41, 5255, 0, 26, 0, 1201]
frame = pd.DataFrame({"arBoth_70yr_Cnty": height70yr, "arBoth_5069yr_Cnty": height50to69, "arBoth_1549yr_Cnty": height15to49, "arBoth_5yr_Cnty": heightUnder5})
arBoth_70yr_Cnty = frame['arBoth_70yr_Cnty']
arBoth_5069yr_Cnty = frame['arBoth_5069yr_Cnty']
arBoth_1549yr_Cnty = frame['arBoth_1549yr_Cnty']
arBoth_5yr_Cnty = frame['arBoth_5yr_Cnty']
#Y Values for Stacked bar chart
yUnder5 = (arBoth_5yr_Cnty/2).tolist()
y15to49 = (arBoth_5yr_Cnty+arBoth_1549yr_Cnty/2).tolist()
y50to69 = (arBoth_5yr_Cnty+arBoth_1549yr_Cnty+arBoth_5069yr_Cnty/2).tolist()
y70yr = (arBoth_5yr_Cnty+arBoth_1549yr_Cnty+arBoth_5069yr_Cnty+arBoth_70yr_Cnty/2).tolist()
output_file('UW_TobaccoDeath.html')
#Figure for Stacked bar chart
p1 = figure(title="Top Countries with Death Due to Tobacco by Age",
x_range=country_both, y_range=[0, max(arBoth_5yr_Cnty+arBoth_1549yr_Cnty+arBoth_5069yr_Cnty+arBoth_70yr_Cnty)],
background_fill=redcolor5['background'],
plot_width=700, plot_height = 400,
outline_line_color= None)
#source for callback
source1 = ColumnDataSource(data=dict(x=country_both, yUnder5 = yUnder5, heightUnder5 = heightUnder5, y15to49 = y15to49, height15to49 = height15to49, y50to69 = y50to69, height50to69 = height50to69, y70yr=y70yr, height70yr = height70yr))
#Use rect glyphs for stached bars
p1.rect(x ='x', y ='yUnder5', width =.8, height = 'heightUnder5', source = source1, color=redcolor5['Under 5 years'], alpha=0.8, name = "Under 5")
p1.rect(x = 'x', y ='y15to49', width = .8, height ='height15to49', source = source1, color=redcolor5['15-49 years'], alpha=0.8, name = "15 to 49")
p1.rect(x = 'x', y ='y50to69', width = .8, height ='height50to69', source = source1, color=redcolor5['50-69 years'], alpha = .8, name = "50 to 69")
p1.rect(x = 'x', y ='y70yr', width = .8, height ='height70yr', source = source1, color=redcolor5['70+ years'], alpha = .8, name = "70+ yrs")
#Java script Callbacks for age
#I want this to recognize the 70+ year old drop down selection
#and change the plot so that the height of the glyph is the same as the y value and the 70 year old glyph is the only one that displays
Callback_Age = Callback(args=dict(source1 = source1), code="""
var f = cb_obj.get('value');
var data = source1.get('data');
var x = data['x'];
if (f == '70+ years') {
var y = 0;
var height = data['height70yr'];
alert(height);
}
data['x'].push(x);
data['y'].push(y);
data['height'].push(height);
source1.trigger('change');
""")
#Use the Select widget
dropdown_age = Select(title="Ages:", value=ages_gen[4], options= ages_gen, callback = Callback_Age)
#Display data
filters = VBox(dropdown_age)
tot = HBox(filters, gridplot([[p1]]))
show(tot)
wowza!非常感謝!! –
創建CustomJS的使用。一個問題。你能爲顏色(年齡段)添加一個圖例嗎? – PR102012