Javascript 如何将highcharts车速表保持在范围内

Javascript 如何将highcharts车速表保持在范围内,javascript,highcharts,Javascript,Highcharts,我正在研究一个动态加载的速度计。除了Ajax调用和速度计的问题外,我还无法将“指针”保持在范围(0-20)内。当指针沿错误方向旋转超过0或20时,整数输出显示小于或大于20的数字。我想把它保持在0的右边和20的左边。我的代码: <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html; charset=U

我正在研究一个动态加载的速度计。除了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>

特里布尔公司销售仪表
$(函数(){
var图表=新的Highcharts.图表({
图表:{
renderTo:'容器',
类型:“仪表”,
plotBackgroundColor:null,
plotBackgroundImage:空,
绘图边框宽度:0,
plotShadow:false
},
标题:{
文字:“销售计量表”
},
窗格:{
startAngle:-150,
端角:150,
背景:[{
背景颜色:{
线性半径:{x1:0,y1:0,x2:0,y2:1},
停止:[
[0,#FFF'],
[1, '#333']
]
},
边框宽度:0,
外层:109%
}, {
背景颜色:{
线性半径:{x1:0,y1:0,x2:0,y2:1},
停止:[
[0, '#333'],
[1'#FFF']
]
},
边框宽度:1,
外层:107%
}, {
//默认背景
}, {
背景颜色:“#DDD”,
边框宽度:0,
外层:105%,
内半径:“103%”
}]
},
//价值轴
亚克斯:{
分:0,,
最高:20,
minorTickInterval:“自动”,
minorTickWidth:1,
minorTickLength:10,
minorTickPosition:“内部”,
minorTickColor:“#666”,
像素间隔:20,
宽度:2,
位置:'内部',
长度:1,
勾选颜色:'#666',
标签:{
步骤:2,
旋转:“自动”
},
标题:{
文本:“销售额/分钟”
},
绘图带:[{
起:0,,
致:4,,
颜色:'#DF5353'//绿色
}, {
发信人:4,,
致:8,,
颜色:'#DDDF0D'//黄色
}, {
起:8,,
致:20,,
颜色:'#55BF3B'//红色
}]
},
系列:[{
名称:“速度”,
数据:[18],
工具提示:{
valueSuffix:'公里/小时'
}
}]
},
//增添活力
功能(图表){
setInterval(函数(){
变量点=图表。系列[0]。点[0],
纽瓦尔,
inc=数学楼层((数学随机()-1)*20);
newVal=点y+inc;
如果(newVal<0 | | newVal>20){
newVal=point.y-inc;
}
更新点(newVal);
}, 3000);
});
});
我不完全确定我做错了什么…也许Math.floor()函数关闭了,但我不这么认为。有人能帮忙吗?

inc=Math.floor((Math.random()-1)*20);
                    inc = Math.floor((Math.random() - 1) * 20);

            newVal = point.y + inc;
            if (newVal < 0 || newVal > 20) {
                newVal = point.y - inc;
newVal=点y+inc; 如果(newVal<0 | | newVal>20){ newVal=point.y-inc;
此代码生成一个负inc,将其添加到点,使点为负,然后将其减去点,使点为正。它出错,因为inc可以大于点

您需要的算法取决于您希望图表如何变化。如果您希望指针摆动,则生成一个较小的inc,将其添加到点,然后进行边界检查

                    inc = (Math.random() * 2.0)-1.0;

            newVal = point.y + inc;
            if (newVal < 0 || newVal > 20) {
                newVal = point.y - inc;
inc=(Math.random()*2.0)-1.0;
newVal=点y+inc;
如果(newVal<0 | | newVal>20){
newVal=point.y-inc;

谢谢Steve。我确实在他们最初编写代码的过程中找到了答案。我没有使用Math.round((Math.random()-.5)*20)。这会将其舍入为整数,并使其保持在0-20或其他任何数字之间。