Highcharts图表来自sql的动态json数据

时间:2022-12-04 16:20:05

I want to make the array suitable for Highchart data returned from SQL.

我想使数组适合从SQL返回的Highchart数据。

This array should be made available to Highchart.

此数组应该可供Highchart使用。

Can you help me on this topic ?

你能帮帮我这个话题吗?

From SQL returned json value :

从SQL返回的json值:

 {
                      "FuelConsumption": [{
                        "Unit": "Computing",
                        "UnitID": 9,
                        "DateofPurchase": "18.04.2016",
                        "Liter": 2
                      }, {
                        "Unit": "Transportation",
                        "UnitID": 10,
                        "DateofPurchase": "18.04.2016",
                        "Liter": 3453
                      }, {
                        "Unit": "GeneralManager",
                        "UnitID": 7,
                        "DateofPurchase": "20.04.2016",
                        "Liter": 5
                      }, {
                        "Unit": "Boss",
                        "UnitID": 8,
                        "DateofPurchase": "20.04.2016",
                        "Liter": 4564
                      }, {
                        "Unit": "Computing",
                        "UnitID": 9,
                        "DateofPurchase": "20.04.2016",
                        "Liter": 579
                      }, {
                        "Unit": "Transportation",
                        "UnitID": 10,
                        "DateofPurchase": "20.04.2016",
                        "Liter": 337
                      }, {
                        "Unit": "Boss",
                        "UnitID": 8,
                        "DateofPurchase": "21.04.2016",
                        "Liter": 3
                      }, {
                        "Unit": "Transportation",
                        "UnitID": 10,
                        "DateofPurchase": "21.04.2016",
                        "Liter": 22
                      }]
                    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

I want to get results this way :

我希望以这种方式获得结果:

 {
   name: 'Transportation',
   data: [3453, 337, 22]
 }, {
   name: 'Computing',
   data: [2, 5, 579]
 }, {
   name: 'Boss',
   data: [4564, 3]
 }

2 个解决方案

#1


1  

You can create a hash from json, 'Unit' value as key, which is an array and keep on adding 'Liter' value to that array. Once that done, create json from that hash.

您可以从json创建一个哈希,'Unit'值作为键,这是一个数组并继续向该数组添加'Liter'值。完成后,从该哈希创建json。

var hash = {};
data.FuelConsumption.forEach(function(obj) {
  if (!hash[obj.Unit]) {
    hash[obj.Unit] = [];
  }
  hash[obj.Unit].push(obj.Liter);
});

var newData = [];
for (var key in hash) {
  newData.push({
    name: key,
    data: hash[key]
  });
}

In case you are using Lodash, then

如果你正在使用Lodash,那么

var newData2 = _.chain(data.FuelConsumption)
  .groupBy('Unit')
  .map(function(obj) {
    return {
      name: _.uniq(_.pluck(obj, 'Unit'))[0],
      data: _.uniq(_.pluck(obj, 'Liter'))
    };
  })
  .value();

#2


0  

If you can use underscore, the answer is the following:

如果你可以使用下划线,答案如下:

_.chain(data["FuelConsumption"])
 .groupBy('Unit')
 .mapObject((val, key)=>({name:key, data:val.map(x => x.Liter)}))
 .value()

It'd similar if you use any other javascript libraries.

如果您使用任何其他JavaScript库,它会类似。

#1


1  

You can create a hash from json, 'Unit' value as key, which is an array and keep on adding 'Liter' value to that array. Once that done, create json from that hash.

您可以从json创建一个哈希,'Unit'值作为键,这是一个数组并继续向该数组添加'Liter'值。完成后,从该哈希创建json。

var hash = {};
data.FuelConsumption.forEach(function(obj) {
  if (!hash[obj.Unit]) {
    hash[obj.Unit] = [];
  }
  hash[obj.Unit].push(obj.Liter);
});

var newData = [];
for (var key in hash) {
  newData.push({
    name: key,
    data: hash[key]
  });
}

In case you are using Lodash, then

如果你正在使用Lodash,那么

var newData2 = _.chain(data.FuelConsumption)
  .groupBy('Unit')
  .map(function(obj) {
    return {
      name: _.uniq(_.pluck(obj, 'Unit'))[0],
      data: _.uniq(_.pluck(obj, 'Liter'))
    };
  })
  .value();

#2


0  

If you can use underscore, the answer is the following:

如果你可以使用下划线,答案如下:

_.chain(data["FuelConsumption"])
 .groupBy('Unit')
 .mapObject((val, key)=>({name:key, data:val.map(x => x.Liter)}))
 .value()

It'd similar if you use any other javascript libraries.

如果您使用任何其他JavaScript库,它会类似。