模拟数据实现Net8.0+WPF曲线图表展示历史报价记录的方法(多年月日+对应年月日的价格数据)

本文介绍了在.NET 8.0 Windows平台下,使用WPF结合LiveCharts2库实现多年历史报价数据(日期+价格)可视化展示的完整方案。内容涵盖NuGet包安装、基于CommunityToolkit.Mvvm的ViewModel设计、DateTimePoint时间序列数据建模、动态X轴(按月分组并格式化为yyyy-MM)、Y轴价格刻度配置、曲线样式定制(颜色、粗细、平滑度、悬停提示),以及预留数据库加载接口。适用于金融、商品价格监控等时序数据分析场景。

一、安装 NuGet 包及框架
 LiveChartsCore.SkiaSharpView.WPF
 CommunityToolkit.Mvvm
 项目框架:net8.0-windows
二、ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.ObjectModel;
namespace HdhCmsDemo.ViewModels
{
    partial class HdhCmsHistoryPriceDetailUCModel : ObservableObject
    {
        // 历史报价集合:DateTimePoint(日期,价格)
        public ObservableCollection<DateTimePoint> HdhCmsHistoryDatas { get; set; } = new();
        [ObservableProperty]
        private ISeries[] _series;
        [ObservableProperty]
        private ICartesianAxis[] _xAxes;
        [ObservableProperty]
        private Axis[] _yAxes;
        public HdhCmsHistoryPriceDetailUCModel()
        {
            // ========== 模拟多年历史报价,你这里换成从数据库读取 ==========
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2023, 01, 05), 126.35));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2023, 02, 10), 132.80));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2023, 03, 15), 129.42));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2023, 05, 02), 141.60));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2023, 08, 22), 137.10));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2023, 11, 08), 145.90));

            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2024, 02, 12), 152.30));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2024, 04, 18), 148.75));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2024, 07, 03), 160.22));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2024, 10, 25), 155.66));

            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2025, 01, 10), 168.40));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2025, 04, 06), 174.12));
            HdhCmsHistoryDatas.Add(new DateTimePoint(new DateTime(2025, 09, 14), 169.88));

            // ========== 曲线配置 ==========
            Series = new ISeries[]
            {
                new LineSeries<DateTimePoint>
                {
                    Values = HdhCmsHistoryDatas,
                    Name = "历史报价",
                    LineSmoothness = 1,
                    Stroke = new SolidColorPaint(SKColors.DarkOrange, 2.2f),
                    Fill = new SolidColorPaint(SKColors.Orange.WithAlpha(35)),
                    GeometrySize = 5,
                    GeometryStroke = new SolidColorPaint(SKColors.DarkOrange,1.8f),
                    YToolTipLabelFormatter = point => $"{point.Model.DateTime:yyyy-MM-dd}\n价格:{point.Model.Value:F2}"
                }
            };

            // ========== 时间X轴(重点!历史多年日期轴) ==========
            XAxes = new ICartesianAxis[]
            {
             new DateTimeAxis(TimeSpan.FromDays(30), dt => dt.ToString("yyyy-MM"))
             {
               Name = "日期",
               LabelsRotation = 30,
               ShowSeparatorLines = true,
               SeparatorsPaint = new SolidColorPaint(SKColors.LightGray, 1)
             }
            };

            // ========== Y价格轴 ==========
            YAxes = new Axis[]
            {
                new Axis
                {
                    Name = "价格",
                    Labeler = v => $"{v:F2}",
                    ShowSeparatorLines = true,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightGray, 1)
                }
            };
        }
        // 从数据库加载历史报价(直接替换你的查询逻辑)预留自主加载
        [RelayCommand]
        public void LoadHdhCmsHistoryDatas()
        {
            HdhCmsHistoryDatas.Clear();
            // var hdhcmsList = db.QuoteHistory.Where(...).OrderBy(p=>p.QuoteDate).ToList();
            // foreach(var item in hdhcmsList)
            // {
            //     HdhCmsHistoryDatas.Add(new DateTimePoint(item.QuoteDate, item.Price));
            // }
        }

        [RelayCommand]
        public void ClearChart()
        {
            HdhCmsHistoryDatas.Clear();
        }

    }
}
三、 XAML 前台页面
<UserControl x:Class="HdhCmsDemo.UserControls.HdhCmsHistoryPriceDetailUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
             xmlns:local="clr-namespace:HdhCmsDemo.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="#f00" Width="1700" Height="800">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <!--第一列-->
        <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="0" Background="LightGray">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Stretch" Background="#F1F5F8">
                <TextBlock Text="&#xf138;" FontSize="20" FontFamily="{StaticResource FontAweSome}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5 0 0 0" />
                <TextBlock Text="报价详情" FontSize="18" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Left" />
            </StackPanel>
            <Border Grid.Row="1" x:Name="borderPriceLeftList" />
        </Grid>
        <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="80" />
                <RowDefinition Height="30" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Stretch" Background="#F1F5F8">
                <TextBlock Text="&#xf138;" FontSize="20" FontFamily="{StaticResource FontAweSome}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5 0 0 0" />
                <TextBlock Text="基本信息介绍" FontSize="18" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Left" />
            </StackPanel>
            <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Stretch" Background="#F1F5F8">
            </StackPanel>
            <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Stretch" Background="#F1F5F8">
                <TextBlock Text="&#xf138;" FontSize="20" FontFamily="{StaticResource FontAweSome}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5 0 0 0" />
                <TextBlock Text="历史报价曲线图" FontSize="18" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Left" />
            </StackPanel>
            <Border Grid.Row="3" HorizontalAlignment="Stretch" Background="#f00000">
                <lvc:CartesianChart
                      Grid.Row="1"
                      Series="{Binding Series}"
                      XAxes="{Binding XAxes}"
                      YAxes="{Binding YAxes}"
                      ZoomMode="X" 
                    >
                </lvc:CartesianChart>
            </Border>
        </Grid>
    </Grid>
</UserControl>
 

附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[腈纶(acrylic), 棉(cotton), 雪纺(crepe), 牛仔(denim), 皮革(leather), 亚麻(linen), 尼龙(nylon), 丝绸(silk)] · 训练集:254 张 · 验证集:100 张 · 测试集:51 张 · 总计:405 张 该数据集聚焦于高精度纺织品面料材质识别场景,专为服装制造、纺织质检与智能零售等环节中对布料种类的快速准确判别需求而构建,具有明确的工业应用导向与实用价值。... 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 71 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.8112** mAP50-95 | 0.8063 Precision | 0.8162 Recall | 0.7480 train/box_loss | 0.1215 train/cls_loss | 0.5972 val/box_loss | 0.1691 val/cls_loss | 1.7964 【训练过程分析】 从一开始,训练过程的曲线图就给我留下了深刻的印象。开始的几轮,loss 值快速下降,模型仿佛找到了自己的节奏,准确率的提升明显。在最初的20轮里,训练曲线像是在说,一切都在按部就班地进行,模型似乎在享受着这个阶段的平滑下降,每一轮都向着最佳值一点点逼近。然而,到了第25轮之后,loss 下降的速度突然... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HDHCMS(建网站+客户管理)系统

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值