如何将javascript数组绘制成jqplot?

时间:2013-01-14 10:24:08

标签: javascript arrays json graph jqplot

我在javascript中有一个数组,它来自json_encode中的jqplot。如何在jqplot中绘制数组。

以下代码获取值并放入两个构成javascript数组的文本框中。我需要在newArray2JSx-axisnewArrayJSy-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>

jqplot

我是{{1}}和json的新手。

我该怎么办?

1 个答案:

答案 0 :(得分: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属性!