使用GChartWrapper为Google图表创建动态标签 [英] Create dynamic labels for Google Chart using GChartWrapper

查看:158
本文介绍了使用GChartWrapper为Google图表创建动态标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用令人敬畏的 Google Chart Wrapper 创建图表,作为我的Django项目。现在我在我的视图中使用Python创建图表实例,然后将实例添加到模板上下文中,并在我的模板中成功显示图表。



我在做什么是显示一组动态的X轴标签。以下是Google Chart Wrapper的相关代码:

  def label(self,index,* args):

一次标记每个轴
参数的格式为< label 1>,...,< label n>
APIPARAM:chxl

self.data ['labels']。append(
str('%s:|%s'%(index,'|'.join(map(str,args))))。替换('None','')

return self.parent

和我目前正在创建图表在我的看法:

 从GChartWrapper导入* 

chart = Sparkline([1,2,3,4,5,6,7],encoding ='text')
chart.axes.label(...)#需要在这里设置标签

我有一个我希望显示为x轴标签的日期字符串列表,有没有人知道我会这样做?

解决方案

为了理解如何正确传递标签,这很重要你知道什么是 * args 意思。



* args (这是一个星号+一个任意的参数名称)在一个函数(或本例中的方法)中定义是指任何数字(包括0)的匿名参数。



这个回答进行了更全面的解释。



这意味着该方法接受一个强制性参数 index ,它是要标记的轴的索引和任意数量的匿名参数(实际标签)。

$ b $知道这一点,应该已经清楚,调用 label()的正确方法如下:

  chart.axes.label(0,'date 1','date 2','date 3',...)

其中 0 是X轴的索引和所有其他参数是各自的标签。



以这种方式实现的原因是指数的数量是未知的 - 这取决于图表,所以接受任何数量的标签似乎是一个好主意



您可能还需要查看这里


I'm using the awesome Google Chart Wrapper to create charts as part of my Django project. Right now I'm creating the chart instance using Python in my views, and then adding the instance to the template context and displaying the charts in my templates successfully.

What I'm having trouble doing is displaying a dynamic set of X-Axis labels. Here is the relevant code from Google Chart Wrapper:

def label(self, index, *args): 
    """ 
    Label each axes one at a time 
    args are of the form <label 1>,...,<label n> 
    APIPARAM: chxl 
    """ 
    self.data['labels'].append( 
    str('%s:|%s'%(index, '|'.join(map(str,args)) )).replace('None','') 
    ) 
    return self.parent 

and how I'm currently creating the chart in my view:

from GChartWrapper import *

chart = Sparkline([1,2,3,4,5,6,7], encoding='text')
chart.axes.label(...) # need to set labels here

I have a list of date strings that I'd like to display as the x-axis labels, does anyone know how I would go about doing that?

解决方案

In order to understand how to properly pass the labels it's important to know what *args means.

*args (which is an asterisk + an arbitrary argument name) in a function (or method as in this case) definition means any number (including 0) of anonymous arguments.

See this answer for a more thorough explanation.

This means that the method in question accepts one obligatory argument index which is the index of the axis to be labelled and any number of anonymous arguments (actual labels).

Knowing this, it should already be clear that the proper way to call label() is as follows:

chart.axes.label( 0, 'date 1', 'date 2', 'date 3', ... )

where 0 is the index of the X-axis and all the other arguments are respective labels.

The reason why this is implemented in this manner is that the number of lables is unknown - it depends on the chart, so accepting any number of labels seems like a good idea.

You may also want to take a look at the example here

这篇关于使用GChartWrapper为Google图表创建动态标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