用amcharts動態生成柱狀圖

客戶端:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>柱狀圖例子</title>
        <link rel="stylesheet" href="${pageContext.request.contextPath }/charts/style.css" type="text/css">
        <script src="${pageContext.request.contextPath }/charts/amcharts.js" type="text/javascript"></script>
        <script src="${pageContext.request.contextPath }/js/jquery.min.js" type="text/javascript"></script>
        <script src="${pageContext.request.contextPath }/charts/serial.js" type="text/javascript"></script>

        <script>
	        var chartData = [];
	        var chart;
	        var legend;

            AmCharts.ready(function () {
                // SERIAL CHART
                chart = new AmCharts.AmSerialChart();
                chart.dataProvider = chartData;
                chart.categoryField = "product_no";//橫座標名字
                chart.startDuration = 1;

                // AXES
                // category
                var categoryAxis = chart.categoryAxis;
                categoryAxis.labelRotation = 90;
                categoryAxis.gridPosition = "start";

                // value
                // in case you don't want to change default settings of value axis,
                // you don't need to create it, as one value axis is created automatically.

                // GRAPH
                var graph = new AmCharts.AmGraph();
                graph.valueField = "money";//縱座標名字
                graph.balloonText = "[[product_no]]: <b>[[value]]</b>";//氣球
                graph.type = "column";//表示是一個柱狀圖
                graph.lineAlpha = 0;
                graph.fillAlphas = 0.8;
                chart.addGraph(graph);

                // CURSOR
                var chartCursor = new AmCharts.ChartCursor();
                chartCursor.cursorAlpha = 0;
                chartCursor.zoomable = false;
                chartCursor.categoryBalloonEnabled = false;
                chart.addChartCursor(chartCursor);

                chart.creditsPosition = "top-right";

                chart.write("chartdiv");
            });
            function loadStringData() {//發送ajax數據動態獲取數據
				$.ajax({
				   type: "POST",
				   url: "/jk/run/productsale/productsale.action",
				   cache: false,
				   success: function(msg){
					 chart.dataProvider = msg;  
					 chart.validateNow();  
					 chart.validateData();  //刷新柱狀圖
				   },
				  error: function(XMLHttpRequest, textStatus, errorThrown) {
				  }
				});
			}
			setTimeout("loadStringData()", 100);
        </script>
    </head>

    <body>
        <div id="chartdiv" style="width: 100%; height: 400px;"></div>
    </body>

</html>

服務器端:

//貨物銷售情況柱狀圖
	@ResponseBody 
	@RequestMapping(value="/run/productsale/productsale.action", method=RequestMethod.POST, produces="application/json;charset=UTF-8")
	 public String productsale(){
		String sql = "SELECT product_no,SUM(amount) AS money FROM contract_product_c GROUP BY product_no ORDER BY money DESC LIMIT 20";
		List<Map<String, Object>> dataList = super.getJdbcTemplate().queryForList(sql);
		StringBuffer sb = new StringBuffer("[");
		for(Map map: dataList) {
			sb.append(",").append("{\"product_no\":").append("\""+map.get("product_no")+"\"");
			sb.append(",").append("\"money\":").append("\""+map.get("money")+"\"").append("}");
		}
		sb.append("]");
		return sb.toString().replaceFirst(",", "");
	}

運行情況:



發佈了52 篇原創文章 · 獲贊 24 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章