Skip to content

Instantly share code, notes, and snippets.

@VictorVelarde
Last active April 17, 2017 07:15
Show Gist options
  • Save VictorVelarde/e8dc81273cbe100f17980ccd8b3b88c8 to your computer and use it in GitHub Desktop.
Save VictorVelarde/e8dc81273cbe100f17980ccd8b3b88c8 to your computer and use it in GitHub Desktop.
dimplejs - Food chart

Simple dimple.js chart

Mini.Project 2

The original viz lacks of a clear objective. It seems that it justs wants to show some pretty photos and random numbers. The viz doesn't allow an easy comparison between different food magnitudes.

A little dimplejs chart has been created to achieve this objective

category group percent
Fruit and nuts Total 51
Honey Total 61
Fresh vegetables/melons Total 20
Lamb Total 52
Seafood Total 88
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v2.0.0.min.js"></script>
<style>
h2 {
text-align: left;
padding-left: 50px;
}
</style>
<script type="text/javascript">
function draw(data) {
/*
D3.js setup code
*/
"use strict";
var margin = 75,
width = 600 - margin,
height = 500 - margin;
d3.select("body")
.append("h2")
.text("Imported Food in USA (2012)")
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class','chart');
/*
Dimple.js Chart construction code
*/
var myChart = new dimple.chart(svg, data);
var x = myChart.addCategoryAxis("x", "category");
myChart.addMeasureAxis("y", "percent");
myChart.addSeries("category", dimple.plot.bar);
myChart.assignColor(null, "#03A1C4");
myChart.draw();
};
</script>
</head>
<body>
<script type="text/javascript">
/*
Use D3 (not dimple.js) to load the TSV file
and pass the contents of it to the draw function
*/
d3.csv("food.csv", draw);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment