d3js - 我想看到线条

时间:2018-04-05 01:08:49

标签: javascript json d3.js

我有一个使用d3js平台的项目。

但是当我对d3js的文件和json文件进行客户化时,

var diameter = 960,
  radius = diameter / 2,
  innerRadius = radius - 120;

var cluster = d3.cluster().size([360, innerRadius]);

var line = d3.radialLine().curve(d3.curveBundle.beta(0.85)).radius(
  function(d) {
    return d.y;
  }).angle(function(d) {
  return d.x / 180 * Math.PI;
});

var svg = d3.select("body").append("svg").attr("width", diameter).attr(
  "height", diameter).append("g").attr("transform",
  "translate(" + radius + "," + radius + ")");

var link = svg.append("g").selectAll(".link"),
  node = svg.append("g")
  .selectAll(".node");

var classes = [{
    "name": "LineOne.Seoul",
    "size": 3938,
    "imports": [{
      "station": "LineOne.Cityhall",
      "value": 2000
    }, {
      "station": "1호선.종로3가",
      "value": 1000
    }]
  },
  {
    "name": "LineOne.Cityhall",
    "size": 3812,
    "imports": [{
      "station": "LineOne .Seoul",
      "value": 3000
    }]
  },
  {
    "name": "LineOne.Jongro-3",
    "size": 3812,
    "imports": [{
      "station": "LineOne.Seoul",
      "value": 3500
    }]
  },
  {
    "name": "LineOne.Jongro-5",
    "size": 38120,
    "imports": [{
      "station": "LineOne.Seoul",
      "value": 40000
    }]
  },
  {
    "name": "LineOne.DongDaeMoon",
    "size": 1812,
    "imports": [{
      "station": "LineOne.Seoul",
      "value": 3500
    }]
  },
  {
    "name": "LineOne.DongMyo",
    "size": 28120,
    "imports": [{
      "station": "LineOne.Seoul",
      "value": 25000
    }]
  }
];

var root = packageHierarchy(classes).sum(function(d) {

  return d.size;
});


cluster(root);
var index_size = 0;

link = link.data(packageImports(root.leaves()))
  .enter().append("path")
  .each(function(d) {
    console.log("link : " + d)
    d.source = d[0], d.target = d[d.length - 1];
  })
  .attr("class", "link").attr("d", line).style("stroke", function(d) {


    if (d[0].value < 2000) {
      return "red";
    } else if (d[0].value >= 2000 && d[0].value < 10000) {
      return "blue";
    } else if (d[0].value >= 10000) {
      return "black";
    }
  });


node = node.data(root.leaves()).enter().append("text").attr(
  "class", "node").attr("dy", "0.31em").attr(
  "transform",
  function(d) {
    return "rotate(" + (d.x - 90) + ")translate(" +
      (d.y + 8) + ",0)" +
      (d.x < 180 ? "" : "rotate(180)");
  }).attr("text-anchor", function(d) {
  return d.x < 180 ? "start" : "end";
}).text(function(d) {
  return d.data.key;
}).on("mouseover", mouseovered).on("mouseout", mouseouted);

function mouseovered() {}

function mouseouted() {}

function packageHierarchy(classes) {
  var map = {};

  function find(name, data) {

    var node = map[name],
      i;
    if (!node) {
      node = map[name] = data || {
        name: name,
        children: []
      };
      if (name.length) {
        node.parent = find(name.substring(0, i = name
          .lastIndexOf(".")));
        node.parent.children.push(node);
        node.key = name.substring(i + 1);


      }
    }
    return node;
  }

  classes.forEach(function(d) {
    find(d.name, d);
  });

  return d3.hierarchy(map[""]);
}

// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
  var map = {},
    imports = [];

  // Compute a map from name to node.
  nodes.forEach(function(d) {
    map[d.data.name] = d;
  });

  // For each import, construct a link from the source to target node.
  nodes.forEach(function(d) {
    if (d.data.imports.station)
      d.data.imports.station.forEach(function(i) {
        imports.push(map[d.data.name].path(map[i]));
      });

  });

  return imports;
}
.node {
	font: 300 15px "Helvetica Neue", Helvetica, Arial, sans-serif;
	fill: #bbb;
}

.node:hover {
	fill: #000;
}

.link {
	stroke: red;
	stroke-opacity: 0.5;
	fill: none;
	pointer-events: none;
	stroke-width: 3px;
}


.node:hover, .node--source, .node--target {
	font-weight: 700;
}

.node--source {
	fill: #2ca02c;
}

.node--target {
	fill: #d62728;
}

.link--source, .link--target {
	stroke-opacity: 1;
	stroke-width: 2px;
}

.link--source {
	stroke: #d62728;
}

.link--target {
	stroke: #2ca02c;
}
<script src="https://d3js.org/d3.v4.min.js"></script>

镀铬上没有图形线条。只有更改是json文件中的导入变量格式(Arraylist - &gt; Map)。我该如何解决这个问题?

0 个答案:

没有答案