POST 要求

在瀏覽器中將圖表指定為網址,或 <img> 標記稱為 GET 要求。建立 GET 要求很簡單,但 GET 網址長度最多為 2, 000 個字元。如果您有更多資料,該怎麼辦?

幸好,Chart API 支援的圖表要求時間上限為 16, 000 個。權衡利弊使用 POST 會增加複雜度。

以下是最基本的 POST 要求類型範例:使用 <form> 元素:

這個圖表其實是託管在 <iframe> 中的網頁。表單代碼如下:

<form action='https://chart.googleapis.com/chart' method='POST'>
  <input type="hidden" name="cht" value="lc"  />
  <input type="hidden" name="chtt" value="This is | my chart"  />
  <input type='hidden' name='chs' value='600x200' />
  <input type="hidden" name="chxt" value="x,y" />
  <input type='hidden' name='chd' value='t:40,20,50,20,100'/>
  <input type="submit"  />
</form>

有效 POST 要求的回應是 PNG 圖表,與 GET 要求回應相同。

使用 POST 的方法有很多種,這些都需要在網頁程式碼或代管網頁的伺服器上額外編寫程式碼。如要使用 POST,您通常會為每一張圖表建立個別頁面,並使用 <iframe><img> 標記在主頁面中嵌入或連結到這些網頁,如下所示。

以下舉例說明同時使用 POST 和 JavaScript 及 PHP 的例子。

針對 POST 要求使用 JavaScript

如要提出 JavaScript POST 要求,最簡單的方法是建立一個頁面,用來託管內含圖表資料的表單的 <input> 元素,並在其 onLoad() 處理常式中將該網頁設為 POST 要求,而該網頁會由圖表 PNG 取代。代管這張圖表的頁面應使用 <iframe> 加入這個頁面。 以下是圖表頁面的程式碼:

post_chart.html

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type='application/javascript'>
    // Send the POST when the page is loaded,
    // which will replace this whole page with the retrieved chart.
    function loadGraph() {
      var frm = document.getElementById('post_form');
      if (frm) {
       frm.submit();
      }
    }
  </script>
  </head>
  <body onload="loadGraph()">
    <form action='https://chart.googleapis.com/chart' method='POST' id='post_form'>
      <input type='hidden' name='cht' value='lc'/>
      <input type='hidden' name='chtt' value='This is | my chart'/>
      <input type='hidden' name='chs' value='300x200'/>
      <input type='hidden' name='chxt' value='x'/>
      <input type='hidden' name='chd' value='t:40,20,50,20,100'/>
      <input type='submit'/>
    </form>
  </body>
</html>

如果您使用 <form> 元素,就不需要對字串進行網址編碼 (不過,您仍需要使用特殊字元 (例如 |),才能在圖表標題中建立回車字元)。

接著,您可以在代管頁面中使用 <iframe>,將這張圖表載入其他頁面,如下所示:

another_page.html

<iframe src="post_chart.html" width="300" height="200"></iframe>

針對 POST 要求使用 PHP

大多數的伺服器端語言都支援明確的 POST 要求。以下示範如何使用 PHP 提出 POST 要求。這個範例展示的網頁會產生含有 150 個隨機值的折線圖。如要自行使用,您可以自訂 $chart 陣列加入自己的值。

chartserver-image.php

<?php
  // Create some random text-encoded data for a line chart.
  header('content-type: image/png');
  $url = 'https://chart.googleapis.com/chart';
  $chd = 't:';
  for ($i = 0; $i < 150; ++$i) {
    $data = rand(0, 100000);
    $chd .= $data . ',';
  }
  $chd = substr($chd, 0, -1);

  // Add data, chart type, chart size, and scale to params.
  $chart = array(
    'cht' => 'lc',
    'chs' => '600x200',
    'chds' => '0,100000',
    'chd' => $chd);

  // Send the request, and print out the returned bytes.
  $context = stream_context_create(
    array('http' => array(
      'method' => 'POST',
      'content' => http_build_query($chart))));
  fpassthru(fopen($url, 'r', false, $context));
?>

相較於 JavaScript 範例,嵌入這張圖表非常簡單,因為您只要使用 <img> 標記指向 POST 網頁即可,如下所示:

another_page.html

<img width='600' height='200' src='chartserver-image.php'>