javascript highcharts 构建器函数

标签 javascript highcharts

我正在尝试创建一个函数,该函数将根据传递的参数动态构建 Highcharts 图表。我按照以下方式进行:

function makeChart(name, title, series)
{
  var options = {
    chart: {
        type: 'areaspline',
        renderTo: name
    },
    credits: { enabled: false },           
    legend: { enabled: true },
    title: {
        text: title
    },
    xAxis: {
        type: 'datetime'
    },
    yAxis: {             
      gridLineDashStyle: 'dot',             
      title: {               
        text: 'Quantity'
      }
    },
    plotOptions: {
        areaspline: {               
          animation: false,               
          stacking: '',               
          lineWidth: 1,               
          marker: { enabled: false }             
        }
    },
    series: [] //chart does not display except title. It will draw if I paste the data here manually
 };
 this.chart = new Highcharts.Chart(options);
 for (index = 0; index < series.length; ++index) {

 options.series[index] = {'name':series[index][0], 'data':series[index][1], 'color':series[index][2], 'fillOpacity': .3};
  }
}
makeChart('container2', 'second chart', [['thisisname1', [20,21,22,23,24,25,26,27,28], '#d8d8d8']]);//calling function with test parameters

但我能看到的只是图表标题。我猜问题在于将数据添加到系列数组中。我尝试用多种方式添加它,但它不起作用,尽管我看到如果我 console.log(options.series) 则数据已添加。有什么想法如何解决这个问题吗?谢谢。

最佳答案

this.chart = new Highcharts.Chart(options); 放在 for 循环之后。

您要在图表初始化后添加数据,要以这种方式工作,您需要告诉 HighCharts 重新绘制自身,更简单的选择是在循环后初始化。 :)

关于javascript highcharts 构建器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40261218/

相关文章:

javascript - 使用 jQuery 将光标更改为自定义图像

javascript - Canvas Sprite 表错误

javascript - .css 转换不适用于 jquery

javascript - 如何在 Highcharts 中为轴设置静态最小值

javascript - wkhtmltoimage 不显示 highcharts 图形的网格线

c# - 如何删除危险字符(即脚本标签)?

javascript - Angular 2 更改禁用/启用选择选项

javascript - 仅当客户端浏览器开始卡顿时,是否可以在 highcharts 中打开升压模式?

mysql - 如何从 mysql 获取 highcharts-convert.js 中的系列和类别数据

javascript - 如何向 HighChart 图表中的 Y 轴值添加 % 符号?