如何在数组中获取对象值

时间:2016-06-05 12:30:04

标签: javascript jquery highcharts

这是我生成图表的代码:

jQuery(document).ready(function()
            {
                var dataChart = {
                    "series_data":[
                        {"name":"Brunei Darussalam","data":[1]},
                        {"name":"Saudi Arabia","data":[1]},
                        {"name":"Singapore","data":[2]},
                        {"name":"Malaysia","data":[3]},
                        {"name":"Korea","data":[3]},
                        {"name":"Taiwan","data":[5]}],
                    "total":[{"total":15}]
                };
                var option = {
                    chart: {
                        type: 'column',
                        borderWidth: 1
                    },
                    title: {
                        text: 'Jumlah Registrasi'
                    },
                    subtitle: {
                        text: '*berdasarkan negara tujuan*'
                    },
                    xAxis: {
                        visible: false
                    },
                    yAxis: {
                        title: {
                            text: 'Jumlah'
                        },
                        allowDecimals: false,
                        minRange: 1,
                        tickInterval: 1
                    },
                    labels: {
                       items:[{
                         html: 'Total: '+dataChart.total, // this is the problem that im having now
                           style: {
                              left: '400px',
                              top: '10px'
                           }
                       }]
                    },
                    plotOptions: {
                        column: {
                            dataLabels: {
                                enabled: true
                            },
                            enableMouseTracking: false
                        }
                    },
                    series: ''
                };

                option.chart.renderTo = 'negara';
                option.series   = dataChart.series_data;
                var chartNegara = new Highcharts.Chart(option);
            });

正如您所看到的,var dataChart,series_data和total中有2个数组。总数是series_data的总和。所以我有一个问题,如何在图表中显示这个总数。

这是我的代码的jsfiddle:https://jsfiddle.net/HealMee/pq30un8f/1

现在的错误是:

Total: [object Object]

1 个答案:

答案 0 :(得分:1)

您可以将chart更改为以下内容:

chart: {
  type: 'column',
    borderWidth: 1,
      events: {
        load: function(event) {
          var total = dataChart.total[0].total;
          var text = this.renderer.text(
            'Total: ' + total,
            this.plotLeft,
            this.plotTop - 20
          ).attr({
            zIndex: 5
          }).add()
          }
      }
},

小提琴here ..干杯。