使用gviz的日期格式_api.py文件

2024-06-01 10:14:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用gviz_api(googlevisualizationpython)构建一些折线图。 http://code.google.com/p/google-visualization-python/

我编辑了一个取自google文档的折线图示例。
但是,我不确定如何将日期传递到数据表中

这是我用过的编辑过的例子。 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将日期加载到数据表中?在

google文档描述了如何使用javascript创建一个新的Date(),但是,我想继续使用gviz_api.py文件. 在

谷歌文档中的注释https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable

*JSON修改 Google的helper库以及所有发送到Google的查询都返回一个稍微不标准的JSON/JSONP版本。如果您不亲自解析返回的代码,这对您来说应该不重要。可视化API客户端支持JSON的标准版本和修改版本。以下是差异总结:

JSON不支持JavaScript日期值(例如,“new Date(2008,1,28,0,31,26)”;API实现支持。但是,API现在确实支持自定义的、有效的JSON日期表示形式,格式如下:Date(年、月、日[、小时、分钟、秒[、毫秒]]),其中日期之后的所有内容都是可选的,月份是从零开始的。在

JSON对字典键使用双引号;API实现使用不带引号的键。在

JSON需要在字符串值两边加上双引号;API实现使用单引号。*


Tags: 文档版本comapijson编辑datadate
1条回答
网友
1楼 · 发布于 2024-06-01 10:14:52

实际上,您可以像在标准Python中那样处理创建日期—API为您处理到JS的转换。您只需import datetime在脚本的开头,将year的列类型从string更改为date,然后像在标准Python中一样创建日期:

# 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()

相关问题 更多 >