mirror of https://github.com/sveltejs/svelte
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;
|
|
||||||
|
|
||||||
function formatMobile(tick) {
|
const xTicks = [1990, 1995, 2000, 2005, 2010, 2015];
|
||||||
return "'" + tick % 100;
|
const yTicks = [0, 5, 10, 15, 20];
|
||||||
}
|
const padding = { top: 20, right: 15, bottom: 20, left: 25 };
|
||||||
|
|
||||||
function barWidth() {
|
let width = 500;
|
||||||
var innerWidth = width - (padding.left + padding.right);
|
let height = 200;
|
||||||
return innerWidth / xTicks.length;
|
let barWidth;
|
||||||
}
|
let xScale;
|
||||||
|
let yScale;
|
||||||
|
|
||||||
function xScale() {
|
function formatMobile(tick) {
|
||||||
return xScale()
|
return "'" + tick % 100;
|
||||||
.domain([0, xTicks.length])
|
|
||||||
.range([padding.left, width - padding.right]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function yScale() {
|
|
||||||
return yScale()
|
|
||||||
.domain([0, Math.max.apply(null, yTicks)])
|
|
||||||
.range([height - padding.bottom, padding.top]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
$: xScale = scaleLinear()
|
||||||
resize();
|
.domain([0, xTicks.length])
|
||||||
});
|
.range([padding.left, width - padding.right]);
|
||||||
|
|
||||||
// [svelte-upgrade suggestion]
|
$: yScale = scaleLinear()
|
||||||
// review these functions and remove unnecessary 'export' keywords
|
.domain([0, Math.max.apply(null, yTicks)])
|
||||||
export function resize() {
|
.range([height - padding.bottom, padding.top]);
|
||||||
var bcr = svg.getBoundingClientRect();
|
|
||||||
|
|
||||||
width = bcr.right - bcr.left, height = bcr.bottom - bcr.top;
|
$: {
|
||||||
|
const innerWidth = width - (padding.left + padding.right);
|
||||||
|
barWidth = innerWidth / xTicks.length;
|
||||||
}
|
}
|
||||||
</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…
Reference in new issue