【发布时间】:2018-06-08 12:07:04
【问题描述】:
我有一个自定义nvd3scatterChart绘图工具提示的功能。在我想更新状态的函数中,我正在调用另一个函数setState:
chart.tooltip.contentGenerator(function (d) {
var html = "<div>";
d.series.forEach(function(elem){
Object.keys(data_obj).forEach(function(key_1) {
var outer_obj = data_obj[key_1];
if (outer_obj["key"] === elem.key) {
// THIS FUNCTION UPDATES STATE
this.showBarChart(elem.key);
var expr = outer_obj["values"][0]["expr"];
html += "<p>" + elem.key + "</p>";
html += "<p>x = " + d.value + ", y = " + elem.value + "</p>";
}
});
})
html += "</div>";
return html;
}).bind(this);
在我的构造函数内部:
class ScatterChart extends React.Component {
constructor(props){
super(props);
this.createScatterChart = this.createScatterChart.bind(this);
this.showBarChart = this.showBarChart.bind(this);
this.state = {
neuron_name: ""
}
}
还有render:
render() {
console.log(this.state.neuron_name);
return <div width={500} height={500}>
<svg ref={node => this.node = node} width={500} height={500}></svg>
<BarChart datum = { expressionDatum.getDatumForNeuron(this.state.neuron_name) }/>
</div>
}
我怎样才能让contentGenerator 函数看到this 对象?
【问题讨论】:
-
很难说你想做什么/问什么,但也许这个
chart.tooltip.contentGenerator(function (d) {到chart.tooltip.contentGenerator( (d) => { -
我试过了,
this仍然是undefined
标签: javascript reactjs nvd3.js