gpt4 book ai didi

javascript - dc.js:如何从减少的子箱中计算平均值?

转载 作者:行者123 更新时间:2023-11-29 23:49:46 24 4
gpt4 key购买 nike

我有来自虚拟机集群的信息,我试图在 dc.js 复合折线图上按天绘制这些信息。

这是 JSON 格式:

[{
Date: "Feb 15 14:25:53 2017",
Institution: "institution x",
Memory: 57777556,
Nodes: 1,
Processors: 4,
Queue: "workq",
TotalCpus: 81600,
TotalMemory: 64172851200,
TotalNodes: 300
},
{
Date: "Feb 15 14:26:09 2017",
Institution: "institution y",
Memory: 57542224,
Nodes: 1,
Processors: 4,
Queue: "workq",
TotalCpus: 81600,
TotalMemory: 64172851200,
TotalNodes: 300
}]

通过使用 reduceSum() 减少,我能够绘制每个机构随时间消耗的总内存。这是我的 fiddle :https://jsfiddle.net/catkinso/cxr1L6fq/

但是,我一直无法弄清楚如何计算每个机构每天消耗的平均内存。

我知道我需要实现一个自定义缩减,一个使用像这样的子箱:

var usedMemoryGroup = dateDimension.group().reduce(
function (p, v)
{
// add
if (v.Queue === queue)
{
p[v.Institution] = (p[v.Institution] || 0) + (v.Memory / v.TotalMemory);
}
return p;
},
function (p, v)
{
// remove
if (v.Queue === queue)
{
p[v.Institution] -= (v.Memory / v.TotalMemory);
}
return p;
},
function ()
{
// init
return {};
});

但我不确定如何创建自定义缩减来创建子箱,同时计算每天的平均值。最终,我希望用每个机构的线条在综合时间表上绘制出这种减少。

如有任何帮助,我们将不胜感激!

最佳答案

对于任何感兴趣的人,这就是我所做的...

为机构名称创建维度:

institutionDimension = ndx.dimension(function (d) {
if (d.Queue === queue) {
return d.Institution;
}
})

然后创建一个包含所有机构名称的数组:

 var institutions = [];
var names = institutionDimension.group().all();

names.forEach(function (x) {
if (institutions.indexOf(x.key) === -1) {
institutions.push(x.key);
}
});

institutions.sort();

然后为每个机构创建一个组:

var institutionGroups = [];
institutions.forEach(function (institution)
{
institutionGroups.push(createAverageMemoryGroup(dateDimension,queue,institution));
});

function createAverageMemoryGroup(dimension, queue, institution)
{
var group = dimension.group()
.reduce(
function (p, v) {
if (v.Queue === queue && v.Institution === institution) {
++p.count;
p.queue = queue;
p.aggregateMemory += v.Memory/1000000;
p.averageMemoryUsed = p.count ? d3.round((p.aggregateMemory / p.count), 2) : 0;
}
return p;
},
function (p, v) {
if (v.Queue === queue && v.Institution === institution) {
--p.count;
p.aggregateMemory -= v.Memory/1000000;
p.averageMemoryUsed = p.count ? d3.round((p.aggregateMemory / p.count), 2) : 0;
}
return p;
},
function () {
return {
count: 0,
aggregateMemory: 0,
averageMemoryUsed: 0,
queue
};
});

return group;
}

然后为每个组创建一个组合:

var composition = createComposition(usedMemoryTimeChart, dateDimension, institutionGroups, institutions);    
function createComposition(chart, dimension, groups, institutions)
{
var composition = [];
for (var i = 0; i < institutions.length; i++) {
composition.push(
dc.lineChart(chart)
.dimension(dimension)
.colors(getColor(i))
.group(groups[i], institutions[i])
.dashStyle([2, 2])
.valueAccessor(function (x) {
return x.value.averageMemoryUsed;
})
);
}

return composition;
}

最后,将构图应用于图表:

usedMemoryTimeChart.compose(composition);

enter image description here

关于javascript - dc.js:如何从减少的子箱中计算平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43107660/

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