add chart

This commit is contained in:
Muhammad Izza Alfiansyah 2024-04-10 18:57:55 +07:00
parent f3b7b14788
commit 0a1ec45f2b
2 changed files with 82 additions and 2 deletions

View File

@ -10,7 +10,9 @@ export default function (props: TableProps) {
return ( return (
<div class="overflow-x-auto"> <div class="overflow-x-auto">
<table class={"border-collapse w-full " + otherProps.class}> <table
class={"border-collapse w-full whitespace-nowrap " + otherProps.class}
>
<thead> <thead>
<tr> <tr>
<For each={props.headers}> <For each={props.headers}>

View File

@ -1,7 +1,85 @@
import { createSignal, onMount } from "solid-js";
import BeakerIcon from "../icons/BeakerIcon"; import BeakerIcon from "../icons/BeakerIcon";
import EyeDropperIcon from "../icons/EyeDropperIcon"; import EyeDropperIcon from "../icons/EyeDropperIcon";
import { Chart, registerables } from "chart.js";
export default function () { export default function () {
let canvas: any;
const [chart, setChart] = createSignal<Chart | null>(null);
const renderChart = () => {
const date = new Date();
const his =
date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
const val = Math.round(Math.random() * 30);
let labels = [his];
let data = [val];
while (labels.length < 10) {
labels.push("-");
}
while (data.length < 10) {
data.push(0);
}
if (!chart()) {
const myChart = new Chart(canvas, {
type: "line",
data: {
labels: labels.reverse(),
datasets: [
{
label: "Kadar Gas",
data: data.reverse(),
borderColor: "#4784f5",
fill: true,
cubicInterpolationMode: "monotone",
tension: 0.4,
},
],
},
options: {
responsive: true,
scales: {
x: {
display: true,
title: {
display: true,
},
},
y: {
display: true,
title: {
display: true,
text: "Nilai",
},
suggestedMin: 0,
suggestedMax: 50,
},
},
},
});
setChart(myChart);
} else {
chart()?.data.labels?.splice(0, 1);
chart()?.data.labels?.push(his);
chart()?.data.datasets[0].data.splice(0, 1);
chart()?.data.datasets[0].data.push(val);
chart()?.update();
}
setTimeout(renderChart, 1000 * 60);
};
onMount(() => {
Chart.register(...registerables);
renderChart();
});
return ( return (
<div class="space-y-5"> <div class="space-y-5">
<div class="flex flex-wrap gap-5"> <div class="flex flex-wrap gap-5">
@ -21,7 +99,7 @@ export default function () {
</div> </div>
<div class="bg-white rounded shadow p-5"> <div class="bg-white rounded shadow p-5">
<div class="text-xl">Grafik Kadar Gas</div> <div class="text-xl">Grafik Kadar Gas</div>
<div class="h-[300px]"></div> <canvas ref={canvas}></canvas>
</div> </div>
</div> </div>
); );