Javascript MPLD3:条形图的标签信息
我试图扩展提供的示例: 我的目的是在单击对象时显示标签,但使用条形图而不是散点图 它不适用于相同的Javascript代码:Javascript MPLD3:条形图的标签信息,javascript,python,django,d3.js,mpld3,Javascript,Python,Django,D3.js,Mpld3,我试图扩展提供的示例: 我的目的是在单击对象时显示标签,但使用条形图而不是散点图 它不适用于相同的Javascript代码: from mpld3 import utils class ClickInfo(plugins.PluginBase): """Plugin for getting info on click""" JAVASCRIPT = """ mpld3.register_plugin("clickinfo", ClickInfo); Click
from mpld3 import utils
class ClickInfo(plugins.PluginBase):
"""Plugin for getting info on click"""
JAVASCRIPT = """
mpld3.register_plugin("clickinfo", ClickInfo);
ClickInfo.prototype = Object.create(mpld3.Plugin.prototype);
ClickInfo.prototype.constructor = ClickInfo;
ClickInfo.prototype.requiredProps = ["id"];
function ClickInfo(fig, props){
mpld3.Plugin.call(this, fig, props);
};
ClickInfo.prototype.draw = function(){
var obj = mpld3.get_element(this.props.id);
obj.elements().on("mousedown",
function(d, i){alert("clicked on bar[" + i + "]");});
}
"""
def __init__(self, bars):
self.dict_ = {"type": "clickinfo",
"id": utils.get_id(bars)}
x = range(0,10)
y = np.random.rand(10)
fig, ax = plt.subplots()
bars = ax.bar(x, y)
plugins.connect(fig, ClickInfo(bars))
但是,我可以为其中一个条获取工作行为。例如,对于plugins.connectfig,单击信息栏[0],单击第一个栏将触发警报Javascript代码
问题:
我怎么能对每个酒吧都有相同的行为
此外,由于我对D3和Javascript缺乏经验,对代码的工作原理进行简短的解释将非常有帮助。由于我找不到MPLD3教程,所以也欢迎任何要学习的资源 你走在正确的轨道上。以下是一种让您拥有的东西发挥作用的方法:
from mpld3 import utils, plugins
class ClickInfo(plugins.PluginBase):
"""Plugin for getting info on click"""
JAVASCRIPT = """
mpld3.register_plugin("clickinfo", ClickInfo);
ClickInfo.prototype = Object.create(mpld3.Plugin.prototype);
ClickInfo.prototype.constructor = ClickInfo;
ClickInfo.prototype.requiredProps = ["ids"];
function ClickInfo(fig, props){
mpld3.Plugin.call(this, fig, props);
};
ClickInfo.prototype.draw = function(){
this.props.ids.forEach(function(id, i) {
var obj = mpld3.get_element(id);
obj.elements().on("mousedown",
function(d){alert("clicked on bar[" + i + "]");});
});
}
"""
def __init__(self, bars):
self.dict_ = {"type": "clickinfo",
"ids": [utils.get_id(bar) for bar in bars]}
x = range(0,10)
y = np.random.rand(10)
fig, ax = plt.subplots()
bars = ax.bar(x, y)
plugins.connect(fig, ClickInfo(bars))
你可以。也许其他人会有时间进一步解释代码的工作原理来扩展这个答案。我遇到了同样的问题,我扩展了带有浮动标签的堆叠条形图的答案,您可以在这里找到: 代码可在此处找到:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import mpld3
from mpld3 import plugins, utils
class BarLabelToolTip(plugins.PluginBase):
JAVASCRIPT = """
mpld3.register_plugin("barlabeltoolTip", BarLabelToolTip);
BarLabelToolTip.prototype = Object.create(mpld3.Plugin.prototype);
BarLabelToolTip.prototype.constructor = BarLabelToolTip;
BarLabelToolTip.prototype.requiredProps = ["ids","labels"];
BarLabelToolTip.prototype.defaultProps = {
hoffset: 0,
voffset: 10,
location: 'mouse'
};
function BarLabelToolTip(fig, props){
mpld3.Plugin.call(this, fig, props);
};
BarLabelToolTip.prototype.draw = function(){
var svg = d3.select("#" + this.fig.figid);
var objs = svg.selectAll(".mpld3-path");
var loc = this.props.location;
var labels = this.props.labels
test = this.fig.canvas.append("text")
.text("hello world")
.style("font-size", 72)
.style("opacity", 0.5)
.style("text-anchor", "middle")
.attr("x", this.fig.width / 2)
.attr("y", this.fig.height / 2)
.style("visibility", "hidden");
function mousemove(d) {
if (loc === "mouse") {
var pos = d3.mouse(this.fig.canvas.node())
this.x = pos[0] + this.props.hoffset;
this.y = pos[1] - this.props.voffset;
}
test
.attr("x", this.x)
.attr("y", this.y);
};
function mouseout(d) {
test.style("visibility", "hidden")
};
this.props.ids.forEach(function(id, i) {
var obj = mpld3.get_element(id);
function mouseover(d) {
test.style("visibility", "visible")
.style("font-size", 24)
.style("opacity", 0.7)
.text(labels[i])
};
obj.elements().on("mouseover", mouseover.bind(this))
});
objs.on("mousemove", mousemove.bind(this))
.on("mouseout", mouseout.bind(this));
}
"""
def __init__(self, ids, labels=None, location="mouse"):
self.dict_ = {"type": "barlabeltoolTip",
"ids": ids,
"labels": labels,
"location": location}
fig, ax = plt.subplots()
x = range(0,10)
y = np.random.rand(10)
bars = ax.bar(x, y)
labels = [round(bar.get_height(),2) for bar in bars]
ids = [utils.get_id(bar) for bar in bars]
plugins.connect(fig, BarLabelToolTip(ids, labels))
谢谢我理解python部分,但对这个仍然感到困惑。props.ids.forEachfunctionid,I{。不过,答案是完美的!这是一个更具python风格的函数编程版本:maplambda id:…,[id1,id2,id3]你好,Robert,虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。请查看此处:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import mpld3
from mpld3 import plugins, utils
class BarLabelToolTip(plugins.PluginBase):
JAVASCRIPT = """
mpld3.register_plugin("barlabeltoolTip", BarLabelToolTip);
BarLabelToolTip.prototype = Object.create(mpld3.Plugin.prototype);
BarLabelToolTip.prototype.constructor = BarLabelToolTip;
BarLabelToolTip.prototype.requiredProps = ["ids","labels"];
BarLabelToolTip.prototype.defaultProps = {
hoffset: 0,
voffset: 10,
location: 'mouse'
};
function BarLabelToolTip(fig, props){
mpld3.Plugin.call(this, fig, props);
};
BarLabelToolTip.prototype.draw = function(){
var svg = d3.select("#" + this.fig.figid);
var objs = svg.selectAll(".mpld3-path");
var loc = this.props.location;
var labels = this.props.labels
test = this.fig.canvas.append("text")
.text("hello world")
.style("font-size", 72)
.style("opacity", 0.5)
.style("text-anchor", "middle")
.attr("x", this.fig.width / 2)
.attr("y", this.fig.height / 2)
.style("visibility", "hidden");
function mousemove(d) {
if (loc === "mouse") {
var pos = d3.mouse(this.fig.canvas.node())
this.x = pos[0] + this.props.hoffset;
this.y = pos[1] - this.props.voffset;
}
test
.attr("x", this.x)
.attr("y", this.y);
};
function mouseout(d) {
test.style("visibility", "hidden")
};
this.props.ids.forEach(function(id, i) {
var obj = mpld3.get_element(id);
function mouseover(d) {
test.style("visibility", "visible")
.style("font-size", 24)
.style("opacity", 0.7)
.text(labels[i])
};
obj.elements().on("mouseover", mouseover.bind(this))
});
objs.on("mousemove", mousemove.bind(this))
.on("mouseout", mouseout.bind(this));
}
"""
def __init__(self, ids, labels=None, location="mouse"):
self.dict_ = {"type": "barlabeltoolTip",
"ids": ids,
"labels": labels,
"location": location}
fig, ax = plt.subplots()
x = range(0,10)
y = np.random.rand(10)
bars = ax.bar(x, y)
labels = [round(bar.get_height(),2) for bar in bars]
ids = [utils.get_id(bar) for bar in bars]
plugins.connect(fig, BarLabelToolTip(ids, labels))