我想在列中绘制多个图-最终结果将是一个Actigram。我认为D3.js是我想要使用的库-并且我可以按照下面的代码绘制多个图(只有一些测试数据-如果有一个聪明的方法来双重绘制我们最终拥有的时间序列数据,请告诉我)。然而,它们是单独的svg。有没有办法使其成为一个svg?
非常开放的其他图书馆,如果有一个更容易的方法。
const signalData = [{
name: "Signal 1",
data: [1, 2, 3, 4, 5, 6, 7],
},
{
name: "Signal 2",
data: [2, 3, 1, 4, 5, 1, 3],
},
{
name: "Signal 3",
data: [1, 7, 2, 6, 3, 5, 4],
},
];
// This is a line generator. Normally, you pass the x- and y-scales in,
// see also d3-scale on github
const line = d3.line()
// The first argument is always the datum object, the second is the index
// Note that I use the index to determine the x coordinate of each point here
.x((d, i) => i * 50)
// And the datum object for the y-coordinate
.y(d => 150 - (15 * d));
// The term `datum` is unrelated to date, but is the singular value of `data`.
// One datum, many data.
d3.select("#plotsHere")
.selectAll("svg")
// Append one svg per array entry, look up the so-called enter, update, exit
// cycle for this. It's the most complex part of d3
.data(signalData)
.enter()
.append("svg")
// Execute a custom function for each element. People who are new to d3.js
// over-use this function, very often you don't need it!
.each(function(d, i) {
// I pass the current svg element, the datum object, and the index for convenience
draw(d3.select(this), d, i);
});
function draw(svg, data, index) {
// Append a label, set the text to the name
svg.append("text")
.attr("x", 20)
.attr("y", 20)
.text(d => d.name);
// Append a path, take the datum of the svg, pick it's `data` property,
// which is the array of numbers, and set that as the datum of the path,
// then call the line generator
svg.append("path")
.datum(d => d.data)
.attr("d", line);
}
path {
stroke: darkblue;
stroke-width: 2px;
fill: none;
}
svg {
border: solid 2px red;
}
#plotsHere {
display: grid;
grid-auto-flow: row;
grid-gap: 0px;
grid-template-rows: 150px 150px;
grid-template-columns: 300px 300px;
}
<script src="https://d3js.org/d3.v7.min.js"></script>
<body>
<div id="plotsHere">
</div>
</body>
1条答案
按热度按时间6pp0gazn1#
你可以使用svg
g
标签添加群组: