Skip to content

Instantly share code, notes, and snippets.

@siddharthpolisiti
Last active May 24, 2016 14:13
Show Gist options
  • Save siddharthpolisiti/8051046e3940d7e46d4292c5b9ab4c40 to your computer and use it in GitHub Desktop.
Save siddharthpolisiti/8051046e3940d7e46d4292c5b9ab4c40 to your computer and use it in GitHub Desktop.
Bar and Line Chart for Temperature Comparision (Daringbadi vs Titlagarh) from (2007 - 16)
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
div.tooltip {
position: absolute;
padding: 10px;
background: #f4f4f4;
border: 0px;
border-radius: 3px;
pointer-events: none;
font-size: 11px;
color: #000;
line-height: 16px;
border: 1px solid #d4d4d4;
}
.bar {
fill: steelblue;
}
footer {
background-color: orange;
position: absolute;
left: 0;
bottom: 0;
height: 65px;
width: 100%;
overflow:hidden;
}
header {
background-color: orange;
position: relative;
left: 0;
bottom: 0;
height: 45px;
width: 100%;
overflow:hidden;
}
</style>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>
<body>
<header>
<u><h1> Compararitive study of temperature variations in Daringbadi and Titlagarh over 2007 - 2016 during month of April</h1></u>
</header>
<form action="">
<input type="radio" onClick="line()" name="chart" value="line" checked> Line Chart
<input type="radio" onClick="bar()" name="chart" value="bar"> Bar Chart
</form>
<script>
line();
function line(){
d3.select("svg").remove();
d3.json("https://api.myjson.com/bins/34llw", function(error, maindata) {
if (error) throw error;
var cities = [];
var parseDate = d3.time.format("%B-%Y").parse;
maindata.forEach(function(d) {
cities.push(d3.keys(d).filter(function(key) {
return key !== "date" && key !== "maxLength";
}));
});
cities = d3.set(d3.merge(cities)).values();
console.log(cities)
var myData = [];
var allValues = [];
var allDates =[];
cities.forEach(function(city){
var cityData = {};
cityData.name = city;
cityData.values = [];
maindata.forEach(function(md){
if (md[city]){
allValues.push(md[city])
allDates.push(parseDate(md.date))
cityData.values.push({date: parseDate(md.date), value: md[city]})
}
})
myData.push(cityData)
});
//alert(JSON.stringify(myData))
var that = this;
var deepClonedCopy = jQuery.extend(true, {}, maindata);
var data;
data = $.map(deepClonedCopy, function(el) {
return el
});
var margin = {
top: 15,
right: 30,
bottom: 20,
left: 30
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
if (data.length >= 1) {
var parseDate = d3.time.format("%B-%Y").parse;
var maxDate = d3.time.year.offset(parseDate(d3.max(data, function(d) {
return d.date;
})), +1);
var minDate = d3.time.year.offset(parseDate(d3.min(data, function(d) {
return d.date;
})), -1);
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var x = d3.time.scale()
.domain([minDate, maxDate])
//.domain(d3.extent(allDates))
.range([0, width]);
var y = d3.scale.linear()
.domain([20, d3.max(allValues)])
.range([Math.ceil(height), 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom").ticks(d3.time.year,1);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left").ticks(4).tickSize(-width, 0, 0);
var line = d3.svg.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.value);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis).append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Temperature(°C)");
var wsf = svg.selectAll(".wsf")
.data(myData)
.enter().append("g")
.attr("class", "wsf");
wsf.append("path")
.attr("class", "line")
.attr("d", function(d) {
return line(d.values);
})
.style("stroke", function(d) {
return color(d.name);
});
wsf.selectAll(".dot")
.data(function(d) {
return d.values;
})
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3)
.attr("cx", function(d) {
return x(d.date);
})
.attr("cy", function(d) {
return y(d.value);
})
.attr("stroke", function(d) {
return color(this.parentNode.__data__.name)
})
.attr("fill", function(d) {
return color(this.parentNode.__data__.name)
})
//.attr("fill-opacity", .5)
//.attr("stroke-width", 2)
.on("mouseover", function(d) {
formatDate = d3.time.format("%B-%Y");
div.transition().duration(100).style("opacity", .9);
div.html(this.parentNode.__data__.name + "<br/>" +d.value+"°C during "+formatDate(d.date) )
.style("left", (d3.event.pageX) + "px").style("top", (d3.event.pageY - 28) + "px").attr('r', 8);
d3.select(this).attr('r', 8)
}).on("mouseout", function(d) {
div.transition().duration(600).style("opacity", 0)
d3.select(this).attr('r', 3);
});
var legend = svg.selectAll(".legend")
.data(cities)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) {
return "translate(0," + i * 20 + ")";
});
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 4)
.style("fill", function(d) {
return color(d);
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 6)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) {
return d;
});
}
});
}
function bar(){
d3.select("svg").remove();
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.ordinal()
.range(["#d0743c","#6b486b","#98abc5", "#8a89a6", "#7b6888", "#a05d56", "#ff8c00"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("https://api.myjson.com/bins/34llw", function(error, data) {
if (error) throw error;
var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "date"; });
data.forEach(function(d) {
d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
});
x0.domain(data.map(function(d) { return d.date; }));
x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })+10]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Temperature(°C)");
var date = svg.selectAll(".date")
.data(data)
.enter().append("g")
.attr("class", "date")
.attr("transform", function(d) { return "translate(" + x0(d.date) + ",0)"; });
date.selectAll("rect")
.data(function(d) { return d.ages; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); });
date.selectAll("text")
.data(function(d) { return d.ages; })
.enter().append("text")
.text(function(d) {
return d.value+"°C";
})
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name)+5; })
.attr("y", function(d) { return y(d.value)-5; })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", "red");
var legend = svg.selectAll(".legend")
.data(ageNames.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
}
</script>
</body>
<footer>
<u><h1> Note:</h1></u>
<li> The above data are absolute values. All stats and data as per weather.com and accuweather.</li>
</footer>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment