本文介绍了Google Chart API:呈现XYLine图表时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过不太详细的库。



以下是应该生成图的代码的一部分:

  data = [] 
traffic_max = 0
date_minmax = []

#我有两个系列的时间戳读数
用于网站站点:$ b​​ $ b site_data = get_datapoints_for(网站)

date_values = [date_to_unix(item.timestamp)for site in site_data]
date_minmax = minmax(date_minmax + date_values)#get
的最小值和最大值#scaling
traffic_values = [item.traffic for site in site_data]
traffic_max = max(traffic_max,* traffic_values)#just获得读数的最大值

#build图
data.append(date_values)
data.append(traffic_values)

#添加到轴的余地
date_minmax [0] - = 500
date_minmax [1] + = 500
traffic_max / = 0.9

#build graph
chart = LineXY(data)
chart.legend(* sites)
chart.color(green,red)
chart.size(600,400 )
chart.scale(date_minmax [0],date_minmax [1],0,traffic_max)
chart.axes.type(xy)
chart.axes.range(0,* date_minmax)
chart.axes.range(1,0,traffic_max)

chart_url = chart.url

不过,这是我得到的:



问题:红色系列发生了什么变化?






$ p


















$ b $ [[1286116241.448437,1286118544.4077079,1286119431.18503,1286121011.5838161],
[6710.9899999999998,
6716.2799999999997,
6641.8900000000003,
6644.9499999999998],
[1286116241.979831,
1286118545.0123601,
1286119431.9650409,
1286121012.1839011],
[38323.860000000001,
38326.620000000003,
38327.660000000003,
38329.610000000001]]

下面是包装器生成的参数列表,也是一些漂亮的爱之后:

  chco = 008000,FF0000 
chd = t:1286116241.0,1286118544.0,12861 19431.0,1286121011.0
6711.0,6716.3,6641.9,6644.9
1286116241.0,1286118545.0,1286119431.0,1286121012.0
38323.9,38326.6,38327.7,38329.6
chdl = gaming.stackexchange.com
serverfault.com
chds = 1286115741.0,1286121512.0,0,42588.4555556
chs = 600x400
cht = lxy
chxr = 0,1286115741.0,1286121512.0
1,0, 42588.4555556
chxt = x,y
cht = lxy
chxr = 0,1286116241.45,1286121012.18
1,0,42588.4555556
chxt = x,y


解决方案

问题出在这里:

  chart.scale(date_minmax [0],date_minmax [1],0,traffic_max)

Google API允许您为每个系列定义一个不同的比例。因此,您需要为每个系列指定缩放比例,而上述仅影响第一个系列。



将该行更改为: p>

  chart.scale(* [date_minmax [0],date_minmax [1],0,traffic_max] * len(sites))

...强制所有系列的缩放比例相同。


I'm trying to make an XYLine with the Google Chart API via the not-so-well-documented GChartWrapper libraries.

Here's a portion of the code that is supposed to generate the graph:

data = []
traffic_max = 0
date_minmax = []

#I have a number of timestamped readings in two series
for site in sites:
  site_data = get_datapoints_for(site)

  date_values = [datetime_to_unix(item.timestamp) for item in site_data]
  date_minmax = minmax(date_minmax + date_values) #get min and max values for
                                                  #scaling
  traffic_values = [item.traffic for item in site_data]
  traffic_max = max(traffic_max, *traffic_values) #just get maximum for readings

  #build graph
  data.append(date_values)
  data.append(traffic_values)

#add leeway to axes
date_minmax[0] -= 500
date_minmax[1] += 500
traffic_max /= 0.9

#build graph
chart = LineXY(data)
chart.legend(*sites)
chart.color("green", "red")
chart.size(600,400)
chart.scale(date_minmax[0], date_minmax[1], 0, traffic_max)
chart.axes.type("xy")
chart.axes.range(0, *date_minmax)
chart.axes.range(1, 0, traffic_max)

chart_url = chart.url

However, this is what I get:

Question: What happened to the red series?


For your convenience, here is data after some pretty print love:

[[1286116241.448437, 1286118544.4077079, 1286119431.18503, 1286121011.5838161],
 [6710.9899999999998,
  6716.2799999999997,
  6641.8900000000003,
  6644.9499999999998],
 [1286116241.979831,
  1286118545.0123601,
  1286119431.9650409,
  1286121012.1839011],
 [38323.860000000001,
  38326.620000000003,
  38327.660000000003,
  38329.610000000001]]

Here is the list of parameters generated by the wrapper, also after some pretty print love:

chco=008000,FF0000
chd=t:1286116241.0,1286118544.0,1286119431.0,1286121011.0
    6711.0,6716.3,6641.9,6644.9
    1286116241.0,1286118545.0,1286119431.0,1286121012.0
    38323.9,38326.6,38327.7,38329.6
chdl=gaming.stackexchange.com
    serverfault.com
chds=1286115741.0,1286121512.0,0,42588.4555556
chs=600x400
cht=lxy
chxr=0,1286115741.0,1286121512.0
    1,0,42588.4555556
chxt=x,y
cht=lxy
chxr=0,1286116241.45,1286121012.18
    1,0,42588.4555556
chxt=x,y
解决方案

The problem was here:

chart.scale(date_minmax[0], date_minmax[1], 0, traffic_max)

The Google API allows you to define a different scale per each series. Thus, you need to specify the scaling for each series, whereas the above only affected the first series.

Changing that line to:

chart.scale(*[date_minmax[0], date_minmax[1], 0, traffic_max]*len(sites))

...forces all series to the same scaling.

这篇关于Google Chart API:呈现XYLine图表时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:47