gpt4 book ai didi

javascript - 使用多数组通过 dc.js、d3 和 crossfilter 在散点图上绘制数据

转载 作者:太空宇宙 更新时间:2023-11-04 15:44:28 25 4
gpt4 key购买 nike

我想在散点图上绘制一些值,但我不知道如何从数据中获取所需的值并将其绘制到散点图上。我尝试过各种方法,但我是 d3 的新手,所以有点令人困惑。

我刚刚准备了一把 fiddle ,也许有人想看一下?

数据结构为

var data = [
{"name":"comp","arr":[[1493301233000, 123, 456], [1493301234000, 3, 4], [1493301235000, 5, 6]]},
{"name":"comp","arr":[[1493301236000, 11, 22]]},
{"name":"dogg","arr":[[1493301238000, 33, 55], [1493301239000, 66, 88]]}];

hit me to get to the fiddle

最佳答案

希望这能让您开始。我更新了您的 Crossfilter 和 dc 版本,以便您可以在 1.4 中使用 Crossfilter 的新数组维度功能,从而使 dc 的行为更加可预测。

您在 fiddle 中的设置(请将来将所有代码放入您的问题中):

var data = [
{"name":"comp","arr":[[1493301233000, 123, 456], [1493301234000, 3, 4], [1493301235000, 5, 6]]},
{"name":"comp","arr":[[1493301236000, 11, 22]]},
{"name":"dogg","arr":[[1493301238000, 33, 55], [1493301239000, 66, 88]]}
];
//arr[x][0] = 1493301233000 comes from getTime(), i guess it should be on the x-axis
//arr[x][1] = 123 is not really important, would be nice if you see this value if you hover over a point
//arr[x][2] = 456 is the value, i guess it should be on the y-axis

设置您的维度和组。数组元素(数组本身)将是您的维度键。

var cf = crossfilter(data);

var arrDim = cf.dimension(function(d){ return d.arr;}, true);
var arrGroup = arrDim.group();

设置散点图。我对 x 和 y 域进行了硬编码,并且没有将您的值转换为日期。如果将它们转换为 Date 对象,dc.js 的表现会更好。使用访问器函数(位于底部)告诉散点图 x (keyAccessor)、y (valueAccessor) 和悬停标签 (title) 使用什么值)。

我相信,除非您设置brushOn(false),否则悬停标签将不会显示,因此您目前无法刷此图表进行过滤。

var scatter = dc.scatterPlot("#chart");

scatter
.width(300)
.height(300)
.x(d3.scale.linear().domain([1493301233000, 1493301239000]))
.y(d3.scale.linear().domain([0, 456]))
.yAxisLabel("y")
.xAxisLabel("x")
.brushOn(false)
.clipPadding(10)
.dimension(arrDim)
.excludedOpacity(0.5)
.group(arrGroup)
.valueAccessor(function(d) { return d.key[2]; })
.keyAccessor(function(d) { return d.key[0]; })
.title(function(d) { return d.key[1]; });

dc.renderAll();

工作示例:http://jsfiddle.net/esjewett/vunftfdn/1/

关于javascript - 使用多数组通过 dc.js、d3 和 crossfilter 在散点图上绘制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43660672/

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