ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# ScottPlot 绘图控件 源码阅读心得体会

2021-12-04 22:00:53  阅读:308  来源: 互联网

标签:控件 心得体会 Plot signal formsPlot1 源码 Plottables null 图片


ScottPlot的介绍可以看这篇博客:https://www.cnblogs.com/myshowtime/p/15606399.html 我对代码的理解是这样的: 图像的呈现是靠bitmap,每进行拖动图像,放大缩小,等等操作,都会新创建一张图片,并把这张图片作为最新的展示在界面上。 为了保证图片内存的回收效率,还专门创建了一个队列,这个队列会把旧图片在新图片创建时入队,当对当前图形有改变时并且当队列里的图片个数大于3时,才会出队释放图片内存。 在【Settings】这个类里,主要是存储图像的配置和数据的,比如x y轴, 长宽 ,轴的长度限制,以及一个非常核心的观察模式的集合对象: ObservableCollection<IPlottable> Plottables 图片所包括的所有显示需要渲染到图片上的对象都实现了IPlottable接口,比如坐标 刻度 插图 x轴y轴线,里面的代码如下:
public interface IPlottable
    {
        bool IsVisible { get; set; }
        void Render(PlotDimensions dims, System.Drawing.Bitmap bmp, bool lowQuality = false);

        int XAxisIndex { get; set; }
        int YAxisIndex { get; set; }

        /// <summary>
        /// Returns items to show in the legend. Most plottables return a single item. in this array will appear in the legend.
        /// Plottables which never appear in the legend can return null.
        /// </summary>
        LegendItem[] GetLegendItems();

        /// <summary>
        /// Return min and max of the horizontal and vertical data contained in this plottable.
        /// Double.NaN is used for axes not containing data.
        /// </summary>
        /// <returns></returns>
        AxisLimits GetAxisLimits();

        /// <summary>
        /// Throw InvalidOperationException if ciritical variables are null or have incorrect sizes. 
        /// Deep validation is slower but also checks every value for NaN and Infinity.
        /// </summary>
        void ValidateData(bool deep = false);
    }
其中核心的就是Render方法了,这个在底层调用 bmp参数的gdi,这个bmp参数就是最终显示给用户的图像。所有在 Plottables 里的对象都会渲染到这张图片上面去。【PlotDimensions】 这个对象表示该元素的长宽 位置等信息。 暴露给用户使用的类是【Plot】类,里面有不同样式的 x y 数据类型,有单一的接受y轴数据,而x轴就平均分取。比如:
public SignalPlot AddSignal(double[] ys, double sampleRate = 1, Color? color = null, string label = null)
        {
            SignalPlot signal = new SignalPlot()
            {
                Ys = ys,
                SampleRate = sampleRate,
                Color = color ?? settings.GetNextColor(),
                Label = label,

                // TODO: FIX THIS!!!
                MinRenderIndex = 0,
                MaxRenderIndex = ys.Length - 1,
            };
            Add(signal);
            return signal;
        }

 最后在返回之前 ,这个Add(signal)会把这个对象加入到Plottables 集合中去 我们还可以添加很多元素,比如:

// plot the data
formsPlot1.Plot.AddScatter(xs, sin);
formsPlot1.Plot.AddScatter(xs, cos);

// customize the axis labels
formsPlot1.Plot.Title("ScottPlot Quickstart");
formsPlot1.Plot.XLabel("Horizontal Axis");
formsPlot1.Plot.YLabel("Vertical Axis");

 最后渲染的时候就用这个:formsPlot1.Refresh(); 它会调用Plot的 Render方法,这个方法会去遍历Plottables 这个集合,然后挨个用gdi画到bitmap上面去。 在formsPlot1控件里,有一个非常重要的类:【ControlBackEnd】,这里面有关于对图像 放大 缩小 移动 ,点击 的基本操作方法。 我们自己还可以注册这些事件写额外的代码 也不会影响。

标签:控件,心得体会,Plot,signal,formsPlot1,源码,Plottables,null,图片
来源: https://www.cnblogs.com/HelloQLQ/p/15643373.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有