英文:
highcharter: chart.hide is not a function
问题
我正在尝试使用R中的highcharter将渲染的图表替换为另一个图表,使用以下代码:
test_code <- JS("
function toggleSeries_test() {
var chart1 = Highcharts.charts[0];
var chart2 = Highcharts.charts[1];
if (chart1.visible) {
chart1.hide();
chart2.show();
} else {
chart2.hide();
chart1.show();
}
}
document.getElementById('toggleButton_test').addEventListener('click', toggleSeries_test);
")
但我收到一个错误:chart2.hide不是一个函数
无法确定这是语法错误还是其他什么错误?
英文:
I'm trying to replace the chart rendered with another chart using highcharts, using highcharter in R:
test_code <- JS("
function toggleSeries_test() {
var chart1 = Highcharts.charts[0];
var chart2 = Highcharts.charts[1];
if (chart1.visible) {
chart1.hide();
chart2.show();
} else {
chart2.hide();
chart1.show();
}
}
document.getElementById('toggleButton_test').addEventListener('click', toggleSeries_test);
")
But I'm getting an error: chart2.hide is not a function
Can't figure out whether this is a syntax error or something else?
答案1
得分: 1
没有hide
或show
函数,也没有visible
属性的定义,除非你自己定义它。如果感兴趣,这里是Chart
实例的完整文档,包括所有属性和方法。
通常隐藏图表(而不是销毁它们)的方法是使用CSS。以下是一组非常基本的函数,用于显示、隐藏和查询可见性:
function toggleSeries_test() {
var chart1 = Highcharts.charts[0];
var chart2 = Highcharts.charts[1];
if (isChartVisible(chart1)) {
hideChart(chart1);
showChart(chart2);
} else {
hideChart(chart2);
showChart(chart1);
}
}
function isChartVisible(chart){
return chart.container.style.display !== 'none';
}
function showChart(chart){
return chart.container.style.display = '';
}
function hideChart(chart){
return chart.container.style.display = 'none';
}
当然,所有这些都应该放在JS
调用中,并与事件监听器一起使用。
这还未经测试,因为这里没有完整的代码;还有其他可能性,但我主要关心的是你如何在初始时隐藏第二个图表。请告诉我。
英文:
No, there are no hide
or show
functions, nor is the visible
property ever defined, unless you define it yourself. If ever interested, here's the full documetation for the Chart
instances, with all properties and methods.
The usual way charts are hidden (not to be disposed of) is by using CSS. Here's a very basic set of functions to show, hide and enquire visibility:
function toggleSeries_test() {
var chart1 = Highcharts.charts[0];
var chart2 = Highcharts.charts[1];
if (isChartVisible(chart1)) {
hideChart(chart1);
showChart(chart2);
} else {
hideChart(chart2);
showChart(chart1);
}
}
function isChartVisible(chart){
return chart.container.style.display !== 'none';
}
function showChart(chart){
return chart.container.style.display = '';
}
function hideChart(chart){
return chart.container.style.display = 'none';
}
Of course, all this goes inside inside the JS
call, with the event listener.
This is untested because there isn't the full code here; there are also other possibilities but my doubts are mainly related by the way you manage to hide the second chart initially. Please let me know.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论