C#:绘制自己的条形图
我试图通过C#:绘制自己的条形图,c#,.net,graphics,drawing,C#,.net,Graphics,Drawing,我试图通过C绘制一个简单的条形图,但我从未尝试过图形和绘图名称空间。我曾想过生成一个“开始”和“结束”图形,然后以某种方式重复一个图像(以显示“长度”),但我不知道如何做到这一点 如果您能为我指出正确的方向和/或如果您有这样做的示例代码,我将非常高兴。为什么不试试呢。构建(和测试)您自己的图形控件所需的时间太长了我必须同意Eros。有很多非常好的图形库可以实现您想要的功能。我遇到的最好的: -甚至还有一个Visual Studio和一个好的 -这是基于jQuery的,非常适合web应用 -简单
C
绘制一个简单的条形图,但我从未尝试过图形和绘图名称空间。我曾想过生成一个“开始”和“结束”图形,然后以某种方式重复一个图像(以显示“长度”),但我不知道如何做到这一点
如果您能为我指出正确的方向和/或如果您有这样做的示例代码,我将非常高兴。为什么不试试呢。构建(和测试)您自己的图形控件所需的时间太长了我必须同意Eros。有很多非常好的图形库可以实现您想要的功能。我遇到的最好的:
- -甚至还有一个Visual Studio和一个好的
- -这是基于jQuery的,非常适合web应用
- -简单的API,甚至是
void Panel1Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int objCount = 5;
for (int n=0; n<objCount; n++)
{
g.FillRectangle(Brushes.AliceBlue, 0, n*(panel1.Height/objCount),
panel1.Width/(n+1), panel1.Height/objCount);
g.DrawRectangle(new Pen(Color.Black), 0, n*(panel1.Height/objCount),
panel1.Width/(n+1), panel1.Height/objCount);
g.DrawString(n.ToString(), new Font("Arial", 10f), Brushes.Black,
2, 2+n*(panel1.Height/objCount));
}
}
void Panel1Paint(对象发送器,PaintEventArgs e)
{
图形g=e.图形;
int objCount=5;
对于(int n=0;n基于Paul Sasik的回复,我创建了一个稍微不同的简单条形图。我希望这可能会帮助其他人
void DrawTheBar(int[] Values, Panel p)
{
//configuration of the bar chart
int barWidth = 20; //Width of the bar
int barSpace = 5; //Space between each bars.
int BCS = 5; //Background cell size
float fontSize = 8;
string fontFamily = "Arial";
Color bgColor = Color.White;
Brush fillColor = Brushes.CadetBlue;
Color borderColor = Color.Black;
Color bgCellColor = Color.Silver;
Brush textColor = Brushes.Black;
//do some magic here...
p.BackColor = bgColor; //set the bg color on panel
Graphics g = p.CreateGraphics();
g.Clear(bgColor); //cleans up the previously draw pixels
int Xpos = barSpace; //initial position
int panelHeight = panel1.Height-1; //fix panel height for drawing border
//Drawing rectangles for background.
for(int c = 0; c < Convert.ToInt16(panel1.Width / BCS); c++)
{
for(int r = 0; r < Convert.ToInt16(panel1.Height / BCS); r++)
{
g.DrawRectangle(new Pen(bgCellColor), c * BCS, r * BCS, BCS, BCS);
}
}
//Drawing the bars
for(int i = 0; i < Values.Length; i++)
{
//Drawing a filled rectangle. X = Xpos; Y = ((panel Height - 1) - Actual value); Width = barWidth, Height = as value define it
g.FillRectangle(fillColor, Xpos, (panelHeight - Values[i]), barWidth, Values[i]);
//Draw a rectangle around the previously created bar.
g.DrawRectangle(new Pen(borderColor), Xpos, (panelHeight - Values[i]), barWidth, Values[i]);
//Draw values over each bar.
g.DrawString(Values[i].ToString(), new Font(fontFamily, fontSize), textColor, (Xpos + 2), (panelHeight - Values[i]) - (fontSize + barSpace));
//calculate the next X point for the next bar.
Xpos += (barWidth + barSpace);
}
//here you will be happy with the results. :)
}
void绘图条(int[]值,面板p)
{
//条形图的配置
int barWidth=20;//条的宽度
int barSpace=5;//每个条之间的间距。
int BCS=5;//背景单元格大小
浮点数=8;
字符串fontfaliam=“Arial”;
颜色bgColor=颜色。白色;
画笔填充颜色=画笔.CadetBlue;
颜色边框颜色=颜色。黑色;
颜色bgCellColor=颜色。银色;
画笔文本颜色=画笔。黑色;
//在这里施展魔法。。。
p、 BackColor=bgColor;//设置面板上的bg颜色
图形g=p.CreateGraphics();
g、 清除(bgColor);//清除以前绘制的像素
int Xpos=barSpace;//初始位置
int-panelHeight=panel1.Height-1;//固定图形边框的面板高度
//为背景绘制矩形。
对于(int c=0;c
使用简单循环有什么问题?