赞
踩
先从官网给的demo开始看,最简单的demo: columnSimple 图如下:
代码如下:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
-
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>amCharts examples</title>
- <link rel="stylesheet" href="style.css" type="text/css">
- <script src="../amcharts/amcharts.js" type="text/javascript"></script>
- <script type="text/javascript">
- var chart;
- var chartData = [{
- country: "USA",
- visits: 4025
- }, {
- 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
- }];
-
-
- AmCharts.ready(function () {
- chart = new AmCharts.AmSerialChart();//创建一个AmSerialChart对象
- chart.dataProvider = chartData;//dataProvider 数据提供的对象 对应上面的json数据
- chart.categoryField = "country";//X轴上的值
- chart.startDuration = 1;
-
- //X轴
- var categoryAxis = chart.categoryAxis;
- //categoryAxis.labelRotation =45 ;//X轴下面值的角度

- //Y轴
- var graph = new AmCharts.AmGraph();
- graph.valueField = "visits";//柱形图显示的值
- graph.balloonText = "[[category]]: [[value]]";//鼠标放上所显示的
- graph.type = "column";//Y轴的类型 有line、column、step、smoothedLine、candlestick、ohlc
- graph.lineAlpha = 0;//border透明度
- graph.fillAlphas = 0.8;//柱子的透明度
- chart.addGraph(graph);
- chart.write("chartdiv");
- });
- </script>
- </head>
-
- <body>
- <div id="chartdiv" style="width: 100%; height: 400px;"></div>
- </body>
- </html>

AmSerialChart 是画地域图、柱形图 等等的 对象。
AmGraph 是画Y轴的对象。
如果你想不同的柱子加 不同的颜色的话 需要做2件事:
1.把json数据多加一个属性 ex:{country:"USA",visits:4025,color: "#F8FF01"}
2.在Y轴的对象上添加一个颜色属性 graph.colorField = "color";
这样就可以得到不同颜色。
第2个demo是双柱形图,如图:
- var chart;
-
- var chartData = [{
- dealer: "一月",
- a: 2.6,
- b: 2.7,
-
- }, {
- dealer: "二月",
- a: 2.6,
- b: 2.7,
-
-
- }, {
- dealer: "三月",
- a: 2.6,
- b: 2.7,
-
-
- }, {
- dealer: "四月",
- a: 2.6,
- b: 2.7,
-
-
- }];
-
- AmCharts.ready(function () {
- // SERIALL CHART
- chart = new AmCharts.AmSerialChart();
- chart.dataProvider = chartData;
- chart.categoryField = "dealer";
- //chart.rotate = true;//把柱形图变成横向
-
- var categoryAxis = chart.categoryAxis;
- categoryAxis.gridAlpha = 0.1;//数格的边线透明度
- categoryAxis.axisAlpha = 0.4;//X轴的透明度
- categoryAxis.inside=false;//X轴数值在图里面还是瓦面
- categoryAxis.position = "bottom";//X轴位置
-
- var valueAxis = new AmCharts.ValueAxis();
- //valueAxis.stackType = "regular";
- valueAxis.gridAlpha = 0;
- valueAxis.axisAlpha = 0.4;
- chart.addValueAxis(valueAxis);
-
- // firstgraph
- var graph = new AmCharts.AmGraph();
- graph.title = "注册用户";
- //graph.labelText = "[[value]]";
- graph.valueField = "a";//跟json数据中a相对应
- graph.balloonText="[[title]]:[[value]]人" //鼠标放上显示的值
- graph.type = "column";
- graph.lineAlpha = 1;
- graph.fillAlphas = 1;
- graph.lineColor = "#83c417";
- chart.addGraph(graph);
-
- // second graph
- graph = new AmCharts.AmGraph();
- graph.title = "停用用户";
- //graph.labelText = "[[value]]";
- graph.valueField = "b";
- graph.balloonText="[[title]]:[[value]]人"
- graph.type = "column";
- graph.lineAlpha = 1;
- graph.fillAlphas = 1;
- //graph.fillColors="#ffc92e"
- graph.lineColor = "#f27324";
- chart.addGraph(graph);
-

- //控制柱子上鼠标事件的代码,修改tip的样式
- var balloon = chart.balloon;
- balloon.adjustBorderColor = true;
- balloon.horizontalPadding = 10;
- balloon.fontSize = 14;
- balloon.pointerWidth= 5;
- balloon.color = "#000000";
- balloon.cornerRadius = 0;
- balloon.borderThickness=1;
- balloon.borderColor="#999999";
- balloon.borderAlpha=1;
- balloon.fillColor = "#FFFFFF";
-
-
- // LEGEND 用来显示下面分组的代码
- var legend = new AmCharts.AmLegend();
- legend.position = "bottom";
- legend.borderAlpha = 0;
- legend.horizontalGap = 10;
- legend.switchType = "v";//图片上面默认显示对号
- chart.addLegend(legend);
-
- // WRITE
- chart.write("chartdiv");
- });

amCharts.AmLegend() 对象 用来生成分组。
只需要增加valueAxis.stackType = "regular"; 代码就可以了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。