简体   繁体   English

Windows Forms Chart中如何获取AxisY和AxisY2的位置

[英]How to get the location of AxisY and AxisY2 in a Windows Forms Chart

I need to get the location of AxisY and AxisY2 in a chart.我需要在图表中获取 AxisY 和 AxisY2 的位置。 There are a lot of things I would like to do with this information.我想用这些信息做很多事情。 However, as a simple example, I just want to draw a horizontal line between the two y axes, but not have it extend past them.然而,作为一个简单的例子,我只想在两个 y 轴之间画一条水平线,但不要让它超过它们。

Using AxisX.Minimum and Maximum works, as long as all of the values appear on the chart.只要所有值都出现在图表上,使用 AxisX.Minimum 和 Maximum 就可以了。 However, if the chart has a scroll bar, the lines extend to the left of axisY or the right of axisY2 depending on where the chart is scrolled.但是,如果图表有滚动条,则线条会延伸到 axisY 的左侧或 axisY2 的右侧,具体取决于图表滚动的位置。 Therefore, what I need is the location of AxisY and AxisY2 as displayed.因此,我需要的是显示的 AxisY 和 AxisY2 的位置。

public Form1() 
{
    InitializeComponent();

    // Add an event handler
    this.chart1.Paint += new PaintEventHandler(this.DrawLine);

    // Add some values
    for (int value = 0; value <= 10; value++)
        chart1.Series[0].Points.AddXY(value, value);

    // DrawLine no longer works when a scroll bar is added,
    //chart1.ChartAreas[0].AxisX.ScaleView.Zoom(0, 5);
}

// Draw a line from axisY to axisY2
private void DrawLine(object sender, PaintEventArgs e) 
{
    // Get the left and right edges of the values
    // This only works if all of the values appear on the chart at the same time, ie. there is no scroll bar
    // How can I find the locations of axisY and axisY2 as drawn on the chart?
    int axisYLocation = (int)chart1.ChartAreas[0].AxisX.ValueToPixelPosition(chart1.ChartAreas[0].AxisX.Minimum);
    int axisY2Location = (int)chart1.ChartAreas[0].AxisX.ValueToPixelPosition(chart1.ChartAreas[0].AxisX.Maximum);

    e.Graphics.DrawLine(new Pen(Color.Red, 5), new Point(axisYLocation, 50), new Point(axisY2Location, 50)); 
}

Thanks to kirsan31 at winforms-datavisualization , this does exactly what I want感谢winforms-datavisualization的 kirsan31,这正是我想要的

int axisYLocation = (int)chart1.ChartAreas[0].AxisX.ValueToPixelPosition(chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum);
int axisY2Location = (int)chart1.ChartAreas[0].AxisX.ValueToPixelPosition(chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM