1
0
Fork 0
mirror of synced 2024-09-18 02:08:34 +12:00
budibase/packages/standard-components/src/DataChart.svelte

50 lines
1.1 KiB
Svelte
Raw Normal View History

2020-05-08 01:59:06 +12:00
<script>
2020-05-18 22:01:09 +12: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-08 09:15:09 +12:00
2020-05-18 22:01:09 +12:00
fcRoot(FusionCharts, Charts, FusionTheme)
2020-05-08 01:59:06 +12:00
export let _bb
export let model
2020-05-08 09:15:09 +12:00
export let type = "column2d"
2020-05-08 01:59:06 +12:00
2020-05-08 09:15:09 +12:00
let store = _bb.store
$: chartConfigs = {
type,
2020-05-18 22:01:09 +12:00
width: "600",
height: "400",
dataFormat: "json",
2020-05-08 09:15:09 +12:00
dataSource: {
data: $store[model] || [],
2020-05-18 22:01:09 +12:00
},
2020-05-08 09:15:09 +12:00
}
2020-05-08 01:59:06 +12:00
2020-08-04 01:42:37 +12:00
$: console.log("CHART CONFIGS", chartConfigs)
2020-05-08 01:59:06 +12:00
async function fetchData() {
2020-06-19 03:59:31 +12:00
const FETCH_RECORDS_URL = `/api/views/all_${model}`
2020-05-08 01:59:06 +12:00
const response = await _bb.api.get(FETCH_RECORDS_URL)
if (response.status === 200) {
const json = await response.json()
2020-05-08 09:15:09 +12:00
store.update(state => {
state[model] = json
2020-05-08 09:15:09 +12:00
return state
})
2020-05-08 01:59:06 +12:00
} else {
throw new Error("Failed to fetch records.", response)
}
}
onMount(async () => {
await fetchData()
})
2020-05-08 09:15:09 +12:00
</script>
2020-05-08 01:59:06 +12:00
2020-05-08 09:15:09 +12:00
<div id="container">
<SvelteFC {...chartConfigs} />
2020-05-18 22:01:09 +12:00
</div>