0

json_encodeから派生したjavascriptの配列がありjqplotます。jqplot で配列をプロットする方法。

次のコードは値を取得し、JavaScript 配列を作成する 2 つのテキスト ボックスに配置します。newArray2JSinx-axisnewArrayJSinをプロットする必要がありますy-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 に答える