- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 4 个 csv 文件(比如 4 个类),每个文件有 100x200 个元素(100 个案例,200 个特征)。我需要使用平行坐标来可视化其中一些功能(功能 1、功能 2、...、功能 8;所以我将为每个 csv 文件设置 100x8)。要识别类别,我需要使用 4 种不同的颜色。
我已经有了可用于在一个 csv 文件中可视化数据 (100x10) 的平行坐标。代码如下:
<style type="text/css">
svg {
font: 10px sans-serif;
}
.background path {
fill: none;
stroke: #ccc;
stroke-opacity: .1;
shape-rendering: crispEdges;
}
.foreground path {
fill: none;
stroke: steelblue;
stroke-opacity: .7;
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
shape-rendering: crispEdges;
}
.axis line, .axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
text-shadow: 0 1px 0 #fff;
cursor: move;
}
</style>
</head>
<body>
<script type="text/javascript">
var m = [30, 10, 10, 10],
w = 1300 - m[1] - m[3],
h = 500 - m[0] - m[2];
var x = d3.scale.ordinal().rangePoints([0, w], 1),
y = {},
dragging = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
var svg = d3.select("body").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
d3.csv("myData.csv")
.row(function(d) {
return {
// Features that are needed to be visualize
feature1 : d.feature1,
feature2 : d.feature2,
feature3 : d.feature3,
feature4 : d.feature4,
feature5 : d.feature5,
feature6 : d.feature6,
feature7 : d.feature7,
feature8 : d.feature8
};
})
.get(function(e, data) {
x.domain(
dimensions = d3.keys(data[0])
.filter(function(d) {
return d != "source" &&
(y[d] = d3.scale.linear()
.domain(d3.extent(data, function(p) { return +p[d]; }))
.range([h, 0]));
}
));
// Add grey background lines for context.
background = svg.append("svg:g")
.attr("class", "background")
.selectAll("path")
.data(data)
.enter().append("svg:path")
.attr("d", path);
// Add blue foreground lines for focus.
foreground = svg.append("svg:g")
.attr("class", "foreground")
.selectAll("path")
.data(data)
.enter().append("svg:path")
.attr("d", path);
// Add a group element for each dimension.
var g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("svg:g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; })
.call(d3.behavior.drag()
.on("dragstart", function(d) {
dragging[d] = this.__origin__ = x(d);
background.attr("visibility", "hidden");
})
.on("drag", function(d) {
dragging[d] = Math.min(w, Math.max(0, this.__origin__ += d3.event.dx));
foreground.attr("d", path);
dimensions.sort(function(a, b) { return position(a) - position(b); });
x.domain(dimensions);
g.attr("transform", function(d) { return "translate(" + position(d) + ")"; })
})
.on("dragend", function(d) {
delete this.__origin__;
delete dragging[d];
transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
transition(foreground)
.attr("d", path);
background
.attr("d", path)
.transition()
.delay(500)
.duration(0)
.attr("visibility", null);
}));
// Add an axis and title.
g.append("svg:g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(axis.scale(y[d])); })
.append("svg:text")
.attr("text-anchor", "middle")
.attr("y", -9)
.text(String);
// Add and store a brush for each axis.
g.append("svg:g")
.attr("class", "brush")
.each(function(d) { d3.select(this).call(y[d].brush = d3.svg.brush().y(y[d]).on("brush", brush)); })
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
});
function position(d) {
var v = dragging[d];
return v == null ? x(d) : v;
}
function transition(g) {
return g.transition().duration(500);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) { return [position(p), y[p](d[p])]; }));
}
// Handles a brush event, toggling the display of foreground lines.
function brush() {
var actives = dimensions.filter(function(p) { return !y[p].brush.empty(); }),
extents = actives.map(function(p) { return y[p].brush.extent(); });
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
</script>
</body>
我的示例数据也是这样的:
path,Ktype,label,CGX,CGY,C_1,C_2,C_3,C_4,total1,total2,totalI3,total4,feature1,feature2,feature3,feature4,feature5,feature6,feature7,feature8,feature9,feature10,feature11,feature12,A,B,C,D,feature13,feature14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
.\Mydata\Case1,k1,1,42,33,0,57.44534,0,52597,71,16,10,276,4038,3789.631,0.6173469,0.6499337,2.103316,0.6661285,1.065539,248.3694,0.630161,0.000192848,0.9999996,0.000642777,1,0,0,1,9.60E-05,3136.698,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
.\Mydata\Case2,k1,2,163,29,0,43.28862,0,49050,71,16,10,248,2944,2587.956,0.5726808,0.5681185,2.130387,0.601512,1.137578,356.0444,0.6335613,0.000327267,1.000029,0.001271235,1,0,0,1,0.00010854,2676.418,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
.\Mydata\Case3,k1,3,774,19,0,45.26291,0,53455,71,16,10,212,2366,1982.547,0.408179,0.4579566,1.994296,0.6615351,1.193415,383.4534,0.7153812,0.000264522,1.000031,0.001210507,1,1,0,0,9.54E-05,3221.289,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
.\Mydata\Case4,k1,4,1116,25,0,80.76469,0,57542,71,16,10,284,3908,3453.988,0.3549117,0.4811547,1.982244,0.6088744,1.131446,454.0122,0.6166388,0.000314288,0.9999836,0.00129846,0,1,1,0,0.000140592,2143.42,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
.\Mydata\Case5,k1,5,1364,59,1,52.96776,0,49670,71,16,10,228,2725,2642.675,0.4328255,0.475517,1.859871,0.6587288,1.031152,82.32471,0.5775694,0.000466264,0.9999803,0.001765345,0,1,1,0,0.00012014,2439.636,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
现在,我有两个问题:
首先,我不知道如何同时加载 4 个 csv 文件?将来,我可能需要增加类的数量(即增加需要可视化的 csv 文件的数量),所以可能合并 csv 文件,只加载一个 csv 文件不是一个好主意。我该如何处理?
其次,我应该如何为与它们所属的 csv 文件相关的行着色?
最佳答案
要异步加载多个文件,可以使用队列库,例如queue.js .
然后,一旦文件被加载,您可以在单独重叠的 g
组元素中渲染它们,并根据(例如,根据文件顺序的索引)将颜色应用于整个组文件已加载。
下面是一个未经测试且未完成的解决方案,可能会给您一些线索:
var q = queue(), // create the queue
dataSources = [ // your data sources
'dataSource1.csv',
'dataSource2.csv',
...
],
colorScale = d3.scale.category20(); // categorical color scale bundled with d3
// Returns a different color for elements based on their index in the data:
function colorByIndex(d, i) {
return colorScale(i);
}
// Go through each data source and add it to the queue:
dataSources.forEach(function (source) {
q.defer(function (callback) {
d3.csv(source, callback);
});
});
// Wait for all requests to be completed:
q.awaitAll(function (error, results) {
// TODO: check for errors!
// Select and create groups of elements that are to be colored differently:
d3.selectAll('.group').data(results) // array of arrays
.append('g')
.style('color', colorByIndex) // this applies color to the entire group
.selectAll('???') // whatever you render, paths perhaps
.data(function (d) { return d; })
...
// at this point the d variable is an array of data
// straight from each of your csv files, you can
// proceed rendering as you usually do and there's
// no longer need to apply any colors
});
或者,您可以将结果
合并到一个数组中,并用指示符标记每一行,表明它来自哪个文件,如下所示:
['dataSource1.csv', 'a1', 'b1', 'c1', ...],
['dataSource1.csv', 'a2', 'b2', 'c2', ...],
['dataSource2.csv', 'a3', 'b3', 'c3', ...],
...
然后根据指标值应用颜色。
关于javascript - D3 : Loading multiple CSV files, 并使用平行坐标可视化它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24720208/
这个问题在这里已经有了答案: Can I get `cabal install` to use multiple cores? (3 个回答) 关闭 7 年前。 在使用类似于 GNU make 的 -
我正在尝试通过 akeeba backup 在 parallels plesk 面板中安装一个 joomla 站点。我在哪里面临文件权限问题。 An error occured Could not o
我在 MATLAB 中使用箱线图函数。我需要为 6 个“XTicks”绘制 6 个不同数据集的箱线图,即 x 轴上的每个刻度线应包含 6 个相应的框、晶须、中线和其域内的异常值集。我尝试通过为每个变量
我需要在 Kaplan Meier 图上呈现 at_risk 数字。 最终结果应该与此类似: 我在渲染时遇到的问题是 No。处于危险中的患者数量位于图表底部。此处显示的值对应于 x 轴上的值。因此本质
我想知道你们中的任何一个人为什么知道我的表现糟透了吗? 我正在努力实现的目标; 生成220万个文件。要创建每个文件,平均需要2-5个数据库调用。 我正在使用的服务器具有24个内核和190GB的RAM。
请帮忙。我正在研究具有此要求的算法。 给定 4 个“右”矩形(右矩形的边平行于 x 或 y),找出它们中的任何一个覆盖的区域 例如,灰色区域被下图中的 4 个矩形中的任何一个覆盖。 enter ima
我是一名优秀的程序员,十分优秀!