首页 > 其他分享 >wpf + LiveCharts.wpf 做个漂亮的图表

wpf + LiveCharts.wpf 做个漂亮的图表

时间:2023-12-26 14:34:49浏览次数:24  
标签:ChartValues 12 get 图表 set using wpf public LiveCharts

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

参考:WPF LiveChart 图表详解

接着上一篇博客: wpf + MaterialDesign + Prism8 实现导航功能

 1、项目引入图表包

wpf + LiveCharts.wpf 做个漂亮的图表_xml

 2、定义用户控件 IndexView 的 IndexViewModel,如下

using LiveCharts;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace WpfApp.ViewModels
{
    public class IndexViewModel : BindableBase
    {
        private ChartValues<int> dataXCenters;

        public ChartValues<int> DataXCenters
        {
            get { return dataXCenters; }
            set { dataXCenters = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 
        /// </summary>
        private ChartValues<int> dataYCenters;

        public ChartValues<int> DataYCenters
        {
            get { return dataYCenters; }
            set { dataYCenters = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<IndexXamlModel> indexData;

        public ObservableCollection<IndexXamlModel> IndexData
        {
            get { return indexData; }
            set { indexData = value; RaisePropertyChanged(); }
        }




        public IndexViewModel()
        {
            DataXCenters = new ChartValues<int>() { 1, 2, 3, 4, 5, 6, 2, 3, 2, 2, 3, 4, 6, 2, 3, 4, 1, 2, 3, 4, 5, 6, 2, 3, 2, 2, 3, 4, 6, 2, 3, 4 };
            DataYCenters = new ChartValues<int>() { 2, 4, 6, 8, 10, 12, 18, 19, 16, 12, 13, 14, 18, 10, 12, 12, 2, 4, 6, 8, 10, 12, 18, 19, 16, 12, 13, 14, 18, 10, 12, 12 };

            // 
            IndexData = new ObservableCollection<IndexXamlModel>();
            for (int i = 0; i < 10; i++)
            {
                IndexData.Add(new IndexXamlModel() { count = (i + 1) * 3, title = (i + 1) + "级传销商" });
            }
        }
    }
    public class IndexXamlModel
    {
        public string title { get; set; }
        public int count { get; set; }
    }
}

View Code

3、IndexView.Xaml 如下

<UserControl x:Class="chengxiangSite.UserControls.IndexView"
             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:local="clr-namespace:chengxiangSite.UserControls" xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ItemsControl Grid.Row="0" ItemsSource="{Binding IndexData}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="10"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="#ccc" BorderThickness="0,0,1,0" Margin="5,8" Height="70" Background="#fafafa"  >
                        <Grid  DockPanel.Dock="Right">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding count}" HorizontalAlignment="Center" Margin="0,9"  FontWeight="Bold" Foreground="#747474" FontSize="22"/>
                            <TextBlock Grid.Row="1" Text="{Binding title}" FontSize="12" HorizontalAlignment="Center" FontWeight="Light" Foreground="#747474"/>

                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        
        
        <lvc:CartesianChart Grid.Row="1" LegendLocation="Top" Margin="0,12">
            <lvc:CartesianChart.Series>
                <!--设置Series的类型为 Line 类型, 该类型提供了一些折线图的实现-->
                <lvc:LineSeries LineSmoothness="1" DataLabels="False" StrokeDashArray="1"  Title="payCount"  Values="{Binding DataXCenters}"/>
                <lvc:LineSeries LineSmoothness="1" DataLabels="False"   Title="totalCount"     Values="{Binding DataYCenters}"/>
            </lvc:CartesianChart.Series>
            <!--定义Y轴-->
            <lvc:CartesianChart.AxisY>
                <!-- 定义Y轴名称 -->
                <lvc:Axis Title="单位/次"></lvc:Axis>
            </lvc:CartesianChart.AxisY>

            <!--定义X轴-->
            <lvc:CartesianChart.AxisX>
                <!-- 定义X轴名称 -->
                <lvc:Axis Title="单位/次"></lvc:Axis>
            </lvc:CartesianChart.AxisX>
            <!--<lvc:CartesianChart.VisualElements>
                <lvc:VisualElement X="{Binding MinX}" Y="{Binding MaxY}">
                    <lvc:VisualElement.UIElement>
                        <TextBlock Foreground="Green">
                            Hello!, this is a note merged in the chart.
                        </TextBlock>
                    </lvc:VisualElement.UIElement>
                </lvc:VisualElement>
            </lvc:CartesianChart.VisualElements>-->
           
        </lvc:CartesianChart>
    </Grid>
</UserControl>

View Code

效果如下:

wpf + LiveCharts.wpf 做个漂亮的图表_microsoft_02

 @天才卧龙的博客



标签:ChartValues,12,get,图表,set,using,wpf,public,LiveCharts
From: https://blog.51cto.com/u_15316082/8983323

相关文章

  • wpf + MaterialDesign + Prism8 实现导航功能
    十年河东,十年河西,莫欺少年穷学无止境,精益求精实现的效果: 1、初始化Prism 1.1、项目引入如下包 1.2、按照Prism规则,项目中创建如下文件夹 Prism规则:必须将窗体放入Views文件夹中,窗体名称必须以View结尾,必须将数据上下文放入ViewModels文件夹中,上下文类必须以Model结尾另外两个......
  • WPF 装饰器 、 转换器 、行为
    十年河东,十年河西,莫欺少年穷学无止境,精益求精行为请参考:WPF行为 装饰器参考: wpf转换器详情参考:单值转换器需继承自IValueConverterpublicclassMyNumberConverter:IValueConverter{publicobjectConvert(objectvalue,TypetargetType,objectparameter,......
  • wpf + MaterialDesign + Prism8 实现导航功能
    十年河东,十年河西,莫欺少年穷学无止境,精益求精实现的效果: 1、初始化Prism 1.1、项目引入如下包 1.2、按照Prism规则,项目中创建如下文件夹 Prism规则:必须将窗体放入Views文件夹中,窗体名称必须以View结尾,必须将数据上下文放入ViewModels文件夹中,上下文类必须以Model结尾另外两个......
  • WPF+SqlSugar+MVVM实现增删改查(二)
    这相对于上一版本的升级版如果不理解看请看第一版:WPF+SqlSugar+MVVM实现增删改查-六子12138-博客园(cnblogs.com)ViewModels代码1usingEntitys;2usingSqlSugar;3usingSystem;4usingSystem.Collections.Generic;5usingSystem.Collections.Object......
  • wpf + MaterialDesign + Prism8 + DataGrid 实现表格内数据编辑,下拉
    十年河东,十年河西,莫欺少年穷学无止境,精益求精效果如下: xaml如下:<UserControlx:Class="WpfApp.UserControls.MemoView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/w......
  • C++ Qt开发:Charts绘制各类图表详解
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍TreeWidget与QCharts的常用方法及灵活运用。在之前的文章中笔者介绍了如何使用QCharts模块来绘制......
  • WPF Halcon机器视觉和运动控制软件通用框架,插件式开发,开箱即用 仅供学习!
    点我下载,仅供个人学习使用参考easyvision开发,集成几十个软件算子此版本以添加ui设计器。具体功能如上所示,可以自定义变量,写c#脚本,自定义流程,包含了halcon脚本和封装的算子,可自定义ui,通过插件形式开发很方便拓展自己的功能。......
  • wpf + MaterialDesign + Prism8 + DataGrid 实现表格数据+分页
    十年河东,十年河西,莫欺少年穷学完止境,精益求精1、不分页,带有排序功能(每个字段都可以排序) xaml如下:<UserControlx:Class="WpfApp.UserControls.UserView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http:......
  • 深入理解WPF中的Dispatcher:优化UI操作的关键
     概述:Dispatcher是WPF中用于协调UI线程和非UI线程操作的关键类,通过消息循环机制确保UI元素的安全更新。常见用途包括异步任务中的UI更新和定时器操作。在实践中,需注意避免UI线程阻塞、死锁,并使用CheckAccess方法确保在正确的线程上执行操作。这有助于提升应用程序的性能和用户......
  • 使用MVVM Toolkit简化WPF开发
    最近.NET8的WPF推出了 WPFFileDialog改进,这样无需再引用 Win32 命名空间就可以实现文件夹的选择与存储了,算是一个很方便的改进了。顺手写了一个小的WPF程序,在使用 Model-View-ViewModel(MVVM) 模式的时候,我不想使用 Prism 等重量级的框架,找了一个轻量级的MVVMCo......