简体   繁体   English

在d3.js中绘制带有重复图像图案的时间线图

[英]Plot timeline graph with repetitive image patten in d3.js

I am very much new to d3.js. 我对d3.js非常陌生。 I need to implement a horizontal bar with timeline. 我需要用时间轴实现单杠。 And the content of the bar would be a sequence of image pattern. 条的内容将是一系列图像图案。

Can anyone help me to find the way to implement that, preferably using d3.js? 谁能帮我找到实现该方法的方法,最好使用d3.js?

Please find the attached picture. 请找到所附图片。

在此处输入图片说明

I'm not going to give a full answer, just a possible skeleton (among other options) 我不会给出完整的答案,只是可能的骨架(在其他选项中)

Say you have your data as a list of pairs (duration, event), and a list of images matching the events: 假设您将数据作为成对的列表(持续时间,事件)和与事件匹配的图像列表:

var data = [{duration:15, event:0},
            {duration:5, event:1},
            {duration:25, event:0},
            ...
           ];
var img = ["black.png", "colorbar.png", ...];
var width = 25; //width of a frame
var scale = 4;  //time units per frame

you can do the following to get the strips start and end positions, as well as a list of frames: 您可以执行以下操作以获取条带的开始和结束位置以及框架列表:

var x=0;
var frames=[];
data.forEach(function(d) {
   d.position = x;
   var nFrames= Math.ceil(d.duration/scale);
   for (var i=0; i<nFrames; i++) {
     frames.push({event: d.event, position:x});       
     x+=width; 
   }
})

frames would look like: [{event:0, position:0}, {event:0,position:25}, ...] 帧看起来像: [{event:0, position:0}, {event:0,position:25}, ...]

Now you need to insert the corresponding images: the basic is the following: 现在您需要插入相应的图像:基本如下:

d3.select("svg")
  .selectAll(".frame")
  .data(frames)
  .enter()
  .append("image")
  .attr("class", "frame")
  .attr("x",function(d){return d.position})
  .attr("y",0)
  .attr("xlink:href",function(d){return img[d.event]});

This should give you a good start already, now you can try the ticks and the legend by yourself :) 这应该已经给您一个良好的开始,现在您可以自己尝试滴答和图例了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM