我正在使用 Bokeh、DataShader 和 HoloViews 为大数据实现 ScatterPlot。ScatterPlot 部分本身已经完成,但有一个要求我遇到了问题:我需要能够捕获用户双击的数据坐标。
我在 Jupyter notebook 中找到了一些对我有用的代码,但由于某种原因,当迁移到 Python 和 Bokeh Server 时,它没有。
这是代码:
from os.path import dirname, join
import csv
import holoviews as hv
import datashader as ds
import pandas as pd
#import parambokeh
from holoviews.operation.datashader import aggregate, shade, datashade, dynspread
from holoviews.operation import decimate
from holoviews.streams import RangeXY
from holoviews import streams
from bokeh.plotting import curdoc
hv.extension('bokeh')
class MyScatterPlotBokeh:
#
# PARAMETERS:
# - keyFieldName : Field that uniquely identify a row
# - xFieldName : Field Name used for x-Axis
# - yFieldName : Field Name used for y-Axis
# - colorFieldName : Field Name used to select circle colors
# - otherFieldNames : Comma separated value variable with the name of all fields we want to show data
#
def __init__(self, filePath, title, screenWidth, screenHeight, keyFieldName, xFieldName, yFieldName, colorFieldName, otherFieldNames):
self._screenHeight = screenHeight
self._screenWidth = screenWidth
# Read data from file
df = pd.read_csv(filePath)
df[colorFieldName]=df[colorFieldName].astype("category")
# Creating graphic
hover_opts = hv.opts("QuadMesh [tools=['hover']] (alpha=0 hover_alpha=0.2)")
self._points = hv.Points(df,kdims=[xFieldName,yFieldName])
self._dynamic_hover = datashade(self._points) * dynspread(datashade(self._points, aggregator=ds.count_cat(colorFieldName))) * \
hv.util.Dynamic(aggregate(self._points, width=50, height=50, streams=[RangeXY]),operation=hv.QuadMesh)
# Interaction - Point selection
double_tap = streams.DoubleTap(transient=True, source=self._points)
self._countTap = 0
self._taps = []
def record_taps(x,y):
print "clicked"
self._countTap += 1
if self._countTap>2:
self._countTap = 0
if None not in [x,y]:
if self._countTap == 1:
self._taps.insert(0,(x, y))
else:
self._taps.append((x, y))
print "TAPS LOOK LIKE " + str(self._taps)
return hv.Points(self._taps, vdims='Taps')
self._finalPlot = self._dynamic_hover + hv.DynamicMap(record_taps, streams=[double_tap])
def draw(self):
hv.renderer('bokeh').server_doc(self._finalPlot)
执行服务器后,我可以看到显示“单击”消息,但在双击浏览器后从未出现过。就像双击事件没有响应一样。最终我需要将点击的 X 和 Y 坐标发送到浏览器,我认为我可以通过从 python 代码创建一个输入框来完成,但我还没有。
任何帮助将不胜感激。