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

99 lines
2.1 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";
2021-12-14 20:03:37 +01:00
import { Chart, PointElement, LineElement, LinearScale, CategoryScale, LineController, Filler, Tooltip } from "chart.js"
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-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()
}
2021-12-14 20:03:37 +01:00
Chart.register(PointElement, LineElement, LinearScale, CategoryScale, LineController, Filler, Tooltip)
Chart.defaults.color = "#"+window.style.textColor;
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: {
legend: { display: false },
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,
color: "#"+window.style.layer3Color,
},
},
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%;
height: 200px;
}
</style>