2016-08-04 48 views
0

我想将数据表添加到图表。如何将数据表添加到动态生成的高图

我想在这里显示的实施方案:http://jsfiddle.net/highcharts/z9zXM/

但它没有为我工作。

我怀疑它是因为我如何实例化highcharts。

在上面的例子中,图表是通过实例化Highcharts对象生成的。

我的代码:

// data from an ajax call 
$.each(data, function(indicator, questions) { 

      indicator_type = ""; 

      $.each(questions, function(question, value) { 

       dataChartType = "column"; 

       series = []; 

       categories = []; 

       category_totals = {}; 

       if(value.programs == null) { 
        return true; 
       } 

       $.each(value.programs, function(program, body) { 

        total = 0; 

        values = []; 

        $.each(body, function(j, k) { 

         if (categories.indexOf(j) == -1) { 

          categories.push(j); 

          category_totals[j] = 0; 

         } 

         if(k != 0) { 
          values.push(k); 
         } else { 
          values.push(null); 
         } 


         category_totals[j] += parseInt(k, 10); 

         total += k; 


        }); 

        series.push({ 
         data: values, 
         total: total, 
         name: program //question 
        }); 

       }); // eo each program 

       var chartDiv = document.createElement('div'); 

       chartDiv.className = "chart"; 

       $('.charts_wrap').append(chartDiv); 

       $(chartDiv).highcharts({ 
        events: { 
         load: Highcharts.drawTable 
        }, 
        chart: { 
         type: dataChartType 
        }, 
        xAxis: { 
         categories: categories 
        }, 
        legend: { 
         layout: 'vertical', 
         backgroundColor: '#FFFFFF', 
         align: 'right', 
         verticalAlign: 'top', 
         y: 60, 
         x: -60 
        }, 
        tooltip: { 
         formatter: function() { 
          return '<strong>' + this.series.name + '</strong><br/>' + this.x + ': ' + this.y; 
         } 
        }, 
        plotOptions: { 
         line: { 
          connectNulls: true 
         }, 
         column: { 
          stacking: 'normal', 
          dataLabels: { 
           enabled: false, 
           color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
           style: { 
            textShadow: '0 0 3px w' 
           } 
          } 
         } 
        }, 
        series: series, 
        title:{ text: indicator }, 
        subtitle:{ text: question } 
       }); 


      }); // EO each question 

     }); // eo each indicator 

回答

1

当实例highcharts这样的:

$("#container").highcharts({ ... 

事件选项需要包含图表选项里面:

$("#container").highcharts({ 
    chart: { 
     type: 'column', 
     events: { 
      load: Highcharts.drawTable 
     }, 
    }, 
    ... 
相关问题