Amcharts 入门教程

最近需要学习 Amcharts ,他的图表功能确实很强大,比原来用的jfreechart强大多了。但是网上搜索到的教程很少,

从开始学起的确有点不方便。于是我决定把我学习的觉得好的途径,以教程的方式写下来。

我写这个教程的目的只是为了让大家去学习怎么编写第一个简单的 Amcharts 小 demo,至于一些较深的,因为我也是刚接触,

也不好多说,万一误导大家就不好了,而且那些较深的网上也很好搜。好了废话不多说,开始吧。

首先需要下载资源包:

官网地址:www.amcharts.com写本教程时,本来想直接把给出下载的地址的,但是官网始终打不开。

或者可以去我的资源下载:http://download.youkuaiyun.com/detail/cdmamata/4886019

官方上有四个组件,其他的我们不管,我们只管 amcharts_x.x.x ,里面的 index.html 就是教程跟api,我也是根据这个学习的,虽然是 en 文的,但是看看还是不错的。

导入 amcharts js库

因为这是 java web project,所以我们把 amcharts 文件夹放到 webroot 下。在 jsp 页面中,导入 amcharts.js。

代码如下:

<html>
  <head>
	<title>My JSP 'index.jsp' starting page</title>
	
	<!-- 导入Amcharts js 库 -->
	<script src="amcharts/amcharts.js" type="text/javascript"></script>
	<script type="text/javascript">
		
		var fun1 = function(){
			var chartData = [{ country: "USA", visits: 4252 },
	                { country: "China", visits: 1882 },
	                { country: "Japan", visits: 1809 },
	                { country: "Germany", visits: 1322 },
	                { country: "UK", visits: 1122 },
	                { country: "France", visits: 1114 },
	                { country: "India", visits: 984 },
	                { country: "Spain", visits: 711 },
	                { country: "Netherlands", visits: 665 },
	                { country: "Russia", visits: 580 },
	                { country: "South Korea", visits: 443 },
	                { country: "Canada", visits: 441 },
	                { country: "Brazil", visits: 395 },
	                { country: "Italy", visits: 386 },
	                { country: "Australia", visits: 384 },
	                { country: "Taiwan", visits: 338 },
	                { country: "Poland", visits: 328}];
			
			//AmSerialChart 类
			var chart = new AmCharts.AmSerialChart();
			chart.dataProvider = chartData;		//指定数据源
			chart.categoryField = "country";	//数据的分类
			
			//创建
			var graph = new AmCharts.AmGraph();
			graph.valueField = "visits";	//数值字段名称
			graph.type = "column";			//列名称
			chart.addGraph(graph);
			
			chart.write(document.getElementById("chartContainer"));	//write 到 div 中
		}
	</script>
  </head>
  
  <body οnlοad="fun1()">
	<div id="chartContainer" style="width: 640px; height: 400px;"></div>
  </body>
</html>

效果如下:

图像


不太好看,接下来就美化一下,这是最后的代码

<html>
  <head>
	<title>My JSP 'index.jsp' starting page</title>
	
	<!-- 导入Amcharts js 库 -->
	<script src="amcharts/amcharts.js" type="text/javascript"></script>
	<script type="text/javascript">
		
		AmCharts.ready(function(){
			var chartData = [{ country: "USA", visits: 4252 },
	                { country: "China", visits: 1882 },
	                { country: "Japan", visits: 1809 },
	                { country: "Germany", visits: 1322 },
	                { country: "UK", visits: 1122 },
	                { country: "France", visits: 1114 },
	                { country: "India", visits: 984 },
	                { country: "Spain", visits: 711 },
	                { country: "Netherlands", visits: 665 },
	                { country: "Russia", visits: 580 },
	                { country: "South Korea", visits: 443 },
	                { country: "Canada", visits: 441 },
	                { country: "Brazil", visits: 395 },
	                { country: "Italy", visits: 386 },
	                { country: "Australia", visits: 384 },
	                { country: "Taiwan", visits: 338 },
	                { country: "Poland", visits: 328}];
			
			//AmSerialChart 类
			var chart = new AmCharts.AmSerialChart();
			chart.dataProvider = chartData;		//指定数据源
			chart.categoryField = "country";	//数据的分类
			
			
			//设置类别轴标签显示总数
			var catAxis = chart.categoryAxis;
			catAxis.gridCount = 18;
			catAxis.labelRotation = 90;
			//创建
			var graph = new AmCharts.AmGraph();
			graph.valueField = "visits";	//数值字段名称
			graph.type = "column";			//列名称
			chart.addGraph(graph);
			//填充3D
			graph.lineAlpha = 1;		//边框透明度
			graph.fillAlphas = 0.8;		//内部透明度
			graph.balloonText = "[[category]]: [[value]]";	//设置悬停显示内容
			
			chart.angle = 30;			//三维角度
			chart.depth3D = 15;			//三维深度
			
			chart.write(document.getElementById("chartContainer"));	//write 到 div 中
			
		});
	</script>
  </head>
  
  <body>
	<div id="chartContainer" style="width: 640px; height: 400px;"></div>
  </body>
</html>


最后的效果图



这里有一个问题,当div的宽度不够时,类别标签也会显示不全,正在查找解决方法










xml <!-- [xml] (xml / csv) 数据类型xml/csv--> ; <!-- 如果使用csv作为数据的话,需要使用这个属性;表示文件数据分隔符,(平常以";"和","为主) [;] (string) csv file data separator (you need it only if you are using csv file for your data) --> 1 <!-- 如果使用的是csv数据,可以设置跳过几行再显示数据,默认为0表示csv中的数据全部显示,大于n(n>0);表示前面n行都不显示[0] (Number) if you are using csv data type, you can set the number of rows which should be skipped here --> <!-- 设置系统中的字体[Arial] (font name) use device fonts, such as Arial, Times New Roman, Tahoma, Verdana... --> <!-- 设置所有文本的大小,默认为11,具体的文本的字体大小也可以在下面的设置中设置[11] (Number) text size of all texts. Every text size can be set individually in the settings below --> <!-- 同上[#000000] (hex color code) main text color. Every text color can be set individually in the settings below--> . <!-- 小数分隔符,默认为[,]注:该属性只是用来显示,而在csv数据文件中,必须使用[.] (string) decimal separator. Note, that this is for displaying data only. Decimals in data xml file must be separated with a dot --> <!-- 千位分隔符,默认为空[ ] (string) thousand separator. use "none" if you don't want to separate --> 3 <!-- 如果百分数格式的数字,后面的小数位小于该属性的值,则在小数后面加0补充。如54.2%,该属性设置为3,那么显示的效果为54.200%。[] (Number) if your value has less digits after decimal then is set here, zeroes will be added --> <!--设置科学记数法的最小值 [0.000001] If absolute value of your number is equal or less then scientific_min, this number will be form
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值