WinForm AxisX IsReversed true Chart Problem 窗体 AxisX IsReversed 属性为 true 图表问题

huangapple go评论74阅读模式
英文:

WinForm AxisX IsReversed true Chart Problem

问题

以下是已翻译的内容:

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

请注意,这是源代码的一部分,未翻译的代码部分没有包含在内。如果您需要进一步的翻译或解释,请提出具体问题。

英文:
namespace WinFormTest
{
    partial class Form1
    {
        /// &lt;summary&gt;
        /// Required designer variable.
        /// &lt;/summary&gt;
        private System.ComponentModel.IContainer components = null;

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

        #region Windows Form Designer generated code

        /// &lt;summary&gt;
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// &lt;/summary&gt;
        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 = &quot;ChartArea1&quot;;
            this.chart1.ChartAreas.Add(chartArea1);
            this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
            legend1.Name = &quot;Legend1&quot;;
            this.chart1.Legends.Add(legend1);
            this.chart1.Location = new System.Drawing.Point(0, 0);
            this.chart1.Name = &quot;chart1&quot;;
            series1.ChartArea = &quot;ChartArea1&quot;;
            series1.Legend = &quot;Legend1&quot;;
            series1.Name = &quot;Series1&quot;;
            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 = &quot;chart1&quot;;
            // 
            // 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 = &quot;Form1&quot;;
            this.Text = &quot;Form1&quot;;
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.ResumeLayout(false);

        }



        #endregion

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

Above is the source code.

WinForm AxisX IsReversed true Chart Problem
窗体 AxisX IsReversed 属性为 true 图表问题

First screen

WinForm AxisX IsReversed true Chart Problem
窗体 AxisX IsReversed 属性为 true 图表问题

Second screen (Chart is zoomed)

WinForm AxisX IsReversed true Chart Problem
窗体 AxisX IsReversed 属性为 true 图表问题

If I drag like this

WinForm AxisX IsReversed true Chart Problem
窗体 AxisX IsReversed 属性为 true 图表问题

The screen turns this(which is bug)

How can I prevent this?

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

I tried to print the position of mouse on chart.MouseMove event, but I cannot assert which position is out of chart area since the chart size is not absolute.

答案1

得分: 0

Short solution:

chartArea.CursorX.AutoScroll = false

Long solution:

我们可以通过使用`Position`和`InnerPlotPosition`来找到内部绘图的宽度和X位置
```cs
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);
        }
    }
}

<details>
<summary>英文:</summary>
I found two solutions by myself.
Short solution:
chartArea.CursorX.AutoScroll = false
Long solution:
We can find width and X of inner plot by using `Position` and `InnerPlotPosition`
```cs
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 &lt; mouseX &amp;&amp; mouseX &lt; end - 1)
{
base.OnMouseMove(e);
}
}
}

huangapple
  • 本文由 发表于 2023年7月23日 23:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定