I am trying to calculate euclidean distance of two points. Initial distance is calculated in the data.
Then, when the user is moving the line, I’d like the distance
column to update based on the new coordinates. I can see that x
and y
columns are updating, but not the distance
column. Below is my attempt:
JavaScript
x
58
58
1
output_file("tools_point_draw.html")
2
3
_tools_to_show = 'box_zoom,pan,save,hover,reset,tap'
4
5
p = figure(x_range=(0, 10), y_range=(0, 10), tools=_tools_to_show,
6
plot_width=862, plot_height=604,
7
title='Plot name')
8
9
p.background_fill_color = 'white'
10
11
d_true = {'x': [2, 3], 'y': [4, 1],
12
'color': ['red', 'red'],
13
'desc': ['true','true']}
14
15
df_true = pd.DataFrame(d_true)
16
df_true['distance'] = np.sqrt(np.sum((df_true['x'] - df_true['y'])**2))
17
source = ColumnDataSource(df_true)
18
19
renderer2 = p.scatter(x='x', y='y', source=source, color='color', size=15,
20
line_color='red', line_width=5)
21
renderer = p.line(x='x', y='y', source=source, color='red',
22
line_dash='dashed', line_width=10)
23
24
columns = [TableColumn(field="x", title="I am X"),
25
TableColumn(field="y", title="I am Y"),
26
TableColumn(field='color', title='color'),
27
TableColumn(field='desc', title='desc'),
28
TableColumn(field='distance', title='distance')]
29
30
update = CustomJS(args=dict(source_data=source), code="""
31
var data = source_data.data;
32
var f = cb_obj.value; //is this necessary?
33
34
//Sum of squares for euclidean
35
for(var i = 0, i < data['x'].length; i < size ; i++) {
36
var res += Math.pow(data['x'][i] - data['y'][i], 2)
37
}
38
39
//Take square root
40
var res2 = Math.sqrt(res)
41
42
//Update table
43
data['distance'] = res2
44
45
source_data.change.emit();
46
47
""")
48
49
update.js_on_change('tap', update)
50
51
table = DataTable(source=source, columns=columns, editable=True, height=200, width=862)
52
53
draw_tool = PointDrawTool(renderers=[renderer, renderer2], empty_value='black')
54
p.add_tools(draw_tool)
55
p.toolbar.active_tap = draw_tool
56
57
show(Column(p, table))
58
Advertisement
Answer
Your callback actually never triggers here. It’s just the pointdraw event doing its thing. You should have your callback trigger when source.data is changed.
JavaScript
1
2
1
source.js_on_change('data', update)
2
I did it for distance from the first point but you could do from origin too.
If it’s from the first point you need to update all the distances each time (since the draw tool lets you drag existing points)
JavaScript
1
62
62
1
from bokeh.plotting import figure
2
from bokeh.io import output_file, show
3
from bokeh.models import DataTable, TableColumn, Column, PointDrawTool, ColumnDataSource, CustomJS
4
import pandas as pd
5
import numpy as np
6
7
output_file("tools_point_draw.html")
8
9
_tools_to_show = 'box_zoom,pan,save,hover,reset,tap'
10
11
p = figure(x_range=(0, 10), y_range=(0, 10), tools=_tools_to_show,
12
plot_width=862, plot_height=604,
13
title='Plot name')
14
15
p.background_fill_color = 'white'
16
17
d_true = {'x': [2, 3], 'y': [4, 1],
18
'color': ['red', 'red'],
19
'desc': ['true','true']}
20
21
df_true = pd.DataFrame(d_true)
22
df_true['distance'] = [0]+[np.sqrt((df_true['x'][i]-df_true['x'][i-1])**2+(df_true['y'][i]-df_true['y'][i-1])**2) for i in range(1,len(df_true['x']))]
23
source = ColumnDataSource(df_true)
24
25
renderer2 = p.scatter(x='x', y='y', source=source, color='color', size=15,
26
line_color='red', line_width=5)
27
renderer = p.line(x='x', y='y', source=source, color='red',
28
line_dash='dashed', line_width=10)
29
30
columns = [TableColumn(field="x", title="I am X"),
31
TableColumn(field="y", title="I am Y"),
32
TableColumn(field='color', title='color'),
33
TableColumn(field='desc', title='desc'),
34
TableColumn(field='distance', title='distance')]
35
36
update = CustomJS(args=dict(source_data=source), code="""
37
var data = source_data.data;
38
var res = 0;
39
40
//Sum of squares for euclidean
41
for(var i = 1; i < data['x'].length ; i++) {
42
res += Math.sqrt(Math.pow(data['x'][i] - data['x'][i-1], 2)+Math.pow(data['y'][i] - data['y'][i-1], 2));
43
44
//Update table
45
data['color'][i] = 'red';
46
data['desc'][i] = 'true';
47
data['distance'][i] = res;
48
}
49
50
source_data.change.emit();
51
""")
52
53
source.js_on_change('data', update)
54
55
table = DataTable(source=source, columns=columns, editable=True, height=200, width=862)
56
57
draw_tool = PointDrawTool(renderers=[renderer, renderer2])
58
p.add_tools(draw_tool)
59
p.toolbar.active_tap = draw_tool
60
61
show(Column(p, table))
62