您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

WPF LiveCharts动态曲线(折线图)

效果

 前端:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        Title="MainWindow" Width="700" Height="400" Loaded="Window_Loaded" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Background="AntiqueWhite">
            <Button Content="测试"  Margin="0,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        </Grid>
        <Grid Grid.Row="1">
            <lvc:CartesianChart Series="{Binding LineSeriesCollection}" LegendLocation="Right">
                <lvc:CartesianChart.AxisX>
                    <lvc:Axis LabelFormatter="{Binding CustomFormatterX}"  MaxValue="{Binding AxisXMax}" MinValue="{Binding AxisXMin}">
                        <lvc:Axis.Separator>
                            <lvc:Separator Step="1" />
                        </lvc:Axis.Separator>
                    </lvc:Axis>
                </lvc:CartesianChart.AxisX>
                
                <lvc:CartesianChart.AxisY>
                    <lvc:Axis LabelFormatter="{Binding CustomFormatterY}" MaxValue="{Binding AxisYMax}" MinValue="{Binding AxisYMin}">
                    </lvc:Axis>
                </lvc:CartesianChart.AxisY>

            </lvc:CartesianChart>
        </Grid>
    </Grid>
</Window>

后端:

using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication7
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Mode Modes = new Mode();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = Modes;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Modes.OnClick();
        }
    }


    public class Mode : ViewModelBase
    {
        public SeriesCollection LineSeriesCollection { get; set; }

        private double axisXMax;
        public double AxisXMax
        {
            get { return axisXMax; }
            set { axisXMax = value; this.OnPropertyChanged("AxisXMax"); }
        }

        private double axisXMin;
        public double AxisXMin
        {
            get { return axisXMin; }
            set { axisXMin = value; this.OnPropertyChanged("AxisXMin"); }
        }

        private double axisYMax;
        public double AxisYMax
        {
            get { return axisYMax; }
            set { axisYMax = value; this.OnPropertyChanged("AxisYMax"); }
        }
        private double axisYMin;
        public double AxisYMin
        {
            get { return axisYMin; }
            set { axisYMin = value; this.OnPropertyChanged("AxisYMin"); }
        }

        private Random Randoms = new Random();


        public Func<double, string> CustomFormatterX { get; set; }
        public Func<double, string> CustomFormatterY { get; set; }


        //绑定的X轴数据
        private ChartValues<double> ValueList { get; set; }

        //表中最大容纳个数
        private int TabelShowCount = 10;
        //曲线图的总个数
        private int ValueListCount = 0;


        public List<double> TestList = new List<double>();

        private string CustomFormattersX(double val)
        {
            return string.Format("{0}天", val);
        }

        private string CustomFormattersY(double val)
        {
            return string.Format("{0}公斤", val);
        }


        public void OnClick()
        {
            int yValue = Randoms.Next(2, 1000);

            //向图表中添加数据
            ValueList.Add(yValue);
            ValueListCount++;
            TestList.Add(yValue);

            //确保Y轴曲线不会超过图表
            int maxY = (int)ValueList.Max();
            AxisYMax = maxY + 30;

            //Y轴保持数据居中(曲线会上下晃动)
            //int minY = ValueList.Count == 1 ? 0 : (int)ValueList.Min();
            //AxisYMin = minY - 10;

            //y轴的设置
            if (ValueList.Count >= TabelShowCount)
            {
                AxisXMax = ValueListCount -1;
                AxisXMin = ValueListCount - TabelShowCount;
            }
            //if (ValueList.Count > 20)
            //{
            //    ValueList.RemoveAt(0);
            //}
        }

        public Mode()
        {
            AxisXMax = 10;
            AxisXMin = 0;
            AxisYMax = 10;
            AxisYMin = 0;

            ValueList = new ChartValues<double>();
            LineSeriesCollection = new SeriesCollection();

            CustomFormatterX = CustomFormattersX;
            CustomFormatterY = CustomFormattersY;

            LineSeries lineseries = new LineSeries();
            lineseries.DataLabels = true;
            lineseries.Values = ValueList;
            LineSeriesCollection.Add(lineseries);
        }
    }
}

写作不易,欢迎关注 + 点赞 + 留言,谢谢

end


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进