用Jqplot做的一个简易柱状图

本文介绍了一种基于JQuery的图表绘制插件Jqplot,并通过一个具体示例展示了如何使用该插件来实现水平条形图的绘制。代码中详细介绍了所需的库文件加载方式及JavaScript函数编写技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=medium]Jqplot应该说也是属于基于JQuery制作的一个插件,或者说是类库.运用Jqplot能达到不错的效果,感觉和JFreeChart差不多,不过各有所长吧,以下是页面代码,全都写一个JSP里了:[/size]

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.*, javax.servlet.jsp.*;" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Horizontal Bar with Vertical lines Test</title>

<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/jqplot/jquery.jqplot.css" />

<!-- BEGIN: load jquery -->
<script language="javascript" type="text/javascript"
src="${pageContext.request.contextPath}/jquery-1.3.2.min.js"></script>
<!-- END: load jquery -->

<!-- BEGIN: load jqplot -->
<script language="javascript" type="text/javascript"
src="${pageContext.request.contextPath}/jqplot/excanvas.js"></script>
<script language="javascript" type="text/javascript"
src="${pageContext.request.contextPath}/jqplot/jquery.jqplot.js"></script>
<script language="javascript" type="text/javascript"
src="${pageContext.request.contextPath}/jqplot/jqplot.barRenderer.js"></script>
<script language="javascript" type="text/javascript"
src="${pageContext.request.contextPath}/jqplot/jqplot.categoryAxisRenderer.js"></script>
<!-- END: load jqplot -->

<script language="javascript" type="text/javascript">
function drawHistogram(type, arrayKey, arrayValue) {

$.jqplot.config.enablePlugins = true;
line = arrayValue;
plot = $.jqplot('chart', [ line ], {
title : type,
series : [ {
renderer : $.jqplot.BarRenderer,
color : 'blue',
rendererOptions : {
barDirection : 'horizontal',
//the interval of two bars
barMargin : 15,
//the width of each bar
barWidth : 22
}
} ],
axes : {
xaxis : {
min : 0,
max : 5,
tickInterval : '1',
renderer : $.jqplot.LinearAxisRenderer
},
yaxis : {
renderer : $.jqplot.CategoryAxisRenderer,
ticks : arrayKey
}
}
});
}

$(function() {
$("#chart").empty();
var url = "testAction.action";
var type = $("#type").val();
var param = {
type : type
};
$.post(url, param, function(data) {
var json = eval("(" + data + ")")[0];
var index = 0;
//the values on the Y axis to display
var arrayKey = new Array();
//the values compared to the scale on x axis to display
var arrayValue = new Array();
$.each(json, function(key, value) {
arrayKey[index] = key;
arrayValue[index] = [ value, index + 1 ];
index++;
});
//the width of the div #chart
var chartWidth = 500;
//the height of the div #chart,37 is calculated by barMargin+barWidth
var chartHeight = (index + 1) * 37 + 15;
var chart = {
width : chartWidth + 'px',
height : chartHeight + 'px'
};
$("#chart").css(chart);
drawHistogram(type, arrayKey, arrayValue);

$("#chart").css("left",
($(window).width() - $("#chart").width()) / 2);
$("#chart").css("top",
($(window).height() - $("#chart").height()) / 2);
}, 'json');
});
</script>

</head>
<body>
<div id="chart"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值