D <- data.frame(
x = c(2, 3, 5, 6, 8, 7),
y = c(6, 4, 3, 8, 7, 5))
links <- list(
"0" = c(1, 2),
"1" = c(0, 3, 4),
"2" = c(0, 5),
"3" = c(1, 4),
"4" = c(1, 3),
"5" = c(2, 3, 4)
)
src <- data.frame(
x0 = numeric(0), y0 = numeric(0),
x1 = numeric(0), y1 = numeric(0))
p <- figure(width = 400, height = 500, toolbar_location = NULL,
title="Hover Over Points", tools = NULL) %>%
ly_segments(x0 = x0, y0 = y0, x1 = x1,y1 = y1, color = "olive",
alpha = 0.6, line_width = 3, data = src, lname = "segment") %>%
ly_points(x = x, y = y, data = D, color = "olive", size = 30,
alpha = 0.4, lname = "circle")
callback <- custom_callback(code = sprintf("
var links=%s;
var data = {'x0': [], 'y0': [], 'x1': [], 'y1': []};
var cdata = circle_data.get('data');
var indices = cb_data.index['1d'].indices;
for (i=0; i < indices.length; i++) {
ind0 = indices[i]
for (j=0; j < links[ind0].length; j++) {
ind1 = links[ind0][j];
data['x0'].push(cdata.x[ind0]);
data['y0'].push(cdata.y[ind0]);
data['x1'].push(cdata.x[ind1]);
data['y1'].push(cdata.y[ind1]);
}
}
segment_data.set('data', data);
", rjson::toJSON(links)), lnames = c("circle","segment"))
p <- tool_hover(p, callback = callback, c("circle"))
Created
February 4, 2016 20:30
-
-
Save hafen/aa5ba7414457c9052a87 to your computer and use it in GitHub Desktop.
rbokeh callback test 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdn.pydata.org/bokeh/release/bokeh-0.11.0.min.js"></script> | |
<link href="https://cdn.pydata.org/bokeh/release/bokeh-0.11.0.min.css" rel="stylesheet"> | |
</head> | |
<body> | |
<div id="ee22353cfc993ea8d197d6b630d2791c" class="plotdiv"></div> | |
<script type="text/javascript"> | |
Bokeh.$(function() { | |
var modelid = "1116c50234c5ff97089bd20ab7c95f67"; | |
var elementid = "ee22353cfc993ea8d197d6b630d2791c"; | |
var docid = "16369648e25c9e5dfa47fe4d5d7683e4"; | |
var docs_json = {"16369648e25c9e5dfa47fe4d5d7683e4":{"version":"0.11.0","title":"Bokeh Figure","roots":{"root_ids":["1116c50234c5ff97089bd20ab7c95f67"],"references":[{"type":"Plot","id":"1116c50234c5ff97089bd20ab7c95f67","attributes":{"title":"Hover Over Points","id":"1116c50234c5ff97089bd20ab7c95f67","plot_width":390,"plot_height":490,"x_range":{"type":"Range1d","id":"67f6bbf50224ca223636805f05ea17be"},"y_range":{"type":"Range1d","id":"e0c30dc07d8e3ebe566561e306f7160a"},"left":[{"type":"LinearAxis","id":"b4dfb3170b397de64c06a6afe9434429"}],"below":[{"type":"LinearAxis","id":"65427a36d634a8d3223d4c3975110e61"}],"right":[],"above":[],"renderers":[{"type":"GlyphRenderer","id":"69a056283479df5cd0d89c47e40bb7df"},{"type":"GlyphRenderer","id":"4168d6d854cdfa978cfb42f0aafe577b"},{"type":"LinearAxis","id":"65427a36d634a8d3223d4c3975110e61"},{"type":"Grid","id":"7001d43300a90cada9f6878b73c10ea2"},{"type":"LinearAxis","id":"b4dfb3170b397de64c06a6afe9434429"},{"type":"Grid","id":"50830b64c26942155509bee7b0301df6"}],"tools":[{"type":"HoverTool","id":"796bc2ea1f820f1ef291c812890234d6"}],"tool_events":[],"extra_y_ranges":{},"extra_x_ranges":{},"tags":[],"doc":null,"toolbar_location":null,"min_border_left":4,"min_border_right":4,"min_border_top":4,"min_border_bottom":4,"lod_threshold":null},"subtype":"Figure"},{"type":"ColumnDataSource","id":"cb80add43bc01ce34faed08660f331ab","attributes":{"id":"cb80add43bc01ce34faed08660f331ab","tags":[],"doc":null,"column_names":["x0","y0","x1","y1"],"selected":[],"discrete_ranges":{},"cont_ranges":{},"data":{"x0":[],"y0":[],"x1":[],"y1":[]}}},{"type":"Segment","id":"83c797ce89c036a52ed242343abee908","attributes":{"id":"83c797ce89c036a52ed242343abee908","tags":[],"doc":null,"visible":true,"line_width":{"units":"data","value":3},"line_color":{"units":"data","value":"olive"},"line_alpha":{"units":"data","value":0.6},"x0":{"units":"data","field":"x0"},"y0":{"units":"data","field":"y0"},"x1":{"units":"data","field":"x1"},"y1":{"units":"data","field":"y1"}}},{"type":"Segment","id":"a69b8c07bf13e32450815f3720402fbd","attributes":{"id":"a69b8c07bf13e32450815f3720402fbd","tags":[],"doc":null,"visible":true,"line_width":{"units":"data","value":3},"line_color":{"units":"data","value":"#e1e1e1"},"line_alpha":{"units":"data","value":0.6},"x0":{"units":"data","field":"x0"},"y0":{"units":"data","field":"y0"},"x1":{"units":"data","field":"x1"},"y1":{"units":"data","field":"y1"}}},{"type":"Segment","id":"b62d843b823785b8597d5ccbf4b5a3fe","attributes":{"id":"b62d843b823785b8597d5ccbf4b5a3fe","tags":[],"doc":null,"visible":true,"line_width":{"units":"data","value":3},"line_color":{"units":"data","value":"olive"},"line_alpha":{"units":"data","value":1},"x0":{"units":"data","field":"x0"},"y0":{"units":"data","field":"y0"},"x1":{"units":"data","field":"x1"},"y1":{"units":"data","field":"y1"}}},{"type":"GlyphRenderer","id":"69a056283479df5cd0d89c47e40bb7df","attributes":{"id":"69a056283479df5cd0d89c47e40bb7df","tags":[],"doc":null,"selection_glyph":null,"nonselection_glyph":{"type":"Segment","id":"a69b8c07bf13e32450815f3720402fbd"},"hover_glyph":{"type":"Segment","id":"b62d843b823785b8597d5ccbf4b5a3fe"},"server_data_source":null,"name":null,"data_source":{"type":"ColumnDataSource","id":"cb80add43bc01ce34faed08660f331ab"},"glyph":{"type":"Segment","id":"83c797ce89c036a52ed242343abee908"}}},{"type":"ColumnDataSource","id":"f27cba4b56dd9b82389bca6370f7c545","attributes":{"id":"f27cba4b56dd9b82389bca6370f7c545","tags":[],"doc":null,"column_names":["x","y","line_color","fill_color"],"selected":[],"discrete_ranges":{},"cont_ranges":{},"data":{"x":[2,3,5,6,8,7],"y":[6,4,3,8,7,5],"line_color":["olive","olive","olive","olive","olive","olive"],"fill_color":["olive","olive","olive","olive","olive","olive"]}}},{"type":"Circle","id":"81add56994bfbeb570c74c66b7fb57b9","attributes":{"id":"81add56994bfbeb570c74c66b7fb57b9","tags":[],"doc":null,"size":{"units":"screen","value":30},"visible":true,"line_alpha":{"units":"data","value":0.4},"fill_alpha":{"units":"data","value":0.2},"x":{"units":"data","field":"x"},"y":{"units":"data","field":"y"},"line_color":{"units":"data","field":"line_color"},"fill_color":{"units":"data","field":"fill_color"}}},{"type":"Circle","id":"a0da3bdc072ef8378d6863df92e157f1","attributes":{"id":"a0da3bdc072ef8378d6863df92e157f1","tags":[],"doc":null,"size":{"units":"screen","value":30},"visible":true,"line_alpha":{"units":"data","value":0.4},"fill_alpha":{"units":"data","value":0.2},"x":{"units":"data","field":"x"},"y":{"units":"data","field":"y"},"line_color":{"units":"data","value":"#e1e1e1"},"fill_color":{"units":"data","value":"#e1e1e1"}}},{"type":"Circle","id":"256c4da2dae4d25deff94d71cf671698","attributes":{"id":"256c4da2dae4d25deff94d71cf671698","tags":[],"doc":null,"size":{"units":"screen","value":30},"visible":true,"line_alpha":{"units":"data","value":1},"fill_alpha":{"units":"data","value":1},"x":{"units":"data","field":"x"},"y":{"units":"data","field":"y"},"line_color":{"units":"data","field":"line_color"},"fill_color":{"units":"data","field":"fill_color"}}},{"type":"GlyphRenderer","id":"4168d6d854cdfa978cfb42f0aafe577b","attributes":{"id":"4168d6d854cdfa978cfb42f0aafe577b","tags":[],"doc":null,"selection_glyph":null,"nonselection_glyph":{"type":"Circle","id":"a0da3bdc072ef8378d6863df92e157f1"},"hover_glyph":{"type":"Circle","id":"256c4da2dae4d25deff94d71cf671698"},"server_data_source":null,"name":null,"data_source":{"type":"ColumnDataSource","id":"f27cba4b56dd9b82389bca6370f7c545"},"glyph":{"type":"Circle","id":"81add56994bfbeb570c74c66b7fb57b9"}}},{"type":"CustomJS","id":"f10687b56bdeb489f1577e5c896bc84c","attributes":{"id":"f10687b56bdeb489f1577e5c896bc84c","tags":[],"doc":null,"code":"\nvar links={\"0\":[1,2],\"1\":[0,3,4],\"2\":[0,5],\"3\":[1,4],\"4\":[1,3],\"5\":[2,3,4]};\nvar data = {'x0': [], 'y0': [], 'x1': [], 'y1': []};\nvar cdata = circle_data.get('data');\nvar indices = cb_data.index['1d'].indices;\nfor (i=0; i < indices.length; i++) {\n ind0 = indices[i]\n for (j=0; j < links[ind0].length; j++) {\n ind1 = links[ind0][j];\n data['x0'].push(cdata.x[ind0]);\n data['y0'].push(cdata.y[ind0]);\n data['x1'].push(cdata.x[ind1]);\n data['y1'].push(cdata.y[ind1]);\n }\n}\nsegment_data.set('data', data);\n","args":{"circle_glyph":{"type":"Circle","id":"81add56994bfbeb570c74c66b7fb57b9"},"circle_ns_glyph":{"type":"Circle","id":"a0da3bdc072ef8378d6863df92e157f1"},"circle_hov_glyph":{"type":"Circle","id":"256c4da2dae4d25deff94d71cf671698"},"circle_data":{"type":"ColumnDataSource","id":"f27cba4b56dd9b82389bca6370f7c545"},"circle_glyph_rend":{"type":"GlyphRenderer","id":"4168d6d854cdfa978cfb42f0aafe577b"},"segment_glyph":{"type":"Segment","id":"83c797ce89c036a52ed242343abee908"},"segment_ns_glyph":{"type":"Segment","id":"a69b8c07bf13e32450815f3720402fbd"},"segment_hov_glyph":{"type":"Segment","id":"b62d843b823785b8597d5ccbf4b5a3fe"},"segment_data":{"type":"ColumnDataSource","id":"cb80add43bc01ce34faed08660f331ab"},"segment_glyph_rend":{"type":"GlyphRenderer","id":"69a056283479df5cd0d89c47e40bb7df"}}}},{"type":"HoverTool","id":"796bc2ea1f820f1ef291c812890234d6","attributes":{"id":"796bc2ea1f820f1ef291c812890234d6","tags":[],"doc":null,"plot":{"type":"Plot","id":"1116c50234c5ff97089bd20ab7c95f67","subtype":"Figure"},"renderers":[{"type":"GlyphRenderer","id":"4168d6d854cdfa978cfb42f0aafe577b"}],"names":[],"always_active":true,"tooltips":null,"callback":{"type":"CustomJS","id":"f10687b56bdeb489f1577e5c896bc84c"}}},{"type":"Range1d","id":"67f6bbf50224ca223636805f05ea17be","attributes":{"id":"67f6bbf50224ca223636805f05ea17be","tags":[],"doc":null,"start":1.58,"end":8.42}},{"type":"Range1d","id":"e0c30dc07d8e3ebe566561e306f7160a","attributes":{"id":"e0c30dc07d8e3ebe566561e306f7160a","tags":[],"doc":null,"start":2.65,"end":8.35}},{"type":"LinearAxis","id":"65427a36d634a8d3223d4c3975110e61","attributes":{"id":"65427a36d634a8d3223d4c3975110e61","tags":[],"doc":null,"plot":{"type":"Plot","id":"1116c50234c5ff97089bd20ab7c95f67","subtype":"Figure"},"axis_label":"x0","formatter":{"type":"BasicTickFormatter","id":"213a7183b29377cbf73d65248b32d589"},"ticker":{"type":"BasicTicker","id":"b740287c69ab4b6fed4f3c6086aecf4f"},"visible":true,"axis_label_text_font_size":"12pt"}},{"type":"BasicTickFormatter","id":"213a7183b29377cbf73d65248b32d589","attributes":{"id":"213a7183b29377cbf73d65248b32d589","tags":[],"doc":null}},{"type":"BasicTicker","id":"b740287c69ab4b6fed4f3c6086aecf4f","attributes":{"id":"b740287c69ab4b6fed4f3c6086aecf4f","tags":[],"doc":null,"num_minor_ticks":5}},{"type":"Grid","id":"7001d43300a90cada9f6878b73c10ea2","attributes":{"id":"7001d43300a90cada9f6878b73c10ea2","tags":[],"doc":null,"dimension":0,"plot":{"type":"Plot","id":"1116c50234c5ff97089bd20ab7c95f67","subtype":"Figure"},"ticker":{"type":"BasicTicker","id":"b740287c69ab4b6fed4f3c6086aecf4f"}}},{"type":"LinearAxis","id":"b4dfb3170b397de64c06a6afe9434429","attributes":{"id":"b4dfb3170b397de64c06a6afe9434429","tags":[],"doc":null,"plot":{"type":"Plot","id":"1116c50234c5ff97089bd20ab7c95f67","subtype":"Figure"},"axis_label":"y0","formatter":{"type":"BasicTickFormatter","id":"12f86eacc8a677ce31c906887fe7770b"},"ticker":{"type":"BasicTicker","id":"4e08495e9900e7922b61ce97557df5e2"},"visible":true,"axis_label_text_font_size":"12pt"}},{"type":"BasicTickFormatter","id":"12f86eacc8a677ce31c906887fe7770b","attributes":{"id":"12f86eacc8a677ce31c906887fe7770b","tags":[],"doc":null}},{"type":"BasicTicker","id":"4e08495e9900e7922b61ce97557df5e2","attributes":{"id":"4e08495e9900e7922b61ce97557df5e2","tags":[],"doc":null,"num_minor_ticks":5}},{"type":"Grid","id":"50830b64c26942155509bee7b0301df6","attributes":{"id":"50830b64c26942155509bee7b0301df6","tags":[],"doc":null,"dimension":1,"plot":{"type":"Plot","id":"1116c50234c5ff97089bd20ab7c95f67","subtype":"Figure"},"ticker":{"type":"BasicTicker","id":"4e08495e9900e7922b61ce97557df5e2"}}}]}}}; | |
var refkey = Object.keys(docs_json)[0] | |
var refs = docs_json[refkey].roots.references | |
function traverseObject(obj) { | |
for(var key in obj) { | |
if(obj[key].constructor === Object) { | |
traverseObject(obj[key]); | |
} else if(obj[key].constructor === Array) { | |
for (var i = 0; i < obj[key].length; i++) { | |
if(obj[key][i] === null) | |
obj[key][i] = NaN; | |
}; | |
} | |
}; | |
} | |
for(var i = 0; i < refs.length; i++) { | |
if(refs[i].type === "ColumnDataSource") | |
traverseObject(refs[i].attributes.data); | |
}; | |
var render_items = [{ | |
"docid": docid, | |
"elementid": elementid, | |
"modelid": modelid | |
}]; | |
Bokeh.set_log_level('info'); | |
Bokeh.embed.embed_items(docs_json, render_items); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment