如何保持航海图速度计在范围内
How to keep highcharts speedometer within range
我正在研究一个动态加载的速度计。除了我的问题与Ajax调用结合速度计,我有麻烦保持"针"在范围内(0-20)。当指针在错误的方向上旋转超过0或20时,整数输出显示小于或大于20的数字。我想把它放在0的右边,20的左边。我的代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" >
<title>Tribble Inc Sales Meter</title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'gauge',
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
plotShadow: false
},
title: {
text: 'Sales-O-Meter'
},
pane: {
startAngle: -150,
endAngle: 150,
background: [{
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#FFF'],
[1, '#333']
]
},
borderWidth: 0,
outerRadius: '109%'
}, {
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#333'],
[1, '#FFF']
]
},
borderWidth: 1,
outerRadius: '107%'
}, {
// default background
}, {
backgroundColor: '#DDD',
borderWidth: 0,
outerRadius: '105%',
innerRadius: '103%'
}]
},
// the value axis
yAxis: {
min: 0,
max: 20,
minorTickInterval: 'auto',
minorTickWidth: 1,
minorTickLength: 10,
minorTickPosition: 'inside',
minorTickColor: '#666',
tickPixelInterval: 20,
tickWidth: 2,
tickPosition: 'inside',
tickLength: 1,
tickColor: '#666',
labels: {
step: 2,
rotation: 'auto'
},
title: {
text: 'sales/min'
},
plotBands: [{
from: 0,
to: 4,
color: '#DF5353' // green
}, {
from: 4,
to: 8,
color: '#DDDF0D' // yellow
}, {
from: 8,
to: 20,
color: '#55BF3B' // red
}]
},
series: [{
name: 'Speed',
data: [18],
tooltip: {
valueSuffix: ' km/h'
}
}]
},
// Add some life
function (chart) {
setInterval(function () {
var point = chart.series[0].points[0],
newVal,
inc = Math.floor((Math.random() - 1) * 20);
newVal = point.y + inc;
if (newVal < 0 || newVal > 20) {
newVal = point.y - inc;
}
point.update(newVal);
}, 3000);
});
});
</script>
</head>
<body>
<script src="highcharts.js"></script>
<script src="highcharts-more.js"></script>
<!--<script src="exporting.js"></script>-->
<div id="container" style="width: 500px; height: 400px; margin: 0 auto"></div>
</body>
</html>
我不完全确定我做错了什么…也许Math.floor()函数是关闭的,但我不这么认为。有人能帮忙吗?
inc = Math.floor((Math.random() - 1) * 20);
newVal = point.y + inc;
if (newVal < 0 || newVal > 20) {
newVal = point.y - inc;
此代码生成一个负的inc,将其与point相加,使point为负,然后将其与point相减,使point更正。它会出错,因为inc可以大于point。
您需要的算法取决于您希望图表如何变化。如果你想让指针摇摆,那么生成一个较小的inc,将其添加到point,然后进行边界检查。
inc = (Math.random() * 2.0)-1.0;
newVal = point.y + inc;
if (newVal < 0 || newVal > 20) {
newVal = point.y - inc;
相关文章:
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 如何使用D3生成特定范围内的随机颜色
- 将自定义css保持在角度范围内
- Angular JS-文本框未在独立范围内更新
- cookie不在网站范围内
- 获取给定JavaScript范围内的解析函数
- 如何在JavaScript中使用此函数对范围内的所有数字求和
- JavaScript-仅验证数值并且在范围内
- JS:从数组中查找特定范围内的最低/最高数字
- 试图检查表单中的值是否为一个设定范围内的数字
- 在十进制范围内搜索
- 基于日期范围的一组日期范围内的天数
- 如何在 Google 应用脚本中 sendEmail 函数的范围内的唯一非空白列中返回值
- 将数组传递给函数或确保它在范围内
- socket.io 客户端是否必须存在于全局范围内
- 查找某个日期范围内的可用天数
- 网站范围内jQuery/javascript选择器的最佳实践
- 如果指令包含在另一个指令中,我如何在隔离范围内添加双向数据绑定属性
- JS日期对象在某个范围内的百分比
- 如何保持航海图速度计在范围内