下面代码是我画条形图的代码,我想将其显示在panel容器控件上,请大侠指教一下该如何调用这个方法
private void DrawBar()
{
try
{
Graphics myG = this.CreateGraphics();
myG.Clear(Color.White);
Pen pen = new Pen(Color.Black);
pen.EndCap = LineCap.ArrowAnchor;
pen.DashStyle = DashStyle.Solid; //画坐标
Point pCenter = new Point(300, 350);
//x
myG.DrawLine(pen, new Point(pCenter.X - 200, pCenter.Y), new Point(pCenter.X + 100, pCenter.Y));
myG.DrawString("x", new Font("宋体", 9), Brushes.Black, new Point(pCenter.X + 103, pCenter.Y));
//y
myG.DrawLine(pen, new Point(pCenter.X - 200, pCenter.Y), new Point(pCenter.X - 200, pCenter.Y - 175));
myG.DrawString("y", new Font("宋体", 9), Brushes.Black, new Point(pCenter.X - 210, pCenter.Y - 175));
//画X轴上的表格
for (int i = 0; i < 10; i++)
{
myG.DrawLine(Pens.Black, new Point((pCenter.X - 200) + 25 * (i + 1), pCenter.Y), new Point((pCenter.X - 200) + 25 * (i + 1), pCenter.Y - 5));//x
} myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 25, pCenter.Y - 60, 25, 60);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 25, pCenter.Y - 60, 25, 60);
myG.DrawString("一月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 25, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 75, pCenter.Y - 100, 25, 100);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 75, pCenter.Y - 100, 25, 100);
myG.DrawString("二月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 75, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 125, pCenter.Y - 150, 25, 150);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 125, pCenter.Y - 150, 25, 150);
myG.DrawString("三月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 125, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 175, pCenter.Y - 125, 25, 125);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 175, pCenter.Y - 125, 25, 125);
myG.DrawString("四月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 175, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 225, pCenter.Y - 50, 25, 50);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 225, pCenter.Y - 50, 25, 50);
myG.DrawString("五月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 225, pCenter.Y + 10)); myG.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void DrawBar()
{
try
{
Graphics myG = this.CreateGraphics();
myG.Clear(Color.White);
Pen pen = new Pen(Color.Black);
pen.EndCap = LineCap.ArrowAnchor;
pen.DashStyle = DashStyle.Solid; //画坐标
Point pCenter = new Point(300, 350);
//x
myG.DrawLine(pen, new Point(pCenter.X - 200, pCenter.Y), new Point(pCenter.X + 100, pCenter.Y));
myG.DrawString("x", new Font("宋体", 9), Brushes.Black, new Point(pCenter.X + 103, pCenter.Y));
//y
myG.DrawLine(pen, new Point(pCenter.X - 200, pCenter.Y), new Point(pCenter.X - 200, pCenter.Y - 175));
myG.DrawString("y", new Font("宋体", 9), Brushes.Black, new Point(pCenter.X - 210, pCenter.Y - 175));
//画X轴上的表格
for (int i = 0; i < 10; i++)
{
myG.DrawLine(Pens.Black, new Point((pCenter.X - 200) + 25 * (i + 1), pCenter.Y), new Point((pCenter.X - 200) + 25 * (i + 1), pCenter.Y - 5));//x
} myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 25, pCenter.Y - 60, 25, 60);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 25, pCenter.Y - 60, 25, 60);
myG.DrawString("一月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 25, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 75, pCenter.Y - 100, 25, 100);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 75, pCenter.Y - 100, 25, 100);
myG.DrawString("二月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 75, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 125, pCenter.Y - 150, 25, 150);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 125, pCenter.Y - 150, 25, 150);
myG.DrawString("三月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 125, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 175, pCenter.Y - 125, 25, 125);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 175, pCenter.Y - 125, 25, 125);
myG.DrawString("四月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 175, pCenter.Y + 10)); myG.DrawRectangle(Pens.Blue, (pCenter.X - 200) + 225, pCenter.Y - 50, 25, 50);
myG.FillRectangle(new SolidBrush(Color.Blue), (pCenter.X - 200) + 225, pCenter.Y - 50, 25, 50);
myG.DrawString("五月", new Font("宋体", 9), Brushes.Black, new Point((pCenter.X - 200) + 225, pCenter.Y + 10)); myG.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货