当前位置: 首页 > article >正文

wpf 使用Oxyplot 库制作图表示例

方法:

InitTable 方法:负责初始化图表模型,包括设置图表的样式、坐标轴、系列和注释。这个方法包括多个 Init 方法的调用,表示图表的初始化过程可以分步骤进行。
InitGoalPoint 方法:当前未实现,但预留了子类进行重写。
InitXInitY 方法:分别初始化 X 和 Y 轴。
AddPoint 方法:向图表中添加点,并更新 Y 轴的最大值。
ClearPoint 方法:清空图表中的点并重新绘制。

字段和属性:

使用了 protected 修饰符来保护图表的各个部分(如 X 轴、Y 轴、系列等),允许子类访问和修改。
DataSrc 是一个公开的字典,用于存储数据源。
ChartModel 是公开的,只读属性,返回当前的图表模型。
GoalLines 也是公开的,只读属性,返回目标线的集合。

    [AddINotifyPropertyChangedInterface]
    public abstract class ChartBase<T>
    {
        public ChartBase()
        {
            InitTable();
        }

        protected virtual string ChartName { get; set; } = "";

        protected virtual string ChartXName { get; set; } = "";

        protected virtual string ChartYName { get; set; } = "";

        protected virtual double ChartXMin { get; set; } = double.NaN;

        protected virtual double ChartXMax { get; set; } = double.NaN;

        protected virtual double ChartXStep { get; set; } = double.NaN;

        protected virtual double ChartYMin { get; set; } = double.NaN;

        protected virtual double ChartYMax { get; set; } = double.NaN;

        protected virtual double ChartYStep { get; set; } = double.NaN;

        protected virtual void InitTable()
        {
            DataSrc = new();

            chartModel = new PlotModel()
            {
                Title = ChartName,
                IsLegendVisible = true,
                LegendMargin = 5,
                LegendPlacement = LegendPlacement.Outside,
                LegendOrientation = LegendOrientation.Horizontal,
                LegendPosition = LegendPosition.TopLeft,
                LegendFontSize = 15,
                PlotAreaBorderThickness = new OxyThickness(1, 0, 0, 1),
                TextColor = foreground,
                TitleColor = foreground,
                PlotAreaBorderColor = foreground,
            };

            InitX();
            InitY();
            InitGoalLine();
            InitGoalPoint();

            if(chart_X != null)
            {
                ChartModel.Axes.Add(chart_X);
            }

            if (chart_Y != null)
            {
                ChartModel.Axes.Add(chart_Y);
            }

            if(chart_X != null && chart_Y != null)
            {
                lineSeries = new LineSeries()
                {
                    //IsVisible = isShowAcResistance,
                    //Title = "奈奎斯特图",
                    MarkerType = MarkerType.Circle,
                    MarkerFill = OxyColors.Transparent,
                    MarkerStroke = OxyColors.DarkSeaGreen,
                    MarkerStrokeThickness = 1,
                    //StrokeThickness = seriesStrokeThickness,
                    Color = OxyColors.CadetBlue,
                    TextColor = foreground,
                    XAxisKey = "Chart_X",
                    YAxisKey = "Chart_Y",
                };

                ChartModel.Series.Add(lineSeries);
            }

            if(goalPoints!=null)
            {
                ChartModel.Series.Add(goalPoints);
            }

            if(goalLines!=null)
            {
                goalLines.ForEach(x => ChartModel.Annotations.Add(x));
            }

        }

        protected virtual void InitGoalPoint()
        {
           
        }

        OxyColor foreground = OxyColors.White;

        protected virtual void InitGoalLine()
        {
            //goalLines = new List<LineAnnotation>();

            //goalLines.Add(new LineAnnotation()
            //{
            //    Type = LineAnnotationType.Horizontal,
            //    Y = 20,
            //    LineStyle = LineStyle.Dash,
            //    StrokeThickness = 2,
            //    Color = OxyColors.DeepPink,
            //    TextColor = OxyColors.DeepPink,
            //    Text = "20",

            //});
        }

        protected virtual void InitY()
        {
            chart_Y = new LinearAxis()
            {
                Position = AxisPosition.Left,
                Title = ChartYName,
                TitlePosition = 0.5,
                Minimum = ChartYMin,
                Maximum = ChartYMax,
                MajorStep = ChartYStep,
                TextColor = foreground,
                TitleColor = foreground,
                TicklineColor = foreground,
                MinorTicklineColor = foreground,
                IsZoomEnabled = true,
                IsPanEnabled = true,
                Key = "Chart_Y",
            };
        }

        protected virtual void InitX()
        {
            chart_X = new LinearAxis()
            {
                Position = AxisPosition.Bottom,
                Title = ChartXName,
                Minimum = ChartXMin,
                Maximum = ChartXMax,
                MajorStep = ChartXStep,
                TextColor = foreground,
                TitleColor = foreground,
                TicklineColor = foreground,
                MinorTicklineColor = foreground,
                IsZoomEnabled = true,
                IsPanEnabled = true,
                Key = "Chart_X",
            };
        }


        public virtual void AddPoint(double x,double y)
        {
            lineSeries.Points.Add(new(x,y));

            if(y > ChartModel.Axes[1].Maximum)
            {
                ChartModel.Axes[1].Maximum = y + 2;
            }

            chartModel.InvalidatePlot(true);
        }

        public virtual void ClearPoint()
        {
            lineSeries.Points.Clear();

            if(goalPoints!=null)
            {
                goalPoints.Points.Clear();
            }

            chartModel.InvalidatePlot(true);
        }

        //public void SetGoalLine1(double goal)
        //{
        //    goalLines[0].Y = goal;

        //    goalLines[0].Text = goal.ToString();

        //    chartModel.InvalidatePlot(true);
        //}

        //public void SetGoalPoint(double x, double y)
        //{
        //    goalPoints.Points.Add(new ScatterPoint(x, y));

        //    chartModel.InvalidatePlot(true);
        //}

        protected LinearAxis chart_X = null;

        protected LinearAxis chart_Y = null;

        protected LineSeries lineSeries = null;

        protected List<LineAnnotation> goalLines = null;

        protected ScatterSeries goalPoints = null;

        protected PlotModel chartModel;

        public Dictionary<int, T> DataSrc;

        public PlotModel ChartModel { get => chartModel; }

        public List<LineAnnotation> GoalLines { get => goalLines; }
    }

