Make proper line chart with random data
This commit is contained in:
parent
80ae1831e8
commit
10fa939558
3
src-tauri/Cargo.lock
generated
3
src-tauri/Cargo.lock
generated
@ -306,8 +306,10 @@ checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"android-tzdata",
|
"android-tzdata",
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
"wasm-bindgen",
|
||||||
"windows-targets 0.52.6",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -2731,6 +2733,7 @@ dependencies = [
|
|||||||
name = "tauri-eval"
|
name = "tauri-eval"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|||||||
@ -15,6 +15,7 @@ tauri = { version = "1", features = ["shell-open"] }
|
|||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
chrono = "0.4.38"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
|
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
|
use chrono::prelude::*;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
@ -11,20 +12,25 @@ fn greet(name: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Dataset {
|
struct DataPoint {
|
||||||
data: [u8; 3],
|
value: f32,
|
||||||
|
timestamp: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
fn random_data() -> Dataset {
|
fn random_data() -> Vec<DataPoint> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
Dataset {
|
|
||||||
data: [
|
let mut data: Vec<DataPoint> = Vec::with_capacity(100);
|
||||||
rng.gen_range(0..100),
|
|
||||||
rng.gen_range(0..100),
|
for _ in 0..100 {
|
||||||
rng.gen_range(0..100),
|
data.push(DataPoint {
|
||||||
],
|
value: rng.gen_range(0..100) as f32,
|
||||||
|
timestamp: Utc::now().format("%H:%M:%S").to_string(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -1,18 +1,23 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
import { invoke } from "@tauri-apps/api/tauri";
|
import { invoke } from "@tauri-apps/api/tauri";
|
||||||
import { Bar } from 'vue-chartjs';
|
import { Line } from 'vue-chartjs';
|
||||||
import { Chart as ChartJS, Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale } from 'chart.js';
|
import { Chart as ChartJS, Title, Tooltip, Legend, LineElement, CategoryScale, LinearScale, PointElement } from 'chart.js';
|
||||||
|
|
||||||
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale);
|
ChartJS.register(Title, Tooltip, Legend, LineElement, CategoryScale, LinearScale, PointElement);
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'BarChart',
|
name: 'LineChart',
|
||||||
components: { Bar },
|
components: { Line },
|
||||||
setup() {
|
setup() {
|
||||||
const chartData = ref({
|
const chartData = ref({
|
||||||
labels: ['January', 'February', 'March'],
|
datasets: [{ // Ensure datasets is an array
|
||||||
datasets: [{ data: [40, 20, 12] }]
|
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
|
||||||
|
}]
|
||||||
});
|
});
|
||||||
|
|
||||||
const chartOptions = {
|
const chartOptions = {
|
||||||
@ -21,6 +26,15 @@ export default {
|
|||||||
y: {
|
y: {
|
||||||
min: 0,
|
min: 0,
|
||||||
max: 100
|
max: 100
|
||||||
|
},
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -28,9 +42,18 @@ export default {
|
|||||||
const updateData = async () => {
|
const updateData = async () => {
|
||||||
const my_data = await invoke("random_data");
|
const my_data = await invoke("random_data");
|
||||||
|
|
||||||
|
const values = my_data.map((point: { value: number }) => point.value);
|
||||||
|
const timestamps = my_data.map((point: { timestamp: string }) => point.timestamp);
|
||||||
|
|
||||||
chartData.value = {
|
chartData.value = {
|
||||||
labels: ['January', 'February', 'March'],
|
labels: timestamps,
|
||||||
datasets: [{ data: my_data.data }]
|
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
|
||||||
|
}]
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -44,7 +67,7 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Bar id="my-chart-id" :options="chartOptions" :data="chartData" />
|
<Line id="my-chart-id" :options="chartOptions" :data="chartData" />
|
||||||
<button @click="updateData">Click me</button>
|
<button @click="updateData">Click me</button>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user