Skip to content

Commit

Permalink
move all data to API
Browse files Browse the repository at this point in the history
  • Loading branch information
kjj6198 committed Nov 21, 2020
1 parent 0bb0468 commit 2247d96
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 4,413 deletions.
28 changes: 28 additions & 0 deletions src/API.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
import { onMount, createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
let loaded = false;
let data;
export let api;
onMount(() => {
api()
.then((res) => res.json())
.then((d) => {
loaded = true;
data = d;
});
});
$: {
if (data) {
dispatch("loaded", {
data: data,
});
}
}
</script>

{#if loaded}
<slot />
{:else}<span>loading</span>{/if}
11 changes: 8 additions & 3 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script>
import BackgroundImage from "./BackgroundImage.svelte";
import ByPlaceChart from "./ByPlaceChart.svelte";
import GenderChart from "./GenderChart.svelte";
import RelationshipChart from "./RelationshipChart.svelte";
import Section from "./Section.svelte";
import SexualCaseMap from "./SexualCaseMap.svelte";
Expand Down Expand Up @@ -35,6 +36,13 @@
</BackgroundImage>
</div>

<Section
title="性侵害案件統計"
description="台灣歷年性侵害案件統計與男女比例"
num={0}>
<GenderChart />
</Section>

<Section
title="性侵害發生地點"
description="台灣有多少性侵害案件,其中又有哪些驚人的數據?我們一起來看看統計資料!"
Expand All @@ -61,8 +69,5 @@
description="將性侵害案件用縣市作劃分,觀察性侵害案件的多寡。點擊地圖中的縣市區域查看詳細的性侵害案件統計。顏色越深代表案件數越多。"
num={3}>
<SexualCaseMap />
<p>
從圖表上可發現,性侵害案件大多集中在直轄市,包含新北市、台中市、台南市、高雄市,其中以新北市居第一,下圖的統計圖表可查看更多。
</p>
</Section>
</main>
4 changes: 3 additions & 1 deletion src/BarChart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { stack, stackOrderDescending } from "d3-shape";
import colors from "./utils/colors";
import { createEventDispatcher } from "svelte";
import formatNumber from "./utils/formatNumber";
export let selected = [];
export let data = [];
export let xTicks;
Expand Down Expand Up @@ -75,6 +76,7 @@
<style>
text {
font-size: 12px;
font-family: Oswald;
}
line {
Expand Down Expand Up @@ -125,7 +127,7 @@
<text
x={xScale(+d['總計']) + padding.left + 10}
y={yScale(d['年齡層']) + 4}>
{d['總計']}
{formatNumber(d['總計'])}
</text>
{/each}
</g>
Expand Down
43 changes: 26 additions & 17 deletions src/ByPlaceChart.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<script>
import omit from "lodash/omit";
import place from "./place";
import PlaceChart from "./PlaceChart.svelte";
import YearSwitch from "./YearSwitch.svelte";
import RankingTable from "./RankingTable.svelte";
import chartConfig from "./store/chartConfig";
import API from "./API.svelte";
let place = [];
let currentYear = 2018;
$: data = place.find((p) => p["年份"] === currentYear.toString());
let data;
let ranking = [];
$: data = place.find((p) => p["年份"] === currentYear.toString());
$: {
const obj = {};
Object.keys(omit(data, ["年份"])).forEach((k) => {
Expand All @@ -37,17 +38,25 @@
];
</script>

<YearSwitch
initialYear={currentYear}
years={place.map((d) => +d['年份'])}
on:change={(e) => (currentYear = e.detail.year)} />
<PlaceChart
fillColor="var(--main)"
barHeight={10}
yTicks={Object.keys(omit(place[0], ['年份']))}
xTicks={$chartConfig.placeChart.xTicks}
{data} />
<RankingTable
caption={currentYear}
headers={config}
data={ranking.slice(0, 10)} />
<API
on:loaded={(e) => {
place = e.detail.data;
}}
api={() => fetch('https://vdata.kalan.dev/api/v1/data/sexual_assault/place')}>
{#if place.length > 0 && data}
<YearSwitch
initialYear={currentYear}
years={place.map((d) => +d['年份'])}
on:change={(e) => (currentYear = e.detail.year)} />
<PlaceChart
fillColor="var(--main)"
barHeight={10}
yTicks={Object.keys(omit(place[0], ['年份']))}
xTicks={$chartConfig.placeChart.xTicks}
{data} />
<RankingTable
caption={currentYear}
headers={config}
data={ranking.slice(0, 10)} />
{/if}
</API>
113 changes: 113 additions & 0 deletions src/GenderChart.svelte
Original file line number Diff line number Diff line change
@@ -1,2 +1,115 @@
<script>
import { fade } from "svelte/transition";
import YearSwitch from "./YearSwitch.svelte";
import { tweened } from "svelte/motion";
export let probability = 0.5;
export let total = 100;
let currentYear = 2019;
let data;
let loading = true;
const res = fetch(
"https://vdata.kalan.dev/api/v1/data/sexual_assault/case_year"
)
.then((res) => res.json())
.then((d) => {
loading = false;
data = d;
});
$: totalAmount = Array.from({ length: total }).fill(0);
$: currentData = data
? data.find((data) => data.year === currentYear.toString())
: null;
let femaleCount = tweened(0);
let maleCount = tweened(0);
$: {
if (currentData) {
femaleCount.set(currentData.female);
maleCount.set(currentData.male);
}
}
</script>

<style>
.container {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(2, 1fr);
}
.inner {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(10, 40px);
grid-template-rows: repeat(10, 65px);
}
.title {
color: var(--main);
grid-column: 1 / 11;
}
@media screen and (max-width: 960px) {
.container {
display: block;
}
.inner {
grid-template-columns: repeat(5, 1fr);
grid-template-rows: initial;
}
.title {
grid-column: span 5;
}
}
</style>

<YearSwitch
initialYear={currentYear}
years={[2015, 2016, 2017, 2018, 2019]}
on:change={(e) => (currentYear = e.detail.year)}>
<div class="wrapper" slot="chart">
{#if loading}
<span>loading</span>
{:else if data}
<div class="container">
<div class="inner">
<h2 class="title">女({Math.floor($femaleCount)} 人)</h2>
<p class="title">
在受害者當中,每 100 人中有
{Math.floor(currentData.probability * 100)}
人為女性
</p>
{#each Array.from({
length: currentData.probability * 100,
}) as item, i (i)}
<img
alt="女性"
aria-hidden="true"
transition:fade
src="icons/female.svg" />
{/each}
</div>
<div class="inner">
<h2 class="title">男({Math.floor($maleCount)} 人)</h2>
<p class="title">
在受害者中,每 100 人中有
{Math.floor((1 - currentData.probability) * 100)}
人為男性
</p>
{#each Array.from({
length: (1 - currentData.probability) * 100,
}) as item, i (i)}
<img
alt="男性"
aria-hidden="true"
transition:fade
src="icons/male.svg" />
{/each}
</div>
</div>
{/if}
</div>
</YearSwitch>
Loading

0 comments on commit 2247d96

Please sign in to comment.