Skip to content

Instantly share code, notes, and snippets.

@jamesleesaunders
Last active July 16, 2019 20:12
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 jamesleesaunders/cc4439445d228fc06358 to your computer and use it in GitHub Desktop.
Save jamesleesaunders/cc4439445d228fc06358 to your computer and use it in GitHub Desktop.
d3-ez : HTML Table Example
<!DOCTYPE html>
<html>
<head>
<title>d3-ez : HTML Table Example</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://raw.githack.com/jamesleesaunders/d3-ez/master/dist/d3-ez.js"></script>
<link rel="stylesheet" type="text/css" href="http://rawgit.com/jamesleesaunders/d3.ez/master/dist/d3-ez.css" />
</head>
<body>
<div id="tableholder"></div>
<br/>
<div>Value: <span id="message"></span></div>
<script type="text/javascript">
d3.json("uk_elections.json").then(function(data) {
// UK Election Results Source: http://www.ukpolitical.info/2015.htm
// Create chart object
var myTable = d3.ez.component.htmlTable()
.width(600)
.on("customValueMouseOver", function(d) {
d3.select("#message").text(d.value);
})
.on("customSeriesClick", function(d) {
console.log(d);
});
// Add to page
d3.select("#tableholder")
.datum(data)
.call(myTable);
});
</script>
</body>
</html>
[
{
"key": "1992",
"values": [
{
"key": "Conservative",
"value": 336
},
{
"key": "Labour",
"value": 271
},
{
"key": "Liberal Democrat",
"value": 20
},
{
"key": "Scottish National",
"value": 3
}
]
},
{
"key": "1997",
"values": [
{
"key": "Conservative",
"value": 165
},
{
"key": "Labour",
"value": 418
},
{
"key": "Liberal Democrat",
"value": 46
},
{
"key": "Scottish National",
"value": 6
}
]
},
{
"key": "2001",
"values": [
{
"key": "Conservative",
"value": 412
},
{
"key": "Labour",
"value": 166
},
{
"key": "Liberal Democrat",
"value": 52
},
{
"key": "Scottish National",
"value": 5
}
]
},
{
"key": "2005",
"values": [
{
"key": "Conservative",
"value": 356
},
{
"key": "Labour",
"value": 198
},
{
"key": "Liberal Democrat",
"value": 62
},
{
"key": "Scottish National",
"value": 6
}
]
},
{
"key": "2010",
"values": [
{
"key": "Conservative",
"value": 306
},
{
"key": "Labour",
"value": 258
},
{
"key": "Liberal Democrat",
"value": 57
},
{
"key": "Scottish National",
"value": 6
}
]
},
{
"key": "2015",
"values": [
{
"key": "Conservative",
"value": 331
},
{
"key": "Labour",
"value": 232
},
{
"key": "Liberal Democrat",
"value": 8
},
{
"key": "Scottish National",
"value": 56
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment