2016-11-09 25 views
1

グラフ上の2点の間に線を描きたいが、私はChartStyle.Lineを使うことができないので、Graphics.DrawLineを使用しようとした。チャートや他のコントロールの上に描画する方法

私の問題は、私がチャートの上に描画することができないということです。これをどうすれば解決できますか?

 PointF pontoantigo = new PointF(); 

      if (chart1.Series[0].Points.Count > 0) 
      { 
       pontoantigo = new PointF((int)chart1.Series[0].Points[0].XValue, (int)chart1.Series[0].Points[0].YValues[0]); 
      } 


      chart1.Series[0].Points.Clear(); 

      chart1.Series[0].Points.AddXY(posicao_atual_master.X, posicao_atual_master.Y); 
      PointF pontoatual = new PointF((int)chart1.Series[0].Points[0].XValue, (int)chart1.Series[0].Points[0].YValues[0]); 

      Pen p = new Pen(Color.Red); 
      Graphics g = chart1.CreateGraphics(); 
      g.DrawLine(p, pontoantigo, pontoatual); 

EDIT:古いものと新しいポイントの値を更新

FUNCTION:

pontoantigo = new PointF(); 
      if (chart1.Series[0].Points.Count > 0) 
      { 
       pontoantigo = new PointF((int)chart1.Series[0].Points[0].XValue, (int)chart1.Series[0].Points[0].YValues[0]); 
      } 


      chart1.Series[0].Points.Clear(); 

      chart1.Series[0].Points.AddXY(posicao_atual_master.X, posicao_atual_master.Y); 
      pontoatual = new PointF((int)chart1.Series[0].Points[0].XValue, (int)chart1.Series[0].Points[0].YValues[0]); 

POSTPAINT:STILL

private void chart1_PostPaint(object sender, ChartPaintEventArgs e) 
    { 

     Pen p = new Pen(Color.Red); 
     Graphics g = e.ChartGraphics.Graphics; 
     g.DrawLine(p, pontoantigo, pontoatual); 

    } 

に動作していない

+0

[よくある解決策](http://stackoverflow.com/search?q=user%3A3152130+Chart++prepaint)とこちら[こちら](http:// stackoverflow)を投稿しました。 com/search?q =ユーザー%3A3152130 + Chart ++ポストペイント)を使用してグラフに描画します。 - また、__Never__は 'control.CreateGraphics'を使用します!コントロールの 'PrePaint'や' PostPaint'イベントで、 'e.ChartGraphics.Graphics'パラメータを使用します。 - 作成したペンや他のgdiオブジェクトも処分します。そして、チャートの座標系について理解してください! (ヒント:それらのうち__3__があります!!) – TaW

+1

C#は多くの多くのUI技術に使用されています。将来、使用しているUI技術を示すタグを含めてください。 –

+0

Nvmいつも呼ばれています。コードを更新します。それはatm働いていない。 –

答えて

0

することができますこの楽しいものを使うPointsを描くにDataPointsを変換するction:

Point PointFromDataPoint(Chart chart, ChartArea ca, DataPoint pt) 
{ 
    Axis ax = chart2.ChartAreas[0].AxisX; 
    Axis ay = chart2.ChartAreas[0].AxisY; 
    int x = (int)ax.ValueToPixelPosition(pt.XValue);      
    int y = (int)ay.ValueToPixelPosition(pt.YValues[0]); 
    return new Point(x, y); 
} 

あなたは2 DataPointsを設定している場合(!!)pontoantigopontoatualあなたがPrePaintイベントを書き込むことができます。ここでは

private void chart1_PrePaint(object sender, ChartPaintEventArgs e) 
{ 
    using (Pen pen = new Pen(Color.Green, 2f)) 
     e.ChartGraphics.Graphics.DrawLine(pen, 
      PointFromDataPoint(chart1, chart1.ChartAreas[0], pontoantigo), 
      PointFromDataPoint(chart1, chart1.ChartAreas[0], pontoatual)); 
} 

this little postを組み合わせた結果であります2つをこのように設定します。

DataPoint pontoantigo = chart1.Series[0].Points.First(); 
DataPoint pontoatual = chart1.Series[0].Points.Last(); 

enter image description here

+1

それは奇妙な催眠術です... –

関連する問題