javascript - 如何将值从数组放入 Zingchart.js

标签 javascript arrays stacked-chart zingchart

我想在 Zingchart.js 中创建堆叠条形图并且我有以下数据:

var data = [{
  month : 1,
  name : 'Alex',
 count : '20'
 },
 {
 month : 2,
  name : 'Alex',
 count : '20'
 } ,
 {
 month : 2,
  name : 'John',
 count : '30'
 } ,
 {
 month : 2,
  name : 'Jane',
 count : '25'
 } ,
 {
 month : 3,
  name : 'Alex',
 count : '15'
 } ,
 {  
 month : 3,
  name : 'John',
 count : '25'
 } ,
 {
 month : 3,
  name : 'Jane',
 count : '23'
 } 
}]

我将数据转换为:

var data = {  "Alex" : ["20", " 20", "15"],
              "John" : ["0", "30", "25" ],
              "Jane" : ["0", "25", "23"]
           }

我想将数组中的值放入 Zingchart.js 以创建堆叠条形图 以及将值放入 zingchart 的示例:

var myConfig = {
  type: "bar",
  plot:{
    stacked:true,
    stackType:"normal"
  },
  "scale-x": { 
            "labels": ["1","2","3"],
            "label":{"offsetY": 5,
                    "text": "Month",
                    "fontColor": "#777",
                    "fontSize": 14
   }
  },
  series:[
    {
      values: [20, 20, 15]
    },
    {
      values:[0, 30, 25 ] 
    },
    {
      values: [0, 25, 23] 
    }
  ]
};
 
zingchart.render({ 
	id : 'myChart', 
	data : myConfig, 
	height: "100%", 
	width: "100%" 
});
      
html, body, #myChart {
  width:100%;
  height:100%;
}
<!DOCTYPE html>
<html>
	<head>
		<script src= "https://cdn.zingchart.com/zingchart.min.js"></script>
		<script> zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/";
		ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9","ee6b7db5b51705a13dc2339db3edaf6d"];</script></head>
	<body>
		<div id='myChart'></div>
	</body>
</html>

zingchart.js怎么给array赋值,谁有什么想法?谢谢。 https://www.zingchart.com/docs/chart-types/bar-charts/#bar__props_stacked

最佳答案

你可以这样做:

var xLabels = Object.keys(data)

var yValues = xLabels.map(function (key) {
  return {
    values: data[key].map(Number)
  }
})

查看此演示:

var data = {
  "Alex": ["20", " 20", "15"],
  "John": ["0", "30", "25"],
  "Jane": ["0", "25", "23"]
}

var xLabels = Object.keys(data)

var series = xLabels.map(function (key) {
  return {
    values: data[key].map(Number)
  }
})

var myConfig = {
  type: "bar",
  plot: {
    stacked: true,
    stackType: "normal"
  },
  "scale-x": {
    "labels": xLabels,
    "label": {
      "offsetY": 5,
      "text": "Month",
      "fontColor": "#777",
      "fontSize": 14
    }
  },
  series: series
};

zingchart.render({
  id: 'myChart',
  data: myConfig,
  height: "100%",
  width: "100%"
});
html,
body,
#myChart {
  width: 100%;
  height: 100%;
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdn.zingchart.com/zingchart.min.js"></script>
  <script>
    zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/";
    ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9", "ee6b7db5b51705a13dc2339db3edaf6d"];
  </script>
</head>

<body>
  <div id='myChart'></div>
</body>

</html>

关于javascript - 如何将值从数组放入 Zingchart.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46011920/

相关文章:

javascript - 在 livequery 中初始化用户类

javascript - jquery - 点击动画返回页面顶部

c - 大小数组的分配非常大

c - 请帮我释放这个动态分配的数组

d3.js - 水平堆叠条: one tick not displaying

javascript - AngularUI Router + Ionic - 路由可以在浏览器中工作,但不能在 Ionic View 中工作?

javascript - 从 Angular JS 应用程序打开谷歌地图应用程序

arrays - 如何限制 PostgreSQL 中数组的长度?

javascript - Highcharts 堆栈组列的正确 x 轴

matplotlib - 子图堆叠条形图