[reportlab-users] How to color each bar in a barchart ?

Paul Winkler reportlab-users@reportlab.com
Mon, 8 Jul 2002 14:39:01 -0400


On Mon, Jul 08, 2002 at 02:06:15PM -0300, Marcus Vinicius Laranjeira wrote:
> Let's supose I have a data series like:
> 
> [[1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1, 4, 1, 1, 1, 1, 2, 2, 1, 
> 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1]]

I assume there is a reason this is a list with a list as its only
element...

> and the lenght of this data serie may change based on some configuration of 
> the program...
> 
> How do I know how many bars I have, like:
> 
> for bar in ???:
>     bar.fillColor(color.Color(r,g,b)
> 
> ?!??
> 
> Any hints on this !?

This is basic python stuff, unless I'm misunderstanding what you want.

If you need to know how many elements there are, and keep track
of which one you're currently looking at, use the built in len()
function to find how many items there are, and then use range()
to generate a series of indexes from 0 to len()-1.
Like this:

count = len(data_series[0])
for i in range(count):
    bar = data_series[0][i]
    # do something with bar...


If you just need to loop over the elements, 
and don't care about the count, just do this:

for bar in data_series[0]:
    # do something with bar ...
 
-- 

Paul Winkler
home:  http://www.slinkp.com
"Muppet Labs, where the future is made - today!"