budibase/packages/standard-components/src/DataChart.svelte

48 lines
1.1 KiB
Svelte
Raw Normal View History

2020-05-07 15:59:06 +02:00
<script>
2020-05-18 12:01:09 +02:00
import { onMount } from "svelte"
import FusionCharts from "fusioncharts"
import Charts from "fusioncharts/fusioncharts.charts"
import FusionTheme from "fusioncharts/themes/fusioncharts.theme.fusion"
import SvelteFC, { fcRoot } from "svelte-fusioncharts"
2020-05-07 23:15:09 +02:00
2020-05-18 12:01:09 +02:00
fcRoot(FusionCharts, Charts, FusionTheme)
2020-05-07 15:59:06 +02:00
export let _bb
export let model
2020-05-07 23:15:09 +02:00
export let type = "column2d"
2020-05-07 15:59:06 +02:00
2020-05-07 23:15:09 +02:00
let store = _bb.store
$: chartConfigs = {
type,
2020-05-18 12:01:09 +02:00
width: "600",
height: "400",
dataFormat: "json",
2020-05-07 23:15:09 +02:00
dataSource: {
data: $store[model] || [],
2020-05-18 12:01:09 +02:00
},
2020-05-07 23:15:09 +02:00
}
2020-05-07 15:59:06 +02:00
async function fetchData() {
2020-06-18 17:59:31 +02:00
const FETCH_RECORDS_URL = `/api/views/all_${model}`
2020-05-07 15:59:06 +02:00
const response = await _bb.api.get(FETCH_RECORDS_URL)
if (response.status === 200) {
const json = await response.json()
2020-05-07 23:15:09 +02:00
store.update(state => {
state[model] = json
2020-05-07 23:15:09 +02:00
return state
})
2020-05-07 15:59:06 +02:00
} else {
throw new Error("Failed to fetch records.", response)
}
}
onMount(async () => {
await fetchData()
})
2020-05-07 23:15:09 +02:00
</script>
2020-05-07 15:59:06 +02:00
2020-05-07 23:15:09 +02:00
<div id="container">
<SvelteFC {...chartConfigs} />
2020-05-18 12:01:09 +02:00
</div>