Skip to content

Instantly share code, notes, and snippets.

@pg1647
Last active June 1, 2020 03:54
Show Gist options
  • Save pg1647/79bd129c3b0f6f2b3a2091460396d497 to your computer and use it in GitHub Desktop.
Save pg1647/79bd129c3b0f6f2b3a2091460396d497 to your computer and use it in GitHub Desktop.
Data Vis. Summer 2020 || Lab 1- Task 4 // source https://jsbin.com/kuqevov
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>DV1 - Lab : Task 4</title>
<style id="jsbin-css">
.bar {
fill: salmon;
}
</style>
</head>
<body>
<div id="chart"></div>
<script id="jsbin-javascript">
function addElementSVG(parent, name, attrs={}) {
const SVGNS = "http://www.w3.org/2000/svg";
let element = document.createElementNS(SVGNS, name);
for (let key in attrs)
element.setAttributeNS(null, key, attrs[key]);
parent.appendChild(element);
return element;
}
var data = Array(10).fill(null).map(x => Math.random()*100);
var maxValue = Math.max(...data);
// a(...[1,2,3]) -> a(1,2,3)
var chart = document.getElementById('chart');
var svg = addElementSVG(chart, 'svg', {
'width': 300,
'height': 400,
});
data.forEach((d, i) => {
addElementSVG(svg, "rect", {
"class": "bar",
"x": 60,
"y": 50 + i*20,
"width": d/maxValue*200,
"height": 18,
});
});
addElementSVG(svg, "text", {
"x": 20,
"y": 20,
}).textContent = "My Chart Title";
</script>
<script id="jsbin-source-css" type="text/css">.bar {
fill: salmon;
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">function addElementSVG(parent, name, attrs={}) {
const SVGNS = "http://www.w3.org/2000/svg";
let element = document.createElementNS(SVGNS, name);
for (let key in attrs)
element.setAttributeNS(null, key, attrs[key]);
parent.appendChild(element);
return element;
}
var data = Array(10).fill(null).map(x => Math.random()*100);
var maxValue = Math.max(...data);
// a(...[1,2,3]) -> a(1,2,3)
var chart = document.getElementById('chart');
var svg = addElementSVG(chart, 'svg', {
'width': 300,
'height': 400,
});
data.forEach((d, i) => {
addElementSVG(svg, "rect", {
"class": "bar",
"x": 60,
"y": 50 + i*20,
"width": d/maxValue*200,
"height": 18,
});
});
addElementSVG(svg, "text", {
"x": 20,
"y": 20,
}).textContent = "My Chart Title";</script></body>
</html>
.bar {
fill: salmon;
}
function addElementSVG(parent, name, attrs={}) {
const SVGNS = "http://www.w3.org/2000/svg";
let element = document.createElementNS(SVGNS, name);
for (let key in attrs)
element.setAttributeNS(null, key, attrs[key]);
parent.appendChild(element);
return element;
}
var data = Array(10).fill(null).map(x => Math.random()*100);
var maxValue = Math.max(...data);
// a(...[1,2,3]) -> a(1,2,3)
var chart = document.getElementById('chart');
var svg = addElementSVG(chart, 'svg', {
'width': 300,
'height': 400,
});
data.forEach((d, i) => {
addElementSVG(svg, "rect", {
"class": "bar",
"x": 60,
"y": 50 + i*20,
"width": d/maxValue*200,
"height": 18,
});
});
addElementSVG(svg, "text", {
"x": 20,
"y": 20,
}).textContent = "My Chart Title";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment