滑块回调与散景和JavaScript

时间:2017-09-17 19:26:12

标签: javascript python bokeh

我正在使用滑块来显示一些单词。所有的名单都很好。我有20多个元素。滑块使用Bokeh 0.12.6 ,但现在有些元素根本没有显示 0.12.9 。奇怪的是,每次运行代码时,这似乎都不是随机的。例如,元素6不会每次出现但都存在于列表中。 我正在使用滑块的值来同时更改hbar和文本。 有同样问题的人吗?我在回调函数中犯了错误吗?

sourcesl = ColumnDataSource(data=dict(ysl=ysl, rightsl=rightsl, hightsl=hightsl, leftsl=leftsl, thertpctn0=thertpctn0, thertpct0=thertpct0, wlen=wlen, wtext=wtext, thepct=thepct, prtg=prtg))
callback = CustomJS(args=dict(source=sourcesl), code="""
            var data = source.data;
            var w = cb_obj.value

            rightsl = data['rightsl']
            prtg = data['prtg']
            wtext = data['wtext']
            ysl = data['ysl']            
            thertpctn0 = data['thertpctn0']
            thertpct0 = data['thertpct0']
            wlen = data['wlen']                
            thepct = data['thepct']           

            var som = 0
            for (i = 0; i < w; i++) {
                som = som +thertpctn0[i]
            }
            rightsl[0] = 100*(som/wlen[0])
            rightsl[1] = 100                
            wtext[0] = thertpct0[w] 
            prtg[0] = thepct[w]

            source.change.emit();
        """)
        slider = Slider(start=1, end=20, value=20, step=1, title="SLIDER for Words Most Used")
        slider.js_on_change('value', callback)
psl = figure(width=355, height=200,tools="pan,reset,previewsave")
        psl.y_range = Range1d(0,4)
        psl.x_range = Range1d(0, 100)
        psl.hbar('ysl', 'hightsl','rightsl', 'leftsl',  color="LightSeaGreen", source=sourcesl)
psl.text(45, 3, text='wtext',
                alpha=0.6667, text_font_size='32pt', text_baseline='middle',
                text_align='center', source=sourcesl)
        nby = 2
        psl.text(45, nby, text='prtg',
                 alpha=1, text_font_size='12pt', text_baseline='middle',
                 text_align='center', source=sourcesl)
        tex = ['\u0025']
        sourcespct = ColumnDataSource(data=dict(tex=tex))
        psl.text(54, nby, text='tex',
                 alpha=0.6667, text_font_size='12pt', text_baseline='middle',
                 text_align='center', source=sourcespct)

enter image description here enter image description here

*** UPDATED ***最后要测试的代码:6(?)等元素不显示 以下是使用虚假数据进行测试的代码:

    def test100():
    from bokeh.models import Slider, Range1d

    psl = figure(width=355, height=150, tools="pan,reset,previewsave")

    ysl = [1,2]
    hightsl = [0.5, 0.1]
    leftsl = [0, 0]
    thertpctn0 = [0]
    thertpctn = []
    for i in range(1, 21):
        thertpctn.append(i*.1)
    thertpctn0.extend(thertpctn)

    somme = sum(thertpctn)
    ratio = 100 * (somme / 20)
    rightsl = [ratio, 0]
    thertpct0= ["0"]
    thertpct = []
    for i in range(1, 21):
        j = "{}abcd".format(str(i))
        thertpct.append(j)
    thertpct0.extend(thertpct)
    wlen = [20]
    wtext = [thertpct0[-1]]
    # for i in range(0, 20):
    #     j = "{}abcdefghij".format(str(i))
    #     wtext.append(j)
    thepct = []
    prtg = []

    sourcesl = ColumnDataSource(data=dict(ysl=ysl, rightsl=rightsl, hightsl=hightsl, leftsl=leftsl, thertpctn0=thertpctn0,
            thertpct0=thertpct0, wlen=wlen, wtext=wtext)) #, thepct=thepct, prtg=prtg))

    callback = CustomJS(args=dict(source=sourcesl), code="""
                    var data = source.data;
                    var w = cb_obj.value

                    rightsl = data['rightsl']

                    wtext = data['wtext']
                    ysl = data['ysl']            
                    thertpctn0 = data['thertpctn0']
                    thertpct0 = data['thertpct0']
                    wlen = data['wlen']                


                    var som = 0
                    for (i = 0; i < w; i++) {
                        som = som +thertpctn0[i]
                    }
                    rightsl[0] = 100*(som/wlen[0])
                    rightsl[1] = 100                
                    wtext[0] = thertpct0[w] 


                    source.change.emit();
                """) # prtg[0] = thepct[w] thepct = data['thepct'] prtg = data['prtg']

    slider = Slider(start=1, end=20, value=20, step=1, title="SLIDER for Words Most Used")
    slider.js_on_change('value', callback)

    psl.y_range = Range1d(0, 4)
    psl.x_range = Range1d(0, 100)
    psl.hbar('ysl', 'hightsl', 'rightsl', 'leftsl', color="LightSeaGreen", source=sourcesl)

    psl.text(45, 3, text='wtext',
                 alpha=0.6667, text_font_size='32pt', text_baseline='middle',
                 text_align='center', source=sourcesl)
    # nby = 2
    # psl.text(45, nby, text='prtg',
    #              alpha=1, text_font_size='12pt', text_baseline='middle',
    #              text_align='center', source=sourcesl)

    # tex = ['\u0025']
    # sourcespct = ColumnDataSource(data=dict(tex=tex))
    # psl.text(54, nby, text='tex',
    #          alpha=0.6667, text_font_size='12pt', text_baseline='middle',
    #          text_align='center', source=sourcespct)
    wdgsld = WidgetBox(slider)
    ltest = column([wdgsld, psl])
    show(ltest)

0 个答案:

没有答案