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

49 lines
1.1 KiB
Svelte
Raw Normal View History

2020-05-07 15:59:06 +02:00
<script>
2020-05-07 23:15: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';
fcRoot(FusionCharts, Charts, FusionTheme);
2020-05-07 15:59:06 +02:00
export let _bb
export let _instanceId
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,
width: '600',
height: '400',
dataFormat: 'json',
dataSource: {
data: $store[model._id] || []
}
}
2020-05-07 15:59:06 +02:00
async function fetchData() {
2020-05-07 23:15:09 +02:00
const FETCH_RECORDS_URL = `/api/${_instanceId}/all_${model._id}/records`
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._id] = json
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} />
</div>