1

我需要图表控件中特定点的左值(或屏幕上的位置)。基本上是 0,0 点,因为它在调整窗体大小时会发生变化。

干杯

4

2 回答 2

0

解决方案

这是我需要的代码。它在调整图表大小时使标签靠近左上角。同样,当 Y 轴移动时,标签会粘在上面。

感谢@TaW 提供所需的代码(见答案 1)

            ChartArea ca = prodChart.ChartAreas[0];

            Axis ax = ca.AxisX;
            Axis ay = ca.AxisY;
            int px = (int)ax.ValueToPixelPosition(ax.Minimum + (ax.Maximum * 0.01));
            int py = (int)ay.ValueToPixelPosition(ay.Maximum - (ay.Maximum * 0.02));
            px = px - 5;
            qtyLabel.Location = new Point(px, py);
            sheetNameLabel.Location = new Point(px, py + 17);
            dateRangeLabel.Location = new Point(px, py + 34);
于 2016-10-20T12:37:05.763 回答
0

假设你的意思是 a DataPointwith的位置XValue = 0YValue[0] = 0你可以使用这个XAxis函数ValueToPixelPosition;这是一个示例,假设您已将 a 添加Label lbl 到图表中Controls,并将其保持Label在 3rd 的位置DataPoint

private void chart1_Resize(object sender, EventArgs e)
{
    DataPoint dp = chart1.Series[0].Points[2];
    ChartArea ca = chart1.ChartAreas[0];
    Axis ax = ca.AxisX;
    Axis ay = ca.AxisY;

    int px = (int) ax.ValueToPixelPosition(dp.XValue);
    int py = (int) ay.ValueToPixelPosition(dp.YValues[0]);

    lbl.Location = new Point(px, py);
}

请注意,此函数以及其他转换函数 ( PixelPositionToValue) 仅适用于Pre/PostPaint事件或鼠标事件。该Resize事件似乎也有效。

在其他时间使用它们,尤其是在图表完成布局之前,会产生错误或空值。

在此处输入图像描述

px、py 像素值是相对于图表的。要将它们转换为相对于 Form 的 Point,您可以使用通常的转换函数PointToScreenPointToClient.


更新:

如果你真的想要左上角的像素坐标,ChartArea.InnerPlotPosition你可以使用这两个函数

RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA)
{
    RectangleF CAR = CA.Position.ToRectangleF();
    float pw = chart.ClientSize.Width / 100f;
    float ph = chart.ClientSize.Height / 100f;
    return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height);
}

RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA)
{
    RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
    RectangleF CArp = ChartAreaClientRectangle(chart, CA);

    float pw = CArp.Width / 100f;
    float ph = CArp.Height / 100f;

    return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y,
                            pw * IPP.Width, ph * IPP.Height);
}

Resize在这样的事件中使用它:

ChartArea ca = chart1.ChartAreas[0];
Rectangle ipr = Rectangle.Round(InnerPlotPositionClientRectangle(chart1, ca));
lbl.Location =  ipr.Location;

在此处输入图像描述

如果您现在需要,您可以轻松地将其偏移几个像素。

于 2016-10-19T20:46:43.743 回答