折线图形的数据库设计:

id  int

name  varchar(50)

dataTime datetime

饼形图和柱状图使用同一个表的数据:

id   int

name    varchar(50)

shuliang    int

下面的ChartControl1、ChartControl2、ChartControl3都是从工具箱拖进页面的webChart控件的ID

1、折线图代码:(实现的为当年的按月份的趋势)

定义颜色:

1
2
3 private string[] myColor = new string[]
4
5 {
6
7 "Tomato",//西红柿
8  
9 "Black",
10
11 "Gold",
12
13 "Blue",
14
15 "Green",
16
17 "Orange",
18
19 "Pink",//粉红
20
21 "Violet",//紫罗兰
22
23 "Orchid",//淡紫色
24
25 "Lime",//亮绿
26
27 "Tan",//茶色
28
29 "Red",
30
31 "Navy"//橘红
32
33 };

给webChart绑定数据:

1 private void GetLineChart()
2
3 {
4
5 //创建折线对象
6
7 LineChart myLineChart = new LineChart();
8
9 myLineChart.Line.Color = Color.FromName(myColor[0]);
10
11 myLineChart.Fill.Color = Color.FromName(myColor[0]);
12
13 myLineChart.LineMarker = new DiamondLineMarker(8,Color.FromName(myColor[0]),Color.FromName(myColor[0]));
14
15 myLineChart.DataSource = GetData().DefaultView;//GetData()方法见下
16
17 myLineChart.DataXValueField = "mMonth";
18
19 myLineChart.DataYValueField = "count";
20
21 myLineChart.DataLabels.Visible = true;
22
23 myLineChart.Legend = "2011年";
24
25 myLineChart.DataBind();
26
27 //myLineChart.Data.Add(new ChartPoint("一",float.Parse("20"))); 此注释部分为添加静态数据
28
29 //myLineChart.Data.Add(new ChartPoint("二",float.Parse("40")));
30
31 //myLineChart.Data.Add(new ChartPoint("3", float.Parse("50")));
32
33 //myLineChart.Data.Add(new ChartPoint("4", float.Parse("30")));
34
35 //this.ChartControl1.YCustomEnd = 60;
36
37 //this.ChartControl1.YValuesInterval = 10;
38
39 this.ChartControl1.Charts.Add(myLineChart);
40
41 this.ChartControl1.RedrawChart();
42
43 }
44
45
46
47
48
49 private DataTable GetData()
50
51 {
52
53 Class1 c = new Class1();
54
55 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";
56
57 //此处的sql语句有点……
58
59 SqlCommand cmd = new SqlCommand(sql, c.Conn);
60
61 DataSet ds = new DataSet();
62
63 SqlDataAdapter da = new SqlDataAdapter(cmd);
64
65 da.Fill(ds);
66
67 DataTable dt = ds.Tables[0];
68
69 return dt;
70
71 }

2、柱状图的代码:

