2024-09-19 11:00:37 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { ref } from "vue";
|
|
|
|
|
import { invoke } from "@tauri-apps/api/tauri";
|
2024-09-19 13:10:46 +00:00
|
|
|
import { Line } from 'vue-chartjs';
|
|
|
|
|
import { Chart as ChartJS, Title, Tooltip, Legend, LineElement, CategoryScale, LinearScale, PointElement } from 'chart.js';
|
2024-09-19 11:00:37 +00:00
|
|
|
|
2024-09-19 13:10:46 +00:00
|
|
|
ChartJS.register(Title, Tooltip, Legend, LineElement, CategoryScale, LinearScale, PointElement);
|
2024-09-19 11:00:37 +00:00
|
|
|
|
|
|
|
|
export default {
|
2024-09-19 13:10:46 +00:00
|
|
|
name: 'LineChart',
|
|
|
|
|
components: { Line },
|
2024-09-19 11:00:37 +00:00
|
|
|
setup() {
|
|
|
|
|
const chartData = ref({
|
2024-09-19 13:10:46 +00:00
|
|
|
datasets: [{ // Ensure datasets is an array
|
|
|
|
|
label: 'My Dataset', // Add a label for the dataset
|
|
|
|
|
data: [], // Initial data
|
|
|
|
|
fill: false, // Set to true if you want the area under the line to be filled
|
|
|
|
|
borderColor: 'blue', // Set the line color
|
|
|
|
|
tension: 0.1 // Adjust the tension for smoothness
|
|
|
|
|
}]
|
2024-09-19 11:00:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const chartOptions = {
|
|
|
|
|
responsive: true,
|
|
|
|
|
scales: {
|
|
|
|
|
y: {
|
|
|
|
|
min: 0,
|
|
|
|
|
max: 100
|
2024-09-19 13:10:46 +00:00
|
|
|
},
|
|
|
|
|
x: {
|
|
|
|
|
ticks: {
|
|
|
|
|
callback: (value, index, values) => {
|
|
|
|
|
// Show only 5 timestamps on the x-axis
|
|
|
|
|
const step = Math.floor(values.length / 5);
|
|
|
|
|
return index % step === 0 ? chartData.value.labels[index] : ''; // Show the label only for selected indices
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-19 11:00:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateData = async () => {
|
|
|
|
|
const my_data = await invoke("random_data");
|
|
|
|
|
|
2024-09-19 13:10:46 +00:00
|
|
|
const values = my_data.map((point: { value: number }) => point.value);
|
|
|
|
|
const timestamps = my_data.map((point: { timestamp: string }) => point.timestamp);
|
|
|
|
|
|
2024-09-19 11:00:37 +00:00
|
|
|
chartData.value = {
|
2024-09-19 13:10:46 +00:00
|
|
|
labels: timestamps,
|
|
|
|
|
datasets: [{
|
|
|
|
|
label: 'My Dataset', // Add a label for the dataset
|
|
|
|
|
data: values, // Access the data property
|
|
|
|
|
fill: false, // Set to true if you want the area under the line to be filled
|
|
|
|
|
borderColor: 'green', // Set the line color
|
|
|
|
|
tension: 0.1 // Adjust the tension for smoothness
|
|
|
|
|
}]
|
2024-09-19 11:00:37 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
chartData,
|
|
|
|
|
chartOptions,
|
|
|
|
|
updateData
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2024-09-19 13:10:46 +00:00
|
|
|
<Line id="my-chart-id" :options="chartOptions" :data="chartData" />
|
2024-09-19 11:00:37 +00:00
|
|
|
<button @click="updateData">Click me</button>
|
|
|
|
|
</template>
|
|
|
|
|
|