mirror of https://github.com/Kozea/pygal.git
Python to generate nice looking SVG graph
http://pygal.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
249 lines
7.2 KiB
249 lines
7.2 KiB
17 years ago
|
#!python
|
||
|
from itertools import chain
|
||
16 years ago
|
from lxml import etree
|
||
|
from svg.charts.graph import Graph
|
||
17 years ago
|
|
||
17 years ago
|
__all__ = ('VerticalBar', 'HorizontalBar')
|
||
17 years ago
|
|
||
17 years ago
|
class Bar(Graph):
|
||
17 years ago
|
"A superclass for bar-style graphs. Do not instantiate directly."
|
||
|
|
||
|
# gap between bars
|
||
|
bar_gap = True
|
||
|
# how to stack adjacent dataset series
|
||
|
# overlap - overlap bars with transparent colors
|
||
|
# top - stack bars on top of one another
|
||
|
# side - stack bars side-by-side
|
||
|
stack = 'overlap'
|
||
|
|
||
|
scale_divisions = None
|
||
|
|
||
16 years ago
|
css_file = 'bar.css'
|
||
|
|
||
17 years ago
|
def __init__(self, fields, *args, **kargs):
|
||
17 years ago
|
self.fields = fields
|
||
17 years ago
|
super(Bar, self).__init__(*args, **kargs)
|
||
17 years ago
|
|
||
17 years ago
|
# adapted from Plot
|
||
17 years ago
|
def get_data_values(self):
|
||
17 years ago
|
min_value, max_value, scale_division = self.data_range()
|
||
17 years ago
|
result = tuple(float_range(min_value, max_value + scale_division, scale_division))
|
||
17 years ago
|
if self.scale_integers:
|
||
|
result = map(int, result)
|
||
|
return result
|
||
|
|
||
|
# adapted from plot (very much like calling data_range('y'))
|
||
17 years ago
|
def data_range(self):
|
||
|
min_value = self.data_min()
|
||
|
max_value = self.data_max()
|
||
17 years ago
|
range = max_value - min_value
|
||
|
|
||
|
data_pad = range / 20.0 or 10
|
||
17 years ago
|
scale_range = (max_value + data_pad) - min_value
|
||
17 years ago
|
|
||
17 years ago
|
scale_division = self.scale_divisions or (scale_range / 10.0)
|
||
17 years ago
|
|
||
|
if self.scale_integers:
|
||
|
scale_division = round(scale_division) or 1
|
||
|
|
||
|
return min_value, max_value, scale_division
|
||
|
|
||
17 years ago
|
def get_field_labels(self):
|
||
17 years ago
|
return self.fields
|
||
|
|
||
17 years ago
|
def get_data_labels(self):
|
||
|
return map(str, self.get_data_values())
|
||
17 years ago
|
|
||
17 years ago
|
def data_max(self):
|
||
|
return max(chain(*map(lambda set: set['data'], self.data)))
|
||
17 years ago
|
# above is same as
|
||
17 years ago
|
# return max(map(lambda set: max(set['data']), self.data))
|
||
17 years ago
|
|
||
17 years ago
|
def data_min(self):
|
||
17 years ago
|
if not getattr(self, 'min_scale_value') is None: return self.min_scale_value
|
||
17 years ago
|
min_value = min(chain(*map(lambda set: set['data'], self.data)))
|
||
|
min_value = min(min_value, 0)
|
||
17 years ago
|
return min_value
|
||
|
|
||
17 years ago
|
def get_bar_gap(self, field_size):
|
||
|
bar_gap = 10 # default gap
|
||
|
if field_size < 10:
|
||
|
# adjust for narrow fields
|
||
|
bar_gap = field_size / 2
|
||
|
# the following zero's out the gap if bar_gap is False
|
||
|
bar_gap = int(self.bar_gap) * bar_gap
|
||
|
return bar_gap
|
||
|
|
||
17 years ago
|
def float_range(start = 0, stop = None, step = 1):
|
||
17 years ago
|
"Much like the built-in function range, but accepts floats"
|
||
|
while start < stop:
|
||
17 years ago
|
yield float(start)
|
||
17 years ago
|
start += step
|
||
|
|
||
|
|
||
17 years ago
|
class VerticalBar(Bar):
|
||
17 years ago
|
""" # === Create presentation quality SVG bar graphs easily
|
||
|
#
|
||
|
# = Synopsis
|
||
|
#
|
||
|
# require 'SVG/Graph/Bar'
|
||
|
#
|
||
|
# fields = %w(Jan Feb Mar);
|
||
|
# data_sales_02 = [12, 45, 21]
|
||
|
#
|
||
|
# graph = SVG::Graph::Bar.new(
|
||
|
# :height => 500,
|
||
|
# :width => 300,
|
||
|
# :fields => fields
|
||
17 years ago
|
# )
|
||
17 years ago
|
#
|
||
|
# graph.add_data(
|
||
|
# :data => data_sales_02,
|
||
|
# :title => 'Sales 2002'
|
||
17 years ago
|
# )
|
||
17 years ago
|
#
|
||
|
# print "Content-type: image/svg+xml\r\n\r\n"
|
||
|
# print graph.burn
|
||
|
#
|
||
|
# = Description
|
||
|
#
|
||
|
# This object aims to allow you to easily create high quality
|
||
|
# SVG[http://www.w3c.org/tr/svg bar graphs. You can either use the default
|
||
|
# style sheet or supply your own. Either way there are many options which
|
||
|
# can be configured to give you control over how the graph is generated -
|
||
|
# with or without a key, data elements at each point, title, subtitle etc.
|
||
|
#
|
||
|
# = Notes
|
||
|
#
|
||
|
# The default stylesheet handles upto 12 data sets, if you
|
||
|
# use more you must create your own stylesheet and add the
|
||
|
# additional settings for the extra data sets. You will know
|
||
|
# if you go over 12 data sets as they will have no style and
|
||
|
# be in black.
|
||
|
#
|
||
|
# = Examples
|
||
|
#
|
||
|
# * http://germane-software.com/repositories/public/SVG/test/test.rb
|
||
|
#
|
||
|
# = See also
|
||
|
#
|
||
|
# * SVG::Graph::Graph
|
||
|
# * SVG::Graph::BarHorizontal
|
||
|
# * SVG::Graph::Line
|
||
|
# * SVG::Graph::Pie
|
||
|
# * SVG::Graph::Plot
|
||
|
# * SVG::Graph::TimeSeries
|
||
|
"""
|
||
|
top_align = top_font = 1
|
||
|
|
||
17 years ago
|
def get_x_labels(self):
|
||
17 years ago
|
return self.get_field_labels()
|
||
17 years ago
|
|
||
17 years ago
|
def get_y_labels(self):
|
||
17 years ago
|
return self.get_data_labels()
|
||
17 years ago
|
|
||
17 years ago
|
def x_label_offset(self, width):
|
||
17 years ago
|
return width / 2.0
|
||
|
|
||
17 years ago
|
def draw_data(self):
|
||
17 years ago
|
min_value = self.data_min()
|
||
|
unit_size = (float(self.graph_height) - self.font_size*2*self.top_font)
|
||
17 years ago
|
unit_size /= (max(self.get_data_values()) - min(self.get_data_values()))
|
||
17 years ago
|
|
||
|
bar_gap = self.get_bar_gap(self.get_field_width())
|
||
17 years ago
|
|
||
|
bar_width = self.get_field_width() - bar_gap
|
||
|
if self.stack == 'side':
|
||
17 years ago
|
bar_width /= len(self.data)
|
||
17 years ago
|
|
||
17 years ago
|
x_mod = (self.graph_width - bar_gap)/2
|
||
17 years ago
|
if self.stack == 'side':
|
||
|
x_mod -= bar_width/2
|
||
|
|
||
|
bottom = self.graph_height
|
||
|
|
||
17 years ago
|
for field_count, field in enumerate(self.fields):
|
||
|
for dataset_count, dataset in enumerate(self.data):
|
||
17 years ago
|
# cases (assume 0 = +ve):
|
||
|
# value min length
|
||
|
# +ve +ve value - min
|
||
|
# +ve -ve value - 0
|
||
|
# -ve -ve value.abs - 0
|
||
|
value = dataset['data'][field_count]
|
||
|
|
||
17 years ago
|
left = self.get_field_width() * field_count
|
||
17 years ago
|
|
||
17 years ago
|
length = (abs(value) - max(min_value, 0)) * unit_size
|
||
17 years ago
|
# top is 0 if value is negative
|
||
17 years ago
|
top = bottom - ((max(value,0) - min_value) * unit_size)
|
||
17 years ago
|
if self.stack == 'side':
|
||
17 years ago
|
left += bar_width * dataset_count
|
||
17 years ago
|
|
||
16 years ago
|
rect = etree.SubElement(self.graph, 'rect', {
|
||
17 years ago
|
'x': str(left),
|
||
|
'y': str(top),
|
||
|
'width': str(bar_width),
|
||
|
'height': str(length),
|
||
17 years ago
|
'class': 'fill%s' % (dataset_count+1),
|
||
17 years ago
|
})
|
||
17 years ago
|
|
||
17 years ago
|
self.make_datapoint_text(left + bar_width/2.0, top-6, value)
|
||
17 years ago
|
|
||
|
class HorizontalBar(Bar):
|
||
|
rotate_y_labels = True
|
||
|
show_x_guidelines = True
|
||
|
show_y_guidelines = False
|
||
|
right_align = right_font = True
|
||
|
|
||
|
|
||
|
def get_x_labels(self):
|
||
|
return self.get_data_labels()
|
||
|
|
||
|
def get_y_labels(self):
|
||
|
return self.get_field_labels()
|
||
|
|
||
|
def y_label_offset(self, height):
|
||
|
return height / -2.0
|
||
|
|
||
|
def draw_data(self):
|
||
|
min_value = self.data_min()
|
||
|
|
||
|
unit_size = float(self.graph_width)
|
||
|
unit_size -= self.font_size*2*self.right_font
|
||
|
unit_size /= max(self.get_data_values()) - min(self.get_data_values())
|
||
|
|
||
|
bar_gap = self.get_bar_gap(self.get_field_height())
|
||
|
|
||
|
bar_height = self.get_field_height() - bar_gap
|
||
|
if self.stack == 'side':
|
||
|
bar_height /= len(self.data)
|
||
|
|
||
|
y_mod = (bar_height / 2) + (self.font_size / 2)
|
||
|
|
||
|
for field_count, field in enumerate(self.fields):
|
||
|
for dataset_count, dataset in enumerate(self.data):
|
||
|
value = dataset['data'][field_count]
|
||
|
|
||
|
top = self.graph_height - (self.get_field_height() * (field_count+1))
|
||
|
if self.stack == 'side':
|
||
|
top += (bar_height * dataset_count)
|
||
|
# cases (assume 0 = +ve):
|
||
|
# value min length left
|
||
|
# +ve +ve value.abs - min minvalue.abs
|
||
|
# +ve -ve value.abs - 0 minvalue.abs
|
||
|
# -ve -ve value.abs - 0 minvalue.abs + value
|
||
|
length = (abs(value) - max(min_value, 0)) * unit_size
|
||
|
# left is 0 if value is negative
|
||
|
left = (abs(min_value) + min(value, 0)) * unit_size
|
||
|
|
||
16 years ago
|
rect = etree.SubElement(self.graph, 'rect', {
|
||
17 years ago
|
'x': str(left),
|
||
|
'y': str(top),
|
||
|
'width': str(length),
|
||
|
'height': str(bar_height),
|
||
|
'class': 'fill%s' % (dataset_count+1),
|
||
|
})
|
||
|
|
||
|
self.make_datapoint_text(left+length+5, top+y_mod, value,
|
||
|
"text-anchor: start; ")
|