1 private void bindchart()
2
3 {
4
5 DataTable dt = this.getdt();//getdt()方法见下
6
7 if (dt != null)
8
9 {
10
11 if (dt.Rows.Count > 0)
12
13 {
14
15 //遍历DataTable为每条记录生成一个柱状
16
17 for (int i = 0; i < dt.Rows.Count; i++)
18
19 {
20
21 //创建对象
22
23 ColumnChart mychart = new ColumnChart();
24
25 //设置柱子宽度
26
27 mychart.MaxColumnWidth = 24;
28
29 //颜色
30
31 mychart.Fill.Color = Color.FromName(this.myColor[i]);
32
33 //在柱子上显示数量
34
35 mychart.DataLabels.Visible = true;
36
37 //数量的字体
38
39 mychart.DataLabels.Font = new Font("Verdana", 14);
40
41 //dt.Rows[i]["name"].ToString()
42
43 //添加
44
45 mychart.Data.Add(new ChartPoint("",float.Parse(dt.Rows[i]["shuliang"].ToString())));
46
47 //备注
48
49 mychart.Legend = dt.Rows[i]["name"].ToString();
50
51
52
53 this.ChartControl2.Charts.Add(mychart);
54
55 }
56
57 //辅助设置
58
59 //背景色
60
61 ChartControl2.Background.Color = Color.FromArgb(165, 0, 16);
62
63 ChartControl2.YAxisFont.ForeColor = Color.FromArgb(165, 0, 16);
64
65 ChartControl2.XAxisFont.ForeColor = Color.FromArgb(165, 0, 16);
66
67 //内部线条
68
69 ChartControl2.Border.Color = Color.FromArgb(200, 200, 200);
70
71 //边框样式
72
73 ChartControl2.BorderStyle = BorderStyle.None;
74
75 //y最大值
76
77 double max = double.Parse(dt.Compute("MAX(shuliang)", "").ToString());
78
79 //递增值
80
81 int intv = 2;
82
83
84
85 //数量小于16的情况
86
87 if (max < 16)
88
89 {
90
91 max = 16;
92
93 }
94
95 //大于16的情况
96
97 else
98
99 {
100
101 int intintv = int.Parse(Math.Ceiling(max / 8).ToString());
102
103 max += intv;
104
105 }
106
107
108
109 //设置Y轴终点值
110
111 //ChartControl2.YCustomEnd = int.Parse(max.ToString());
112
113 ChartControl2.YCustomEnd = 50;
114
115 //y递增值
116
117 ChartControl2.YValuesInterval = 5;
118
119
120
121 //生成
122
123 this.ChartControl2.RedrawChart();
124
125 }
126
127 }
128
129 }
130
131
132
133
134
135 private DataTable getdt()
136
137 {
138
139 Class1 c = new Class1();
140
141 string sql = "select * from chart";
142
143 SqlCommand cmd = new SqlCommand(sql,c.Conn);
144
145 DataSet ds = new DataSet();
146
147 SqlDataAdapter da = new SqlDataAdapter(cmd);
148
149 da.Fill(ds);
150
151 DataTable dt = ds.Tables[0];
152
153 return dt;
154
155 }
156
157

3、饼状图的代码:

1 private void GetPieChart()
2
3 {
4
5 PieChart c = new PieChart();
6
7 c.DataSource = getdt().DefaultView;//getdt()方法与柱状图为同一个
8
9 c.DataXValueField = "name";
10
11 c.DataYValueField = "shuliang";
12
13 c.Shadow.Visible = true;
14
15 c.DataLabels.ForeColor = System.Drawing.Color.Blue;
16
17 //c.Colors = new Color[] { Color.Red, Color.Blue, Color.Yellow, Color.Cyan, Color.AntiqueWhite, Color.RosyBrown };
18
19 c.DataLabels.Visible = true;
20
21 ChartControl1.Background.EndPoint = new Point(500, 350);
22
23 c.Explosion = 1;
24
25 c.DataBind();
26
27 ChartControl3.Charts.Add(c);
28
29 ChartControl3.RedrawChart();
30
31 }
32
33
34
35
36
37 private DataTable getdt()
38
39 {
40
41 Class1 c = new Class1();
42
43 string sql = "select * from chart";
44
45 SqlCommand cmd = new SqlCommand(sql,c.Conn);
46
47 DataSet ds = new DataSet();
48
49 SqlDataAdapter da = new SqlDataAdapter(cmd);
50
51 da.Fill(ds);
52
53 DataTable dt = ds.Tables[0];
54
55 return dt;
56
57 }

来自: http://hi.baidu.com/shenhui%5F1/blog/item/e1214f631cfbae31aa184c80.html

转载于:https://www.cnblogs.com/c3055/articles/2100545.html

