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

03 communication done #43

Open
wants to merge 6 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
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import time
import sys
import random

import pyorient

Expand Down Expand Up @@ -39,6 +40,9 @@ def getData():

records = client.command(query.format(lat1, lat2, lng1, lng2))

random.shuffle(records)
records = records[:100]

numListings = len(records)
print 'received ' + str(numListings) + ' records'

Expand Down
152 changes: 110 additions & 42 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<title>Flask Demo</title>
<title>DMC Web Stack</title>
<script type="text/javascript" src="./static/lib/d3.js"></script>
<script type="text/javascript" src="./static/lib/leaflet.js"></script>
<link rel="stylesheet" href="./static/lib/leaflet.css" />
Expand All @@ -9,15 +9,46 @@
body {
padding: 0;
margin: 0;
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
line-height: 18px;
}
html, body, #map {
height: 100%;
}

circle {
fill-opacity: 0;
stroke: red;
stroke-width: 5px;
fill-opacity: .3;
fill: red;
stroke-width: 0px;
}
circle:hover {
stroke: black;
stroke-width: 2px;
cursor: crosshair;
}

div.tooltip{
padding: 6px;
background: white;
visibility: hidden;
position: absolute;
z-index: 10;
}
p.tooltip-text{
margin: 0px;
padding: 0px;
}

div.menu{
position: fixed;
margin: 0px;
padding: 10px;
top: 0px;
right: 0px;
width: 250px;
height: 500px;
background: rgba(255,255,255,.8);
}
</style>

Expand All @@ -28,8 +59,28 @@

<div id="map"></div>

<div class="tooltip">
<p class="tooltip-text" id="title">This is the title</p>
<p class="tooltip-text" id="price">This is the price</p>
</div>

<div class="menu">
<p><h4>DMC Web Stack</h4></p>
<form>
<p><input id="heatmap" type="checkbox">Heat Map</p>
<p><input type="button" value="Update Data" onclick="updateData()"></p>
</form>
</div>


<script type="text/javascript">


var tooltip = d3.select("div.tooltip");
var tooltip_title = d3.select("#title");
var tooltip_price = d3.select("#price");


var map = L.map('map').setView([22.539029, 114.062076], 16);

//this is the OpenStreetMap tile implementation
Expand Down Expand Up @@ -63,44 +114,61 @@
var transform = d3.geo.transform({point: projectStream});
var path = d3.geo.path().projection(transform);

d3.json("/getData/", function(data) {

//create placeholder circle geometry and bind it to data
var circles = g.selectAll("circle").data(data.features);

circles.enter()
.append("circle")
.attr("r", 10);

// function to update the data
function update() {

// get bounding box of data
var bounds = path.bounds(data),
topLeft = bounds[0],
bottomRight = bounds[1];

var buffer = 50;

// reposition the SVG to cover the features.
svg .attr("width", bottomRight[0] - topLeft[0] + (buffer * 2))
.attr("height", bottomRight[1] - topLeft[1] + (buffer * 2))
.style("left", (topLeft[0] - buffer) + "px")
.style("top", (topLeft[1] - buffer) + "px");

g .attr("transform", "translate(" + (-topLeft[0] + buffer) + "," + (-topLeft[1] + buffer) + ")");

// update circle position and size
circles
.attr("cx", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).x; })
.attr("cy", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).y; });
};

// call function to
update();
map.on("viewreset", update);

});
function updateData(){
d3.json("/getData/", function(data) {

//create placeholder circle geometry and bind it to data
var circles = g.selectAll("circle").data(data.features);

circles.enter()
.append("circle")
.on("mouseover", function(d){
tooltip.style("visibility", "visible");
tooltip_title.text(d.properties.name);
tooltip_price.text("Price: " + d.properties.price);
})
.on("mousemove", function(){
tooltip.style("top", (d3.event.pageY-10)+"px")
tooltip.style("left",(d3.event.pageX+10)+"px");
})
.on("mouseout", function(){
tooltip.style("visibility", "hidden");
})
;

// function to update the data
function update() {

// get bounding box of data
var bounds = path.bounds(data),
topLeft = bounds[0],
bottomRight = bounds[1];

var buffer = 50;

// reposition the SVG to cover the features.
svg .attr("width", bottomRight[0] - topLeft[0] + (buffer * 2))
.attr("height", bottomRight[1] - topLeft[1] + (buffer * 2))
.style("left", (topLeft[0] - buffer) + "px")
.style("top", (topLeft[1] - buffer) + "px");

g .attr("transform", "translate(" + (-topLeft[0] + buffer) + "," + (-topLeft[1] + buffer) + ")");

// update circle position and size
circles
.attr("cx", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).x; })
.attr("cy", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).y; })
.attr("r", function(d) { return Math.pow(d.properties.price,.3); });
};

// call function to
update();
map.on("viewreset", update);
});

};

updateData();

</script>

Expand Down