2011-08-12 37 views
2

我應該畫一個極座標圖中的圓圈,上面有一些文字。轉換圖表COORDS像素

我開始PostPaint玩,得到了圖表圖形,所以我能夠在其上繪製和編寫自定義的東西。

我的主要問題是佈局。

例如我想提請SG在我的X和Y軸交叉,但我沒有找到圖形座標轉換(在我的例子0,0)到像素座標的任何有效的方法。

我該怎麼做?如何將圖表座標轉換爲像素?

.net4/winforms/vs2010/c#

回答

4

所以,我解決了它。

我處理的MSChart的postpaint事件:

private void chartMain_PostPaint(object sender, ChartPaintEventArgs e) 
    { 
     try 
     { 
      if (chartMain.ChartAreas.FirstOrDefault(a=>a.Name == "Default") == null) 
       return; 

      Graphics graph = e.ChartGraphics.Graphics; 

      var day = Date.GetBoundaries(daySelectorMain.DateTimePickerDay.Value); 
      var toDate = day.Item2; //it is maxdate value (max value of x axis) 

      var centerY = (float)chartMain.ChartAreas["Default"].AxisY.ValueToPixelPosition(-80); //-80 is the min value of y (y axis) 
      var centerX = (float)chartMain.ChartAreas["Default"].AxisX.ValueToPixelPosition(toDate.ToOADate()); 

      var origoY = (float)chartMain.ChartAreas["Default"].AxisY.ValueToPixelPosition(0); 
      var origoX = (float)chartMain.ChartAreas["Default"].AxisX.ValueToPixelPosition(toDate.ToOADate()); 

      var radius = (float)(centerY - origoY) - 1; 

      graph.DrawLine(System.Drawing.Pens.Blue, 
          new PointF(origoX, origoY), 
          new PointF(centerX, centerY)); 

      graph.FillEllipse(new SolidBrush(Color.White), centerX - radius, centerY - radius, radius * 2, radius * 2); 
     } 
     catch (System.Exception exc) 
     { 
      Debug.Assert(false, exc.Message); 
     } 
    } 
0

找到比例? 如果簾線是100×100,其中單元被僞,然後變換到200×200像素時,只需要使用比2.0 如果座標爲200x100且物理100x100的X具有比2.0和Y具有比1.0 當我SVG我必須偏移0 ,0(左上角)到笛卡爾座標(中心0,0)通過全部取消+1000 http://en.wikipedia.org/wiki/Cartesian_coordinate_system

+0

THX。真的MSChart中沒有ChartCoordsToPixels函數嗎? – Tom

1

自回答解決方案無論是在更一般的情況下工作,也沒有真正轉化DataPoint像素座標

這裏是畫圓,將永遠工作的解決方案:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e) 
{ 
    RectangleF ipp = InnerPlotPositionClientRectangle(chart1, chart1.ChartAreas[0]); 
    using (Graphics g = chart1.CreateGraphics()) 
     g.DrawEllipse(Pens.Red, new RectangleF(ipp.Location, ipp.Size)); 
} 

這使得兩個輔助功能使用:

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

And here is a link to a general coordinate transformation function for polar charts