Javascript 折线图系列的对象数组?(+;用于交叉过滤器解决方案)
在JavaScript中,如何将“长表格式”转换为适合折线图的数据结构:Javascript 折线图系列的对象数组?(+;用于交叉过滤器解决方案),javascript,time-series,crossfilter,Javascript,Time Series,Crossfilter,在JavaScript中,如何将“长表格式”转换为适合折线图的数据结构: var data = [ {"type":"A"," year":2000," value":50}, {"type":"A"," year":2001," value":51}, {"type":"A"," year":2002," value":52}, {"type":"B"," year":2000," value":60}, {"type":"B"," year":2001," value":
var data = [
{"type":"A"," year":2000," value":50},
{"type":"A"," year":2001," value":51},
{"type":"A"," year":2002," value":52},
{"type":"B"," year":2000," value":60},
{"type":"B"," year":2001," value":55},
{"type":"B"," year":2002," value":57}
]
=>
var series = [
{type: "A", values : [{x: 2000, y: 50}, {x: 2001, y: 52},] },
{type: "B", values : [{x: 2000, y: 60}, {x: 2001, y: 55},] },
]
一个普通的JavaScript解决方案以及一个带有交叉过滤器库的解决方案——可以处理数据的任何维度——都是有价值的:
好问题,但不是直截了当;) 看看 您将得到类似于:
cr = crossfilter(data);
typeDim=cr.dimension(function(d) {return d.type});
typeGroup=typeDim.group();
yearDim=cr.dimension(function(d) {return d.year});
valDim=cr.dimension(function(d) {return d.value});
yearTypeGroup = dateDimension.group();
yearTypeGroup.reduce(
// add
// pivot[0].idx()[i] returns the index of statusGroup
// the reduce function hence construct a key-value object, keys being indexes of the pivot group.
function(p, v, i, pivot) {
++p[pivot[0].idx()[i]].count;
p[pivot[0].idx()[i]].value += +v.value;
return p;
},
//remove
function(p, v,i,pivot) {
--p[pivot[0].idx()[i]].count;
p[pivot[0].idx()[i]].value -= +v.value;
return p;
},
//init
function(pivot) {
var l, i, obj ={};
if(l = pivot[0].all().length){
for(i=0; i<l; ++i) {
obj[i] = {count:0, value:0}
}
}
return obj
}
);
yearTypeGroup.pivot(typeGroup)
cr=交叉滤波器(数据);
typeDim=cr.dimension(函数(d){返回d.type});
typeGroup=typeDim.group();
yearDim=cr.dimension(函数(d){返回d.year});
valDim=cr.dimension(函数(d){返回d.value});
yearTypeGroup=dateDimension.group();
yearTypeGroup.reduce(
//加
//pivot[0]。idx()[i]返回statusGroup的索引
//因此,reduce函数构造一个key-value对象,key是pivot组的索引。
函数(p,v,i,pivot){
++p[pivot[0].idx()[i].count;
p[pivot[0].idx()[i].value+=+v.value;
返回p;
},
//除去
功能(p、v、i、枢轴){
--p[pivot[0].idx()[i].count;
p[pivot[0].idx()[i].value-=+v.value;
返回p;
},
//初始化
功能(枢轴){
变量l,i,obj={};
如果(l=pivot[0].all().length){
对于(i=0;i好问题,但不是直截了当;)
看看
您将得到类似于:
cr = crossfilter(data);
typeDim=cr.dimension(function(d) {return d.type});
typeGroup=typeDim.group();
yearDim=cr.dimension(function(d) {return d.year});
valDim=cr.dimension(function(d) {return d.value});
yearTypeGroup = dateDimension.group();
yearTypeGroup.reduce(
// add
// pivot[0].idx()[i] returns the index of statusGroup
// the reduce function hence construct a key-value object, keys being indexes of the pivot group.
function(p, v, i, pivot) {
++p[pivot[0].idx()[i]].count;
p[pivot[0].idx()[i]].value += +v.value;
return p;
},
//remove
function(p, v,i,pivot) {
--p[pivot[0].idx()[i]].count;
p[pivot[0].idx()[i]].value -= +v.value;
return p;
},
//init
function(pivot) {
var l, i, obj ={};
if(l = pivot[0].all().length){
for(i=0; i<l; ++i) {
obj[i] = {count:0, value:0}
}
}
return obj
}
);
yearTypeGroup.pivot(typeGroup)
cr=交叉滤波器(数据);
typeDim=cr.dimension(函数(d){返回d.type});
typeGroup=typeDim.group();
yearDim=cr.dimension(函数(d){返回d.year});
valDim=cr.dimension(函数(d){返回d.value});
yearTypeGroup=dateDimension.group();
yearTypeGroup.reduce(
//加
//pivot[0]。idx()[i]返回statusGroup的索引
//因此,reduce函数构造一个key-value对象,key是pivot组的索引。
函数(p,v,i,pivot){
++p[pivot[0].idx()[i].count;
p[pivot[0].idx()[i].value+=+v.value;
返回p;
},
//除去
功能(p、v、i、枢轴){
--p[pivot[0].idx()[i].count;
p[pivot[0].idx()[i].value-=+v.value;
返回p;
},
//初始化
功能(枢轴){
变量l,i,obj={};
如果(l=pivot[0].all().length){
对于(i=0;它支持共享!d3.nest方法也非常简洁,但我很高兴学习如何使用交叉过滤器进行同样的操作。你是对的,d3.nest也可以用于此,但你必须在每次交叉过滤器过滤后重新嵌套。使用上述方法,你的图表系列将动态反映交叉过滤器分组和过滤。谢谢分享!d3.nest方法也很简洁,但我很高兴学习如何使用crossfilter进行同样的操作。你是对的,d3.nest也可以用于此,但你必须在每次crossfilter筛选后重新嵌套。使用上述方法,你的图表系列将动态反映crossfilter分组和筛选。