日期:2014-05-16 浏览次数:20500 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Flot曲线图</title>
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
<script type="text/javascript">
$(function () {
var sin = [], cos = [];
// 初始化数据
for (var i = 0; i < 14; i += 0.5) {
sin.push([i, Math.sin(i)]);
cos.push([i, Math.cos(i)]);
}
var plot = $.plot(
$("#placeholder"),
[ { data: sin, label: "sin函数"}, { data: cos, label: "cos函数" } ], // 数据和右上角含义的提示
{
series: {
lines: { show: true }, // 点之间是否连线
points: { show: true } // 是否显示点
},
grid: { hoverable: true, clickable: true }, // 是否可以悬浮,是否可以点击
yaxis: { min: -1.2, max: 1.2 }, // Y 轴 的最大值和最小值
xaxis: { min: 0, max: 15 } // X 轴 的最大值和最小值
});
var previousPoint = null;
// 邦定事件
$("#placeholder").bind("plothover", function (event, pos, item) {
if ($("#enableTooltip:checked").length > 0) { // 如果允许提示
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2);
showTooltip(item.pageX, item.pageY,
"X:" + x + " Y:" + y);
//item.series.label + " of " + x + " = " + y); // 悬浮点时提示的内容
}
}else {
$("#tooltip").remove();
previousPoint = null;
}
}
});
// 悬浮点时进行提示
function showTooltip(x, y, contents) {
$('<div id="tooltip">' + contents + '</div>').css( {
position: 'absolute',
display: 'none',
top: y + 5,
left: x + 5,
border: '1px solid #fdd',
padding: '2px',
'background-color': '#fee',
opacity: 0.80
}).appendTo("body").fadeIn(200);
}
});
</script>
</head>
<body>
<div id="placeholder" style="width:600px;height:300px"></div>
<p><input id="enableTooltip" type="checkbox">Enable tooltip</p>
</body>
</html>