图表:调用FluentUI中的折线图散点图和饼状图

0.文章介绍

调用项目FluentUI中的散点图、折线图和饼状图组件,做定制化改进。
项目FluentUI源码位置:https://github.com/zhuzichu520/FluentUI
项目FluentUI导入教程:https://blog.csdn.net/summer__7777/article/details/139819435

1.源码位置

在这里插入图片描述

2.效果图

在这里插入图片描述

3.代码

3.1 代码结构

在这里插入图片描述

3.2 main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import FluentUI 1.0

Window {
    id: root
    visible: true
    width: 1400
    height: 700

    MyScatterChart{
        id:scatterChart
        anchors.top: parent.top
        anchors.left: parent.left
    }
    MyLineChart{
        id:lineChart
        anchors.left: scatterChart.right
        anchors.leftMargin: 20
        anchors.top: scatterChart.top
    }
    MyPieChart{
        id:myPieChart
        anchors.left: lineChart.right
        anchors.leftMargin: 20
        anchors.top: scatterChart.top
    }
    MyBarChart1{
        id:myBarChart1
        anchors.left: scatterChart.left
        anchors.top: scatterChart.bottom
        anchors.topMargin: 10
    }
    MyBarChart2{
        id:myBarChart2
        anchors.left: lineChart.left
        anchors.top: myBarChart1.top
    }

}

3.3 MyLineChart.qml

// MyLineChart.qml
import QtQuick 2.15
import FluentUI

Rectangle {
    id: myLineChart
    width: 400
    height:300

    property var dataLineChart: [] // 储存数据:y轴
    property var labelsLineChart: []  // 存储时间:x轴
    property int maxDataPoints: 10  // 最多显示的数据点数

    FluFrame {
        anchors.fill: parent
        padding: 10
        // 折线图
        FluChart {
            id: chart
            anchors.fill: parent
            chartType: 'line'
            chartData: { return {
                    labels: myLineChart.labelsLineChart, // x轴标签
                    datasets: [{ // 包含一个或多个数据集的配置
                            label: 'My First Dataset', // 数据集的名称或描述,显示在图表的图例中
                            data: myLineChart.dataLineChart,
                            fill: true, // 决定图表中的线条下方是否填充颜色
                            borderColor: 'rgb(75, 192, 192)', // 线条的颜色
                            // backgroundColor:'red' // 填充区域颜色
                            tension: 0.1  // 线条的平滑度
                        }]
                }
            }
            // 配置图表外观和行为的属性
            chartOptions: { return {
                    maintainAspectRatio: false,
                    title: {
                        display: true,
                        text: '线形图名称'
                    },
                    tooltips: {
                        mode: 'index',
                        intersect: false
                    },
                    scales: {
                        xAxes: [{
                            scaleLabel: {
                                display: true,
                                labelString: 'Time (HH:MM:SS)'  // X轴的单位
                            }
                        }],
                        yAxes: [{
                            scaleLabel: {
                                display: true,
                                labelString: 'Value (Units)'  // Y轴的单位
                            }
                        }]
                    }
                }
            }
        }

        Timer {
            id: timer
            interval: 1000  // 每秒触发一次
            repeat: true
            onTriggered: {
                var currentTime = new Date();
                var timeString = currentTime.toLocaleTimeString();  // 获取当前时间的字符串格式,精确到秒

                // 更新数据和标签
                myLineChart.dataLineChart.push(Math.random() * 1000);
                myLineChart.labelsLineChart.push(timeString);

                if (myLineChart.dataLineChart.length > myLineChart.maxDataPoints) {
                    myLineChart.dataLineChart.shift();
                    myLineChart.labelsLineChart.shift();  // 移除最旧的标签
                }

                chart.animateToNewData();
            }
        }

        Component.onCompleted: {
            timer.restart();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值