Skip to content

Instantly share code, notes, and snippets.

@ayala-usma
Last active October 6, 2017 01:19
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 ayala-usma/e4e1ae42fd7b962b915e4b8406e451ef to your computer and use it in GitHub Desktop.
Save ayala-usma/e4e1ae42fd7b962b915e4b8406e451ef to your computer and use it in GitHub Desktop.
Sucre Crops Linechart
license: mit

This is a derivative of the simple d3.js graph in v4 used as an example in the book D3 Tips and Tricks v4. The stock data was sourced from Mike Bostock's small multiples example.

It is the fourth graph in a series that is building a multi-line graph that incorperates an automatically developed legend and a system that can toggle the lines off and on as desired.

This version is demonstrating the ability to toggle the lines off and on by clicking on the appropriate legend text.

The graph should be taken in context with the text of the book which can be downloaded for free from Leanpub.

forked from d3noob's block: Sucre Crops Linechart

cultivo año utilidad rentabilidad
AGUACATE 2011 848.021984 5107
AHUYAMA 2011 9.272016 4154
AJÍ DULCE 2011 594.806133 58395
BATATA 2011 31.76652 5351
BERENJENA 2011 87.626909 11851
CACAO 2011 401.34252 19052
CAÑA PANELERA 2011 2163.633384 3359
COCO 2011 16.158303 126
COL 2011 19.58718 7708
LIMÓN CRIOLLO 2011 2.4504 2379
MARACUYA 2011 171.917079 12273
ÑAME CRIOLLO ASOCIADO 2011 9662.250931 13135
ÑAME CRIOLLO SOLO 2011 5481.272722 14949
ÑAME ESPINO 2011 4991.62111 6437
NARANJA DULCE 2011 83.68492 3363
PALMA AFRICANA 2011 -130.0123 -3077
PAPAYA 2011 165.042828 42365
PLATANO 2011 95.819856 432
TABACO NEGRO 2011 377.641643 2029
TABACO RUBIO 2011 1908.46758 11617
YUCA ASOCIADA 2011 23070.047224 10143
YUCA INDUSTRIAL 2011 -740.436375 -1107
YUCA SOLA 2011 -5748.706748 -6829
AGUACATE 2012 1650.773128 10394
AHUYAMA 2012 306.5662 37416
AJÍ DULCE 2012 439.83464 38046
BATATA 2012 79.3 122
BERENJENA 2012 225.3174 28188
CACAO 2012 -115.647005 -3979
CAÑA PANELERA 2012 2413.866437 35716
COCO 2012 44.755189 348
COL 2012 16.2244 7626
LIMÓN CRIOLLO 2012 14.9234165 6962
MARACUYA 2012 93.615358 5911
ÑAME CRIOLLO ASOCIADO 2012 18218.32412 32366
ÑAME CRIOLLO SOLO 2012 9964.82793 34371
ÑAME ESPINO 2012 7935.03846 10916
NARANJA DULCE 2012 -91.9730925 -3117
PALMA AFRICANA 2012 -20.0525 -472
PAPAYA 2012 280.3141 60824
PLATANO 2012 403.037808 1685
TABACO NEGRO 2012 876.452104 4126
TABACO RUBIO 2012 1494.56508 16555
YUCA ASOCIADA 2012 57916.961632 25013
YUCA INDUSTRIAL 2012 1364.684751 1642
YUCA SOLA 2012 11874.968873 30884
AGUACATE 2013 4284.861156 32759
AHUYAMA 2013 -23.9967 -2758
AJÍ DULCE 2013 474.06 14257
BATATA 2013 78.5 15243
BERENJENA 2013 265.9362 2723
CACAO 2013 -1340.840845 -8473
CAÑA PANELERA 2013 2427.879 53512
COCO 2013 99.299 83
COL 2013 15.5955 712
LIMÓN CRIOLLO 2013 -8.441559 -3605
MARACUYA 2013 262.95005 804
ÑAME CRIOLLO ASOCIADO 2013 20984.6146 25793
ÑAME CRIOLLO SOLO 2013 12322.0794 32936
ÑAME ESPINO 2013 19084.4504 18728
NARANJA DULCE 2013 -137.988265 -3928
PALMA AFRICANA 2013 -6861.2441 -9496
PAPAYA 2013 186.663 2768
PLATANO 2013 1053.7195 3868
TABACO NEGRO 2013 3983.21192 14688
TABACO RUBIO 2013 5929.78245 26272
YUCA ASOCIADA 2013 26424.42 11988
YUCA INDUSTRIAL 2013 3086.7855 3941
YUCA SOLA 2013 18865.41365 17056
AGUACATE 2014 7676.5935704 54513
AHUYAMA 2014 -4.3045 -5893
AJÍ DULCE 2014 130.55166667 8433
BATATA 2014 38.16 912
BERENJENA 2014 155.493 8382
CACAO 2014 -124.13892 -19
CAÑA PANELERA 2014 2278.60133333 46651
COCO 2014 450.07316667 3026
COL 2014 -14.5436 -8151
LIMÓN CRIOLLO 2014 -6.5049365 -2109
MARACUYA 2014 13.295358 809
ÑAME CRIOLLO ASOCIADO 2014 13851.50540882 32673
ÑAME CRIOLLO SOLO 2014 7450.99 36217
ÑAME ESPINO 2014 14659.74236 21077
NARANJA DULCE 2014 -108.3841575 -2497
PALMA AFRICANA 2014 -613.641 -1687
PAPAYA 2014 268.0555875 45018
PLATANO 2014 1295.9438 4223
TABACO NEGRO 2014 3256.8884025 15306
TABACO RUBIO 2014 8487.372465 36391
YUCA ASOCIADA 2014 21387.25 16496
YUCA INDUSTRIAL 2014 3040.97375 386
YUCA SOLA 2014 17118.32445 23433
AGUACATE 2015 6541.32048375 39485
AHUYAMA 2015 -6.052476 -6118
AJÍ DULCE 2015 129.8846084 5682
BATATA 2015 122.35835 21227
BERENJENA 2015 127.24455333 441
CACAO 2015 2242.86625166 19968
CAÑA PANELERA 2015 2497.0289751 44038
COCO 2015 634.79367314 34
COL 2015 2.069324 106
LIMÓN CRIOLLO 2015 -7.7942524 -22
MARACUYA 2015 279.9157064 12627
ÑAME CRIOLLO ASOCIADO 2015 5553.90666847 2852
ÑAME CRIOLLO SOLO 2015 10619.67356551 22607
ÑAME ESPINO 2015 4563.995006 9384
NARANJA DULCE 2015 376.2521559 6064
PALMA AFRICANA 2015 810.894525 2706
PAPAYA 2015 103.8301124 23968
PLATANO 2015 1164.10106616 3461
TABACO NEGRO 2015 3112.80215396 13515
TABACO RUBIO 2015 6549.63494476 31117
YUCA ASOCIADA 2015 10962.57315089 6061
YUCA INDUSTRIAL 2015 1268.68451409 173
YUCA SOLA 2015 26409.37091515 35023
<!DOCTYPE html>
<head>
<link href="https://fonts.googleapis.com/css?family=Oxygen" rel="stylesheet">
</head>
<meta charset="utf-8">
<style> /* set the CSS */
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: black;
stroke-width: 2;
shape-rendering: crispEdges;
}
.axisLabels
{
font-family: 'Oxygen', sans-serif;
font-size: 15px;
}
.chartTitle
{
font-size: 17px;
font-family: 'Oxygen';
}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 20, right: 200, bottom: 50, left: 80},
width = 1024 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
// Set the ranges
var x = d3.scaleLinear().range([0, width]),
y = d3.scaleLinear().range([height, margin.top]);
// Define the line
var priceline = d3.line()
.x(function(d) { return x(d.año); })
.y(function(d) { return y(d.utilidad); });
// Adds the svg canvas
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 + ")");
// Get the data
d3.csv("formatted_data.csv", function(error, data) {
//This is for assuring that the data is correctly interpreted by JavaScript
data.forEach(function(d) {
d.año = parseInt(d.año);
d.utilidad = parseInt(d.utilidad);
d.rentabilidad = parseInt(d.rentabilidad);
});
//Obtaining maximum, minimum values, and number of years
var yMaxScale = d3.max(data, function(d) { return d.utilidad; }),
yMinScale = d3.min(data, function(d) { return d.utilidad; }),
xMinScale = d3.min(data, function(d) { return d.año; }),
xMaxScale = d3.max(data, function(d) { return d.año; }),
uniqueYears = d3.map(data, function(d){return d.año}).keys().length;
// Scale with the range of the data
x.domain([xMinScale, xMaxScale]);
y.domain([yMinScale, yMaxScale]);
// Nest the entries by symbol
var dataNest = d3.nest()
.key(function(d) {return d.cultivo;})
.entries(data);
// set the colour scale
var color = d3.scaleOrdinal(d3.schemeCategory20);
var legendSpace = 20; // spacing for the legend in arbitrary units
// Loop through each symbol / key
dataNest.forEach(function(d,i) {
svg.append("path")
.attr("class", "line")
.style("stroke", "#ddd")
.attr("id", 'tag'+d.key.replace(/\s+/g, '')) // assign an ID
.attr("d", priceline(d.values))
.attr("opacity", 0.2)
.on("click", function(){
// Determine if current line is visible
var active = d.active ? false : true,
newColor = active ? color(d.key) : "#ddd";
newOpacity = active ? 1 : 0.2;
// Hide or show the elements upon clicking the key on the right
d3.select(this)
.transition().duration(300)
.style("stroke", newColor)
.style("opacity", newOpacity);
//Change the behaviour of the text
d3.select(".legend"+d.key.replace(/\s+/g, ''))
.style("fill", newColor);
// Update whether or not the elements are active
d.active = active;
}) ;
// Add the Legend
svg.append("text")
.attr("x", width + (legendSpace) ) // space legend
.attr("y", height - (i*20))
.attr("font-weight", "bold")
.attr("class", 'legend'+d.key.replace(/\s+/g, '')) // style the legend
.style("fill", "#ddd")
.on("click", function(){
// Determine if current line is visible
var active = d.active ? false : true,
newColor = active ? color(d.key) : "#ddd";
newOpacity = active ? 1 : 0.2;
// Hide or show the elements upon clicking the key on the right
d3.select("#tag"+d.key.replace(/\s+/g, ''))
.transition().duration(300)
.style("stroke", newColor)
.style("opacity", newOpacity);
//Change the behaviour of the text
d3.select(this)
.transition().duration(300)
.style("fill", newColor);
// Update whether or not the elements are active
d.active = active;
})
.text(d.key);
});
var axisLabelProperties = {yOffset: -60, yHeight:-(height/1.5), xLength: width/2, xOffset: height + margin.bottom};
// Add the X Axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(uniqueYears).tickFormat(d3.format("d")));
//Add the X axis label
svg.append("text")
.text("Año")
.attr("class", "axisLabels")
.attr("x",axisLabelProperties.xLength)
.attr("y",axisLabelProperties.xOffset);
// Add the Y Axis
svg.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y));
//Add the Y Axis label
svg.append("text")
.text("Utilidad (Millones de pesos)")
.attr("class", "axisLabels")
.attr("transform", "rotate(-90)")
.attr("x", axisLabelProperties.yHeight)
.attr("y", axisLabelProperties.yOffset);
//Add the chart title
svg.append("text")
.text("Rentabilidad de los productos agrícolas de Sucre en el periodo 2011 - 2015")
.attr("x", width/7)
.attr("y", 0)
.attr("class", "chartTitle");
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment