pygooglechart 定义数据编码

1 投票
2 回答
652 浏览
提问于 2025-04-16 16:02

当你使用 chart.add_date([1,2,3,4,5]) 这个代码时,它会生成一个包含 "chd=e:LNYAczgATN5m" 的网址,这里的数据是经过编码的。但我想要的是文本格式的,像这样 "chd=t:1,2,3,4,5"。

请问有什么函数可以做到这一点呢?

提前谢谢你!

2 个回答

0

如果你想使用 chart.download(),那么 get_url 的 data_class 选项就不能用了。

你可以通过创建一个子类来解决这个问题:

# force the TextData type for any data (real number instead of aabaa)
class LineChart(SimpleLineChart):
   def data_class_detection(self, data):
       return TextData

然后用这个类来替代 SimpleLineChart。

1

pygooglechart这个库会尝试自动识别数据的范围,并决定使用哪种数据编码方式。你可以查看源代码中的def data_class_detection()方法,了解它是怎么做的:

https://github.com/gak/pygooglechart/blob/master/pygooglechart.py#L518

如果你想强制使用某种特定的编码方式,可以调用get_url(self, data_class=None)方法,并把data_class设置为TextData,比如:

import pygooglechart as pygc
chart = pygc.SimpleLineChart(250, 100)
chart.add_data([1, 3, 2, 4, 5])

>>> chart.get_url(data_class=pygc.TextData)
'http://chart.apis.google.com/chart?cht=lc&chs=250x100&chd=t:0.0,40.0,20.0,60.0,80.0'

>>> chart.get_url()
'http://chart.apis.google.com/chart?cht=lc&chs=250x100&chd=e:AAMzZmmZzM'

撰写回答