0

JavaScript 的绝对新手,所以请原谅任何糟糕的编码。

我一直在使用 Highchart 进行一个项目,并希望读取文件的值(实际上是将文件读入数组并获取特定值 - 我将对其他图表使用其他值)。

在其他人的帮助下,我现在可以将数组的值显示为 HTML,但是我无法将其传递给 Highchart。

我的原型页面(所以你可以看到输出)可以在

http://maccas.chickenkiller.com/1.html

代码是

<html debug="false">
    <head>
        <title>Adam's hacked up demo</title>
        <META HTTP-EQUIV="refresh" CONTENT="300">
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/highcharts-more.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
    </head>

<script>

jQuery(function($){ // encapsulate jQuery
    $.ajax(
        {
            url:'livedata.txt',
            async:false,
            success: function(data){
            livedata = data.split(',');
            }
        });

    function drawGraph(phvalue) {
    var phvalue=livedata[3];
        var chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'gauge',
                plotBackgroundColor: null,
                plotBackgroundImage: null,
                plotBorderWidth: 0,
                plotShadow: false
            },

            title: {
                text: 'live pH'
            },
                    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: 8,
                max: 9,

                minorTickInterval: 'auto',
                minorTickWidth: 1,
                minorTickLength: 10,
                minorTickPosition: 'inside',
                minorTickColor: '#666',

                tickPixelInterval: 30,
                tickWidth: 2,
                tickPosition: 'inside',
                tickLength: 10,
                tickColor: '#666',
                labels: {
                    step: 2,
                    rotation: 'auto'
                },
                plotBands: [{
                    from: 8,
                    to: 8.3,
                    color: '#55BF3B' // green
                }, {
                    from: 8.3,
                    to: 8.7,
                    color: '#DDDF0D' // yellow
                }, {
                    from: 8.7,
                    to: 9,
                    color: '#DF5353' // red
                }]
            },

            series: [{
                name: 'Current pH',
                data: [phvalue],
                tooltip: {
                        valueSuffix: ' pH'
                }
            }]
        });
    }

    $(document).ready(function () {
        drawGraph();
    });

});
</script> 

<body>
    <table>
            <tr>
                <td><div id="container" style="width:90%; height:400px;"></div></td>
            </tr>
    </table>
</body>
</html>

有谁知道如何让 var phvalue 在 Highcharts 中显示?

提前致谢。

4

2 回答 2

0

这出乎意料地复杂。你要:

$('#container').highcharts().series[0].points[0].update(x)

x您要更新的号码在哪里

于 2013-06-15T06:34:26.607 回答
0

由于ajax是异步的,一旦请求完成就可以绘制图形

jQuery(function($){ // encapsulate jQuery

    $.ajax(
        {
            url:'livedata.txt',
            async:false,
            success: function(data){
                livedata = data.split(',');
                highcharts(livedata[3])
            }
        });


    function highcharts (phvalue) {
        $('#realtimeph').highcharts({
            chart: {
                type: 'gauge',
                plotBackgroundColor: null,
                plotBackgroundImage: null,
                plotBorderWidth: 0,
                plotShadow: false
            },

            title: {
                text: 'live pH'
            },
            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: 8,
                max: 9,

                minorTickInterval: 'auto',
                minorTickWidth: 1,
                minorTickLength: 10,
                minorTickPosition: 'inside',
                minorTickColor: '#666',

                tickPixelInterval: 30,
                tickWidth: 2,
                tickPosition: 'inside',
                tickLength: 10,
                tickColor: '#666',
                labels: {
                    step: 2,
                    rotation: 'auto'
                },
                plotBands: [{
                    from: 8,
                    to: 8.3,
                    color: '#55BF3B' // green
                }, {
                    from: 8.3,
                    to: 8.7,
                    color: '#DDDF0D' // yellow
                }, {
                    from: 8.7,
                    to: 9,
                    color: '#DF5353' // red
                }]
            },

            series: [{
                name: 'Current pH',
                data: [phvalue],
                tooltip: {
                    valueSuffix: ' pH'
                }
            }]
        });
    }
});
于 2013-06-14T04:45:08.190 回答