wpf 使用Oxyplot 库制作图表示例

发布于:2024-09-17 ⋅ 阅读:(141) ⋅ 点赞:(0)

方法:

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; }
    }

网站公告

今日签到

点亮在社区的每一天
去签到