winforms WinForm AxisX IsReversed true Chart问题

h5qlskok  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(95)
namespace WinFormTest
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint1 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 1D);
            System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint2 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 2D);
            System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint3 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 3D);
            System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint4 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 2.4D);
            this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
            this.SuspendLayout();
            // 
            // chart1
            // 
            chartArea1.AxisX.IsReversed = true;
            chartArea1.CursorX.IsUserSelectionEnabled = true;
            chartArea1.Name = "ChartArea1";
            this.chart1.ChartAreas.Add(chartArea1);
            this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
            legend1.Name = "Legend1";
            this.chart1.Legends.Add(legend1);
            this.chart1.Location = new System.Drawing.Point(0, 0);
            this.chart1.Name = "chart1";
            series1.ChartArea = "ChartArea1";
            series1.Legend = "Legend1";
            series1.Name = "Series1";
            series1.Points.Add(dataPoint1);
            series1.Points.Add(dataPoint2);
            series1.Points.Add(dataPoint3);
            series1.Points.Add(dataPoint4);
            this.chart1.Series.Add(series1);
            this.chart1.Size = new System.Drawing.Size(284, 261);
            this.chart1.TabIndex = 0;
            this.chart1.Text = "chart1";
            // 
            // Form1
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.chart1);
            this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.ResumeLayout(false);

        }


        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
    }
}

上面是源代码。

第一屏幕

第二个屏幕(图表已缩放)

如果我这样拖

屏幕转这个(这是bug)
我该如何预防这种情况?

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    Console.WriteLine("{0} {1}", e.X, e.Y);
}

我试图打印鼠标在chart.MouseMove事件上的位置,但我无法Assert哪个位置超出了图表区域,因为图表大小不是绝对的。

s4chpxco

s4chpxco1#

我自己找到了两个解决办法。
短溶液:
chartArea.CursorX.AutoScroll = false
长时间溶液:
我们可以通过使用PositionInnerPlotPosition来找到内部图的宽度和X

public class Chart2 : Chart
        { 
            protected override void OnMouseMove(MouseEventArgs e)
            {
                var area = ChartAreas[0];
                var pos = area.Position;
                var areaX = pos.X * Width / 100;
                var areaWidth = pos.Width * Width / 100;
                var innerPos = area.InnerPlotPosition;
                var innerX = innerPos.X * areaWidth / 100;
                var innerWidth = innerPos.Width * areaWidth / 100;
                var start = areaX + innerX;
                var end = start + innerWidth;
                int mouseX = e.X;
                if (start + 1 < mouseX && mouseX < end - 1)
                {
                    base.OnMouseMove(e);
                }
            }
        }

相关问题