Skip to content

Instantly share code, notes, and snippets.

@phocks
Last active July 31, 2017 05:52
Show Gist options
  • Save phocks/878340b26d0aa69658854c17cdf2e046 to your computer and use it in GitHub Desktop.
Save phocks/878340b26d0aa69658854c17cdf2e046 to your computer and use it in GitHub Desktop.
Barcode chart
license: mit
height: 200

A barcode chart with D3. Here it is.

{"data":[
{"percent":"85"},
{"percent":"80"},
{"percent":"86"},
{"percent":"86"},
{"percent":"87"},
{"percent":"87"},
{"percent":"88"},
{"percent":"88"},
{"percent":"79"},
{"percent":"77"},
{"percent":"86"},
{"percent":"49"},
{"percent":"80"},
{"percent":"71"},
{"percent":"85"},
{"percent":"78"},
{"percent":"72"},
{"percent":"72"},
{"percent":"79"},
{"percent":"83"},
{"percent":"90"},
{"percent":"56"},
{"percent":"78"},
{"percent":"82"},
{"percent":"81"},
{"percent":"75"},
{"percent":"82"},
{"percent":"79"},
{"percent":"84"},
{"percent":"83"},
{"percent":"61"},
{"percent":"75"},
{"percent":"87"},
{"percent":"86"},
{"percent":"71"},
{"percent":"87"},
{"percent":"55"},
{"percent":"31"},
{"percent":"24"},
{"percent":"45"},
{"percent":"33"},
{"percent":"45"},
{"percent":"16"},
{"percent":"15"},
{"percent":"40"},
{"percent":"59"},
{"percent":"64"},
{"percent":"60"},
{"percent":"82"},
{"percent":"25"},
{"percent":"82"},
{"percent":"34"},
{"percent":"36"},
{"percent":"34"},
{"percent":"44"},
{"percent":"80"},
{"percent":"54"},
{"percent":"66"},
{"percent":"70"},
{"percent":"42"},
{"percent":"79"},
{"percent":"56"},
{"percent":"75"},
{"percent":"82"},
{"percent":"93"},
{"percent":"86"},
{"percent":"61"},
{"percent":"79"},
{"percent":"48"},
{"percent":"68"},
{"percent":"65"},
{"percent":"72"},
{"percent":"66"},
{"percent":"56"},
{"percent":"51"},
{"percent":"69"},
{"percent":"93"},
{"percent":"91"},
{"percent":"65"},
{"percent":"87"},
{"percent":"87"},
{"percent":"38"},
{"percent":"32"},
{"percent":"32"},
{"percent":"59"},
{"percent":"71"},
{"percent":"52"},
{"percent":"37"},
{"percent":"23"},
{"percent":"14"},
{"percent":"38"},
{"percent":"30"},
{"percent":"51"},
{"percent":"44"},
{"percent":"16"},
{"percent":"56"},
{"percent":"54"}
]}
<!DOCTYPE html>
<meta charset="utf-8">
<title>Barcode Chart</title>
<style>
body {
text-align: center;
}
svg {
margin: 76px auto;
}
</style>
<body></body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var chartWidth = 400,
chartHeight = 24,
barWidth = 2, // chartWidth / 100;
barColor = 'rgba(0, 0, 0, 0.1)';
var highlightBarWidth = 3,
highlightBarHeight = 28;
highlightBarColor = 'rgba(0, 161, 230, 1.0)';
var chartScale = d3.scaleLinear()
.domain([0, 100])
.range([0, chartWidth]);
d3.json('data.json', function(data) {
drawChart(data.data);
});
function drawChart(data) {
var svgEl = d3.select('body')
.append('svg')
.attr('width', chartWidth)
.attr('height', chartHeight * 2);
barcodeGroup = svgEl.append('g')
.attr('transform', 'translate(0, ' + chartHeight / 2 + ')');
barcodeGroup.append('rect')
.attr('width', chartWidth)
.attr('height', chartHeight)
.style('fill', '#eee');
barcodeGroup.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('width', barWidth)
.attr('height', chartHeight)
.style('fill', barColor)
.attr('x', function (d, i) {
return Math.floor(chartScale(d.percent));
}).on('mouseenter', function (d, i) {
var highlightBar = barcodeGroup.append('rect')
.attr('width', highlightBarWidth)
.attr('height', highlightBarHeight)
.attr('transform', 'translate(0, ' + '-' + (highlightBarHeight - chartHeight) / 2 + ')')
.style('fill', highlightBarColor)
.attr('x', function () {
return Math.floor(chartScale(d.percent));
});
setTimeout(function() { highlightBar.remove(); }, 700);
});
}
</script>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment