Skip to content

Instantly share code, notes, and snippets.

@ngopal
Created May 3, 2014 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngopal/11507708 to your computer and use it in GitHub Desktop.
Save ngopal/11507708 to your computer and use it in GitHub Desktop.
margin = {top: 20, right: 20, bottom: 30, left: 50}
width = 960 - margin.left - margin.right
height = 500 - margin.top - margin.bottom
parseDate = d3.time.format("%Y-%m-%d").parse
x = d3.time.scale().range([0, width])
y = d3.scale.linear().range([height, 0])
xAxis = d3.svg.axis().scale(x).orient("bottom");
yAxis = d3.svg.axis().scale(y).orient("left")
line = d3.svg.line()
.x((d) -> x d.date)
.y((d) -> y d.close)
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.tsv "http://jsbin.com/sojah/1.js", (error, data) ->
data.forEach (d) ->
d.date = parseDate d.date
d.close = +d.close
d.volume = +d.volume
x.domain d3.extent data, (d) -> d.date
y.domain d3.extent data, (d) -> d.close
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")
.attr("fill", "white")
.style("text-anchor", "end")
.text("Price ($)")
.style("fill", "black")
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line)
max_volume = d3.max(data, (d) -> d.volume)
max_close = d3.max(data, (d) -> d.close)
scaleVolume = (value) ->
(value / max_volume) * max_close
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", (d) -> x d.date)
.attr("width", 1)
.attr("y", (d) -> y scaleVolume d.volume)
.attr("height", (d) -> height - y scaleVolume d.volume)
body {
font: 10px sans-serif;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown; /*Color of the bars*/
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: #00090F; /*Color of the line*/
stroke-width: 1.5px;
}
svg {
background: #E6EDEC;
}
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[add your bin description]" />
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment