-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbase.svelte
93 lines (82 loc) · 2.08 KB
/
base.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<script>
import { onMount, onDestroy } from 'svelte';
import { Chart } from 'frappe-charts';
/**
* PROPS
*/
export let data = {
labels: [],
datasets: [
{ values: [] }
],
yMarkers: {},
yRegions: [],
};
export let title = '';
export let type = 'line';
export let height = 300;
export let animate = true;
export let axisOptions = {};
export let barOptions = {};
export let lineOptions = {};
export let tooltipOptions = {};
export let colors = [];
export let valuesOverPoints = 0;
export let isNavigable = false;
export let maxSlices = 3;
/**
* COMPONENT
*/
// The Chart returned from frappe
let chart = null;
// DOM node for frappe to latch onto
let chartRef;
// Helper HOF for calling a fn only if chart exists
function ifChartThen(fn) {
return function ifChart(...args) {
if (chart) {
return fn(...args);
}
}
}
/**
* Methods for updating / exporting the chart
*/
// Allow the consumer to add a data point
export const addDataPoint = ifChartThen((label, valueFromEachDataset, index) => chart.addDataPoint(label, valueFromEachDataset, index));
// Allow the consumer to remove a data point
export const removeDataPoint = ifChartThen(index => chart.removeDataPoint(index));
// Allow the consumer to export the chart
export const exportChart = ifChartThen(() => chart.export());
// Update the chart when incoming data changes
const updateChart = ifChartThen((newData) => chart.update(newData));
$: updateChart(data);
/**
* Handle initializing the chart when this Svelte component mounts
*/
onMount(() => {
chart = new Chart(chartRef, {
data,
title,
type,
height,
animate,
colors,
axisOptions,
barOptions,
lineOptions,
tooltipOptions,
valuesOverPoints,
isNavigable,
maxSlices,
});
});
// Mark Chart references for garbage collection when component is unmounted
onDestroy(() => {
chart = null;
});
</script>
<div
bind:this={chartRef}
on:data-select
></div>