从不同格式读取Javascript HighCharts

从不同格式读取Javascript HighCharts,javascript,highcharts,Javascript,Highcharts,我正在使用Highcharts,目前我有2个数组,我想停止读取,并开始从我的对象读取数据 以下是我目前拥有的代码: items = ['Item 1', 'Item 2']; Quantity = [10 , 5]; jQuery('#container').highcharts({ chart: { type: 'column' }, xAxis: {

我正在使用Highcharts,目前我有2个数组,我想停止读取,并开始从我的对象读取数据

以下是我目前拥有的代码:

items = ['Item 1', 'Item 2'];
Quantity = [10 , 5];

        jQuery('#container').highcharts({

            chart: {
                type: 'column'
            },
            xAxis: {
                categories: mydata
            },
            title: {
                text: 'Items Title'
            },
            series: [{
                name: 'Items',
                data: Quantity
            }],
            tooltip: {
                pointFormat: '<span style="color:{series.color}">{series.name}<br/>',
                shared: true
            },


            plotOptions: {

                series: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function () {

                                console.log('Clicked');


                            },
                        },
                    },
                },

            },

        });     

The above displays 2 items of height 10 and 5.

Now what I need to do is to be able to read this data instead:

var mydata = {
    "items":[{
        "id":"123",
        "name":"item name here",
        "subitems":[{
            "id":"567",
            "name":"subitem 1"
        },
            {
                "id":"657",
                "name":"subitem 2"
            }],

        "somthing here":null,
    },
        {
            "id":"456",
            "name":"item name here too",
            "subitems":[{
                "id":"567",
                "name":"subitem here"
            },
                {
                    "id":"657",
                    "name":"subitem here roo"
                }],

            "somthing here":null,
        }]
};
items=['item1','item2'];
数量=[10,5];
jQuery(“#容器”).highcharts({
图表:{
类型:“列”
},
xAxis:{
类别:mydata
},
标题:{
文本:“项目标题”
},
系列:[{
名称:'项目',
数据:数量
}],
工具提示:{
pointFormat:“{series.name}
”, 分享:真的 }, 打印选项:{ 系列:{ 光标:“指针”, 要点:{ 活动:{ 单击:函数(){ console.log('Clicked'); }, }, }, }, }, }); 上面显示高度为10和5的两个项目。 现在我需要做的是能够读取这些数据: var mydata={ “项目”:[{ “id”:“123”, “名称”:“此处的项目名称”, “分项”:[{ “id”:“567”, “名称”:“子项1” }, { “id”:“657”, “名称”:“第2项” }], “somthing here”:空, }, { “id”:“456”, “名称”:“此处也有项目名称”, “分项”:[{ “id”:“567”, “名称”:“此处的子项” }, { “id”:“657”, “名称”:“此处的子项roo” }], “somthing here”:空, }] };
数量值必须是子项计数,因此在上述数据的情况下,数量值应为2,2


我如何才能做到这一点?

这应该让您开始:

如果单击列,它将读取mydata并显示子项。要获取子项计数,只需使用mydata.items[i]['subitems'].length。要更新现有项目值,请更改currentItemY或currentItemX

click: function (e) {
        if(this.series.name != 'SubItems'){
            let currentItemName = items[this.index];
          let currentItemY = this.y;
          let currentItemX = this.x;

          let newCategories = [];
          let newSeries = [];

          for(let i in mydata.items){
            if(mydata.items[i]['name'] == currentItemName){
              for(let j in mydata.items[i]['subitems']){
                newCategories.push(mydata.items[i]['subitems'][j]['name']);
                newSeries.push(mydata.items[i]['subitems'][j]['quantity']);
              }
            }
          }

          chart.update({
            xAxis: {
              categories: newCategories
            },
            series: [{
              name: 'SubItems',
              data: newSeries
            }]
          });
        }
},

这不是我想要的。你有一个硬编码部分,你把项目放在那里,另一部分是子项目…我要硬编码一个数据源,我必须从中获取所有信息…有意义吗?@Farley2016硬编码是什么意思?您还可以从ajax获取数据。