0

我在 javascript 中有一个数组,它是从json_encodein派生的jqplot。如何在 jqplot 中绘制数组。

以下代码获取值并放入两个文本框中,创建一个 javascript 数组。我需要绘制newArray2JSinx-axisnewArrayJSiny-axis

<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>

我是jqplotjson 的新手。

我怎样才能做到这一点。

4

1 回答 1

1

首先如果你想使用jqplot,你需要jquery。如果您的页面中有 jquery,则不需要“document.getElement ...”的东西。使用 jquery 和jquery-selectors那里!

鉴于您的输入包含一个作为字符串的数组,您可以对其进行 json.parse 并将其提供给 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!

于 2013-01-14T11:02:01.317 回答