highcharter: chart.hide is not a function

huangapple go评论145阅读模式
英文:

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 &lt;- JS(&quot;
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(&#39;toggleButton_test&#39;).addEventListener(&#39;click&#39;, toggleSeries_test);
&quot;)

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

没有hideshow函数,也没有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 !== &#39;none&#39;;
  }

  function showChart(chart){
     return chart.container.style.display = &#39;&#39;;
  }

  function hideChart(chart){
     return chart.container.style.display = &#39;none&#39;;
  }

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.

huangapple
  • 本文由 发表于 2023年8月11日 02:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定