Skip to content

Instantly share code, notes, and snippets.

@popovichN
Last active February 21, 2017 22:52
Show Gist options
  • Select an option

  • Save popovichN/487174b855b156856dfe9cf454d7bd78 to your computer and use it in GitHub Desktop.

Select an option

Save popovichN/487174b855b156856dfe9cf454d7bd78 to your computer and use it in GitHub Desktop.
Bar chart for Svelte Repl
<!-- responsive bar chart -->
<div class='chart'>
<h2>US birthrate by year</h2>
<svg ref:svg>
<g transform='translate(0,0)'>
<g class='axes'>
<!-- y axis -->
<g class='axis y-axis' transform='translate(0, {{ padding.top }} )'>
{{#each yTicks as tick}}
<g class='tick tick-{{ tick }}'
transform='translate( 0, {{ yScale(tick) - padding.bottom }} )'>
<line stroke='#000'
x2='100%' y1='0' y2='0'></line>
<text fill='#000' x='0' y='0' dy='-2'>{{ tick !== 0 ? tick : ''}} {{tick === 20 ? ' per 1,000 population' : ''}}</text>
</g>
{{/each}}
</g>
<!-- x axis -->
<g class='axis x-axis'>
{{#each myData as datum, t}}
<g class='tick tick-{{ tick }}'
transform='translate( {{ (t * barWidth) + padding.left + barWidth/2 }}, {{ height }} )'>
{{#if width > 380}}
<text fill='#000'
x='0' y='0' dy='-2'>{{ datum.year }}</text>
{{else}}
<text fill='#000'
x='0' y='0' dy='-2'>{{ formatMobile(datum.year) }}</text>
{{/if}}
</g>
{{/each}}
</g>
</g>
<g class='bars'>
{{#each myData as datum, i}}
<rect x='{{ (i * barWidth) + padding.left }}'
y='{{ yScale(datum.birthrate) }}'
width='{{ barWidth }}'
height='{{ height - padding.bottom - yScale(datum.birthrate) }}'>
</rect>
{{/each}}
</g>
</g>
</svg>
</div>
<style>
.chart {
width: 100%;
max-width: 500px;
margin: 0 auto;
}
svg {
position: relative;
width: 100%;
height: 100%;
}
.tick,
p.source {
font-family: Helvetica, Arial;
font-size: .725em;
font-weight: 200;
}
.tick line {
stroke: #e2e2e2;
stroke-dasharray: 2;
}
.tick text {
fill: #ccc;
text-anchor: start;
}
.tick.tick-0 line {
stroke-dasharray: 0;
}
.x-axis .tick text {
text-anchor: middle;
}
.bars rect {
fill: #a11;
stroke: white;
stroke-width: 2;
opacity: 0.65;
}
</style>
<script>
import { scaleLinear } from 'd3-scale';
export default {
data () {
return {
padding: {
top: 20,
right: 15,
bottom: 20,
left: 25
},
height: 200,
width: 500,
yTicks: [0, 5, 10, 15, 20 ],
xTicks: [1990, 1995, 2000, 2005, 2010, 2015],
formatMobile ( tick ) {
return '\'' + tick.toString().slice(-2);
}
};
},
computed: {
barWidth: function (xScale, myData, width, padding) {
var baseBarWidth = (xScale(myData[1].year) - xScale(myData[0].year));
return baseBarWidth - (baseBarWidth/myData.length);
},
yScale: function ( padding, height, yTicks ) {
return scaleLinear()
.domain([Math.min.apply(null, yTicks), Math.max.apply(null, yTicks)])
.range([height - padding.bottom, padding.top]);
},
xScale: function ( padding, width, xTicks ) {
return scaleLinear()
.domain([Math.min.apply(null, xTicks), Math.max.apply(null, xTicks)])
.range([padding.left, width - padding.right]);
}
},
onrender () {
this.container = this.refs.svg;
var self = this;
window.addEventListener( 'resize', function () {
self.resize();
});
this.resize();
},
methods: {
resize () {
this.set({
width: this.container.getBoundingClientRect().width,
height: this.container.getBoundingClientRect().height
});
}
}
};
</script>
{
"myData": [
{
"year": 1990,
"birthrate": 16.7
},
{
"year": 1995,
"birthrate": 14.6
},
{
"year": 2000,
"birthrate": 14.4
},
{
"year": 2005,
"birthrate": 14
},
{
"year": 2010,
"birthrate": 13
},
{
"year": 2015,
"birthrate": 12.4
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment