首页 > 其他分享 >曲线方式观察double数组

曲线方式观察double数组

时间:2024-05-22 17:40:05浏览次数:23  
标签:ldata double 曲线 Visibility Brush 数组 new public

支持功能

  • 查看折线使能
  • 查看点标记使能
  • 数据保存到文件

支持数据类型

  • double[] / float[] / int[] 最常见
  • List / List / List 比较常见
  • double[][] xy组依次排列
  • double[][] 多个y组

依赖
DynamicDataDisplay库

使用(Visual Studio 2019)
dll及相关文件放入文件夹
C:\Users\xxx\Documents\Visual Studio 2019\Visualizers

Visualizers.cs
using ClassLibrary2;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System.Collections.Generic;


[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Lusong.Visualizer.ValueListVisualizer),
typeof(VisualizerObjectSource),
Target = typeof(List<double>),
Description = " 曲线查看 List<double> / List<float> / List<int>")]

[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Lusong.Visualizer.ValueArrayVisualizer),
typeof(VisualizerObjectSource),
Target = typeof(double[]),
Description = " 曲线查看 double[] / float[] / int[]")]

[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Lusong.Visualizer.DoublessVisualizer),
typeof(VisualizerObjectSource),
Target = typeof(double[][]),
Description = " 曲线查看 double[][],多个y组")]

[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Lusong.Visualizer.DoublessPointVisualizer),
typeof(VisualizerObjectSource),
Target = typeof(double[][]),
Description = " 曲线查看 double[][],xy组依次排列")]

namespace Lusong.Visualizer
{
    public class ValueListVisualizer : DialogDebuggerVisualizer
    {
        override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            List<double> datad = objectProvider.GetObject() as List<double>;
            List<float> dataf = objectProvider.GetObject() as List<float>;
            List<int> datai = objectProvider.GetObject() as List<int>;
            CurveForm form = new CurveForm();
            if (datad != null)
                form.AddLine(datad.ToArray());
            else if (dataf != null)
                form.AddLine(dataf.ToArray());
            else if (datai != null)
                form.AddLine(datai.ToArray());
            windowService.ShowDialog(form);
        }
    }
    public class ValueArrayVisualizer : DialogDebuggerVisualizer
    {
        override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            double[] datad = objectProvider.GetObject() as double[];
            float[] dataf = objectProvider.GetObject() as float[];
            int[] datai = objectProvider.GetObject() as int[];
            CurveForm form = new CurveForm();
            if(datad!=null)
                form.AddLine(datad);
            else if(dataf !=null)
                form.AddLine(dataf);
            else if (datai != null)
                form.AddLine(datai);
            windowService.ShowDialog(form);
        }
    }

    public class DoublessVisualizer : DialogDebuggerVisualizer
    {
        override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)

        {

            double[][] data = ((double[][])objectProvider.GetObject());

            if (data == null)
                return;

            CurveForm form = new CurveForm();

            for (int i = 0; i < data.Length; i++)
            {
                form.AddLine(data[i]);
            }

            windowService.ShowDialog(form);

        }
    }
    public class DoublessPointVisualizer : DialogDebuggerVisualizer
    {
        override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            double[][] data = ((double[][])objectProvider.GetObject());

            if (data == null)
                return;
            CurveForm form = new CurveForm();
            for (int i = 0; i < data.Length/2; i++)
            {
                form.AddLine(data[i * 2], data[i * 2 + 1]);
            }
            windowService.ShowDialog(form);
        }
    }
}

CurveControl.xaml
<UserControl x:Class="ClassLibrary2.CurveControl"
             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:ClassLibrary2"
             xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
             mc:Ignorable="d" 
             Loaded="UserControl_Loaded"

             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ToolBar>
            <Button x:Name="HideLineBtn" Click="HideLineBtn_Click">折线</Button>
            <Button x:Name="HideMarkerBtn" Click="HideMarkerBtn_Click">数据点标记</Button>
            <Button x:Name="SaveBtn" Click="SaveBtn_Click">保存</Button>
        </ToolBar>
        <d3:ChartPlotter Grid.Row="1" Name="plotter2" MinHeight="200" >

    <d3:VerticalRange Name="timerange2" Value1="0" Value2="0" Fill="LightGray" BorderBrush="Gray"/>

    <d3:CursorCoordinateGraph/>
            
</d3:ChartPlotter>

    </Grid>
</UserControl>

CurveControl.xaml.cs
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay.PointMarkers;
using Microsoft.Win32;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ClassLibrary2
{
    /// <summary>
    /// CurveControl.xaml 的交互逻辑
    /// </summary>
    public partial class CurveControl : UserControl
    {
        public CurveControl()
        {
            InitializeComponent();
        }


        public string Title = "数据";

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private Brush[] Brush = new Brush[] {Brushes.Red, Brushes.Green, Brushes.Blue, Brushes.DarkGoldenrod, Brushes.Purple, Brushes.Black, Brushes.DarkGray };
        List<LineAndMarker<ElementMarkerPointsGraph>> lines = new List<LineAndMarker<ElementMarkerPointsGraph>>();
        List<ObservableDataSource<OrderPoint>> datass = new List<ObservableDataSource<OrderPoint>>();
        public void AddLine(double[] datas)
        {
            ObservableDataSource<OrderPoint> ldata = new ObservableDataSource<OrderPoint>();
            datass.Add(ldata);

            ldata.SetXMapping(p => p.Index);

            ldata.SetYMapping(p =>
            {

                double v = p.Value;

                return v;

            });
            int c = datas.Length;
            OrderPoint[] ds = new OrderPoint[c];
            for (int i = 0; i < c; i++)
            {
                ds[i] = new OrderPoint(i, (float)datas[i]);
            }
            ldata.AppendMany(ds);

            var lg = plotter2.AddLineGraph(
                ldata,
                new Pen { Brush = this.Brush[lines.Count], Thickness = 2 },
                new CircleElementPointMarker { Size = 5, Fill = this.Brush[lines.Count], Brush = this.Brush[lines.Count] },
                new PenDescription(Title));
            lg.MarkerGraph.Visibility = Visibility.Visible;
            lines.Add(lg);
        }
        public void AddLine(int[] datas)
        {
            ObservableDataSource<OrderPoint> ldata = new ObservableDataSource<OrderPoint>();
            datass.Add(ldata);

            ldata.SetXMapping(p => p.Index);

            ldata.SetYMapping(p =>
            {

                double v = p.Value;

                return v;

            });
            int c = datas.Length;
            OrderPoint[] ds = new OrderPoint[c];
            for (int i = 0; i < c; i++)
            {
                ds[i] = new OrderPoint(i, datas[i]);
            }

            ldata.AppendMany(ds);

            var lg = plotter2.AddLineGraph(
                ldata,
                new Pen { Brush = this.Brush[lines.Count], Thickness = 2 },
                new CircleElementPointMarker { Size = 5, Fill = this.Brush[lines.Count], Brush = this.Brush[lines.Count] },
                new PenDescription(Title));
            lg.MarkerGraph.Visibility = Visibility.Visible;
            lines.Add(lg);
        }
        public void AddLine(float[] datas)
        {
            ObservableDataSource<OrderPoint> ldata = new ObservableDataSource<OrderPoint>();
            datass.Add(ldata);

            ldata.SetXMapping(p => p.Index);

            ldata.SetYMapping(p =>
            {

                double v = p.Value;

                return v;

            });
            int c = datas.Length;
            OrderPoint[] ds = new OrderPoint[c];
            for (int i = 0; i < c; i++)
            {
                ds[i] = new OrderPoint(i, datas[i]);
            }

            ldata.AppendMany(ds);

            var lg = plotter2.AddLineGraph(
                ldata,
                new Pen { Brush = this.Brush[lines.Count], Thickness = 2 },
                new CircleElementPointMarker { Size = 5, Fill = this.Brush[lines.Count], Brush = this.Brush[lines.Count] },
                new PenDescription(Title));
            lg.MarkerGraph.Visibility = Visibility.Visible;
            lines.Add(lg);
        }

        public void AddLine(double[] xs,double[] datas)
        {
            ObservableDataSource<OrderPoint> ldata = new ObservableDataSource<OrderPoint>();
            datass.Add(ldata);

            ldata.SetXMapping(p => p.Index);

            ldata.SetYMapping(p =>
            {

                double v = p.Value;

                return v;

            });
            int c = datas.Length;
            OrderPoint[] ds = new OrderPoint[c];
            for (int i = 0; i < c; i++)
            {
                ds[i] = new OrderPoint(xs[i], datas[i]);
            }
            ldata.AppendMany(ds);

            var lg = plotter2.AddLineGraph(
                ldata,
                new Pen { Brush = this.Brush[lines.Count], Thickness = 2 },
                new CircleElementPointMarker { Size = 5, Fill = this.Brush[lines.Count], Brush = this.Brush[lines.Count] },
                new PenDescription(Title));
            lg.MarkerGraph.Visibility = Visibility.Visible;
            lines.Add(lg);
        }

        private void HideLineBtn_Click(object sender, RoutedEventArgs e)
        {
            foreach (var line in lines)
            {
                if (line.LineGraph.Visibility != Visibility.Collapsed)
                    line.LineGraph.Visibility = Visibility.Collapsed;
                else
                    line.LineGraph.Visibility = Visibility.Visible;
            }
        }

        private void HideMarkerBtn_Click(object sender, RoutedEventArgs e)
        {
            foreach (var line in lines)
            {
                if (line.MarkerGraph.Visibility != Visibility.Collapsed)
                    line.MarkerGraph.Visibility = Visibility.Collapsed;
                else
                    line.MarkerGraph.Visibility = Visibility.Visible;
            }
        }

        public class OrderPoint
        {

            public OrderPoint(double index, double value)
            {
                this.Index = index;
                this.Value = value;
            }

            public double Index;
            public double Value;

        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "*.txt|*.txt";
            if (dialog.ShowDialog() != true)
            {
                return;
            }
            using (FileStream fs = new FileStream(dialog.FileName, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    foreach (var data in datass)
                    {
                        sw.WriteLine("count=" + data.Collection.Count);
                        for (int i = 0; i < data.Collection.Count; i++)
                        {
                            sw.WriteLine(data.Collection[i].Value);
                        }
                    }
                }
            }
        }

    }
}
CurveForm.cs
using System;
using System.Windows.Forms;

namespace ClassLibrary2
{
    public partial class CurveForm : Form
    {
        public CurveForm()
        {
            InitializeComponent();
        }

        public void AddLine(double[] data)

        {
            try
            {
                this.curveControl1.AddLine(data);
            }catch(Exception ex)
            {

            }

        }
        public void AddLine(double[] xs,double[] ys)

        {
            try
            {
                this.curveControl1.AddLine(xs,ys);
            }
            catch (Exception ex)
            {

            }

        }
        public void AddLine(int[] data)

        {

            this.curveControl1.AddLine(data);

        }

        public void AddLine(float[] data)

        {

            this.curveControl1.AddLine(data);

        }
    }
}

标签:ldata,double,曲线,Visibility,Brush,数组,new,public
From: https://www.cnblogs.com/epicblue/p/18206793

相关文章

  • PHP函数 向数组插入元素
    <?phpheader('Content-Type:text/html;charset=utf-8');define('ROOT',$_SERVER['DOCUMENT_ROOT']);includeROOT.'/assets/php/head.php';//PHP向数组插入元素/***1、使用array_unshift()函数,向数组头插入新元素;*2、使用array_push()函......
  • 2024-2030数据集成成熟度曲线(一)
    作者|郭炜导读:最新发布的《技术成熟度曲线2024》全面评估数据集成技术架构的7个维度,包括技术成熟度、技术难度、业务价值、技术成熟周期、管理协作难度、大模型结合等评估维度,报告篇幅较长,我们将报告分为3篇系列文章,本文为报告第一篇,描述了「从ETL到ELT,到EtLT的趋势」。接......
  • 二进制数组与基础类型转换
    ///<summary>///工具类:对象与二进制流间的转换///</summary>classByteConvertHelper{///<summary>///将对象转换为byte数组///</summary>///<paramname="obj">被转换对象</param>///......
  • 在 JavaScript 中递归展开数组
    对嵌套数组使用递归:递归是处理嵌套数组的干净而有效的方法。它允许您处理任意深度的数组。使用该Array.isArray方法检查数组:这有助于确保代码适用于不同的数据类型并且更加健壮。 潜在性能问题:对大型数组要小心:处理非常深或很大的数组时,递归函数可能会导致堆栈溢出错误。在这......
  • NumPy 数组排序、过滤与随机数生成详解
    NumPy数组排序排序数组排序数组意味着将元素按特定顺序排列。顺序可以是数字大小、字母顺序、升序或降序等。NumPy的ndarray对象提供了一个名为sort()的函数,用于对数组进行排序。示例:importnumpyasnparr=np.array([3,2,0,1])print(np.sort(arr))输出:[0......
  • 指针数组练习排列字符串
    用指针数组实现排列字符串#include<stdio.h>#include<math.h>#include<string.h>#defineN5voidOrderString(char*p[],intn);intmain(){char*arr[10]={"Hello","Howareyou?","I'mfine","Ilovecomputer......
  • 水位过程曲线(水位过程线)和水位-流量关系曲线的区别
    水位过程线(Stagehydrograph)是在水利水电工程专业中表示地表来水水位与时间关系的曲线。以时间为横坐标,以水位为纵坐标绘成的曲线,它显示了水位随时间变化的情况。可用以研究测站水位的变化规律。参考:https://baike.baidu.com/item/水位过程线/6124201?fr=ge_ala水位-流量关系曲......
  • 152- Maximum Produce Subarray-最大子数组之乘积
    问题描述Givenanintegerarray nums,finda subarray.thathasthelargestproduct,andreturn theproduct.Thetestcasesaregeneratedsothattheanswerwillfitina 32-bit integer.解释:找出一个数组中乘积最大的子数组,返回子数组的乘积。案例:Input......
  • 实验4 C语言数组应用编程
    task1.1voidtest1(){inta[N]={1,9,8,4};inti;//输出数组a占用的内存字节数printf("sizeof(a)=%d\n",sizeof(a));//输出int类型数组a中每个元素的地址、值for(i=0;i<N;++i)printf("%p:%d\n",&a[i],a[i......
  • 实验4 C语言数组应用编程
    实验任务1task1.1#include<stdio.h>#defineN4voidtest1(){inta[N]={1,9,8,4};inti;printf("sizeof(a)=%d\n",sizeof(a));for(i=0;i<N;++i)printf("%p:%d\n",&am......