1

I need the Left value (Or the Location on the screen) of a specific point in a Chart Control. Basiclly the 0,0 Point, because it changes when resizing the Form.

cheers

Vasmares
  • 13
  • 4
  • if you want it to change while resizing the form you can change the anchor of the chart – NotGI Oct 19 '16 at 11:27

2 Answers2

0

Assuming you mean the position of a DataPoint with XValue = 0 and YValue[0] = 0 you can use the XAxis function ValueToPixelPosition for this; here is an example, which assumes you have added a Label lbl to the chart's Controls and will keep that Label sitting at the position of the 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);
}

Do note that this function as well as the other conversion functions (PixelPositionToValue) will only work either in the Pre/PostPaint events or in mouse events. The Resize event also seems to work.

Using them at other time, most notably before the chart is done with its layout, will either produce wrong or null values.

enter image description here

The px, py pixel values are relative to the Chart. To convert them to a Point relative to the Form you can use the usual conversion functions PointToScreen and PointToClient.


Update:

If you actually want the pixel coordinates of the top left of the ChartArea.InnerPlotPosition you can use these two functions:

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

Use it maybe in the Resize event like this:

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

enter image description here

You can easily offset it by a few pixels if you want now..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Hi, that is basiclly what I am looking for, but I dont want to attach the label to a Datapoint. I want the label to be attached to the upper left corner of the chart. And to stay there. Image : [link](https://www.dropbox.com/s/3pk41k5l5pag3d5/chart.png?dl=0) cheers – Vasmares Oct 20 '16 at 11:24
  • Hi, I have updated the answer to show how to stick the label to the top left portion of the inner chart area. – TaW Oct 20 '16 at 11:58
  • Pefect answer :D Thats exactly wht I was looking for. – Vasmares Oct 20 '16 at 12:37
  • If you are happy with the answer, please consider consider [accepting](http://stackoverflow.com/help/accepted-answer) it..! – TaW Oct 20 '16 at 12:42
0

SOLUTION

this was the Code I needed. It keeps the labels close to the upper left corner while resizeing the chart. Also when the Y Axis moves, the labels stick to it.

Thanks to @TaW for providing the needed Code (See answer 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);
Vasmares
  • 13
  • 4
  • The same effect is what you get from the 2nd part of the answer but without relying on a maximum being set. – TaW Oct 20 '16 at 12:41