Library D3 - How to access data from JSON in this example

I did not explain the problem properly in the title ... Sorry.

I am following D3 Tag Cloud example Simple example https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html

I have a JSON file consisting of tweets, sentimental values ​​and tweets; (Excerpt)

var words = [{"word":"disgrace","sentiment":"0.975","tweet":"Wheres Fred the weatherman? \nIn jail #disgrace #dirtyman @MrJimmyCorkhill"},{"word":"dirtyman","sentiment":"0.975","tweet":"Wheres Fred the weatherman? \nIn jail #disgrace #dirtyman @MrJimmyCorkhill"}];

      

I want to use the "tweet" value as the "title" element of each "text" element. I tried to do this by putting my tweet in a .words (or .map, I don't know: s) function because other data is being accessed this way, but I can't fetch my tweet data;

var fill = d3.scale.category20();
var words = <?php echo $tweets->getTweetTags(); ?>;

d3.layout.cloud().size([1000, 1000])
.words(words.map(function(d) {
return {text: d.word, size: d.sentiment * 40, tweet: d.tweet};
}))
.rotate(function() { return ~~(Math.random() * 2) * Math.random() * 1; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();

function draw(words) {
d3.select("#vis").append("svg")
.attr("width", 1000)
.attr("height", 1000)
.append("g")
.attr("transform", "translate(500,400)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; })
.append("svg:title").text(function(d) { return d.tweet; } );
}

      

d.tweet returns undefined

Perhaps I understand why I can't use my tweet in the .words or .map function as they only expect certain parameters, but I don't know how else to get the "tweet" data into the 'tag' header of each "text" element.

Can anyone help me with this?

Edit: code with d3.json internal function;

var data; // a global
            d3.json("../../../assets/json/tweetTags.json", function(error, json) {
              if (error) return console.warn(error);
              data = json;

              var fill = d3.scale.category20();

              d3.layout.cloud().size([1000, 1000])
                  .words(data.map(function(d) {
                    return {text: d.word, size: d.sentiment * 40, tweet: d.tweet};
                  }))
                  .rotate(function() { return ~~(Math.random() * 2) * Math.random() * 1; })
                  .font("Impact")
                  .fontSize(function(d) { return d.size; })
                  .on("end", draw)
                  .start();

              function draw(data) {
                d3.select("#vis").append("svg")
                    .attr("width", 1000)
                    .attr("height", 1000)
                  .append("g")
                    .attr("transform", "translate(500,400)")
                  .selectAll("text")
                    .data(words)
                  .enter().append("text")
                    .style("font-size", function(d) { return d.size + "px"; })
                    .style("font-family", "Impact")
                    .style("fill", function(d, i) { return fill(i); })
                    .attr("text-anchor", "middle")
                    .attr("transform", function(d) {
                      return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
                    })
                    .text(function(d) { return d.text; })
                    .append("svg:title").text(function(d) { return d.tweet; } );
              }

              d3.layout.cloud().size([300, 300])
              .data.map(function(d) {
                return {text: d, size: 10 + Math.random() * 90};
              })
              .rotate(function() { return ~~(Math.random() * 2) * 90; })
              .font("Impact")
              .fontSize(function(d) { return d.size; })
              .on("end", draw)
              .start();
            });

      

I don't know how to make it work like this. I made a json file, but now I cannot access the data or start the cloud.

Edit: Text elements are now added outside the closing body tag :(

var data; // a global
            d3.json("../../../assets/json/tweetTags.json", function(error, json) {
              if (error) return console.warn(error);
              data = json;

              var fill = d3.scale.category20();

             d3.select("#vis").append("svg")
                 .attr("width", 1000)
                 .attr("height", 1000)
                 .append("g")
                 .attr("transform", "translate(500,400)")
                 .data(json)
                 .enter().append("text")
                 .style("font-size", function(d) { return d.sentiment * 40 + "px"; })
                 .style("font-family", "Impact")
                 .style("fill", function(d, i) { return fill(i); })

                 .text(function(d) {return d.word;})
                 .append("svg:title").text(function(d) { return d.tweet; } );

            });

      

Edit: @Chris This is your sample code in D3 json function and with my settings

d3.json("../../../assets/json/tweetTags.json", function(error, json) {
  if (error) return console.warn(error);

  var fill = d3.scale.category20();

  d3.layout.cloud().size([600, 500])
      .words(json.map(function(d) {;
            return {text: d.word, size: d.sentiment * 40, tweet: d.tweet};
       }))
      .rotate(function(d) { return ~~(Math.random() * 2) * 90; })
      .font("Impact")
      .fontSize(function(d) { return d.size; })
      .on("end", draw)
      .start();

  function draw(words) { 
      d3.select("body").append("svg")
          .attr("width", 600).attr("height", 500)
          .append("g").attr("transform", "translate(350,350)")
          .selectAll("text").data(words)
          .enter().append("text")
          .style("font-size", function(d) { return d.size + "px"; })
          .style("font-family", "Impact")
          .style("fill", function(d, i) { return fill(i); })
          .attr("text-anchor", "middle")
          .attr("transform", function(d) {
              return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
          })
          .text(function(d) { return d.text; }).append("svg:title")
          .text(function(d) { return d.tweet; } );
      }
})

      

But I cannot access d.tweet from draw: s function

+3


source to share


3 answers


I suggest you use the built in import function for json instead of php. I think the problem is that reading the data is asynchronous, so it words

doesn't get populated before using it in the clouds.

The principle d3.json()

is to do everything in this function, which will be executed when the json is loaded:

var data; // a global
d3.json("path/to/file.json", function(error, json) {
  if (error) return console.warn(error);
  data = json;
  visualizeit();
});

      

EDIT



Here is some sample code that should work, I just added a d3 cloud example inside a json function.

d3.json("../../../assets/json/tweetTags.json", function(error, json) {
  if (error) return console.warn(error);

  var fill = d3.scale.category20();

  d3.layout.cloud().size([300, 300])    
      .words([  // To be replaced with data
        "Hello", "world", "normally", "you", "want", "more", "words",
        "than", "this"].map(function(d) {
            return {text: d, size: 10 + Math.random() * 90};
       }))
      .rotate(function() { return ~~(Math.random() * 2) * 90; })
      .font("Impact")
      .fontSize(function(d) { return d.size; })
      .on("end", draw)
      .start();

  function draw(words) {
      d3.select("body").append("svg")
          .attr("width", 300).attr("height", 300)
          .append("g").attr("transform", "translate(150,150)")
          .selectAll("text").data(words)
          .enter().append("text")
          .style("font-size", function(d) { return d.size + "px"; })
          .style("font-family", "Impact")
          .style("fill", function(d, i) { return fill(i); })
          .attr("text-anchor", "middle")
          .attr("transform", function(d) {
              return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
          })
          .text(function(d) { return d.text; });
      }
})

      

Then, when that version works, you can simply replace .words()

with:

.words(json.map(function(d) {
  return {text: d.word, size: d.sentiment * 40, tweet: d.tweet};
}))

      

+6


source


Can you retrieve other records of your data? like words and feelings?
Perhaps you could try it like the following code. If it works, you can change it to your codes.



var data; // a global
d3.json("path/to/file.json", function(error, json) {
  if (error) return console.warn(error);
  d3.select("#vis").append("svg")
    .data(json)
    .enter().append("text")
    .text(function(d) {return d.tweets;});
});

      

0


source


I had the same problem with words coming out of the svg container and I solved it by inserting a variable called origin which I put (width / 2) in the translation method. Border problems were caused by the algorithm being made to keep them inside as long as you are using the correct center of the projection box, eg: width = 300, height = 300, which means start = 150. Hope this helps you.

0


source







All Articles