partial fix for bar chart example. bind:clientWidth doesnt work

pull/1890/head
Rich Harris 7 years ago
parent e8e42a7c95
commit a856fd61b4

@ -1,125 +1,112 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
export let svg;
import { scaleLinear } from 'd3-scale'; import { scaleLinear } from 'd3-scale';
var xScale = scaleLinear();
var yScale = scaleLinear();
export let xTicks = [1990, 1995, 2000, 2005, 2010, 2015];
export let width = 500;
export let padding = { top: 20, right: 15, bottom: 20, left: 25 };
export let height = 200;
export let yTicks = [0, 5, 10, 15, 20];
export let points; export let points;
export let tick;
const xTicks = [1990, 1995, 2000, 2005, 2010, 2015];
const yTicks = [0, 5, 10, 15, 20];
const padding = { top: 20, right: 15, bottom: 20, left: 25 };
let width = 500;
let height = 200;
let barWidth;
let xScale;
let yScale;
function formatMobile(tick) { function formatMobile(tick) {
return "'" + tick % 100; return "'" + tick % 100;
} }
function barWidth() { $: xScale = scaleLinear()
var innerWidth = width - (padding.left + padding.right);
return innerWidth / xTicks.length;
}
function xScale() {
return xScale()
.domain([0, xTicks.length]) .domain([0, xTicks.length])
.range([padding.left, width - padding.right]); .range([padding.left, width - padding.right]);
}
function yScale() { $: yScale = scaleLinear()
return yScale()
.domain([0, Math.max.apply(null, yTicks)]) .domain([0, Math.max.apply(null, yTicks)])
.range([height - padding.bottom, padding.top]); .range([height - padding.bottom, padding.top]);
}
onMount(() => {
resize();
});
// [svelte-upgrade suggestion] $: {
// review these functions and remove unnecessary 'export' keywords const innerWidth = width - (padding.left + padding.right);
export function resize() { barWidth = innerWidth / xTicks.length;
var bcr = svg.getBoundingClientRect();
width = bcr.right - bcr.left, height = bcr.bottom - bcr.top;
} }
</script> </script>
<svelte:window on:resize='{resize}'/>
<div class='chart'>
<h2>US birthrate by year</h2>
<svg ref:svg>
<!-- 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 x2='100%'></line>
<text y='-4'>{tick} {tick === 20 ? ' per 1,000 population' : ''}</text>
</g>
{/each}
</g>
<!-- x axis -->
<g class='axis x-axis'>
{#each points as point, i}
<g class='tick tick-{tick}' transform='translate({xScale()(i)},{height})'>
<text x='{barWidth()/2}' y='-4'>{width > 380 ? point.year : formatMobile(point.year)}</text>
</g>
{/each}
</g>
<g class='bars'>
{#each points as point, i}
<rect
x='{xScale()(i) + 2}'
y='{yScale()(point.birthrate)}'
width='{ barWidth() - 4 }'
height='{ height - padding.bottom - yScale()(point.birthrate) }'
></rect>
{/each}
</g>
</svg>
</div>
<style> <style>
.chart { .chart {
width: 100%; width: 100%;
max-width: 500px; max-width: 500px;
margin: 0 auto; margin: 0 auto;
} }
svg { svg {
position: relative; position: relative;
width: 100%; width: 100%;
height: 200px; height: 200px;
} }
.tick { .tick {
font-family: Helvetica, Arial; font-family: Helvetica, Arial;
font-size: .725em; font-size: .725em;
font-weight: 200; font-weight: 200;
} }
.tick line { .tick line {
stroke: #e2e2e2; stroke: #e2e2e2;
stroke-dasharray: 2; stroke-dasharray: 2;
} }
.tick text { .tick text {
fill: #ccc; fill: #ccc;
text-anchor: start; text-anchor: start;
} }
.tick.tick-0 line { .tick.tick-0 line {
stroke-dasharray: 0; stroke-dasharray: 0;
} }
.x-axis .tick text { .x-axis .tick text {
text-anchor: middle; text-anchor: middle;
} }
.bars rect { .bars rect {
fill: #a11; fill: #a11;
stroke: none; stroke: none;
opacity: 0.65; opacity: 0.65;
} }
</style> </style>
<div class="chart">
<h2>US birthrate by year {width}/{height}</h2>
<svg bind:clientWidth={width} bind:clientHeight={height}>
<!-- 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 x2="100%"></line>
<text y="-4">{tick} {tick === 20 ? ' per 1,000 population' : ''}</text>
</g>
{/each}
</g>
<!-- x axis -->
<g class="axis x-axis">
{#each points as point, i}
<g class="tick" transform="translate({xScale(i)},{height})">
<text x="{barWidth/2}" y="-4">{width > 380 ? point.year : formatMobile(point.year)}</text>
</g>
{/each}
</g>
<g class='bars'>
{#each points as point, i}
<rect
x="{xScale(i) + 2}"
y="{yScale(point.birthrate)}"
width="{barWidth - 4}"
height="{height - padding.bottom - yScale(point.birthrate)}"
></rect>
{/each}
</g>
</svg>
</div>
Loading…
Cancel
Save