To add text labels, call g.DrawString
.
要添加文本标签,请调用g.DrawString。
EDIT: To make the textvertical like your image, rotate the Graphics object to angle + sweep / 2
, and draw your text. To make it draw downward, yopu may be able to draw it in a small width and rely on character wrapping; if that doesn't work, draw it character vy chaaracter and use g.MeasureString
to figure out where to put the next character.
编辑:要使textvertical像图像一样,将Graphics对象旋转到angle + sweep / 2,然后绘制文本。为了使它向下绘制,yopu可能能够以小的宽度绘制它并依靠字符包裹;如果这不起作用,绘制字符vy chaaracter并使用g.MeasureString来确定下一个字符的放置位置。
To rotate the entire chart, call g.RotateTransform
with an angle in degrees before drawing it. EDIT: like this:
要旋转整个图表,请在绘制之前以度数角度调用g.RotateTransform。编辑:像这样:
private void DrawPieChart()
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
Rectangle rect = new Rectangle(0, 0, 300, 300);
float angle = 0;
Random random = new Random();
int sectors = 24;
int sweep = 360 / sectors;
g.RotateTransform(90); //Rotates by 90 degrees
for(int i=0; i<24;i++)
{
Color clr = Color.FromArgb(random.Next(0, 255),random.Next(0, 255), random.Next(0, 255));
g.FillPie(new SolidBrush(clr), rect, angle, sweep);
angle += sweep;
}
g.Dispose();
}
TO animate the rotation, make a field for the angle, increment it on a timer, and pass the field to g.RotateTransform
.
要为旋转设置动画,请为角度创建一个字段,在计时器上增加该字段,然后将该字段传递给g.RotateTransform。
Also, the correct way to draw things is to handle the control's Paint
event, and draw using e.Graphics
. Then, when you want to redraw it, call Invalidate
. To prevent flickering, call this.SetStyle(ControlStyles.DoubleBuffer, true);
in the constructor.
另外,绘制事物的正确方法是处理控件的Paint事件,并使用e.Graphics绘制。然后,当您想重绘它时,请调用Invalidate。要防止闪烁,请调用this.SetStyle(ControlStyles.DoubleBuffer,true);在构造函数中。