gpt4 book ai didi

javascript - 如何在 JavaScript 中以增量方式调用数组的 INDEX 值

转载 作者:行者123 更新时间:2023-11-28 07:54:00 25 4
gpt4 key购买 nike

我正在使用 Js 库(dhtmlx)创建堆积条形图。这是我的输出:enter image description here

json数据格式如下:

var data = [
{ "allocated":"20", "unallocated":"2", "day":"01/01/2014" },
{ "allocated":"12", "unallocated":"0", "day":"02/01/2014" },
{ "allocated":"2", "unallocated":"18", "day":"03/01/2014" },
{ "allocated":"22", "unallocated":"2", "day":"04/01/2014" },
{ "allocated":"3", "unallocated":"13", "day":"05/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"06/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"07/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"08/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"09/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"10/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"11/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"12/01/2014" },
{ "allocated":"6", "unallocated":"2.4", "day":"13/01/2014" },
{ "allocated":"14", "unallocated":"7", "day":"14/01/2014" },
];

但是我设法在 WEEK 之前对我的 JSON 数据进行 GroupBy使用以下方法:

var groupedByWeek = _.groupBy(data, function(item) {
var dateMoment = moment(item.day,"DD/MM/YYYY");
weeks = dateMoment.week();
array = [weeks]
return array;
});

现在,请引用我上传的图像,我已成功获取按周分组的数据。根据我给定的数据,我从中得到了 3 个数组索引。

Object { 1: Array[4], 2: Array[7], 3: Array[3] }

现在我想将索引 1 设置为图表的默认值。然后增量地点击“NEXT”按钮,数组索引不断地变化。

简而言之,我想在 X 轴上每周传递数据。有没有办法将数组索引一一传递?请帮我。提前致谢。

我的 html 文件的完整代码:

    <body>
<div id="chart1" style="width:1020px;height:300px;border:0px solid #000000;">
<div><a href="#">Next</a></div>
</div>


<script>

var barChart1 = new dhtmlXChart({
view:"stackedBar",
container:"chart1",
value:"#allocated#",
label:"#allocated#",
color: "#a24689",
// gradient:"falling",
width:40,
tooltip:{
template:"#allocated#"
},

xAxis:{
title:"Days",
template:"#day#",

},

yAxis:{
title:"Hours",
start:0,
step:4,
end:24
},

legend:{
values:[{text:"Compeleted Activities",color:"#a24689"},{text:"Undefined",color:"#F9D544"}],
valign:"top",
align:"center",
width:110,
layout:"x"
}
});

barChart1.addSeries({
value:"#unallocated#",
color:"#F9D544",
label:"#unallocated#",
tooltip:{
template:"#unallocated#"
}
});

var groupedByWeek = _.groupBy(data, function(item) {
var dateMoment = moment(item.day,"DD/MM/YYYY");
weeks = dateMoment.week();
array = [weeks]
return array;
});
console.log(groupedByWeek)

barChart1.parse(data,"json");
// console.log(data)

</script>

最佳答案

这个解决方案终于奏效了。

var groupedByWeek = _.groupBy(data, function(item) {
var dateMoment = moment(item.day,"DD/MM/YYYY");
return dateMoment.week();

groupedByWeek的值是数组形式。可以通过在 barChart1.parse(groupedByWeek[1],"json"); 处传递数组的索引值来完成,而不是 barChart1.parse(data,"json");

终于成功了!

关于javascript - 如何在 JavaScript 中以增量方式调用数组的 INDEX 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26366387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com