折线图形的数据库设计:
id int
name varchar(50)
dataTimedatetime
饼形图和柱状图使用同一个表的数据:
id int
name varchar(50)
shuliang int
下面的ChartControl1、ChartControl2、ChartControl3都是从工具箱拖进页面的webChart控件的ID
1、折线图代码:(实现的为当年的按月份的趋势)
定义颜色:
private string[] myColor = new string[]
{
"Tomato",//西红柿
"Black",
"Gold",
"Blue",
"Green",
"Orange",
"Pink",//粉红
"Violet",//紫罗兰
"Orchid",//淡紫色
"Lime",//亮绿
"Tan",//茶色
"Red",
"Navy"//橘红
};
给webChart绑定数据:
private void GetLineChart()
{
//创建折线对象
LineChart myLineChart = new LineChart();
myLineChart.Line.Color = Color.FromName(myColor[0]);
myLineChart.Fill.Color = Color.FromName(myColor[0]);
myLineChart.LineMarker = new DiamondLineMarker(8,Color.FromName(myColor[0]),Col or.FromName(myColor[0]));
myLineChart.DataSource = GetData().DefaultView;//GetData()方法见下
myLineChart.DataXValueField = "mMonth";
myLineChart.DataYValueField = "count";
myLineChart.DataLabels.Visible = true;
myLineChart.Legend = "2011年";
myLineChart.DataBind();
//myLineChart.Data.Add(new ChartPoint("一",float.Parse("20"))); 此注释部分为添加静态数据
//myLineChart.Data.Add(new ChartPoint("二",float.Parse("40")));
//myLineChart.Data.Add(new ChartPoint("3", float.Parse("50")));
//myLineChart.Data.Add(new ChartPoint("4", float.Parse("30")));
//this.ChartControl1.YCustomEnd = 60;
//this.ChartControl1.YValuesInterval = 10;
this.ChartControl1.Charts.Add(myLineChart);
this.ChartControl1.RedrawChart();
}
private DataTable GetData()
{
Class1 c = new Class1();
string sql = "select * from(SELECT 1 AS 'mMonth'UNION SELECT 2 AS 'mMonth'UNION SELECT 3 AS 'mMonth'UNION SELECT 4 AS 'mMonth'UNION SELECT 5 AS 'mMonth'UNION SELECT 6 AS 'mMonth'UNION SELECT 7 AS 'mMonth'UNION SELECT 8 AS 'mMonth'UNION SELECT 9 AS 'mMonth' UNION SELECT 10 AS 'mMonth'UNION SELECT 11 AS 'mMonth'UNION SELECT 12 AS 'mMonth') as m left join(select count(dataTime) as [count],month(dataTime) as monthTime from lineChart where year(dataTime)=DATEPART(year, getDate()) group By month(dataTime)) b on m.mMonth = b.monthTime";
//此处的sql语句有点…… SqlCommand cmd = new SqlCommand(sql, c.Conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); DataTable dt = ds.Tables[0]; return dt; } 2、 天梭手表 天梭 柱状图的代码:
private void bindchart()
{
DataTable dt = this.getdt();//getdt()方法见下
if (dt != null)
{
if (dt.Rows.Count > 0)
{
//遍历DataTable为每条记录天生一个柱状
for (int i = 0; i < dt.Rows.Count; i++)
{
//创建对象
ColumnChart mychart = new ColumnChart();
//设置柱子宽度
mychart.MaxColumnWidth = 24;
//颜色
mychart.Fill.Color = Color.FromName(this.myColor[i]);
//在柱子上显示数目
mychart.DataLabels.Visible = true;
//数目的字体
mychart.DataLabels.Font = new Font("Verdana", 14);
//dt.Rows[i]["name"].ToString()
//添加
mychart.Data.Add(new ChartPoint("",float.Parse(dt.Rows[i]["shuliang"].T oString())));
//备注
mychart.Legend = dt.Rows[i]["name"].ToString();
this.ChartControl2.Charts.Add(mychart);
}
//辅助设置
//背景色
ChartControl2.Background.Color = Color.FromArgb(165, 0, 16);
ChartControl2.YAxisFont.ForeColor = Color.FromArgb(165, 0, 16);
ChartControl2.XAxisFont.ForeColor = Color.FromArgb(165, 0, 16);
//内部线条
ChartControl2.Border.Color = Color.FromArgb(200, 200, 200);
//边框样式
ChartControl2.BorderStyle = BorderStyle.None;
//y最大值
double max = double.Parse(dt.Compute("MAX(shuliang)", "").ToString());
//递增值
int intv = 2;
//数目小于16的情况
if (max < 16)
{
max = 16;
}
//大于16的情况
else
{
int intintv = int.Parse(Math.Ceiling(max / 8).ToString());
max += intv;
}
//设置Y轴终点值
//ChartControl2.YCustomEnd = int.Parse(max.ToString());
ChartControl2.YCustomEnd = 50;
//y递增值
ChartControl2.YValuesInterval = 5; //天生 this.ChartControl2.RedrawChart(); } } } private DataTable getdt() { Class1 c = new Class1(); string sql = "select * from chart"; SqlCommand cmd = new SqlCommand(sql,c.Conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); DataTable dt = ds.Tables[0]; return dt; } 3、饼状图的代码:
private void GetPieChart()
{
PieChart c = new PieChart();
c.DataSource = getdt().DefaultView;//getdt()方法与柱状图为同一个
c.DataXValueField = "name";
c.DataYValueField = "shuliang";
c.Shadow.Visible = true;
c.DataLabels.ForeColor = System.Drawing.Color.Blue;
//c.Colors = new Color[] { Color.Red, Color.Blue, Color.Yellow, Color.Cyan, Color.AntiqueWhite, Color.RosyBrown };
c.DataLabels.Visible = true;
ChartControl1.Background.EndPoint = new Point(500, 350);
c.Explosion = 1;
c.DataBind();
ChartControl3.Charts.Add(c);
ChartControl3.RedrawChart();
}
private DataTable getdt()
{
Class1 c = new Class1();
string sql = "select * from chart";
SqlCommand cmd = new SqlCommand(sql,c.Conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataTable dt = ds.Tables[0];
return dt;
id int
name varchar(50)
dataTimedatetime
饼形图和柱状图使用同一个表的数据:
id int
name varchar(50)
shuliang int
下面的ChartControl1、ChartControl2、ChartControl3都是从工具箱拖进页面的webChart控件的ID
1、折线图代码:(实现的为当年的按月份的趋势)
定义颜色:
private string[] myColor = new string[]
{
"Tomato",//西红柿
"Black",
"Gold",
"Blue",
"Green",
"Orange",
"Pink",//粉红
"Violet",//紫罗兰
"Orchid",//淡紫色
"Lime",//亮绿
"Tan",//茶色
"Red",
"Navy"//橘红
};
给webChart绑定数据:
private void GetLineChart()
{
//创建折线对象
LineChart myLineChart = new LineChart();
myLineChart.Line.Color = Color.FromName(myColor[0]);
myLineChart.Fill.Color = Color.FromName(myColor[0]);
myLineChart.LineMarker = new DiamondLineMarker(8,Color.FromName(myColor[0]),Col or.FromName(myColor[0]));
myLineChart.DataSource = GetData().DefaultView;//GetData()方法见下
myLineChart.DataXValueField = "mMonth";
myLineChart.DataYValueField = "count";
myLineChart.DataLabels.Visible = true;
myLineChart.Legend = "2011年";
myLineChart.DataBind();
//myLineChart.Data.Add(new ChartPoint("一",float.Parse("20"))); 此注释部分为添加静态数据
//myLineChart.Data.Add(new ChartPoint("二",float.Parse("40")));
//myLineChart.Data.Add(new ChartPoint("3", float.Parse("50")));
//myLineChart.Data.Add(new ChartPoint("4", float.Parse("30")));
//this.ChartControl1.YCustomEnd = 60;
//this.ChartControl1.YValuesInterval = 10;
this.ChartControl1.Charts.Add(myLineChart);
this.ChartControl1.RedrawChart();
}
private DataTable GetData()
{
Class1 c = new Class1();
string sql = "select * from(SELECT 1 AS 'mMonth'UNION SELECT 2 AS 'mMonth'UNION SELECT 3 AS 'mMonth'UNION SELECT 4 AS 'mMonth'UNION SELECT 5 AS 'mMonth'UNION SELECT 6 AS 'mMonth'UNION SELECT 7 AS 'mMonth'UNION SELECT 8 AS 'mMonth'UNION SELECT 9 AS 'mMonth' UNION SELECT 10 AS 'mMonth'UNION SELECT 11 AS 'mMonth'UNION SELECT 12 AS 'mMonth') as m left join(select count(dataTime) as [count],month(dataTime) as monthTime from lineChart where year(dataTime)=DATEPART(year, getDate()) group By month(dataTime)) b on m.mMonth = b.monthTime";
//此处的sql语句有点…… SqlCommand cmd = new SqlCommand(sql, c.Conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); DataTable dt = ds.Tables[0]; return dt; } 2、 天梭手表 天梭 柱状图的代码:
private void bindchart()
{
DataTable dt = this.getdt();//getdt()方法见下
if (dt != null)
{
if (dt.Rows.Count > 0)
{
//遍历DataTable为每条记录天生一个柱状
for (int i = 0; i < dt.Rows.Count; i++)
{
//创建对象
ColumnChart mychart = new ColumnChart();
//设置柱子宽度
mychart.MaxColumnWidth = 24;
//颜色
mychart.Fill.Color = Color.FromName(this.myColor[i]);
//在柱子上显示数目
mychart.DataLabels.Visible = true;
//数目的字体
mychart.DataLabels.Font = new Font("Verdana", 14);
//dt.Rows[i]["name"].ToString()
//添加
mychart.Data.Add(new ChartPoint("",float.Parse(dt.Rows[i]["shuliang"].T oString())));
//备注
mychart.Legend = dt.Rows[i]["name"].ToString();
this.ChartControl2.Charts.Add(mychart);
}
//辅助设置
//背景色
ChartControl2.Background.Color = Color.FromArgb(165, 0, 16);
ChartControl2.YAxisFont.ForeColor = Color.FromArgb(165, 0, 16);
ChartControl2.XAxisFont.ForeColor = Color.FromArgb(165, 0, 16);
//内部线条
ChartControl2.Border.Color = Color.FromArgb(200, 200, 200);
//边框样式
ChartControl2.BorderStyle = BorderStyle.None;
//y最大值
double max = double.Parse(dt.Compute("MAX(shuliang)", "").ToString());
//递增值
int intv = 2;
//数目小于16的情况
if (max < 16)
{
max = 16;
}
//大于16的情况
else
{
int intintv = int.Parse(Math.Ceiling(max / 8).ToString());
max += intv;
}
//设置Y轴终点值
//ChartControl2.YCustomEnd = int.Parse(max.ToString());
ChartControl2.YCustomEnd = 50;
//y递增值
ChartControl2.YValuesInterval = 5; //天生 this.ChartControl2.RedrawChart(); } } } private DataTable getdt() { Class1 c = new Class1(); string sql = "select * from chart"; SqlCommand cmd = new SqlCommand(sql,c.Conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); DataTable dt = ds.Tables[0]; return dt; } 3、饼状图的代码:
private void GetPieChart()
{
PieChart c = new PieChart();
c.DataSource = getdt().DefaultView;//getdt()方法与柱状图为同一个
c.DataXValueField = "name";
c.DataYValueField = "shuliang";
c.Shadow.Visible = true;
c.DataLabels.ForeColor = System.Drawing.Color.Blue;
//c.Colors = new Color[] { Color.Red, Color.Blue, Color.Yellow, Color.Cyan, Color.AntiqueWhite, Color.RosyBrown };
c.DataLabels.Visible = true;
ChartControl1.Background.EndPoint = new Point(500, 350);
c.Explosion = 1;
c.DataBind();
ChartControl3.Charts.Add(c);
ChartControl3.RedrawChart();
}
private DataTable getdt()
{
Class1 c = new Class1();
string sql = "select * from chart";
SqlCommand cmd = new SqlCommand(sql,c.Conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataTable dt = ds.Tables[0];
return dt;
}
转载自:http://blog.cnfol.com/ed090o/article/47932016.html