简体   繁体   English

使用D3js遍历一组图像

[英]Looping through a set of images using D3js

I have just started to look at D3js and am now getting my head around how SVG works. 我刚刚开始研究D3js,现在开始关注SVG的工作原理。 I am not a designer and I would like to get some of the design team into d3js if I get a simple prototype working. 我不是设计师,如果可以使用一个简单的原型,我希望让一些设计团队加入d3js。

What I would like is have a list of images which are my data set and then fade in the first image from the set and then 5 seconds later fade it out and then fade in the next image. 我想要的是一个图像列表,这些图像是我的数据集,然后在该图像集中的第一个图像中淡出,然后在5秒后将其淡出,然后在下一个图像中淡出。 This would continue looping through the set. 这将继续遍历该集合。

If you can point me to some links or provide some simple code to get me started on my journey then that would be great. 如果您可以指向我一些链接或提供一些简单的代码来开始我的旅程,那就太好了。

The basic functionality of d3 works just as well with HTML as with SVG. d3的基本功能与HTML以及SVG一样都可以正常工作。 There are extra functions specific to svg, but these all have svg in their name, like d3.svg.axis for drawing chart axes. 还有一些特定于svg的附加功能,但是所有这些功能的名称都带有svg,例如用于绘制图表轴的d3.svg.axis However, the core concept applies to all elements, with the idea that you create elements to match your dataset. 但是,核心概念适用于所有元素,即您可以创建与数据集匹配的元素。

So, yes, you could create a simple d3 example just using HTML <img> elements. 因此,是的,您可以仅使用HTML <img>元素创建一个简单的d3示例。 You would want to create a dataset that is a Javascript array with one entry in the array for each image you want to create. 您将要创建一个数据集,该数据集是一个Javascript数组,其中每个要创建的图像在数组中都有一个条目。

As a simple example, assume that your server has returned a JSON file consisting of an array of objects. 举一个简单的例子,假设您的服务器返回了一个由对象数组组成的JSON文件。 Each object has two properties, the image url and the title text. 每个对象都有两个属性,图像URL和标题文本。 Now, you could use d3's file reading method to read and parse the JSON file, or you could parse it using whichever methods you are most comfortable with. 现在,您可以使用d3的文件读取方法来读取和解析JSON文件,或者您可以使用最喜欢的任何一种方法对其进行解析。 Regardless, the end effect should be about the same as if you had hard-coded it like this: 无论如何,最终效果应该与您像这样对它进行硬编码时大致相同:

var dataArray = [{url:"image1.jpeg", title:"First image"},
                 {url:"image2.png", title:"Second image"},
                 {url:"image3.png", title:"Third image"}];

Then you declare an empty d3 selection into which you want to put your <img> elements, and use d3's data-join methods to create the matching elements. 然后,声明一个要放入<img>元素的空d3选择,并使用d3的data-join方法创建匹配的元素。 You do this by 你这样做

  1. using d3 to select the parent element (that you want all the images to be children of); 使用d3选择父元素(您希望所有图像成为其子元素);
  2. creating a selectAll selection for the child elements using a CSS selector that will be specific to the elements you're going to create; 使用CSS选择器为子元素创建一个selectAll选择,该选择器将特定于您要创建的元素;
  3. joining this selection to your data array, which tells d3 that you want to have one element in the selection for each entry in the data array; 将所选内容连接到数据数组,这将告诉d3您要为数据数组中的每个条目选择一个元素;
  4. use the enter() method of the data-bound selection to access the placeholder entries for data values that don't have elements to match, and create them; 使用数据绑定选择的enter()方法访问占位符条目,以获取没有匹配元素的数据值,并创建它们;
  5. use the main selection again to set all the attributes of the image elements based on the data that is attached to each 再次使用主选择来基于附加到每个图像元素的数据设置图像元素的所有属性

Sample code: 样例代码:

var slides = d3.select("div.slideshowContainer") //1. select parent
      .selectAll("img") //2. select children (all images in the div, none to start)
      .data( dataArray ); //3. bind to the array

slides.enter() //4. access the placeholders
      .append( "img" ) 
       //create an <img> as a child of the parent div, one for each placeholder
      .attr( "class", "slide" ); 
       //set classes and any other attributes that won't change

slides.attr("src", function(d){ return d.url;})
      .attr("title", function(d){ return d.title;});
      //set attributes based on the data object bound to each element
      //when you give a function as the value of an attr() call,
      //d3 will call it with the data object as the first parameter

To get a simple slide-show effect, you want to change the visibility of your slides over time, using a d3 transition: 为了获得简单的幻灯片放映效果,您想使用d3过渡随着时间的推移更改幻灯片的可见性:

slides.attr("opacity", 0) //start invisible
      .transition().duration(1000) //schedule a transition to last 1000ms
      .delay(function(d,i){return i*2000;})
        //Delay the transition of each element according to its index
        // in your selection so that it won't start to appear
        // until one second after the last image reached full opacity.
        //The second parameter passed to any function you give to d3 
        // is usually the index of that element within the current selection.
      .attr("opacity", 1); 
        //set the end-value of the transition to full opacity

On it's own, the above code will create a series of images side-by-side that appear one after another, you'd need to adjust your CSS to have them all absolutely positioned one on top of the other inside the container. 单独地,上面的代码将创建一系列并排显示的图像,您需要调整CSS,以使它们在容器内完全相对放置。

That's a quick breeze-through of some very major d3 concepts. 这是对一些非常主要的d3概念的快速了解。 Work your way through the tutorials to get more experience. 逐步学习教程以获取更多经验。

Most of the tutorial examples use SVG, although the initial example in "Let's make a bar chart" uses coloured <div> elements as the bars, and there are a couple other examples as well. 大多数教程示例都使用SVG,尽管“让我们制作条形图”中的初始示例使用彩色的<div>元素作为条形,并且还有另外两个示例。 However, all the examples can be useful: just try to just focus on how elements are created and how attributes, styles, and text content are set, without worrying about how the SVG attributes and element types relate to graphical features. 但是,所有示例都可能有用:仅尝试着重于如何创建元素以及如何设置属性,样式和文本内容,而不必担心SVG属性和元素类型如何与图形功能相关联。

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

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