D3 bar chart experiment
How do I make an d3 bar chart experiment?
Making my hands dirty with some d3js stuff, creating a simple bar chart using HTML DOM elements and transition between two data sets.. What is a d3 bar chart experiment? How do you make a d3 bar chart experiment? This script and codes were developed by Matthias Dittgen on 26 August 2022, Friday.
D3 bar chart experiment - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>d3 bar chart experiment</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <button>update</button>
<div id="vis"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/d3/3.4.7/d3.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
D3 bar chart experiment - Script Codes CSS Codes
body { padding: 20px; background-color: #f0f0f0;
}
button { margin-bottom: 20px;
}
#vis .bar { padding: 3px; margin: 1px; position: relative; background-color: #ccc; color: #fff; font-family: Helvetica, Arial, sans-serif; text-align: right;
}
#vis .bar i { position: absolute; top: 2px; left: 2px; font-style: normal; font-size: 10px;
}
D3 bar chart experiment - Script Codes JS Codes
var bars;
var maxWidth = 500;
var maxFontSize = 60;
var px = function(i) { return i+"px"; };
var mult = function(a, b) { return a*b; };
var data1 = [ {id: 1, value: 15}, {id: 2, value: 2}, {id: 4, value: 12}, {id: 5, value: 20}, {id: 7, value: 11}, {id: 8, value: 10}];
var data2 = [ {id: 1, value: 15}, {id: 2, value: 11}, {id: 3, value: 5}, {id: 4, value: 19}, {id: 5, value: 18}, {id: 6, value: 3}, {id: 7, value: 11}];
var dataMax = d3.max(d3.merge([data1, data2]), function (d) { return d.value; });
var ls = d3.scale.linear().domain([0, dataMax]);
var lsC = d3.scale.linear().domain([0, dataMax]).range(['#9cf', '#369']);
var lsWidth = function (d, i) { return px(mult(maxWidth, ls(d.value))); };
var lsFontSize = function (d, i) { return px(mult(maxFontSize, ls(d.value))); };
var lsColor = function (d, i) { return lsC(d.value); };
var d3Update = function (data) { bars = d3.select('#vis') .selectAll('.bar') .data(data, function (d, i) { return d.id; }); bars.enter().append('div') .classed({'bar': true}) .style('width', "0") .each(function (d, i) { d3.select(this).append('b').text(d.value); d3.select(this).append('i').text(d.id); }); d3.select('#vis') .selectAll('.bar') .sort(function (d1, d2) { console.log(d1,d2); if (d1.id < d2.id) return -1; if (d1.id > d2.id) return +1; return 0; }); bars.transition().duration(500) .style('width', lsWidth) .style('font-size', lsFontSize) .style('background-color', lsColor) .select('b') .text(function(d, i) { return d.value; }); bars.exit().transition().duration(500) .style('width', function(d) { return "0px"; }) .remove();
}
d3Update(data1);
var i = 0;
d3.select('button').on('click', function () { i++; d3Update(i%2==0?data1:data2);
});
Developer | Matthias Dittgen |
Username | matths |
Uploaded | August 26, 2022 |
Rating | 3 |
Size | 2,838 Kb |
Views | 38,456 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Animating non-CSS property | 3,794 Kb |
Beautiful canvas stars | 2,399 Kb |
Timebox | 4,202 Kb |
Smooth font animation | 2,194 Kb |
Additive color mix with triangles | 2,760 Kb |
Centering something | 2,199 Kb |
WIP | 2,592 Kb |
D3 pie chart experiment | 3,821 Kb |
SVG Button | 1,927 Kb |
Draw a line coord | 3,687 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
HTML5 Video Autoplay | Zivcos | 1,352 Kb |
Search Box in Content Moves to Fixed Header | Chriscoyier | 2,768 Kb |
JQuery exercise | Brian-baum | 3,780 Kb |
Button shaking | SusanneLundblad | 2,227 Kb |
Portfolio Page | HuffmanJ25 | 5,240 Kb |
Wikipedia Viewer | Thalpha | 4,426 Kb |
Easing | GreenSock | 2,043 Kb |
Fun form with currentColor | Bnthor | 2,713 Kb |
Typefaces with descriptions | Megandoty | 2,944 Kb |
Playing with FlexBox | _Billy_Brown | 3,162 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!