Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dataCollection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"author": {
"name": "Cambridge Coding Academy"
},
//list of different libraries, incl twit form twehre we take data in this case
"dependencies": {
"cheerio": "^0.18.0",
"geojson-utils": "^1.1.0",
Expand Down
1 change: 1 addition & 0 deletions dataCollection/tweets.log_old

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions dataCollection/twitterDataAPI.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
//load required libraries defined in package.json
var Twit = require('twit');
var fs = require('fs');
var twit = new Twit({
consumer_key : '???',
consumer_secret : '???',
access_token : '???',
access_token_secret : '???'
});
//authentification

});
//coordinates for the UK
var uk = [ '-9.23', '49.84', '2.69', '60.85' ];
var stream = twit.stream('statuses/filter', { locations: uk })
//filter stream of information to data from within the defined coordinates=selects stream of incoming tweets by location
//on twit = authentification, status updates
var stream = twit.stream('statuses/filter', { track: "ebola, Ebola" });
var log = fs.createWriteStream('tweets.log');

//event 'tweet' defined by 'twit' library
stream.on('tweet', processTweet);

function processTweet(tweet) {
var regexp = /[\w'@#]+/g;
var regexp = /.*/g;
//var regexp = /.*CO.*/g
var words = tweet.text.match(regexp);
log.write(words);
console.log(words)
//log.write(words);
//printing streamed tweets as log to console
//console.log(tweet);
//convert tweet into string using JSON
//var strTweet = JSON.stringify(tweet);
//write into file now
//log.write(strTweet)
//log.write('\n')
//fs.fsync
};
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
</body>
<script src="js/d3vis.js"></script>
</body>

</html>
188 changes: 187 additions & 1 deletion js/d3vis.js
Original file line number Diff line number Diff line change
@@ -1 +1,187 @@
// Let's draw something
// Let's draw something

var body =d3.select("body");
var graphics = body.append("svg");
var width = 900;
var height = 600;

graphics.attr("width", width);
graphics.attr("height", height);

//graphics.append("circle")
//.attr("r", 15) //add a circle
//.attr("cx", 20)
// .attr("cy", 20)

//graphics.append("rect")
//.attr("x", 155)
//.attr("y", 150)
//.attr("height", 345)
//.attr("width", 50)
//
//graphics.append("rect")
// .attr("x", 210)
// .attr("y", 350)
// .attr("height", 145)
// .attr("width", 50)
//
//graphics.append("rect")
// .attr("x", 265)
// .attr("y", 360)
// .attr("height", 135)
// .attr("width", 50)
//
//graphics.append("rect")
// .attr("x", 320)
// .attr("y", 375)
// .attr("height", 120)
// .attr("width", 50)
//
//graphics.append("rect")
// .attr("x", 375)
// .attr("y", 400)
// .attr("height", 95)
// .attr("width", 50)
//
//graphics.append("rect")
// .attr("x", 430)
// .attr("y", 455)
// .attr("height", 40)
// .attr("width", 50)
//
//graphics.append("line")
// .attr("x1", 150)
// .attr("y1", 150)
// .attr("x2", 150)
// .attr("y2", 500)
// .attr("stroke", "#000000")
// .attr("stroke-width",2)
//
//graphics.append("line")
// .attr("x1", 150)
// .attr("y1", 500)
// .attr("x2", 500)
// .attr("y2", 500)
// .attr("stroke", "#000000")
// .attr("stroke-width",2)
//
//graphics.append("text")
//.text("X Axis")
//.attr("x", 510)
//.attr("y", 500)
//
//
//graphics.append("text")
// .text("Y Axis")
// .attr("x", 130)
// .attr("y", 140)
//
//graphics.append("circle")
//.attr("r", 40)
//.attr("cx", 50)
//.attr("cy", 50)
//.style("fill", "#4682B4")
//.style("stroke", "#CCCCCC")
//.style("stroke-width", "3px")
//.style("opacity", "0.5")

//graphics.append("circle")
// .attr("r", 20)
// .attr("cx", 260)
// .attr("cy", 260)
// .style("fill", "#000000")
//.style("opacity", "1.0")
//
graphics.append("circle")
.attr("r", 200)
.attr("cx", 300)
.attr("cy", 300)
.style("fill", "#ffff00")
.style("opacity", "0.5")
graphics.append("circle")
.attr("r", 20)
.attr("cx", 260)
.attr("cy", 260)
.style("fill", "#000000")
.style("opacity", "1.0")

graphics.append("circle")
.attr("r", 20)
.attr("cx", 340)
.attr("cy", 260)
.style("fill", "#000000")
.style("opacity", "1.0")

//graphics.append("text")
// .text("I am drawing")
// .attr("x", 190)
// .attr("y", 30)
// .attr("textanchor", "start")
// .attr("transform","rotate(45)")
// .attr("transform","scale(2,2)");

var arc = d3.svg.arc()
.innerRadius(80)
.outerRadius(100)
.startAngle(2.07)
.endAngle(4)

var arc2 = d3.svg.arc()
.innerRadius(40)
.outerRadius(50)
.startAngle(5.5)
.endAngle(7)

var arc2 = d3.svg.arc()
.innerRadius(40)
.outerRadius(50)
.startAngle(5.5)
.endAngle(7)

var arc3 = d3.svg.arc()
.innerRadius(250)
.outerRadius(260)
.startAngle(5)
.endAngle(8)


graphics.append("path")
//.attr("cx", 300)
//.attr("cy", 200)
//.attr("arc", "start")
.attr("transform","rotate(45)")
.attr("transform","translate(300,300)")
.attr("d",arc);

graphics.append("path")
//.attr("cx", 300)
//.attr("cy", 200)
//.attr("arc", "start")
.attr("transform","rotate(45)")
.attr("transform","translate(260,260)")
.attr("d",arc2);

graphics.append("path")
//.attr("cx", 300)
//.attr("cy", 200)
//.attr("arc", "start")
//.attr("transform","rotate(90)")
.attr("transform","translate(340,260)")
.attr("d",arc2);

graphics.append("path")
//.attr("cx", 300)
//.attr("cy", 200)
//.attr("arc", "start")
.attr("transform","rotate(45)")
.attr("transform","translate(300,360)")
.attr("fill", "#D93E14")
.attr("d",arc3);

//graphics.append("line")
//.attr("x1",100)
//.attr("y1", 300)
//.attr("x2", 85)
//.attr("y2", 300)
//.attr("stroke", "#D93E14")
//.attr("stroke-width", 5);
128 changes: 128 additions & 0 deletions js/ukmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Created by pjn on 30/01/15.
*/
var width = 900;
var height = 700;

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

var projection = d3.geo.orthographic()
.center([-4.4, 55.4])
.scale(4000)
.translate([width / 2, height / 2]);


// Don't forget to change the data file name!
d3.json("data/uk.json", loadData);

function loadData(error, dataset) {
if (error) {
console.log(error);
}
else {
console.log(dataset);
drawData(dataset);
}
};

function drawData(dataset) {
// Draw your data
var ukRegions = topojson.feature(dataset,
dataset.objects.subunits).features;

var path = d3.geo.path()
.projection(projection);
var color = d3.scale.ordinal()
.domain(["ENG", "SCT", "WLS", "NIR"])
.range(["#CC0000", "#003399", "#33CC33", "#006600"]);

graphics.selectAll("path")
.data(ukRegions)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(region){
return color(region.id);
}
);

}

function loadUserData(error, dataset) {
if (error) {
console.log(error);
}
else {
console.log(projection([0.1275,51.5072]));
drawUserData(dataset);
}
};

//function get_coordinates(d) {
// var lon = d.tweets[0].geo.coordinates[0];
// var lat = d.tweets[0].geo.coordinates[1];
// var coordinates =[lat, lon];
// console.log(d, coordinates)
// return "translate(" + projection(coordinates) +")";
//}

function getLongitude (d){
return d.geo.coordinates[1];
}

function getLatitude (d){
return d.geo.coordinates[0];
}

function get_coordinates(d) {
//var coordinates = [d3.mean(d.tweets,getLongitude), d3.mean(d.tweets,getLatitude)];
//console.log(d, coordinates)
return "translate(" + projection(d.geo) +")";
}



function drawUserData(dataset){
for(var i =0; i < dataset.nodes.length; i++){
var user = dataset.nodes[i];
var coordinates = [d3.mean(user.tweets, getLongitude), d3.mean(user.tweets, getLatitude)];
user.geo = coordinates;
}
//The new visualisation
//for (var i =0; i < dataset.nodes.length; i++) {
graphics.selectAll(".tweet")
.data(dataset.nodes)
.enter()
.append("circle")
.attr("class", "tweet")
.attr("r", 2.5)
.style("fill", "#000066")
.style("opacity", 0.5)
.attr("transform", get_coordinates);
//}
graphics.selectAll(".link")
.data(dataset.links)
.enter()
.append("line")
.style("stroke", "#999")
.style("opacity", 0.4)
.attr("x1", function (d) {
return projection(dataset.nodes[d.source].geo)[0];
})
.attr("y1", function (d) {
return projection(dataset.nodes[d.source].geo)[1];
})
.attr("x2", function (d) {
return projection(dataset.nodes[d.target].geo)[0];
})
.attr("y2", function (d) {
return projection(dataset.nodes[d.target].geo)[1];
})



}
d3.json("data/usersGraph.json", loadUserData)
Loading