散景 - 将数据帧时间序列数据链接到选择交互

时间:2016-09-08 06:36:03

标签: python pandas bokeh

我尝试在Bokeh中创建单个折线图,并将一个数据框中的不同图表链接到Select交互。数据框结构如下所示:

日期,KPI1,KPI2,KPI3,KPI4 ......

日期始终是x轴,而KPI应该可以在y轴上更改。

我无法让它发挥作用。请参阅下面的我当前的代码。说明显示为评论

from app import app
from flask import render_template,request

import pandas as pd
import numpy as np

from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html
from bokeh.plotting import figure, output_file, show
from bokeh.io import output_file, show, vform
from bokeh.charts import Scatter, output_file, show
from bokeh.models import DatetimeTickFormatter
from bokeh.models import CustomJS, ColumnDataSource, Select

from app import data


def createChartHTML():


    #Import data from Excel file
    #Data Structure Looks as follows: Date, KPI 1, KPI 2, KPI 3, KPI 4 ... ... ...
    allData = pd.read_excel(open('Charts.xlsx', 'rb'), sheetname='DATA')

    #Get list of column names
    columnNameList = list(allData.columns.values)
    #Remove date column from column names
    columnNameList.pop(0)


    #Create line chart with markers with initial data
    p = figure(plot_width=800, plot_height=400,x_axis_type="datetime",title="KPI 1")
    p.left[0].formatter.use_scientific = False
    p.line(allData['Date'], allData["KPI 1"], line_width=2)
    p.circle(allData['Date'], allData["KPI 1"], fill_color="white", size=12)

    #Create callback
    source = ColumnDataSource(data=allData)

    #How do I link this code to the allData dataframe?
    #I need to pass a parameter from the selection box to this code to select the right data from the dataframe
    callback = CustomJS(args=dict(source=source), code="""
            var data = source.get('data');
            var f = cb_obj.get('value')
            x = data['LINK THIS TO NEWLY SELECTED DATA?????']
            y = data['Date']
            source.trigger('change');
        """)


    select = Select(title="Option:", value=columnNameList[0], options=columnNameList,callback=callback)
    layout = vform(select,p)
    output_file('plot.html', title='Plot')

    html = file_html(layout, CDN, "my plot")

    return(html)

1 个答案:

答案 0 :(得分:1)

我在这里找到了一个问题的答案:

https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/2rCCRIyXtk8