【问题标题】:Javascript Graph Google Code Dynamically GeneratedJavascript Graph 谷歌代码动态生成
【发布时间】:2013-08-29 11:19:09
【问题描述】:

我想知道是否有人可以帮助使用这个函数,它来自谷歌代码折线图,我试图做的是从下面的 distanceField 变量中添加值,并希望每次出现新值时更新图表在,不确定我是否甚至采取了处理问题的最佳方法,但对问题的任何见解都会有所帮助 提前谢谢你。

    function drawVisualization2() {
    var d2 = document.getElementById('distanceField').value;
    var i;
    var arr = new Array();
    setInterval(function() {var f1 =document.getElementById('resultField').value;},5000);
    for (i = 0; i < 10; i++) {
    var f1 = document.getElementById('resultField').value;
    var d1 = parseInt(f1, 10);
    setTimeout(function() {
    //setInterval(function() {app.sendConnection()}, 10000);        
    //var f1 = document.getElementById('resultField').value;
    arr[i] = d1;
    }, 6000);
    }
    var data = google.visualization.arrayToDataTable([
    [ 'x', 'title' ], [ '1', arr[0] ], [ '2', arr[1] ],
    [ '3', arr[2] ], [ '4', arr[3] ], [ '5', arr[4] ],
    [ '6', arr[5] ], [ '7', arr[6] ], [ '8', arr[7] ],
    [ '9', arr[8] ], [ '10', arr[9] ],
    //['N',   1,       0.5,         1]
    ]);
    // Create and draw the visualization.
    new google.visualization.LineChart(document
    .getElementById('visualization2')).draw(data, {
    curveType : "function",
    width : 500,
    height : 350,
    vAxis : {
    maxValue : d2 / 2
    }
    });
    }

【问题讨论】:

    标签: javascript static charts


    【解决方案1】:

    您发布的代码块有很多问题,包括范围和同步问题 - 而不是尝试将其拼凑以使其正常工作我已经重写了它并评论了我为什么这样做:

    /**
     * A JS class that you can instansiate with the "new" construct 
     * on page/api load - this keeps everything self-contained and 
     * for the most part, out of the global scope.
     */
    function ChartObject(){
    
        // internal function - neater way to grab elements
        function $(x){ return document.getElementById(x); }
    
        // some default data
        this.data = [['x', 'Metric Title'], ['start',0]];
    
        // read values from our DOM elements and add to our data array
        this.update = function(){
            var dis = $('distanceField').value.trim(),
                res = parseInt($('resultField').value, 10);
            if(!isNaN(res) && !!dis){
                this.data.push([dis, res]);
                this.draw();
            } else {
                alert('invalid entry');
            }
        };
    
        // redraw our graph based on our current data array
        this.draw = function(){   
            new google.visualization.LineChart($('visualization')).draw(
                google.visualization.arrayToDataTable(this.data), 
                {
                    curveType : "function",
                    width : 450,
                    height : 300
                }
            );
        }
    
        // the following is executed once when the ChartObject is first called
    
        // draw the object with initial data
        this.draw();
    
        // add listener to button that user can click to "update" graph
        // this could be adapted to work with "onkeyup" rather than trying
        // to hack with "setTimeout"
        $('addRecord').onclick = function(){
            myChartObject.update();
        }
    
    };
    

    jsFiddle: working example

    【讨论】:

    • 感谢您在这个阶段已经为此工作了一段时间,您已经一口气解决了它,出色的工作谢谢
    • 只是想知道如何让图表变为 0,0 而不是像上面那样从开始处开始?
    • 你的意思是把['start','0']换成['0',0]? (link)
    • 我的意思是在图表中从原点开始。如果像在你的示例中一样,它将 0 沿 x 轴向下放置,有没有办法从 x 轴和 y 轴相交的点开始.?
    • @Maria88 哦。看看 API,它看起来不像。我可以建议的最好的方法是使用一个空字符串 ['',0] 来代替。
    猜你喜欢
    • 1970-01-01
    • 2010-12-17
    • 2015-04-04
    • 2011-09-08
    • 2020-03-08
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多