5

gviz_api (google-visualization-python) を使用して折れ線グラフを作成しています。 http://code.google.com/p/google-visualization-python/

Google ドキュメントから取得した折れ線グラフの例を編集しました。
ただし、日付を DataTable に渡す方法がわかりません

これが私が取り組んできた編集済みの例です。 https://gist.github.com/3941946

これが私が質問したコードです

 # Creating the data
 description = {"year": ("string", "Year"),
             "sales": ("number", "Sales"),
             "expenses": ("number", "Expenses")}

 data = [{"year": '2004', "sales": 1000, "expenses": 300}, 
      {"year": '2005', "sales": 1200, "expenses": 400}, 
      {"year": '2006', "sales": 1300, "expenses": 500}, 
      {"year": '2007', "sales": 1400, "expenses": 600}, 
      {"year": '2008', "sales": 1500, "expenses": 800}]
  # Loading it into gviz_api.DataTable
  data_table = gviz_api.DataTable(description)
  data_table.LoadData(data)

gviz_api を使用して DataTable に日付を読み込むにはどうすればよいですか?

Google のドキュメントには、javascript を使用して新しい Date() を作成する方法が記載されていますが、gviz_api.py を引き続き使用したいと考えています。

https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatableの Google ドキュメント からのメモ

*JSON の変更 Google のヘルパー ライブラリ、および Google に送信されるすべてのクエリは、標準とは少し異なるバージョンの JSON/JSONP を返します。返されたコードを自分で解析していない場合、これは問題になりません。Visualization API クライアントは、JSON の標準バージョンと修正バージョンの両方をサポートしています。相違点の概要は次のとおりです。

JSON は JavaScript 日付値をサポートしていません (たとえば、"new Date(2008,1,28,0,31,26)"; API 実装はサポートしています。ただし、API は現在、日付のカスタムの有効な JSON 表現を次の形式の文字列: Date(year, month, day[,hour, minutes, second[, millisecond]]) ここで、day 以降はすべてオプションで、月は 0 から始まります。

JSON は辞書キーに二重引用符を使用します。API 実装は引用符で囲まれていないキーを使用します。

JSON では、文字列値を二重引用符で囲む必要があります。API 実装では一重引用符を使用します。*

4

1 に答える 1

5

実際には、標準の Python と同じように日付の作成を処理できます。API が JS への変換を処理します。import datetimeスクリプトの先頭で列の型を to に変更し、標準yearのPython で行うように日付を作成するだけです。stringdate

# This is the first modification - importing the library
import datetime
import gviz_api

# page_template stays the same
# ...

def main():
  # Creating the data
  # Here we change the type of column "year" to "date"
  description = {"year": ("date", "Year"),
                 "sales": ("number", "Sales"),
                 "expenses": ("number", "Expenses")}

  # Here we switch out the string dates with an actual Python datetime.date
  # The conversion happens in the the subsequent functions, giving you a 
  # date that is usable in the JS
  data = [{"year": datetime.date(2007,3,7), "sales": 1000, "expenses": 300},
          {"year": datetime.date(2009,6,11), "sales": 1200, "expenses": 400},
          {"year": datetime.date(2009,3,1), "sales": 1300, "expenses": 500},
          {"year": datetime.date(2010,8,6), "sales": 1401, "expenses": 600},
          {"year": datetime.date(2011,7,13), "sales": 1500, "expenses": 800}]
  # Loading it into gviz_api.DataTable
  data_table = gviz_api.DataTable(description)
  data_table.LoadData(data)

  # Creating a JavaScript code string
  jscode = data_table.ToJSCode("jscode_data",
                               columns_order=("year", "sales", "expenses"),
                               order_by="year")
  # Creating a JSon string
  json = data_table.ToJSon(columns_order=("year", "sales", "expenses"),
                           order_by="year")

  # Putting the JS code and JSon string into the template
  print "Content-type: text/html"
  print
  print page_template % vars()


if __name__ == '__main__':
  main()
于 2012-10-23T23:15:34.393 に答える