简体   繁体   English

如何正确传输Javascript代码?

[英]How to transfer Javascript code correctly?

I have two codes, both generate a line chart. 我有两个代码,都生成一个折线图。 However, the first one doesn't use mysql datasource, it uses random math generated datapoints. 但是,第一个不使用mysql数据源,它使用随机数学生成的数据点。 But it uses a refresh interval and thus is live. 但是它使用刷新间隔,因此是实时的。

The second code does in fact use a mysql datasource and displays the data in my database in the line-chart. 第二个代码实际上确实使用了mysql数据源,并在线图中显示了数据库中的数据。 However it is not live, because it does not it has not refresh-interval function. 但是,它不是实时的,因为它不具有刷新间隔功能。

I was trying to transfer the refresh-Interval / chart-update code parts of the first code to my second code that is not live but uses a real data source. 我试图将第一个代码的refresh-interval / chart-update代码部分转移到我的第二个代码,该代码不是实时的,而是使用真实的数据源。

Here is my live code, with random datapoints: 这是我的实时代码,带有随机数据点:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">` 
    <script type="text/javascript">
        window.onload = function () {

            var dps = []; // dataPoints

            var chart = new CanvasJS.Chart("chartContainer2",{
                title :{
                    text: "Patient #01"
                },          
                data: [{
                    type: "line",
                    dataPoints: dps 
                }]
            });

            var xVal = 0;
            var yVal = 100; 
            var updateInterval = 20;
            var dataLength = 500; // number of dataPoints visible at any point

            var updateChart = function (count) {
                count = count || 1;
                // count is number of times loop runs to generate random dataPoints.

                for (var j = 0; j < count; j++) {   
                    yVal = yVal +  Math.round(5 + Math.random() *(-5-5));
                    dps.push({
                        x: xVal,
                        y: yVal
                    });
                    xVal++;
                };
                if (dps.length > dataLength)
                {
                    dps.shift();                
                }

                chart.render();     

            };

            // generates first set of dataPoints
            updateChart(dataLength); 

            // update chart after specified time. 
            setInterval(function(){updateChart()}, updateInterval); 

        }

    </script>

This is my code of the static line chart (not live) but uses real data source: 这是我的静态折线图代码(不是实时代码),但是使用了真实的数据源:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">` 
</script>
    <script type="text/javascript">

                    $().ready(function () {

                    $.getJSON("arduino_data.php", function (result) {

                    var dataPoints = [];

                    for (var i = 0; i <= result.length - 1; i++) {
                    dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
                    }

                    var chart = new CanvasJS.Chart("chartContainer",{
                title :{
                    text: "Patient #01"
                },          
                data: [{
                    type: "line",
                    dataPoints: dataPoints 
                }]
            });

                    chart.render();
                    });

            });
    </script>
    <script type="text/javascript" src="canvasjs.min.js"></script>

This is what I have tried so far: 到目前为止,这是我尝试过的:

<html>
<head>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"> </script>
<script type="text/javascript">

                $().ready(function () {

                    $.getJSON("arduino_data.php", function (result) {

                var chart = new CanvasJS.Chart("chartContainer",{
                title :{
                text: "Patient #01"
                },          
                data: [{
                type: "line",
                dataPoints: dataPoints 
                }]
                });

                var dataPoints = [];
                var updateInterval = 20;
                var dataLength = 500; // number of dataPoints visible at any point

                var updateChart = function (count) {
                          count = count || 1;

                for (var i = 0; i <= result.length - 1; i++) {
                dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
                };

               }
                if (dataPoints.length > dataLength)
                {
                dataPoints.shift();             
                }

                chart.render();     

        )};

        // generates first set of dataPoints
        updateChart(dataLength); 

        // update chart after specified time. 
        setInterval(function(){updateChart()}, updateInterval); 


       }




</script>
<script type="text/javascript" src="canvasjs.min.js"></script>
</head>
<body>
    <div id="chartContainer" style="height: 300px; width:100%;">
    </div>
</body>
</html>

But it keeps saying 但它一直在说

Unexpected token ')' at line 42 第42行出现意外的令牌')'

            chart.render();     

    )};

