Files
fnx_web/svelte/src/util/Chart.svelte

128 lines
2.3 KiB
Svelte
Raw Normal View History

2021-05-25 22:15:29 +02:00
<script>
import { onMount } from "svelte";
import { formatDataVolume, formatNumber } from "./Formatting.svelte";
2022-03-30 19:35:26 +02:00
import { color_by_name } from "./Util.svelte";
2021-12-16 21:42:09 +01:00
import {
Chart,
PointElement,
LineElement,
LinearScale,
CategoryScale,
LineController,
Filler,
Tooltip,
Legend,
} from "chart.js"
Chart.register(
PointElement,
LineElement,
LinearScale,
CategoryScale,
LineController,
Filler,
Tooltip,
Legend,
)
2021-05-25 22:15:29 +02:00
let chart_element
let chart_object
2021-12-16 21:20:05 +01:00
export let data_type = ""
2021-12-16 21:42:09 +01:00
export let legend = true
2021-05-25 22:15:29 +02:00
export const chart = () => {
return chart_object
}
2021-12-16 21:20:05 +01:00
export const data = () => {
return chart_object.data
}
2021-05-25 22:15:29 +02:00
export const update = () => {
return chart_object.update()
}
2022-03-30 19:35:26 +02:00
Chart.defaults.color = color_by_name("body_text_color");
2021-12-14 20:03:37 +01:00
Chart.defaults.font.size = 15;
Chart.defaults.font.family = "system-ui, sans-serif";
Chart.defaults.maintainAspectRatio = false;
Chart.defaults.plugins.tooltip.mode = "index";
Chart.defaults.plugins.tooltip.axis = "x";
Chart.defaults.plugins.tooltip.intersect = false;
Chart.defaults.animation.duration = 500;
Chart.defaults.animation.easing = "linear";
2021-05-25 22:15:29 +02:00
onMount(() => {
chart_object = new Chart(
chart_element.getContext("2d"),
{
type: 'line',
data: {
2021-12-16 21:20:05 +01:00
labels: [],
datasets: [],
2021-05-25 22:15:29 +02:00
},
options: {
2021-12-16 21:42:09 +01:00
plugins: {
legend: {
display: legend,
labels: {
boxWidth: 12,
boxHeight: 12,
}
},
},
2021-12-14 20:03:37 +01:00
layout: {
padding: {
left: 4,
right: 10,
}
},
2021-05-25 22:15:29 +02:00
scales: {
2021-12-14 20:03:37 +01:00
y: {
type: "linear",
display: true,
position: "left",
ticks: {
callback: function (value, index, values) {
2021-12-16 21:20:05 +01:00
if (data_type == "bytes") {
2021-12-14 20:03:37 +01:00
return formatDataVolume(value, 3);
}
return formatNumber(value, 3);
2021-05-25 22:15:29 +02:00
},
2021-12-14 20:03:37 +01:00
},
beginAtZero: true,
grid: {
display: true,
drawBorder: false,
2022-03-30 19:35:26 +02:00
color: color_by_name("separator"),
2021-12-14 20:03:37 +01:00
},
},
x: {
display: true,
ticks: {
sampleSize: 1,
padding: 4,
minRotation: 0,
maxRotation: 0
},
grid: {
display: false,
drawBorder: false,
2021-05-25 22:15:29 +02:00
}
2021-12-14 20:03:37 +01:00
}
2021-05-25 22:15:29 +02:00
}
}
}
);
})
</script>
<div class="chart-container">
<canvas bind:this={chart_element}></canvas>
</div>
<style>
.chart-container {
position: relative;
width: 100%;
2022-10-25 15:59:20 +02:00
height: 300px;
2021-05-25 22:15:29 +02:00
}
</style>