如何绘制javascript数组到jqplot



我有一个javascript数组,我从jqplot中的json_encode派生。如何在jqplot.

中绘制数组

下面的代码获取值并将其放入两个文本框中,从而形成一个javascript数组。我需要画出x-axis中的newArray2JSy-axis中的newArrayJS

<script type='text/javascript'>
function parseMe(){
    var json=document.getElementById('json_text').value;
    var obj=eval('('+json+')');
    document.createElement('u1');

    alert(obj);
    for(val in obj){
        alert(obj[val]);

        //alert(obj[val]);
        //}
        }}
        </script>
        </head>
        <body>
        <form>
        <input type='text' id='json_text1' value='<?php echo $newArrayJS; ?>' />
        <input type='text' id='json_text2' value='<?php echo $newArray2JS; ?>' />
        <input type='button' value='parse' onclick='parseMe();' />
        </form>
        <div id = 'chart1'></div>

我是新的jqplot和json。

我该怎么做呢?

首先,如果你想使用jqplot,你需要jquery。如果你的页面中有jquery,你不需要"document.getElement…"之类的东西。使用jquery和jquery选择器吧!

如果你的输入包含一个字符串数组,你可以json。解析它并把它赋给jqplot:

$(document).ready(function() {
  var yourOptions = {} // fill this with your options (see jqplot docs)
    , stringArray
    , yourArray;  // this is the array you want to be plotted, which is filled onclick
  $('input[type=button]').on('click', function() {
    // the array as a string
    stringArray = $('#json_text1').val();
    try {
      // parse your string to make it a js-array
      yourArray = JSON.parse(stringArray);
      // and now jqplot it (see more info on how to use this in the jqplot-docs: http://www.jqplot.com/docs
      $.jqplot('#chart1', yourArray, yourOptions);
    } catch (e) {
      // you should do something here if your string is not parsable
    }
  });
});
这样,你就可以从html中删除javascript-onclick-attributes了!

最新更新