官网:http://www.ichartjs.com/
ichartjs 是一款基于HTML5的图形库。使用纯javascript语言, 利用HTML5的canvas标签绘制各式图形。 ichartjs致力于为您的应用提供简单、直观、可交互的体验级图表组件。是WEB/APP图表展示方面的解决方案 。如果你正在开发HTML5的应用,ichartjs正好适合您。 ichartjs目前支持饼图、环形图、折线图、面积图、柱形图、条形图。ichartjs是基于Apache License 2.0协议的开源项目。
ichartjs 对新手是十分友好的,它的强大之处在于可以根据需要自动生成图表,然后你只需要将数据绑定就可以了。所以,对于新手来说,不需要你深入了解和学习,只需要绑定数据就可以了。
例子:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增会员分析</title> <%
String basePath = request.getContextPath();
//验证session,防止直接进入该页面
Object obj = request.getSession().getAttribute("current");//获取session对象
if(obj == null) {
response.sendRedirect("../z_login.jsp");
}
%>
<script type="text/javascript" src="../ichartjs/ichart.1.2.1.min.js"></script>
<script type="text/javascript" src="../js/jquery-3.2.1.min.js"></script>
<script type="text/javascript"
src="../jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<link rel="shortcut icon" href="img/logo1.jpg"/>
<link type="text/css" rel="stylesheet"
href="../jquery-easyui-1.5.1/themes/icon.css"></link>
<link type="text/css" rel="stylesheet"
href="../jquery-easyui-1.5.1/themes/default/easyui.css"></link>
<script type="text/javascript"
src="../jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script> <style type="text/css"> *{margin:0;padding:0} .anal{
text-decoration:none;
color:black;
}
</style>
</head>
<body>
<a id="analyear" class="anal" href="#">根据年份查询</a><br>
<a id="analmon" class="anal" href="#">根据月份查询</a><br> <div id='ichart-render'></div>
</body>
</html>
<script>
$(function() { $("#analmon").click(function() {
$.ajax({
type : "post",
url : "../useranal/analmon.do",
dataType : "json",//返回的
success : function(data) {
for(var i in data.rows.baifenbi){
alert(i);
}
var chart = iChart.create({
render:"ichart-render",
width:800,
height:400,
background_color:"#fefefe",
gradient:false,
color_factor:0.2,
border:{
color:"BCBCBC",
width:1
},
align:"center",
offsetx:0,
offsety:0,
sub_option:{
border:{
color:"#BCBCBC",
width:1
},
label:{
fontweight:500,
fontsize:11,
color:"#4572a7",
sign:"square",
sign_size:12,
border:{
color:"#BCBCBC",
width:1
},
background_color:"#fefefe"
}
},
shadow:true,
shadow_color:"#666666",
shadow_blur:2,
showpercent:false,
column_width:"70%",
bar_height:"70%",
radius:"90%",
title:{
text:"新增会员分析",
color:"#111111",
fontsize:20,
font:"微软雅黑",
textAlign:"center",
height:30,
offsetx:0,
offsety:0
},
subtitle:{
text:"月新增会员分析",
color:"#111111",
fontsize:16,
font:"微软雅黑",
textAlign:"left",
height:20,
offsetx:0,
offsety:0
},
footnote:{
text:"",
color:"#111111",
fontsize:12,
font:"微软雅黑",
textAlign:"right",
height:20,
offsetx:0,
offsety:0
},
legend:{
enable:false,
background_color:"#fefefe",
color:"#333333",
fontsize:12,
border:{
color:"#BCBCBC",
width:1
},
column:1,
align:"right",
valign:"center",
offsetx:0,
offsety:0
},
coordinate:{
width:"80%",
height:"84%",
background_color:"#ffffff",
axis:{
color:"#a5acb8",
width:[1,"",1,""]
},
grid_color:"#d9d9d9",
label:{
fontweight:500,
color:"#666666",
fontsize:11
}
},
label:{
fontweight:500,
color:"#666666",
fontsize:11
},
type:"column2d", data:data.rows,
});
chart.draw(); },
error : function(msg) {
cosole.log(msg);
}
});
}); $("#analyear").click(function() {
$.ajax({
type : "post",
url : "../useranal/analyear.do",
dataType : "json",//返回的
success : function(data) {
for(var i in data.rows.baifenbi){
alert(i);
}
var chart = iChart.create({
render:"ichart-render",
width:800,
height:400,
background_color:"#fefefe",
gradient:false,
color_factor:0.2,
border:{
color:"BCBCBC",
width:1
},
align:"center",
offsetx:0,
offsety:0,
sub_option:{
border:{
color:"#BCBCBC",
width:1
},
label:{
fontweight:500,
fontsize:11,
color:"#4572a7",
sign:"square",
sign_size:12,
border:{
color:"#BCBCBC",
width:1
},
background_color:"#fefefe"
}
},
shadow:true,
shadow_color:"#666666",
shadow_blur:2,
showpercent:false,
column_width:"70%",
bar_height:"70%",
radius:"90%",
title:{
text:"新增会员分析",
color:"#111111",
fontsize:20,
font:"微软雅黑",
textAlign:"center",
height:30,
offsetx:0,
offsety:0
},
subtitle:{
text:"年新增会员分析",
color:"#111111",
fontsize:16,
font:"微软雅黑",
textAlign:"left",
height:20,
offsetx:0,
offsety:0
},
footnote:{
text:"",
color:"#111111",
fontsize:12,
font:"微软雅黑",
textAlign:"right",
height:20,
offsetx:0,
offsety:0
},
legend:{
enable:false,
background_color:"#fefefe",
color:"#333333",
fontsize:12,
border:{
color:"#BCBCBC",
width:1
},
column:1,
align:"right",
valign:"center",
offsetx:0,
offsety:0
},
coordinate:{
width:"80%",
height:"84%",
background_color:"#ffffff",
axis:{
color:"#a5acb8",
width:[1,"",1,""]
},
grid_color:"#d9d9d9",
label:{
fontweight:500,
color:"#666666",
fontsize:11
}
},
label:{
fontweight:500,
color:"#666666",
fontsize:11
},
type:"column2d", data:data.rows,
});
chart.draw(); },
error : function(msg) {
cosole.log(msg);
}
});
}); });
</script>
后台代码:
/*
* 查询月增会员
*/
@ResponseBody
@RequestMapping("/analmon")
public JSONObject AnalMon() {
JSONObject jo = new JSONObject();
List<Anal> list=useranalservice.AnalMon();
for(Anal a:list){
a.setColor("#"+Color.getRandColorCode());
} jo.put("rows", list);
return jo;
}
Sql语句:
<!-- 根据月份查询新增会员 -->
<select id="AnalMon" resultType="Anal">
select to_char(cu.createtime, 'yyyy-mm') name, count(*) value
from (select u.*
from CRM_USERS u
) cu
group by to_char(cu.createtime, 'yyyy-mm')
order by to_char(cu.createtime, 'yyyy-mm') </select>
实体类:
package com.hanqi.model; public class Anal {
private String name;
private String value;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Anal() {
super();
// TODO Auto-generated constructor stub
}
public Anal(String name, String value, String color) {
super();
this.name = name;
this.value = value;
this.color = color;
}
@Override
public String toString() {
return "Anal [name=" + name + ", value=" + value + ", color=" + color + "]";
} }
不过,ichartjs这样使用也有一个很大的问题,如果数据太多,展示的时候会很难看,而且目前这种方式只能生成柱状图和饼图。
要解决这两个问题,第一个问题可以尝试在查询数据的时候进行判断,限制用户查询太多的数据,并给出相应的提示。
第二个问题就需要深入的了解并学习iChatjs来自己生成图表了。