【分享】asp.net WebChart 折线图、饼形图、柱状图相关推荐

  1. python画柱状图和折线图-Python读取Excel表格,并同时画折线图和柱状图的方法

    今日给大家分享一个Python读取Excel表格,同时采用表格中的数值画图柱状图和折线图,这里只需要几行代码便可以实. 首先我们需要安装一个Excel操作的库xlrd,这个很简单,在安装Python后 ...

  2. Python使用折线图、柱状图、热力图比较不同班级相同学号学生的成绩

    问题描述:有些学校的学号最后两位是根据入学成绩顺序排的,那么入学之后同学们的学习状态是否会有变化呢,入学成绩较好的同学是否能够一直保持优势呢,会不会有同学是高考时没有发挥好而入学之后才暴露出真实实力呢 ...

  3. python导入数据画折线图_Python读取Excel表格,并同时画折线图和柱状图的方法

    今日给大家分享一个Python读取Excel表格,同时采用表格中的数值画图柱状图和折线图,这里只需要几行代码便可以实. 首先我们需要安装一个Excel操作的库xlrd,这个很简单,在安装Python后 ...

  4. 使用python的matplotlib(pyplot)画折线图和柱状图

    使用python的matplotlib(pyplot)画折线图和柱状图 今天帮师兄赶在deadline之前画论文的图,现学现卖很是刺激,现把使用matplotlib的子库pyplot画折线图和柱状图的 ...

  5. Poi 如何使用Java和POI技术生成折线图,柱状图,饼状图导出到word文档

    这篇文章主要介绍POI生成图表并导出word文档的基本操作.主要介绍三种图表:折线图.柱状图.饼状图. 一.效果展示 使用Java和POI技术生成的折线图,柱状图,饼状图的效果如下图所示: 二.环境准 ...

  6. 用python读取excel数据、并作图_Python读取Excel表格,并同时画折线图和柱状图的方法...

    今日给大家分享一个Python读取Excel表格,同时采用表格中的数值画图柱状图和折线图,这里只需要几行代码便可以实. 首先我们需要安装一个Excel操作的库xlrd,这个很简单,在安装Python后 ...

  7. 柱状图和折线图_SCI论文写作中折线图和柱状图的区别

    编者按 科研人SCI由洪堡学者,香港大学,中科院等多单位的博士团队联合打造,专注于科研论文写作和科研咨询,欢迎大家点击上方蓝字科研人SCI关注我们. SCI论文写作中折线图和柱状图的区别 在SCI论文 ...

  8. python雷达和柱形图_Python Pygal常见数据图(折线图、柱状图、饼图、点图、仪表图和雷达图)详解...

    Pygal 同样支持各种不同的数据图,比如饼图.折线图等.Pygal 的设计很好,不管是创建哪种数据图,Pygal 的创建方式基本是一样的,都是先创建对应的数据图对象,然后添加数据,最后对数据图进行配 ...

  9. MATLAB | 绘图复刻(二) | 折线图+误差棒+柱状图+散点抖动+灰色背景+图片叠加

    看到gzh R语言ggplot2科研绘图发布了一篇绘图复刻类文章,复刻了: Nature(IF=49.962)文章(Gut microbiota modulates weight gain in mi ...

最新文章

  1. linux uname 命令详解
  2. python切片语法-Python新手学习基础之数据类型——字符串的切片截取
  3. python是不是特别垃圾-Python是垃圾?(转)
  4. Oracle 原理:数据装载 ,SQLldr ,外部表
  5. SpringBoot入门:新一代Java模板引擎Thymeleaf(实践)
  6. 公司要禁止QQ?【我们从协议开始分析】
  7. python 运算及注释
  8. css加了固定定位就不显示内容_前端开发必备,学好”定位“向菜鸟说拜拜
  9. zabbix的b编译安装
  10. Spring Boot 使用 Log4j2
  11. MySQL解决中文编码问题
  12. 在北京买车可以上外地牌照吗
  13. Keyword Spotting (KWS) | Deep Spoken Keyword Spotting: An Overview
  14. 深度学习推荐系统之wide deep介绍和代码实现
  15. Android Software
  16. 【软件测试】公司招个测试员,我又面试了100多人,结局......
  17. 最少操作次数(英雄会)
  18. vue路由懒加载的两种方式
  19. html 修改浏览器图标大小设置,如何在网页中插入、编辑图像和调整其大小
  20. 浏览淘宝网页背后所发生的事情

热门文章

  1. 【有利可图网】PS实战系列:制作树林与文字结合的创意文字效果
  2. nativefier(一行代码将任意网页转化为桌面应用)
  3. 联发科的10核Helio X20处理器
  4. Codeforces Round #439 (Div. 2)C - The Intriguing Obsession(简单dp)
  5. 用matlab如何拟合曲线,用MATLAB怎么实现曲线拟合?
  6. php date.getday,getDay()方法_用法示例-javascript词典-js编程词典-php中文网
  7. 混合模式程序集是针对“v1.1.4322”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
  8. 私域运营第五讲:实体店私域流量拉新实体餐饮店如何通过搭建私域流量实现营收增长
  9. keep-alive 的作用及使用场景
  10. python与人工智能:KNN近邻法识别手写数字