[英]C# Draw Circle on top of control
我有一個帶有點的圖表(那些四軸飛行器圖像),我想在該點上繪制一個半徑為x的圓。 我不知道該怎么做。
碼:
for (int i = 0; i < drone.Length; i++)
{
var pos_atual = drone[i].posicao_atual;
var pos_desej = drone[i].posicao_desejada;
chart.Series[i].Points.Clear();
chart.Series[i].Points.AddXY(drone[i].pos_atual().X, drone[i].pos_atual().Y);
}
}
您需要編碼xxxPaint事件之一,也許像這樣:
private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
Series s = chart.Series[yourSeriesIndex];
int yourPointIndex = 4;
if (s.Points.Count < yourPointIndex) return;
DataPoint dp = s.Points[yourPointIndex];
ChartArea ca = chart.ChartAreas[0];
int x = (int) ca.AxisX.ValueToPixelPosition(dp.XValue);
int y = (int) ca.AxisY.ValueToPixelPosition(dp.YValues[0]);
// a circle with 20 pixels diameter
e.ChartGraphics.Graphics.DrawEllipse(Pens.Red, x-10, y-10, 20, 20);
}
作為替代方案,您可以將具有MarkerStyle Circle
, Color Transparent
和MarkerBorderColor Red
的Marker
添加到DataPoint
..:
Chart chart = TestChart;
Series s = chart.Series[0];
DataPoint dp = s.Points[5];
dp.MarkerStyle = MarkerStyle.Circle;
dp.MarkerSize = 20; // diameter in pixels
dp.MarkerColor = Color.Transparent;
dp.MarkerBorderColor = Color.Orange;
dp.MarkerBorderWidth = 2;
更新資料
從您的評論中我了解到,您想要的是設置圓的大小,而不是像素,而是數據點值 。
這也是可能的,並不難。 但是,它確實需要對Chart
規則有所了解。
開始了:
首先,我們定義所需的寬度和高度。 請注意我的數字與您的數字有很大不同,寬度和高度也不相同!!
double vx = 1d;
double vy = 20d;
接下來,我們計算大小; 它們是零與我們的值之間的差(!)。 這似乎很復雜,但請查看我的圖表:y軸從負數開始。 因此,簡單地從一個值獲取像素會產生很大的偏移。
int wx = (int) ( ca.AxisX.ValueToPixelPosition(vx) -
ca.AxisX.ValueToPixelPosition(0)) ;
int wy = (int) ( ca.AxisY.ValueToPixelPosition(vy) -
ca.AxisY.ValueToPixelPosition(0)) ;
通過這些數字,我們可以畫出一個圓圈:
e.ChartGraphics.Graphics.DrawEllipse(Pens.Red, x - wx / 2, y - wy / 2, wx, wy);
請注意,我們是如何開始將圓偏移量的邊界矩形向左和向左偏移 一半大小的!
現在,大小將遵循圖表大小:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.