|
|
@ -1,5 +1,5 @@ |
|
|
|
#!python |
|
|
|
#!python |
|
|
|
# -*- coding: UTF-8 -*- |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
svg.charts.graph |
|
|
|
svg.charts.graph |
|
|
@ -14,20 +14,24 @@ import functools |
|
|
|
|
|
|
|
|
|
|
|
import cssutils |
|
|
|
import cssutils |
|
|
|
from lxml import etree |
|
|
|
from lxml import etree |
|
|
|
|
|
|
|
from xml import xpath |
|
|
|
|
|
|
|
|
|
|
|
from svg.charts import css # causes the SVG profile to be loaded |
|
|
|
from svg.charts import css # causes the SVG profile to be loaded |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
import zlib |
|
|
|
import zlib |
|
|
|
except ImportError: |
|
|
|
except ImportError: |
|
|
|
zlib = None |
|
|
|
zlib = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sort_multiple(arrays): |
|
|
|
def sort_multiple(arrays): |
|
|
|
"sort multiple lists (of equal size) using the first list for the sort keys" |
|
|
|
"sort multiple lists (of equal size) " |
|
|
|
|
|
|
|
"using the first list for the sort keys" |
|
|
|
tuples = zip(*arrays) |
|
|
|
tuples = zip(*arrays) |
|
|
|
tuples.sort() |
|
|
|
tuples.sort() |
|
|
|
return zip(*tuples) |
|
|
|
return zip(*tuples) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Graph(object): |
|
|
|
class Graph(object): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Base object for generating SVG Graphs |
|
|
|
Base object for generating SVG Graphs |
|
|
@ -47,46 +51,48 @@ class Graph(object): |
|
|
|
* svg.charts.time_series |
|
|
|
* svg.charts.time_series |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
width= 500 |
|
|
|
width = 500 |
|
|
|
height= 300 |
|
|
|
height = 300 |
|
|
|
show_x_guidelines= False |
|
|
|
show_x_guidelines = False |
|
|
|
show_y_guidelines= True |
|
|
|
show_y_guidelines = True |
|
|
|
show_data_values= True |
|
|
|
show_data_values = True |
|
|
|
min_scale_value= None |
|
|
|
min_scale_value = None |
|
|
|
show_x_labels= True |
|
|
|
show_x_labels = True |
|
|
|
stagger_x_labels= False |
|
|
|
stagger_x_labels = False |
|
|
|
rotate_x_labels= False |
|
|
|
rotate_x_labels = False |
|
|
|
step_x_labels= 1 |
|
|
|
step_x_labels = 1 |
|
|
|
step_include_first_x_label= True |
|
|
|
step_include_first_x_label = True |
|
|
|
show_y_labels= True |
|
|
|
show_y_labels = True |
|
|
|
rotate_y_labels= False |
|
|
|
rotate_y_labels = False |
|
|
|
stagger_y_labels= False |
|
|
|
stagger_y_labels = False |
|
|
|
step_include_first_y_label= True |
|
|
|
step_include_first_y_label = True |
|
|
|
step_y_labels= 1 |
|
|
|
step_y_labels = 1 |
|
|
|
scale_integers= False |
|
|
|
scale_integers = False |
|
|
|
show_x_title= False |
|
|
|
show_x_title = False |
|
|
|
x_title= 'X Field names' |
|
|
|
x_title = 'X Field names' |
|
|
|
show_y_title= False |
|
|
|
show_y_title = False |
|
|
|
y_title_text_direction= 'bt' # 'bt' for bottom to top; 'tb' for top to bottom |
|
|
|
# 'bt' for bottom to top; 'tb' for top to bottom |
|
|
|
y_title= 'Y Scale' |
|
|
|
y_title_text_direction = 'bt' |
|
|
|
show_graph_title= False |
|
|
|
y_title = 'Y Scale' |
|
|
|
graph_title= 'Graph Title' |
|
|
|
show_graph_title = False |
|
|
|
show_graph_subtitle= False |
|
|
|
graph_title = 'Graph Title' |
|
|
|
graph_subtitle= 'Graph Subtitle' |
|
|
|
show_graph_subtitle = False |
|
|
|
key= True |
|
|
|
graph_subtitle = 'Graph Subtitle' |
|
|
|
key_position= 'right' # 'bottom' or 'right', |
|
|
|
key = True |
|
|
|
|
|
|
|
# 'bottom' or 'right', |
|
|
|
font_size= 12 |
|
|
|
key_position = 'right' |
|
|
|
title_font_size= 16 |
|
|
|
|
|
|
|
subtitle_font_size= 14 |
|
|
|
font_size = 12 |
|
|
|
x_label_font_size= 12 |
|
|
|
title_font_size = 16 |
|
|
|
x_title_font_size= 14 |
|
|
|
subtitle_font_size = 14 |
|
|
|
y_label_font_size= 12 |
|
|
|
x_label_font_size = 12 |
|
|
|
y_title_font_size= 14 |
|
|
|
x_title_font_size = 14 |
|
|
|
key_font_size= 10 |
|
|
|
y_label_font_size = 12 |
|
|
|
|
|
|
|
y_title_font_size = 14 |
|
|
|
css_inline= False |
|
|
|
key_font_size = 10 |
|
|
|
add_popups= False |
|
|
|
|
|
|
|
|
|
|
|
css_inline = False |
|
|
|
|
|
|
|
add_popups = False |
|
|
|
|
|
|
|
|
|
|
|
top_align = top_font = right_align = right_font = 0 |
|
|
|
top_align = top_font = right_align = right_font = 0 |
|
|
|
|
|
|
|
|
|
|
@ -94,7 +100,7 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
stylesheet_names = ['graph.css'] |
|
|
|
stylesheet_names = ['graph.css'] |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config = {}): |
|
|
|
def __init__(self, config={}): |
|
|
|
"""Initialize the graph object with the graph settings.""" |
|
|
|
"""Initialize the graph object with the graph settings.""" |
|
|
|
if self.__class__ is Graph: |
|
|
|
if self.__class__ is Graph: |
|
|
|
raise NotImplementedError("Graph is an abstract base class") |
|
|
|
raise NotImplementedError("Graph is an abstract base class") |
|
|
@ -124,12 +130,15 @@ class Graph(object): |
|
|
|
try: |
|
|
|
try: |
|
|
|
assert(isinstance(conf['data'], (tuple, list))) |
|
|
|
assert(isinstance(conf['data'], (tuple, list))) |
|
|
|
except TypeError, e: |
|
|
|
except TypeError, e: |
|
|
|
raise TypeError, "conf should be dictionary with 'data' and other items" |
|
|
|
raise TypeError( |
|
|
|
|
|
|
|
"conf should be dictionary with 'data' and other items") |
|
|
|
except AssertionError: |
|
|
|
except AssertionError: |
|
|
|
if not hasattr(conf['data'], '__iter__'): |
|
|
|
if not hasattr(conf['data'], '__iter__'): |
|
|
|
raise TypeError, "conf['data'] should be tuple or list or iterable" |
|
|
|
raise TypeError( |
|
|
|
|
|
|
|
"conf['data'] should be tuple or list or iterable") |
|
|
|
|
|
|
|
|
|
|
|
def process_data(self, data): pass |
|
|
|
def process_data(self, data): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def clear_data(self): |
|
|
|
def clear_data(self): |
|
|
|
""" |
|
|
|
""" |
|
|
@ -148,9 +157,11 @@ class Graph(object): |
|
|
|
Raises ValueError when no data set has |
|
|
|
Raises ValueError when no data set has |
|
|
|
been added to the graph object. |
|
|
|
been added to the graph object. |
|
|
|
""" |
|
|
|
""" |
|
|
|
if not self.data: raise ValueError("No data available") |
|
|
|
if not self.data: |
|
|
|
|
|
|
|
raise ValueError("No data available") |
|
|
|
|
|
|
|
|
|
|
|
if hasattr(self, 'calculations'): self.calculations() |
|
|
|
if hasattr(self, 'calculations'): |
|
|
|
|
|
|
|
self.calculations() |
|
|
|
|
|
|
|
|
|
|
|
self.start_svg() |
|
|
|
self.start_svg() |
|
|
|
self.calculate_graph_dimensions() |
|
|
|
self.calculate_graph_dimensions() |
|
|
@ -166,9 +177,12 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
def _burn_compressed(self): |
|
|
|
def _burn_compressed(self): |
|
|
|
if self.compress and not zlib: |
|
|
|
if self.compress and not zlib: |
|
|
|
self.root.addprevious(etree.Comment('Python zlib not available for SVGZ')) |
|
|
|
self.root.addprevious( |
|
|
|
|
|
|
|
etree.Comment('Python zlib not available for SVGZ')) |
|
|
|
|
|
|
|
|
|
|
|
data = etree.tostring(self.root, pretty_print=True, xml_declaration=True, encoding='utf-8') |
|
|
|
data = etree.tostring( |
|
|
|
|
|
|
|
self.root, pretty_print=True, |
|
|
|
|
|
|
|
xml_declaration=True, encoding='utf-8') |
|
|
|
|
|
|
|
|
|
|
|
if self.compress and zlib: |
|
|
|
if self.compress and zlib: |
|
|
|
data = zlib.compress(data) |
|
|
|
data = zlib.compress(data) |
|
|
@ -189,10 +203,14 @@ class Graph(object): |
|
|
|
else: |
|
|
|
else: |
|
|
|
label_lengths = map(len, self.get_y_labels()) |
|
|
|
label_lengths = map(len, self.get_y_labels()) |
|
|
|
max_y_label_len = max(label_lengths) |
|
|
|
max_y_label_len = max(label_lengths) |
|
|
|
max_y_label_height_px = 0.6 * max_y_label_len * self.y_label_font_size |
|
|
|
max_y_label_height_px = (0.6 * max_y_label_len * |
|
|
|
if self.show_y_labels: bl += max_y_label_height_px |
|
|
|
self.y_label_font_size) |
|
|
|
if self.stagger_y_labels: bl += max_y_label_height_px + 10 |
|
|
|
if self.show_y_labels: |
|
|
|
if self.show_y_title: bl += self.y_title_font_size + 5 |
|
|
|
bl += max_y_label_height_px |
|
|
|
|
|
|
|
if self.stagger_y_labels: |
|
|
|
|
|
|
|
bl += max_y_label_height_px + 10 |
|
|
|
|
|
|
|
if self.show_y_title: |
|
|
|
|
|
|
|
bl += self.y_title_font_size + 5 |
|
|
|
self.border_left = bl |
|
|
|
self.border_left = bl |
|
|
|
|
|
|
|
|
|
|
|
def max_y_label_width_px(self): |
|
|
|
def max_y_label_width_px(self): |
|
|
@ -222,17 +240,19 @@ class Graph(object): |
|
|
|
border_top. |
|
|
|
border_top. |
|
|
|
""" |
|
|
|
""" |
|
|
|
self.border_top = 5 |
|
|
|
self.border_top = 5 |
|
|
|
if self.show_graph_title: self.border_top += self.title_font_size |
|
|
|
if self.show_graph_title: |
|
|
|
|
|
|
|
self.border_top += self.title_font_size |
|
|
|
self.border_top += 5 |
|
|
|
self.border_top += 5 |
|
|
|
if self.show_graph_subtitle: self.border_top += self.subtitle_font_size |
|
|
|
if self.show_graph_subtitle: |
|
|
|
|
|
|
|
self.border_top += self.subtitle_font_size |
|
|
|
|
|
|
|
|
|
|
|
def add_popup(self, x, y, label): |
|
|
|
def add_popup(self, x, y, label): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Add pop-up information to a point on the graph. |
|
|
|
Add pop-up information to a point on the graph. |
|
|
|
""" |
|
|
|
""" |
|
|
|
txt_width = len(label) * self.font_size * 0.6 + 10 |
|
|
|
txt_width = len(label) * self.font_size * 0.6 + 10 |
|
|
|
tx = x + [5,-5][int(x+txt_width > self.width)] |
|
|
|
tx = x + [5, -5][int(x + txt_width > self.width)] |
|
|
|
anchor = ['start', 'end'][x+txt_width > self.width] |
|
|
|
anchor = ['start', 'end'][x + txt_width > self.width] |
|
|
|
style = 'fill: #000; text-anchor: %s;' % anchor |
|
|
|
style = 'fill: #000; text-anchor: %s;' % anchor |
|
|
|
id = 'label-%s' % label |
|
|
|
id = 'label-%s' % label |
|
|
|
t = etree.SubElement(self.foreground, 'text', { |
|
|
|
t = etree.SubElement(self.foreground, 'text', { |
|
|
@ -245,7 +265,8 @@ class Graph(object): |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
# add the circle element to the foreground |
|
|
|
# add the circle element to the foreground |
|
|
|
visibility = "document.getElementById('%s').setAttribute('visibility', %%s)" % id |
|
|
|
visibility = ("document.getElementById('%s')." |
|
|
|
|
|
|
|
"setAttribute('visibility', %%s)" % id) |
|
|
|
t = etree.SubElement(self.foreground, 'circle', { |
|
|
|
t = etree.SubElement(self.foreground, 'circle', { |
|
|
|
'cx': str(x), |
|
|
|
'cx': str(x), |
|
|
|
'cy': str(y), |
|
|
|
'cy': str(y), |
|
|
@ -271,8 +292,10 @@ class Graph(object): |
|
|
|
max_x_label_len = reduce(max, label_lengths) |
|
|
|
max_x_label_len = reduce(max, label_lengths) |
|
|
|
max_x_label_height_px *= 0.6 * max_x_label_len |
|
|
|
max_x_label_height_px *= 0.6 * max_x_label_len |
|
|
|
bb += max_x_label_height_px |
|
|
|
bb += max_x_label_height_px |
|
|
|
if self.stagger_x_labels: bb += max_x_label_height_px + 10 |
|
|
|
if self.stagger_x_labels: |
|
|
|
if self.show_x_title: bb += self.x_title_font_size + 5 |
|
|
|
bb += max_x_label_height_px + 10 |
|
|
|
|
|
|
|
if self.show_x_title: |
|
|
|
|
|
|
|
bb += self.x_title_font_size + 5 |
|
|
|
self.border_bottom = bb |
|
|
|
self.border_bottom = bb |
|
|
|
|
|
|
|
|
|
|
|
def draw_graph(self): |
|
|
|
def draw_graph(self): |
|
|
@ -336,7 +359,8 @@ class Graph(object): |
|
|
|
'y': str(y), |
|
|
|
'y': str(y), |
|
|
|
'class': 'dataPointLabel'}) |
|
|
|
'class': 'dataPointLabel'}) |
|
|
|
e.text = str(value) |
|
|
|
e.text = str(value) |
|
|
|
if style: e.set('style', style) |
|
|
|
if style: |
|
|
|
|
|
|
|
e.set('style', style) |
|
|
|
|
|
|
|
|
|
|
|
def draw_x_labels(self): |
|
|
|
def draw_x_labels(self): |
|
|
|
"Draw the X axis labels" |
|
|
|
"Draw the X axis labels" |
|
|
@ -374,7 +398,7 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
if self.rotate_x_labels: |
|
|
|
if self.rotate_x_labels: |
|
|
|
transform = 'rotate(90 %d %d) translate(0 -%d)' % \ |
|
|
|
transform = 'rotate(90 %d %d) translate(0 -%d)' % \ |
|
|
|
(x, y-self.x_label_font_size, self.x_label_font_size/4) |
|
|
|
(x, y - self.x_label_font_size, self.x_label_font_size / 4) |
|
|
|
text.set('transform', transform) |
|
|
|
text.set('transform', transform) |
|
|
|
text.set('style', 'text-anchor: start') |
|
|
|
text.set('style', 'text-anchor: start') |
|
|
|
else: |
|
|
|
else: |
|
|
@ -388,13 +412,14 @@ class Graph(object): |
|
|
|
return 0 |
|
|
|
return 0 |
|
|
|
|
|
|
|
|
|
|
|
def get_field_width(self): |
|
|
|
def get_field_width(self): |
|
|
|
return float(self.graph_width - self.font_size*2*self.right_font) / \ |
|
|
|
return (float( |
|
|
|
(len(self.get_x_labels()) - self.right_align) |
|
|
|
self.graph_width - self.font_size * 2 * self.right_font) / |
|
|
|
|
|
|
|
(len(self.get_x_labels()) - self.right_align)) |
|
|
|
field_width = get_field_width |
|
|
|
field_width = get_field_width |
|
|
|
|
|
|
|
|
|
|
|
def get_field_height(self): |
|
|
|
def get_field_height(self): |
|
|
|
return float(self.graph_height - self.font_size*2*self.top_font) / \ |
|
|
|
return (float(self.graph_height - self.font_size * 2 * self.top_font) / |
|
|
|
(len(self.get_y_labels()) - self.top_align) |
|
|
|
(len(self.get_y_labels()) - self.top_align)) |
|
|
|
field_height = get_field_height |
|
|
|
field_height = get_field_height |
|
|
|
|
|
|
|
|
|
|
|
def draw_y_labels(self): |
|
|
|
def draw_y_labels(self): |
|
|
@ -414,7 +439,8 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
def get_y_offset(self): |
|
|
|
def get_y_offset(self): |
|
|
|
result = self.graph_height + self.y_label_offset(self.field_height()) |
|
|
|
result = self.graph_height + self.y_label_offset(self.field_height()) |
|
|
|
if not self.rotate_y_labels: result += self.font_size/1.2 |
|
|
|
if not self.rotate_y_labels: |
|
|
|
|
|
|
|
result += self.font_size / 1.2 |
|
|
|
return result |
|
|
|
return result |
|
|
|
y_offset = property(get_y_offset) |
|
|
|
y_offset = property(get_y_offset) |
|
|
|
|
|
|
|
|
|
|
@ -425,7 +451,7 @@ class Graph(object): |
|
|
|
text.text = label |
|
|
|
text.text = label |
|
|
|
|
|
|
|
|
|
|
|
y = self.y_offset - (label_height * index) |
|
|
|
y = self.y_offset - (label_height * index) |
|
|
|
x = {True: 0, False:-3}[self.rotate_y_labels] |
|
|
|
x = {True: 0, False: -3}[self.rotate_y_labels] |
|
|
|
|
|
|
|
|
|
|
|
if self.stagger_y_labels and (index % 2): |
|
|
|
if self.stagger_y_labels and (index % 2): |
|
|
|
stagger = self.y_label_font_size + 5 |
|
|
|
stagger = self.y_label_font_size + 5 |
|
|
@ -444,15 +470,16 @@ class Graph(object): |
|
|
|
text.set('transform', transform) |
|
|
|
text.set('transform', transform) |
|
|
|
text.set('style', 'text-anchor: middle') |
|
|
|
text.set('style', 'text-anchor: middle') |
|
|
|
else: |
|
|
|
else: |
|
|
|
text.set('y', str(y - self.y_label_font_size/2)) |
|
|
|
text.set('y', str(y - self.y_label_font_size / 2)) |
|
|
|
text.set('style', 'text-anchor: end') |
|
|
|
text.set('style', 'text-anchor: end') |
|
|
|
|
|
|
|
|
|
|
|
def draw_x_guidelines(self, label_height, count): |
|
|
|
def draw_x_guidelines(self, label_height, count): |
|
|
|
"Draw the X-axis guidelines" |
|
|
|
"Draw the X-axis guidelines" |
|
|
|
if not self.show_x_guidelines: return |
|
|
|
if not self.show_x_guidelines: |
|
|
|
|
|
|
|
return |
|
|
|
# skip the first one |
|
|
|
# skip the first one |
|
|
|
for count in range(1,count): |
|
|
|
for count in range(1, count): |
|
|
|
start = label_height*count |
|
|
|
start = label_height * count |
|
|
|
stop = self.graph_height |
|
|
|
stop = self.graph_height |
|
|
|
path = etree.SubElement(self.graph, 'path', { |
|
|
|
path = etree.SubElement(self.graph, 'path', { |
|
|
|
'd': 'M %(start)s 0 v%(stop)s' % vars(), |
|
|
|
'd': 'M %(start)s 0 v%(stop)s' % vars(), |
|
|
@ -460,9 +487,10 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
def draw_y_guidelines(self, label_height, count): |
|
|
|
def draw_y_guidelines(self, label_height, count): |
|
|
|
"Draw the Y-axis guidelines" |
|
|
|
"Draw the Y-axis guidelines" |
|
|
|
if not self.show_y_guidelines: return |
|
|
|
if not self.show_y_guidelines: |
|
|
|
|
|
|
|
return |
|
|
|
for count in range(1, count): |
|
|
|
for count in range(1, count): |
|
|
|
start = self.graph_height - label_height*count |
|
|
|
start = self.graph_height - label_height * count |
|
|
|
stop = self.graph_width |
|
|
|
stop = self.graph_width |
|
|
|
path = etree.SubElement(self.graph, 'path', { |
|
|
|
path = etree.SubElement(self.graph, 'path', { |
|
|
|
'd': 'M 0 %(start)s h%(stop)s' % vars(), |
|
|
|
'd': 'M 0 %(start)s h%(stop)s' % vars(), |
|
|
@ -470,10 +498,14 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
def draw_titles(self): |
|
|
|
def draw_titles(self): |
|
|
|
"Draws the graph title and subtitle" |
|
|
|
"Draws the graph title and subtitle" |
|
|
|
if self.show_graph_title: self.draw_graph_title() |
|
|
|
if self.show_graph_title: |
|
|
|
if self.show_graph_subtitle: self.draw_graph_subtitle() |
|
|
|
self.draw_graph_title() |
|
|
|
if self.show_x_title: self.draw_x_title() |
|
|
|
if self.show_graph_subtitle: |
|
|
|
if self.show_y_title: self.draw_y_title() |
|
|
|
self.draw_graph_subtitle() |
|
|
|
|
|
|
|
if self.show_x_title: |
|
|
|
|
|
|
|
self.draw_x_title() |
|
|
|
|
|
|
|
if self.show_y_title: |
|
|
|
|
|
|
|
self.draw_y_title() |
|
|
|
|
|
|
|
|
|
|
|
def draw_graph_title(self): |
|
|
|
def draw_graph_title(self): |
|
|
|
text = etree.SubElement(self.root, 'text', { |
|
|
|
text = etree.SubElement(self.root, 'text', { |
|
|
@ -483,10 +515,11 @@ class Graph(object): |
|
|
|
text.text = self.graph_title |
|
|
|
text.text = self.graph_title |
|
|
|
|
|
|
|
|
|
|
|
def draw_graph_subtitle(self): |
|
|
|
def draw_graph_subtitle(self): |
|
|
|
y_subtitle_options = [subtitle_font_size, title_font_size+10] |
|
|
|
y_subtitle_options = [self.subtitle_font_size, |
|
|
|
|
|
|
|
self.title_font_size + 10] |
|
|
|
y_subtitle = y_subtitle_options[self.show_graph_title] |
|
|
|
y_subtitle = y_subtitle_options[self.show_graph_title] |
|
|
|
text = etree.SubElement(self.root, 'text', { |
|
|
|
text = etree.SubElement(self.root, 'text', { |
|
|
|
'x': str(self.width/2), |
|
|
|
'x': str(self.width / 2), |
|
|
|
'y': str(y_subtitle), |
|
|
|
'y': str(y_subtitle), |
|
|
|
'class': 'subTitle', |
|
|
|
'class': 'subTitle', |
|
|
|
}) |
|
|
|
}) |
|
|
@ -495,8 +528,9 @@ class Graph(object): |
|
|
|
def draw_x_title(self): |
|
|
|
def draw_x_title(self): |
|
|
|
y = self.graph_height + self.border_top + self.x_title_font_size |
|
|
|
y = self.graph_height + self.border_top + self.x_title_font_size |
|
|
|
if self.show_x_labels: |
|
|
|
if self.show_x_labels: |
|
|
|
y_size = self.x_label_font_size+5 |
|
|
|
y_size = self.x_label_font_size + 5 |
|
|
|
if self.stagger_x_labels: y_size*=2 |
|
|
|
if self.stagger_x_labels: |
|
|
|
|
|
|
|
y_size *= 2 |
|
|
|
y += y_size |
|
|
|
y += y_size |
|
|
|
x = self.width / 2 |
|
|
|
x = self.width / 2 |
|
|
|
|
|
|
|
|
|
|
@ -509,7 +543,7 @@ class Graph(object): |
|
|
|
|
|
|
|
|
|
|
|
def draw_y_title(self): |
|
|
|
def draw_y_title(self): |
|
|
|
x = self.y_title_font_size |
|
|
|
x = self.y_title_font_size |
|
|
|
if self.y_title_text_direction=='bt': |
|
|
|
if self.y_title_text_direction == 'bt': |
|
|
|
x += 3 |
|
|
|
x += 3 |
|
|
|
rotate = -90 |
|
|
|
rotate = -90 |
|
|
|
else: |
|
|
|
else: |
|
|
@ -560,16 +594,16 @@ class Graph(object): |
|
|
|
x_offset = self.border_left + 20 |
|
|
|
x_offset = self.border_left + 20 |
|
|
|
y_offset = self.border_top + self.graph_height + 5 |
|
|
|
y_offset = self.border_top + self.graph_height + 5 |
|
|
|
if self.show_x_labels: |
|
|
|
if self.show_x_labels: |
|
|
|
max_x_label_height_px = x_label_font_size |
|
|
|
max_x_label_height_px = self.x_label_font_size |
|
|
|
if self.rotate_x_labels: |
|
|
|
if self.rotate_x_labels: |
|
|
|
longest_label_length = max(map(len, self.get_x_labels())) |
|
|
|
longest_label_length = max(map(len, self.get_x_labels())) |
|
|
|
# note: I think 0.6 is the ratio of width to height of characters |
|
|
|
# I think 0.6 is the ratio of width to height of characters |
|
|
|
max_x_label_height_px *= longest_label_length * 0.6 |
|
|
|
max_x_label_height_px *= longest_label_length * 0.6 |
|
|
|
y_offset += max_x_label_height_px |
|
|
|
y_offset += max_x_label_height_px |
|
|
|
if self.stagger_x_labels: |
|
|
|
if self.stagger_x_labels: |
|
|
|
y_offset += max_x_label_height_px + 5 |
|
|
|
y_offset += max_x_label_height_px + 5 |
|
|
|
if self.show_x_title: |
|
|
|
if self.show_x_title: |
|
|
|
y_offset += x_title_font_size + 5 |
|
|
|
y_offset += self.x_title_font_size + 5 |
|
|
|
return x_offset, y_offset |
|
|
|
return x_offset, y_offset |
|
|
|
|
|
|
|
|
|
|
|
def render_inline_styles(self): |
|
|
|
def render_inline_styles(self): |
|
|
@ -612,11 +646,12 @@ class Graph(object): |
|
|
|
'xlink': 'http://www.w3.org/1999/xlink', |
|
|
|
'xlink': 'http://www.w3.org/1999/xlink', |
|
|
|
'a3': 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/', |
|
|
|
'a3': 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/', |
|
|
|
} |
|
|
|
} |
|
|
|
self.root = etree.Element(SVG+"svg", attrib={ |
|
|
|
self.root = etree.Element(SVG + "svg", attrib={ |
|
|
|
'width': str(self.width), |
|
|
|
'width': str(self.width), |
|
|
|
'height': str(self.height), |
|
|
|
'height': str(self.height), |
|
|
|
'viewBox': '0 0 %s %s' % (self.width, self.height), |
|
|
|
'viewBox': '0 0 %s %s' % (self.width, self.height), |
|
|
|
'{http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/}scriptImplementation': 'Adobe', |
|
|
|
'{http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/}' |
|
|
|
|
|
|
|
'scriptImplementation': 'Adobe', |
|
|
|
}, nsmap=NSMAP) |
|
|
|
}, nsmap=NSMAP) |
|
|
|
if hasattr(self, 'style_sheet_href'): |
|
|
|
if hasattr(self, 'style_sheet_href'): |
|
|
|
pi = etree.ProcessingInstruction( |
|
|
|
pi = etree.ProcessingInstruction( |
|
|
@ -630,7 +665,7 @@ class Graph(object): |
|
|
|
' SVG.Graph by Jason R. Coombs ', |
|
|
|
' SVG.Graph by Jason R. Coombs ', |
|
|
|
' Based on SVG::Graph by Sean E. Russel ', |
|
|
|
' Based on SVG::Graph by Sean E. Russel ', |
|
|
|
' Based on Perl SVG:TT:Graph by Leo Lapworth & Stephan Morgan ', |
|
|
|
' Based on Perl SVG:TT:Graph by Leo Lapworth & Stephan Morgan ', |
|
|
|
' '+'/'*66, |
|
|
|
' ' + '/' * 66, |
|
|
|
) |
|
|
|
) |
|
|
|
map(self.root.append, map(etree.Comment, comment_strings)) |
|
|
|
map(self.root.append, map(etree.Comment, comment_strings)) |
|
|
|
|
|
|
|
|
|
|
@ -638,10 +673,11 @@ class Graph(object): |
|
|
|
self.add_defs(defs) |
|
|
|
self.add_defs(defs) |
|
|
|
|
|
|
|
|
|
|
|
if not hasattr(self, 'style_sheet_href') and not self.css_inline: |
|
|
|
if not hasattr(self, 'style_sheet_href') and not self.css_inline: |
|
|
|
self.root.append(etree.Comment(' include default stylesheet if none specified ')) |
|
|
|
self.root.append(etree.Comment( |
|
|
|
|
|
|
|
' include default stylesheet if none specified ')) |
|
|
|
style = etree.SubElement(defs, 'style', type='text/css') |
|
|
|
style = etree.SubElement(defs, 'style', type='text/css') |
|
|
|
# TODO: the text was previously escaped in a CDATA declaration... how |
|
|
|
# TODO: the text was previously escaped in a CDATA declaration... |
|
|
|
# to do that with etree? |
|
|
|
# how to do that with etree? |
|
|
|
style.text = self.get_stylesheet().cssText |
|
|
|
style.text = self.get_stylesheet().cssText |
|
|
|
|
|
|
|
|
|
|
|
self.root.append(etree.Comment('SVG Background')) |
|
|
|
self.root.append(etree.Comment('SVG Background')) |
|
|
@ -678,12 +714,14 @@ class Graph(object): |
|
|
|
return sheets |
|
|
|
return sheets |
|
|
|
|
|
|
|
|
|
|
|
def get_stylesheet(self): |
|
|
|
def get_stylesheet(self): |
|
|
|
cssutils.log.setLevel(30) # disable INFO log messages |
|
|
|
cssutils.log.setLevel(30) # disable INFO log messages |
|
|
|
|
|
|
|
|
|
|
|
def merge_sheets(s1, s2): |
|
|
|
def merge_sheets(s1, s2): |
|
|
|
map(s1.add, s2) |
|
|
|
map(s1.add, s2) |
|
|
|
return s1 |
|
|
|
return s1 |
|
|
|
return reduce(merge_sheets, self.get_stylesheet_resources()) |
|
|
|
return reduce(merge_sheets, self.get_stylesheet_resources()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class class_dict(object): |
|
|
|
class class_dict(object): |
|
|
|
"Emulates a dictionary, but retrieves class attributes" |
|
|
|
"Emulates a dictionary, but retrieves class attributes" |
|
|
|
def __init__(self, obj): |
|
|
|
def __init__(self, obj): |
|
|
|