I am pretty embarrassed but I can't find the solution due to all the bracelets/parenthesizes. 我很尴尬,但由于所有手镯/括号都找不到解决方案。 I have tried everything. 我已经尝试了一切。 With ) and without } but nothing seems to deliver. 有)和无},但似乎无济于事。

If this is solved, will the chronological positions of the code be alright? 如果解决了这个问题,代码的时间顺序会正确吗?

EDIT: FIRST PROBLEM SOLVED, NEW PROBLEM: JS POSITIONING 编辑:解决了第一个问题,新问题:JS定位

<!DOCTYPE HTML>
<html>
<head>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"> </script>
<script type="text/javascript">

                $().ready(function () {

                    $.getJSON("arduino_data.php", function (result) {

                var chart = new CanvasJS.Chart("chartContainer",{
                title :{
                text: "Patient #01"
                },          
                data: [{
                type: "line",
                dataPoints: dataPoints 
                }]
                });

                var dataPoints = [];
                var updateInterval = 20;
                var dataLength = 500; // number of dataPoints visible at any point

                var updateChart = function (count) {
                          count = count || 1;

                for (var i = 0; i <= result.length - 1; i++) {
                dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
                };

               }
                if (dataPoints.length > dataLength)
                {
                dataPoints.shift();             
                }

                chart.render();     

                });

        // generates first set of dataPoints
        updateChart(dataLength); 

        // update chart after specified time. 
        setInterval(updateChart, updateInterval); 


       });




</script>
<script type="text/javascript" src="canvasjs.min.js"></script>
</head>
<body>
    <div id="chartContainer" style="height: 300px; width:100%;">
    </div>
</body>
</html>

output: 输出:

Can't find variable: updateChart 找不到变量:updateChart

You used )}; 您使用了)}; instead of }); 代替});

also at the end of your JS you used only } instead of }); 同样在JS的末尾,您只使用了}而不是});

also call your chart like 也叫你的图表像

setInterval(updateChart, updateInterval); 

and make sure your updateInterval is in the right function scope. 并确保您的updateInterval在正确的函数范围内。
Here's how it should approximately look like: 大致如下所示:

jQuery(function ($) {

    function updateChart( result ) { // move it here!!!
        $.getJSON("arduino_data.php", function( result ){
            var dataPoints = [];
            var dataLength = 500; // number of dataPoints visible at any point
            var updateInterval = 1000;
            var chart = new CanvasJS.Chart("chartContainer",{ // new chart Object
                title :{
                    text: "Patient #01"
                },          
                data: [{
                    type: "line",
                    dataPoints: dataPoints 
                }]
            });
            for (var i = 0; i <= result.length - 1; i++) {
                dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
            }
            if (dataPoints.length > dataLength){
                dataPoints.shift();             
            }        
            chart.render();   
        }); 
    }


    // First read - Start
    updateChart(); 
    // Update chart after specified time. 
    setInterval(updateChart, updateInterval); 

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 php 代码从 javascript 转移到 PHP? - How to transfer php code from javascript to PHP? Javascript:如何在代码块之间传输变量? - Javascript: How to transfer variables between code blocks? 如何将文件类型从Coldfusion代码传输到javascript代码? - How to transfer a file type from Coldfusion code to javascript code? 如何将javascript代码转换为python代码? - how can I transfer the javascript code to python code? 如何使用JavaScript传输图像 - How to transfer image with JavaScript 如何跨文件正确传输DOM元素? - How to correctly transfer DOM elements across files? 如何将工作 HTML、CSS、Javascript 代码从 Codepen 传输到 Visual Studio 代码和浏览器 - How to Transfer Working HTML, CSS, Javascript Code from Codepen to Visual Studio Code and Browser 智能感知 / vs 代码。 如何正确设置javascript导入? - intellisense / vs code. how to set up javascript imports correctly? 如何在chameleon / zpt模板(金字塔)中正确包含javascript代码? - How to correctly include javascript code in chameleon/zpt template (pyramid)? 如何(正确)将 JavaScript 代码添加到 Acumatica 中的自定义项目屏幕? - How to (correctly) add JavaScript code to a Customization Project Screen in Acumatica?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM