Skip to content

Instantly share code, notes, and snippets.

@RockyNiu
Last active March 29, 2016 15:07
Show Gist options
  • Save RockyNiu/a4badcafc7a1d6c5b8d0 to your computer and use it in GitHub Desktop.
Save RockyNiu/a4badcafc7a1d6c5b8d0 to your computer and use it in GitHub Desktop.
A demo for Dimple.js
svg {
height: 80vh; !important;
width: 90vm; !important;
}
circle.dimple-series-1 {
fill: red;
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" content="width=device-width, initial-scale=1.0">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//dimplejs.org/dist/dimple.v2.0.0.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<title>Job Loss By Quarter</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
(function () {
function draw(data) {
var svg = d3.select("#container .row svg")
.append('g')
.attr('class', 'chart');
var myChart = new dimple.chart(svg, data);
var x = myChart.addTimeAxis("x", "quarter", "%m-%Y", "%m-%Y");
var y = myChart.addMeasureAxis("y", "job_loss");
x.title = "";
x.fontSize = "1.2em";
y.title = "Job Loss (millions)";
y.fontSize = "1.2em";
myChart.addSeries(null, dimple.plot.line);
var scatterSeries = myChart.addSeries(null, dimple.plot.scatter);
scatterSeries.getTooltipText = function (e) {
return [
"Time: " + d3.time.format("%m-%Y")(e.xField[0]),
"Job Loss: " + e.yValue + " millions"
];
};
myChart.draw();
myChart.axes[0].shapes.call(
d3.svg.axis()
.scale(myChart.axes[0]._scale)
.tickValues(data.map(function (d, i) {
return d3.time.format("%m-%Y").parse(d.quarter);
}))
.tickFormat(d3.time.format("%m-%Y"))
.orient("bottom")
);
x.shapes.selectAll("text").attr("transform",
function (d) {
return d3.select(this).attr("transform") + " rotate(-90)";
});
}
$('body').append('<div class="container text-center" id="container">\
<div class="row">\
<h2></h2>\
<svg class="col-md-12">\
</svg>\
</div>\
</div>');
d3.select("#container .row h2").text("Job Loss in U.S.");
d3.json("job_loss_by_quarter.json", draw);
})();
[
{
"quarter": "12-2007",
"job_loss": 7
},
{
"quarter": "09-2008",
"job_loss": 9
},
{
"quarter": "03-2009",
"job_loss": 13.5
},
{
"quarter": "06-2010",
"job_loss": 15
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment