本文介绍了如何在javascript中动态添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我是一个总的JavaScript新手,所以请忍受我。我有以下脚本来使用Highchart框架绘制饼图。

$ $(function(){
var options = {
colors:[#66CC00,#FF0000,#FF6600],
图:{
renderTo:'container',
plotBackgroundColor:null,
plotBorderWidth:null,
plotShadow:true
},
title:{
text:'Host Status'
},
tooltip:{
formatter:function(){
return'< b>'+ this.point.name +'< / b> ;:'+ this.total;
}
},
plotOptions:{
pie:{
allowPointSelect:true,
cursor:'pointer',
dataLabels:{
enabled:true,
颜色:'#000000',
connectorColor:'#000000',
formatter:function(){
return'< b>'+ this.point.name +'< / b>';
}
}
}
},
系列:[{
type:'pie',
name:'service status',
data:[]
}]
}

var chart;
options.series.data.push('['
Service Ok',45.0]')
$(document).ready(function(){
chart = new Highcharts .Chart(options)
});

));

我想要做的是动态将值作为一组对象加载到 series.data 数组中。什么在这里做错了,是否有更好的方法来加载数据到数据数组? 系列属性是一个数组,因此您需要像这样编写它(将单个数据点添加到系列中):

  options.series [0] .data.push([Service Ok,45.0]); 

我在看。

First of all I am a total javascript newbie so please bear with me. I have the following script to draw pie charts using the Highchart framework

$(function() {
    var options = {
        colors: ["#66CC00", "#FF0000", "#FF6600"],
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: true
        },
        title: {
            text: 'Host Status'
        },
        tooltip: {
            formatter: function() {
                return '<b>' + this.point.name + '</b>: ' + this.total;
            }
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>' + this.point.name + '</b>';
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'service status',
            data: []
        }]
    }

    var chart;
    options.series.data.push('['
    Service Ok ',   45.0]')
    $(document).ready(function() {
        chart = new Highcharts.Chart(options)
    });

});​

What i am trying to do is to dynamically load the values into series.data array as an array of objects. What am doing wrong here, and is there a better way to load the data into the data array?

解决方案

The series property is an array, so you would need to write it like this (to add a single data point to the series):

options.series[0].data.push( ["Service Ok", 45.0 ]);

I was looking at this JS Fiddle.

这篇关于如何在javascript中动态添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:10