http://www.kler.cn/news/310220.html

相关文章:

  • QT 6.8 C++ snap库 连接西门子PLC
  • 激光雷达点云处理—学习随记
  • 「数据科学」清洗数据,真实数据集中缺失值的查看与处理
  • 高效开发,从暗藏玄机的文件系统开始—合宙Air201资产定位模组LuatOS
  • 产品经理学习笔记
  • scanf()函数的介绍及基础用法
  • 孙怡带你深度学习(1)--神经网络
  • wordpress主题摘要调用显示错误解决办法
  • 计算机毕业设计 家电销售展示平台的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 无人机光电吊舱的技术!!
  • XML_Tomcat_HTTP
  • 如何设计可靠的 IIoT 架构
  • 大数据新视界 --大数据大厂之Kubernetes与大数据:容器化部署的最佳实践
  • 【.NET全栈】ASP.NET实战—基于ASP.NET的求职系统设计与实现
  • uni-app开发流程(开发、预览、构建和发布过程)
  • 基于SSM的“新闻发布及管理系统”的设计与实现(源码+数据库+文档)
  • 【可测试性实践】C++单元测试:gtest gmock
  • MySQL示例:创建数据库与表
  • BOM编程
  • 基于OpenHarmony(开源鸿蒙)的智慧医疗综合应用系统
  • python植物大战僵尸项目源码【免费】
  • 国内版Microsoft Teams 基础版部署方案
  • 小商品市场配电系统安全用电解决方案
  • 创客中国AIGC专题赛冠军天鹜科技:AI蛋白质设计引领者
  • WPF中Viewbox的介绍和用法
  • 1.1 软件测试 + AI
  • 如何在windows中使用mac,要详细的教程
  • 热力学(涨落)单元的探索
  • C++ 起始帧数、结束帧数、剪辑视频
  • 【PHP】使用thinkphp5查询最大值时,把varchar类型字段转换成数字