javascript - 使用 renderTo 在图表上使用 Chart.update

标签 javascript jquery highcharts

我在本地声明了一个 highcart 函数:

function render(){
  var Chart=null
  Chart = new Highcharts.Chart({
      chart: {
          renderTo: container,
          height: 400
      },
      title: {
          text: 'Chart.update'
      },

      subtitle: {
          text: 'Plain'
      },

      xAxis: {
          categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
      },

      series: [{
          type: 'column',
          colorByPoint: true,
          data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
          showInLegend: false
      }]
  });
}

假设我无法更改 render() 函数,我可以使用 Chart.update 工具来更改属性吗?

基本上我想重新创建功能 here ,但是对于 this example不改变功能。

这是否就像要求访问函数外部的局部变量一样,或者它仍然在全局某个地方定义?

最佳答案

您必须在 render 函数之前定义 var Chart=null

Fiddle

JS

var chart = null
function render() {
  chart = new Highcharts.Chart({
    chart: {
      renderTo: container,
      height: 400
    },
    title: {
      text: 'Chart.update'
    },

    subtitle: {
      text: 'Plain'
    },

    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    series: [{
      type: 'column',
      colorByPoint: true,
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
      showInLegend: false
    }]
  });
}
render()

function plain() {
  chart.update({
    chart: {
      inverted: false,
      polar: false
    },
    subtitle: {
      text: 'Plain'
    }
  });
}

function inverted() {
  chart.update({
    chart: {
      inverted: true,
      polar: false
    },
    subtitle: {
      text: 'Inverted'
    }
  });
}

function polar() {
  chart.update({
    chart: {
      inverted: false,
      polar: true
    },
    subtitle: {
      text: 'Polar'
    }
  });
}

HTML

<div id="container"></div>
<button id="plain" class="autocompare" onclick="plain()">Plain</button>
<button id="inverted" class="autocompare" onclick="inverted()">Inverted</button>
<button id="polar" class="autocompare" onclick="polar()">Polar</button>

关于javascript - 使用 renderTo 在图表上使用 Chart.update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848218/

相关文章:

javascript - d3js 为时间刻度轴设置 tickValues

java - 刷新页面后不考虑 utf8 html 输入标记

javascript - 如何使用 HTML 阻止图像

javascript - 如何检查元素在 AngularJS 模板上是否可见?

javascript - 无法从 Node JS 连接到 mongo 本地数据库

php - Ajax 状态为 `success` 但回调无法正常工作

javascript - 使用 Javascript 打印动态生成的页面

python - 来自 django 模型的数据未传递到 highcharts 图表

javascript - Highcharts,将误差线与数据模块结合使用

java - Highcharts-Export Server 2.0.1 Windows 下错误