如何绘制折线图

li8136 2007-01-25 10:26:45
我有一个二维数组,想根据此数组绘制折线图,请问大侠们该怎么办?
...全文
1645 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyactive 2007-01-27
  • 打赏
  • 举报
回复
class ChartComp
{
public Bitmap curBitmap;
public ArrayList ptsArrayList =
new ArrayList();
public float X0 = 0, Y0 = 0;
public float chartX, chartY;
public Color chartColor = Color.Gray;
// chartType: 1=Line, 2=Pie, 3=Bar.
// For future use only.
public int chartType = 1;
private int Width, Height;
private Graphics g;
private Page curPage;
struct ptStructure
{
public float x;
public float y;
public Color clr;
}
// ChartComp constructor
public ChartComp(int cType, Color cColor,
int cWidth, int cHeight, Page cPage)
{
Width = cWidth;
Height = cHeight;
chartX = cWidth;
chartY = cHeight;
curPage = cPage;
chartType = cType;
chartColor = cColor;
curBitmap = new Bitmap(Width, Height);
g = Graphics.FromImage(curBitmap);
}
// Destructor. Disposes of objects.
~ChartComp()
{
curBitmap.Dispose();
g.Dispose();
}
// InsertPoint method. Adds a point
// to the array.
public void InsertPoint(int xPos,
int yPos, Color clr)
{
ptStructure pt;
pt.x = xPos;
pt.y = yPos;
pt.clr = clr;
// Add the point to the array
ptsArrayList.Add(pt);
}
public void InsertPoint(int position,
int xPos, int yPos, Color clr)
{
ptStructure pt;
pt.x = xPos;
pt.y = yPos;
pt.clr = clr;
// Add the point to the array
ptsArrayList.Insert(position, pt);
}
// Draw methods
public void DrawChart()
{
int i;
float x, y, x0, y0;
curPage.Response.ContentType="image/jpeg";
g.SmoothingMode = SmoothingMode.HighQuality;
g.FillRectangle(new SolidBrush(chartColor),
0, 0, Width, Height);
int chWidth = Width-80;
int chHeight = Height-80;
g.DrawRectangle(Pens.Black,
40, 40, chWidth, chHeight);
g.DrawString("GDI+ Chart", new Font("arial",14),
Brushes.Black, Width/3, 10);
// Draw x- and y-axis line, points, positions
for(i=0; i<=5; i++)
{
x = 40+(i*chWidth)/5;
y = chHeight+40;
string str = (X0 + (chartX*i/5)).ToString();
g.DrawString(str, new Font("Verdana",10),
Brushes.Blue, x-4, y+10);
g.DrawLine(Pens.Black, x, y+2, x, y-2);
}
for(i=0; i<=5; i++)
{
x = 40;
y = chHeight+40-(i*chHeight/5);
string str = (Y0 + (chartY*i/5)).ToString();
g.DrawString(str, new Font("Verdana",10),
Brushes.Blue, 5, y-6);
g.DrawLine(Pens.Black, x+2, y, x-2, y);
}
// Transform coordinates so that point (0,0)
// is in the lower left corner
g.RotateTransform(180);
g.TranslateTransform(-40, 40);
g.ScaleTransform(-1, 1);
g.TranslateTransform(0, -(Height));
// Draw all points from the array
ptStructure prevPoint = new ptStructure();
foreach(ptStructure pt in ptsArrayList)
{
x0 = chWidth*(prevPoint.x-X0)/chartX;
y0 = chHeight*(prevPoint.y-Y0)/chartY;
x = chWidth*(pt.x-X0)/chartX;
y = chHeight*(pt.y-Y0)/chartY;
g.DrawLine(Pens.Black, x0, y0, x, y);
g.FillEllipse(new SolidBrush(pt.clr),
x0-5, y0-5, 10, 10);
g.FillEllipse(new SolidBrush(pt.clr),
x-5, y-5, 10, 10);
prevPoint = pt;
}
curBitmap.Save(curPage.Response.OutputStream,
ImageFormat.Jpeg);
}
}

private void Button1_Click(object sender,
System.EventArgs e)
{
// Get the chart background color
Color clr = Color.FromName(TextBox3.Text);
// Create a ChartComp object
ChartComp chart =
new ChartComp(1, clr, 400, 300, this.Page);
chart.X0 = 0;
chart.Y0= 0;
chart.chartX = Convert.ToInt16(TextBox1.Text);
chart.chartY = Convert.ToInt16(TextBox2.Text);
// Add points to the chart
chart.InsertPoint(Convert.ToInt16(TextBox4.Text),
Convert.ToInt16(TextBox5.Text),
Color.FromName(TextBox6.Text) );
chart.InsertPoint(Convert.ToInt16(TextBox7.Text),
Convert.ToInt16(TextBox8.Text),
Color.FromName(TextBox9.Text) );
chart.InsertPoint(Convert.ToInt16(TextBox10.Text),
Convert.ToInt16(TextBox11.Text),
Color.FromName(TextBox12.Text) );
chart.InsertPoint(Convert.ToInt16(TextBox13.Text),
Convert.ToInt16(TextBox14.Text),
Color.FromName(TextBox15.Text) );
chart.InsertPoint(Convert.ToInt16(TextBox16.Text),
Convert.ToInt16(TextBox17.Text),
Color.FromName(TextBox18.Text) );
// Draw chart
chart.DrawChart();
}

根据这个就能绘出
lxmfll2000 2007-01-26
  • 打赏
  • 举报
回复
用RDLC。
dahuzizyd 2007-01-26
  • 打赏
  • 举报
回复
ZedGraph:
http://www.cnblogs.com/dahuzizyd/archive/2007/01/16/ZedGraph_Dynamic_Line_Chart.html

4,816

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