我需要获取图表中AxisY和AxisY2的位置。我想利用此信息做很多事情。但是,作为一个简单的示例,我只想在两个y轴之间绘制一条水平线,但不想让它延伸到它们之外。
使用AxisX.Minimum和Maximum可以工作,只要所有值都显示在图表上即可。但是,如果图表有滚动条,则线条会延伸到axisY的左侧或axisY2的右侧,具体取决于图表的滚动位置。因此,我需要的是所显示的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));
}
1条答案
按热度按时间huwehgph1#
感谢winforms-datavisualization的kirsan31,这正是我想要的