无法在ZedGraph中绘制垂直线

问题描述 投票:0回答:1

以下源代码应该使用 ZedGraph 库绘制一条垂直线。

AddVerticalLine()
函数应该能够处理线性尺度和对数尺度。

但是,由于某种原因,它无法绘制线条,并且我看到一个空白的图形窗格。

我做错了什么?

class Program
{
    static void Main(string[] args)
    {
        DataPlotter plotter = new DataPlotter();
        plotter.IsLogX = true;
        plotter.IsLogY = true;
        
        plotter.AddVerticalLine("Vertical Line at 10^2.2", 2.2);
        plotter.ShowDialog();
    }
}
using ZedGraph;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;

public class DataPlotter
{
    private GraphPane myPane_;
    private int curveCounter_ = 0;

    #region [Single line properties]
    public ZedGraphControl ZedGraphCtrl { get; set; }
    public SymbolType DefaultSymbolType { get; set; } = SymbolType.Circle;
    public float DefaultSymbolSize { get; set; } = 7;
    public Color DefaultLineColor { get; set; } = Color.Blue;

    public DashStyle DefaultLineDashStyle { get; set; } = DashStyle.Solid;
    public float DefaultLineWidth { get; set; } = 1;

    public Color? CurveColor { get; set; }
    public SymbolType? CurveSymbolType { get; set; }
    public float? CurveSymbolSize { get; set; }
    public DashStyle? CurveDashStyle { get; set; }
    #endregion

    #region [Multi-line properties]
    public string ImageTitle
    {
        get => myPane_.Title.Text;
        set => myPane_.Title.Text = value;
    }

    public string XAxisTitle
    {
        get => myPane_.XAxis.Title.Text;
        set => myPane_.XAxis.Title.Text = value;
    }

    public string YAxisTitle
    {
        get => myPane_.YAxis.Title.Text;
        set => myPane_.YAxis.Title.Text = value;
    }

    public int Width
    {
        get => ZedGraphCtrl.Width;
        set => ZedGraphCtrl.Width = value;
    }

    public int Height
    {
        get => ZedGraphCtrl.Height;
        set => ZedGraphCtrl.Height = value;
    }

    public bool IsLogX
    {
        get => myPane_.XAxis.Type == AxisType.Log;
        set => myPane_.XAxis.Type = value ? AxisType.Log : AxisType.Linear;
    }

    public bool IsLogY
    {
        get => myPane_.YAxis.Type == AxisType.Log;
        set => myPane_.YAxis.Type = value ? AxisType.Log : AxisType.Linear;
    }

    private bool _isSymbolVisible = true;

    public bool IsSymbolVisible
    {
        get => _isSymbolVisible;
        set
        {
            _isSymbolVisible = value;
            foreach (CurveItem curve in myPane_.CurveList)
            {
                if (curve is LineItem lineItem)
                {
                    lineItem.Symbol.IsVisible = value;
                }
            }
            ZedGraphCtrl.Invalidate();
        }
    }

    public bool IsLegendVisible
    {
        get => myPane_.Legend.IsVisible;
        set
        {
            myPane_.Legend.IsVisible = value;
            ZedGraphCtrl.Invalidate();
        }
    }
    #endregion

    #region [ctor]
    public DataPlotter()
    {
        ZedGraphCtrl = new ZedGraphControl();
        ZedGraphCtrl.Size = GetDisplayScreenResolution();
        myPane_ = ZedGraphCtrl.GraphPane;

        ImageTitle = "Image Title";
        XAxisTitle = "X Axis";
        YAxisTitle = "Y Axis";
        IsLogX = false;
        IsLogY = false;
        IsLegendVisible = true;
        IsSymbolVisible = false;
        ZedGraphCtrl.Dock = System.Windows.Forms.DockStyle.Fill;
    }
    #endregion



    // Method to bring the specified curve to the front
    private void BringCurveToFront(LineItem curve)
    {
        myPane_.CurveList.Remove(curve);
        myPane_.CurveList.Insert(0, curve);
        ZedGraphCtrl.Invalidate();
    }

    public void SavePlot(string outputPath, string outputFileName)
    {
        try
        {
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            string fullPath = Path.Combine(outputPath, outputFileName);

            ZedGraphCtrl.AxisChange();
            ZedGraphCtrl.Invalidate();

            using (Bitmap bmp = new Bitmap(ZedGraphCtrl.Width, ZedGraphCtrl.Height))
            {
                ZedGraphCtrl.DrawToBitmap(bmp, new Rectangle(0, 0, ZedGraphCtrl.Width, ZedGraphCtrl.Height));
                bmp.Save(fullPath, ImageFormat.Png);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred while saving the plot: " + ex.Message);
        }
    }

    private Size GetDisplayScreenResolution()
    {
        // PrimaryScreen represents the primary display monitor
        Rectangle resolution = Screen.PrimaryScreen.Bounds;

        // Create a Size structure to hold the width and height of the primary screen
        Size screenSize = new Size(resolution.Width, resolution.Height);

        return screenSize;
    }

    public void Clear()
    {
        myPane_.CurveList.Clear();
        ZedGraphCtrl.Invalidate();
    }

    public void ShowDialog()
    {
        DataPlotterForm f = new DataPlotterForm();
        ZedGraphCtrl.Dock = DockStyle.Fill;
        f.Controls.Add(ZedGraphCtrl);
        f.WindowState = FormWindowState.Maximized;
        f.ShowDialog();
    }


    #region [AddVerticalLine]
    public void AddVerticalLine(string curveName, double xValue, Color? curveColor = null)
    {
        Color actualCurveColor = curveColor ?? DefaultLineColor;

        if (IsLogX)
        {
            xValue = Math.Pow(10, xValue);
        }

        PointPairList list = new PointPairList();
        double minY = myPane_.YAxis.Scale.Min;
        double maxY = myPane_.YAxis.Scale.Max;

        if (myPane_.YAxis.Type == AxisType.Log)
        {
            minY = Math.Max(minY, 1.0); 
            maxY = Math.Max(maxY, minY * 10); 
        }

        list.Add(xValue, minY); 
        list.Add(xValue, maxY); 

        LineItem myCurve = myPane_.AddCurve(curveName, list, actualCurveColor, SymbolType.None);

        myCurve.Line.IsVisible = true;
        myCurve.Line.Style = DashStyle.Solid;
        myCurve.Line.Width = 2.0F;

        myCurve.Symbol.IsVisible = false;

        ZedGraphCtrl.Invalidate();
    }
    #endregion
}
c# plot zedgraph
1个回答
0
投票

我怀疑这条线可能偏离了图表。我只看到原点处的比例设置。我在某些应用程序中使用垂直线,并用少量修改替换了您的代码(仅限 AddVerticalLine 方法)(功能没有变化)。你的代码有效。尝试使用滚轮缩小,看看是否可以找到线条的放置位置。如果这不起作用,那么您可能必须首先加载除垂直线之外的某种曲线。我的代码总是加载一条或多条曲线,然后 ZedGraph 轴更改会调整比例。然后我才添加一条垂直线。

© www.soinside.com 2019 - 2024. All rights reserved.