WPF编程,Live Charts使用说明(39)——数据分页

使用Axis.MinValue和Axis.MaxValue 属性来设置轴的范围,在本示例中,我们有一个包含100个点的线系列,并且只显示了用户想要查看的25条记录。

在这里插入图片描述

后台:

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using LiveCharts;
using Wpf.Annotations;
 
namespace Wpf.CartesianChart.ManualZAndP
{
    public partial class ManualZAndPExample : UserControl, INotifyPropertyChanged
    {
        private double _to;
        private double _from;
 
        public ManualZAndPExample()
        {
            InitializeComponent();
 
            Values = new ChartValues<double>();
 
            var r = new Random();
            for (var i = 0; i < 100; i++)
            {
                Values.Add(r.Next(0, 10));
            }
 
            //In this case we are paginating the data only showing the first 25 records
            //clicking the buttons previous and next changes the page
            From = 0;
            To = 25;
 
            DataContext = this;
        }
 
        public ChartValues<double> Values { get; set; }
 
        public double From
        {
            get { return _from; }
            set
            {
                _from = value;
                OnPropertyChanged("From");
            }
        }
 
        public double To
        {
            get { return _to; }
            set
            {
                _to = value;
                OnPropertyChanged("To");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
        
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
 
        private void NextOnClick(object sender, RoutedEventArgs e)
        {
            From += 25;
            To += 25;
        }
 
        private void PrevOnClick(object sender, RoutedEventArgs e)
        {
            From -= 25;
            To -= 25;
        }
 
        private void ManualZoom(object sender, RoutedEventArgs e)
        {
            //you only need to change the axis limits to zoom in/out any axis.
            From = 5;
            To = 10;
        }
    }
}

前台:

<UserControl x:Class="Wpf.CartesianChart.ManualZAndP.ManualZAndPExample"
             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:Wpf.CartesianChart.ManualZAndP"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <Button Click="PrevOnClick">Prev</Button>
            <Button Click="NextOnClick">Next</Button>
        </StackPanel>
        <Button Grid.Row="1" Click="ManualZoom" HorizontalAlignment="Left">Zoom to 5 > x < 10</Button>
        <lvc:CartesianChart Grid.Row="2">
            <lvc:CartesianChart.Series>
                <lvc:LineSeries Values="{Binding Values}"></lvc:LineSeries>
            </lvc:CartesianChart.Series>
            <lvc:CartesianChart.AxisX>
                <lvc:Axis MinValue="{Binding From}" MaxValue="{Binding To}" 
                          Separator="{x:Static lvc:DefaultAxes.CleanSeparator}">
                </lvc:Axis>
            </lvc:CartesianChart.AxisX>
        </lvc:CartesianChart>
    </Grid>
</UserControl>
Live-Charts
Release Announcements

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/105646243