首页 > 其他分享 >WPF draw thumbs on livecharts and drag drop thumb

WPF draw thumbs on livecharts and drag drop thumb

时间:2024-08-28 21:36:59浏览次数:5  
标签:draw thumb drop System Next new using rnd 1000

//xaml
<Window x:Class="WpfApp299.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        xmlns:local="clr-namespace:WpfApp299"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow">
    <Grid x:Name="gd">
        <Button HorizontalAlignment="Right" VerticalAlignment="Top" Width="200" Height="50" Click="Button_Click" 
                Panel.ZIndex="2">Refresh</Button>
        <Thumb x:Name="tb" Panel.ZIndex="10" Width="10" 
               DragStarted="Thumb_DragStarted"
               DragDelta="Thumb_DragDelta"
               DragCompleted="Thumb_DragCompleted"
               Height="{Binding Path=ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">
            <Thumb.Template>
                <ControlTemplate>
                    <Rectangle>
                        <Rectangle.Fill>Red</Rectangle.Fill>
                    </Rectangle>
                </ControlTemplate>
            </Thumb.Template>
        </Thumb>
        
        <lvc:CartesianChart x:Name="chart" Series="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                            LegendLocation="Right"
                            MouseDown="CartesianChart_MouseDown" Panel.ZIndex="1">
            <lvc:CartesianChart.AxisX>
                <lvc:Axis Title="Salesman" Labels="{Binding Labels,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
            </lvc:CartesianChart.AxisX>
            <lvc:CartesianChart.AxisY>
                <lvc:Axis Title="Sold Apps" LabelFormatter="{Binding Formatter}"/>
            </lvc:CartesianChart.AxisY>
        </lvc:CartesianChart>
    </Grid>
</Window>




//cs
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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;
using LiveCharts;
using LiveCharts.Wpf;
using Newtonsoft.Json;

namespace WpfApp299
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        private int idx = 0;
        public MainWindow()
        {
            InitializeComponent();
            rnd = new Random();
            this.DataContext = this;
            InitData();
        }

        
        private void InitData()
        {           
            BooksCollection = new SeriesCollection();
            BooksCollection.Add(new ColumnSeries
            {
                Title = "2015",
                Values=new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1,1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2016",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2017",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2018",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2019",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });
            
            Labels = new List<string>();
            for(int i=0;i<5;i++)
            {
                Labels.Add($"SalesMan_{rnd.Next(1,100)}");
            }
        }

        private Random rnd { get; set; }

        private SeriesCollection booksCollection;
        public SeriesCollection BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                booksCollection = value;
                OnPropertyChanged(nameof(BooksCollection));
            }
        }

        private List<string> labels;
        public List<string> Labels
        {
            get
            {
                return labels;
            }
            set
            {
                if(value!=labels)
                {
                    labels = value;
                    OnPropertyChanged(nameof(Labels));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                InitData();
            }));
            //Task.Run(() =>
            //{
            //    InitData();
            //});
        }

        private void Thumb_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {

        }

        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            
        }

        private void Thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            var margin = tb.Margin.Left;
            var xDelta = e.HorizontalChange;
            tb.Margin = new Thickness(tb.Margin.Left + xDelta, 0, 0, 0);
        }

        int zIndex = 20;
        private void CartesianChart_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var pt = e.GetPosition(this);
            Thumb newTb = new Thumb();
            newTb.Width = 10;
            newTb.Background = new SolidColorBrush(Colors.Red);
            newTb.BorderBrush = new SolidColorBrush(Colors.Red);
            newTb.Margin = new Thickness(pt.X, 0, 0, 0);
            chart.Width = this.ActualWidth;
            Grid.SetZIndex(newTb, 10);             
            gd.Children.Add(newTb);
        } 
    }

    public class  BookTrim
    {
        public string Title { get; set; }
        public string Values { get; set; }
    }
}

 

 

 

 

 

 

 

标签:draw,thumb,drop,System,Next,new,using,rnd,1000
From: https://www.cnblogs.com/Fred1987/p/18385577

相关文章

  • WPF thumb Drag DragDelta,DragStarted,DragCompleted
    //xaml<Windowx:Class="WpfApp302.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • 使用跨平台库SixLabors.ImageSharp.Drawing生成图片验证码
     ///<summary>///绘制图片验证码///</summary>///<paramname="webRootPath"></param>///<paramname="width"></param>///<paramname="height"></param>///<returns>&......
  • Qt/QML学习-Drawer
    QML学习Drawer例程视频讲解代码main.qmlimportQtQuick2.15importQtQuick.Window2.15importQtQuick.Controls2.15Window{width:640height:480visible:truetitle:qsTr("Drawer")Drawer{id:drawerwidth:pare......
  • C# generate thumbnailimage via System.Drawing
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media.I......
  • [图文直播]基于Mermaid代码借助draw.io绘制依赖关系图
    安装draw.io开源仓库地址:GitHub-jgraph/drawio-desktop:Officialelectronbuildofdraw.io安装包地址Releases·jgraph/drawio-desktop·GitHub安装、具体实现......
  • [ARC175E] Three View Drawing
    MyBlogs[ARC175E]ThreeViewDrawing哎,构造。首先考虑\(m=n^2\)怎么做:显然是最上面一层填满第一条主对角线,第二层填满第二条主对角线...(主对角线指可以循环的对角线)。把\(n\)变成满足\(n^2\geqm\)的最小的\(n\)。然后考虑删去\(n^2-m\)个。可以发现(谁能发现啊啊啊......
  • 使用DropZone+SpringBoot实现图片的上传和浏览
    经常在项目中需要使用上传文件功能,找了不少前端上传组件,都不是很好用,今天尝试了一下DropZone,发现不错,顺便记录一下使用过程,方便后续查阅。在做开发的时候,经常需要调研一些技术,因此前后端都需要用到,为方便开发,这里采用传统的开发方式,没有做前后端分离,方便调试。前端采用HTML+......
  • 拖拽神器:Pragmatic-drag-and-drop!
    前言在前端开发中,拖拽功能是一种常见的交互方式,它能够极大提升用户体验。今天,我们要介绍的是一个开源的前端拖拽组件—pragmatic-drag-and-drop,它以其轻量级、高性能和强大的兼容性,成为了前端开发者的新宠。什么是pragmatic-drag-and-drop?pragmatic-drag-and-drop是由A......
  • Odin Inspector教程 | (四)创建自定义 Drawers(抽屉)
    【OdinInspectorandSerializer最新版免费下载地址】引言在Unity开发过程中,编辑器的用户体验同样重要。OdinInspector作为一个强大的编辑器扩展工具,允许开发者通过创建自定义Drawers来优化和个性化Inspector界面。自定义Drawers可以改变属性的显示方式,增加新的交......
  • limu|P10-14|多层感知机、激活函数、模型选择、欠拟合、过拟合、权重衰减、dropout、
    从感知机到多层感知机:感知机:只能产生线性分割面,不能拟合XOR为突破线性模型的限制,可以通过在网络中加入一个/多个隐藏层,即多层感知机MLP。但是如果只是单纯添加隐藏层,还是等价于一个线性模型(仿射变换的仿射变换还是仿射变换),没有带来益处!此时,需要加入额外因素以激发多层架构的潜......