简体   繁体   English

如何将JSON对象数据输入到D3图表(例如气泡图,条形图)中,以代替.JSON文件?

[英]How to feed json object data into D3 charts(eg bubble chart, bar chart) in place of .JSON files?

  1. I have the json object in a js file(eg MyFirstMashup.js ) and I have copied the script for bubble chart in the same js file. 我在js文件(例如MyFirstMashup.js )中有json对象,并且已经在同一js文件中复制了气泡图脚本。 In all the examples avaliable on the web, they have used json files eg. 在网络上所有可用的示例中,他们都使用了json文件,例如。 flare.json, xyz.tsv etc. I cannot afford to have this file as I am getting the data dynamically in MyFirstMashup.js file. flare.json,xyz.tsv等。由于我正在MyFirstMashup.js文件中动态获取数据,因此我无法拥有该文件。 Instead I have attached the bubble chart code below my existing js codes. 相反,我将气泡图代码附加在现有js代码下方。 But unfortunately the bubble chart code isn't working specially in data.json() part. 但是不幸的是,气泡图代码在data.json()部分中并不是特别有效。 I think I don't have to use this because I already have the JSON object. 我想我不必使用它,因为我已经有了JSON对象。 If not then what needs to be edited in the bubble chart code. 如果没有,那么需要在气泡图代码中进行编辑。 The MyFirstMashup.js with the bubble chart part and json object**(var jsonObj)** is--- 随着气泡图表部分和JSON对象MyFirstMashup.js **(VAR jsonObj)**是---

    var jsonStr = JSON.stringify(matrix); var jsonStr = JSON.stringify(matrix);
    var jsonObj = JSON.parse(jsonStr); var jsonObj = JSON.parse(jsonStr); //json object // json对象

     var bubble = d3.layout.pack() .sort(null) .size([diameter, diameter]) .padding(1.5); var svg = d3.select("body").append("svg") .attr("width", diameter) .attr("height", diameter) .attr("class", "bubble"); d3.json("flare.json", function(error, root) { //hw to replace flare.json if (error) throw error; //with jsonObj(any json object) var node = svg.selectAll(".node") .data(bubble.nodes(classes(root)) .filter(function(d) { return !d.children; })) .enter().append("g") .attr("class", "node") .attr("transform", function(d) { return "translate(" + dx + "," + dy + ")"; }); node.append("title") .text(function(d) { return d.className + ": " + format(d.value); }); node.append("circle") .attr("r", function(d) { return dr; }) .style("fill", function(d) { return color(d.packageName); }); node.append("text") .attr("dy", ".3em") .style("text-anchor", "middle") .text(function(d) { return d.className.substring(0, dr / 3); }); }); // Returns a flattened hierarchy containing all leaf nodes under the root. function classes(root) { var classes = []; function recurse(name, node) { if (node.children) node.children.forEach(function(child) { recurse(node.name, child); }); else classes.push({packageName: name, className: node.name, value: node.size}); } recurse(null, root); return {children: classes}; } d3.select(self.frameElement).style("height", diameter + "px"); 

Then, instead of using the d3.json function you can create a render function like this: 然后,无需使用d3.json函数,可以创建如下的render函数:

var jsonStr = JSON.stringify(matrix);
var jsonObj = JSON.parse(jsonStr); //json object

var bubble = d3.layout.pack()
      .sort(null)
      .size([diameter, diameter])
      .padding(1.5);

var svg = d3.select("body").append("svg")
      .attr("width", diameter)
      .attr("height", diameter)
      .attr("class", "bubble");

render(jsonObj); // And simply call it like this.

function render(root) {  

    var node = svg.selectAll(".node")
        .data(bubble.nodes(classes(root))
        .filter(function(d) { return !d.children; }))
      .enter().append("g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

    node.append("title")
        .text(function(d) { return d.className + ": " + format(d.value); });

    node.append("circle")
        .attr("r", function(d) { return d.r; })
        .style("fill", function(d) { return color(d.packageName); });

    node.append("text")
        .attr("dy", ".3em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.className.substring(0, d.r / 3); });
  };

// Returns a flattened hierarchy containing all leaf nodes under the root.
  function classes(root) {
    var classes = [];

    function recurse(name, node) {
      if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
      else classes.push({packageName: name, className: node.name, value: node.size});
    }

    recurse(null, root);
    return {children: classes};
  }



  d3.select(self.frameElement).style("height", diameter + "px");

or remove the render function and directly use jsonObj inside .data(bubble.nodes(classes(jsonObj )) 或删除渲染函数并直接在.data(bubble.nodes(classes(jsonObj ))内使用jsonObj

var jsonStr = JSON.stringify(matrix);
var jsonObj = JSON.parse(jsonStr); //json object

var bubble = d3.layout.pack()
      .sort(null)
      .size([diameter, diameter])
      .padding(1.5);

var svg = d3.select("body").append("svg")
      .attr("width", diameter)
      .attr("height", diameter)
      .attr("class", "bubble");  

var node = svg.selectAll(".node")
        .data(bubble.nodes(classes(jsonObj))
        .filter(function(d) { return !d.children; }))
      .enter().append("g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

node.append("title")
        .text(function(d) { return d.className + ": " + format(d.value); });

node.append("circle")
        .attr("r", function(d) { return d.r; })
        .style("fill", function(d) { return color(d.packageName); });

node.append("text")
        .attr("dy", ".3em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.className.substring(0, d.r / 3); });


// Returns a flattened hierarchy containing all leaf nodes under the root.
  function classes(root) {
    var classes = [];

    function recurse(name, node) {
      if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
      else classes.push({packageName: name, className: node.name, value: node.size});
    }

    recurse(null, root);
    return {children: classes};
  }



  d3.select(self.frameElement).style("height", diameter + "px");

Example fiddle: http://jsfiddle.net/mzz9B/47/ 小提琴示例: http : //jsfiddle.net/mzz9B/47/

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

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