简体   繁体   English

节点上的D3元数据

[英]D3 metadata on nodes

I am following this tutorial for a fore-directed graph, http://bl.ocks.org/mbostock/1153292 . 我正在遵循本教程的前向图http://bl.ocks.org/mbostock/1153292 I was wondering, how do you pass meta-data / user defined data to the d3 node and keep it around for later actions. 我想知道,如何将元数据/用户定义的数据传递到d3节点,并保留它以便以后进行操作。

Example: 例:

A user clicks on a node, and a table appears with the relevant meta-data attached to the node, such as label, alerts, comments, and other data that is not specifc to the d3 node/edge architecture. 用户单击一个节点,将出现一个表,其中带有与该节点相连的相关元数据,例如标签,警报,注释以及其他不是d3节点/边缘体系结构所特定的数据。

You can pass whatever data you need to d3 and it will automatically store it on the node based on the nodes data function. 您可以将所需的任何数据传递到d3,它会根据节点数据功能自动将其存储在节点上。 Whether you use that data to populate nodes and edges is really up to you. 是否使用该数据填充节点和边线完全取决于您。

Here's a basic pie chart (note I'm leaving out the pie and arc definitions for brevity). 这是一个基本的饼图(请注意,为简洁起见,我省略了饼图和圆弧的定义)。 In this example, data contains the information needed to build the pie chart. 在此示例中, data包含构建饼图所需的信息。 These get initially stored on the svg node and then each array element gets stored on the corresponding arc node. 这些元素最初存储在svg节点上,然后每个数组元素存储在相应的arc节点上。 This data can contain whatever information you want. 该数据可以包含您想要的任何信息。 I use it to store a name and css class that I then apply to the pie wedges. 我用它来存储名称和CSS类,然后将其应用于饼形图。 However it can contain as rich of data as you need. 但是,它可以根据需要包含尽可能丰富的数据。

var data = [
  { name: 'Schedule slip', classname: 'c', value: 5},
  { name: 'Slightly behind', classname: 'b', value: 10},
  { name: 'On track', classname: 'a', value: 20}

];

  // select the svg element if it exists
  var svg = d3.selectAll('svg').data([data]);

  // otherwise create the skeletal chart
  var svgEnter = svg.enter().append('svg');

  // chart skeleton
  var gEnter = svgEnter.append('g').attr('class', 'donut');
  gEnter.append('g').attr('class', 'arcs');

  // Dimensions
  var width = (2 * outerRadius) + margin.left + margin.right,
      height = (2 * outerRadius) + margin.top + margin.bottom,
      hCenter = width/2,
      vCenter = height/2;

  svg
    .attr('width', width)
    .attr('height', height)
    .select('.donut')
      .attr('transform', translate(hCenter, vCenter));

    // draw the arcs
    var arcs = svg.select('.arcs').selectAll('.arc')
        .data(pie);  // compute pie wedge info and store it in arc node

    var gArc = arcs.enter().append('g').attr('class', 'arc');
    gArc.append('path').each(function(d) { this._current = d; });

    arcs.exit().remove();

    arcs.select('path')
        .attr('class', function(d) { return d.data.classname; })
      .transition().duration(500)
        .attrTween('d', arcTween);

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

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