/    Sign up×
Community /Pin to ProfileBookmark

[ D3 Library ] How To Access Data from JSON

Hello, I’m following the D3 Tag Cloud Simple example
[url]https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html[/url]

I have a JSON file that consists of tweet tags, sentimental values and tweet text;
(excerpt)

[CODE]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”}];[/CODE]

I want to use the “tweet” value as a <title> attribute of each <text> element.
I tried doing this by putting my tweet in the .words function (or .map, I don’t know :s), as other data is accessed that way, but I can’t extract my ‘tweet’ data;

[CODE]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; } );
}[/CODE]

d.tweet returns undefined

I can probably 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 <title> tags of each <text> element.

Can anyone help me with this?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @anevins spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...