Skip to content

Commit

Permalink
Added graph titles
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina7479 committed Jul 11, 2014
1 parent fccaaa8 commit 2ffa98b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/barcharttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def BarChartTest():
data = [("Sunday", 11.5), ("Monday", 13.9), ("Tuesday", 15.1), ("Wednesday", 15.2), ("Thursday", 16.3),
("Friday", 18.0), ("Saturday", 12.1)]

document.add_simple_bar_chart(data, cursor, 400, 300, axis_titles=("day", "percent chance of retweet"), y_axis_limits=(11, 19), y_axis_frequency=1)
document.add_simple_bar_chart(data, cursor, 400, 300, "Blog Re-tweets By Day of the Week", axis_titles=("day", "percent chance of re-tweet"), y_axis_limits=(11, 19), y_axis_frequency=1)

# Close Document
writer.close()
Expand Down
2 changes: 1 addition & 1 deletion bin/linegraphtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def LineGraphTest():
data = [{"series1": [(0, 100), (3600, 300), (7200, 550), (10800, 425), (17000, 825)]},
{"series2": [(0, 50), (3600, 240), (7200, 675), (10800, 800), (14400, 980)]}]

document.add_line_graph(data, cursor, 400, 300, None, None, (3600, 50), ("time (s)", "count"), "Auto", "S", padding=0.11)
document.add_line_graph(data, cursor, 400, 300, "Hits over Time", None, None, (3600, 50), ("time (s)", "count"), "Auto", "S", padding=0.11)

# Close Document
writer.close()
Expand Down
2 changes: 1 addition & 1 deletion bin/piecharttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def PieChartTest():
("Japanese", 99182000), ("Portuguese", 82586600), ("German", 75422674),
("Korean", 39440000)]

document.add_pie_chart(data, cursor, 400, 300, labels=True)
document.add_pie_chart(data, cursor, 400, 300, title="Languages Spoken on the Internet", labels=True)

# Close Document
writer.close()
Expand Down
12 changes: 6 additions & 6 deletions pypdflite/pdfdocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def draw_arc(self, center_cursor, radius, starting_angle, arc_angle, inverted=Fa
arc = PDFArc(self.session, self.page, center_cursor, radius, starting_angle, arc_angle, inverted, end_angle, border_color, fill_color, style, stroke, size)
arc._draw()

def add_line_graph(self, data, cursor, width, height, x_axis_limits=None, y_axis_limits=None, frequency=None, axis_titles=None, axis_labels=None,
def add_line_graph(self, data, cursor, width, height, title=None, x_axis_limits=None, y_axis_limits=None, frequency=None, axis_titles=None, axis_labels=None,
background='S', background_color=None, border_size=1, line_colors=None, axis_font_size=None, padding=0.1):

save_font_size = self.get_font_size()
Expand All @@ -488,23 +488,23 @@ def add_line_graph(self, data, cursor, width, height, x_axis_limits=None, y_axis
else:
self.set_font_size(axis_font_size)

graph = PDFLineGraph(self.session, self.page, cursor, data, width, height, x_axis_limits, y_axis_limits, frequency, axis_titles, axis_labels, line_colors, padding)
graph = PDFLineGraph(self.session, self.page, cursor, data, width, height, title, x_axis_limits, y_axis_limits, frequency, axis_titles, axis_labels, line_colors, padding)
self.set_font_size(save_font_size)

def add_simple_bar_chart(self, data, cursor, width, height, axis_titles=None, axis_font_size=None, y_axis_limits=None, y_axis_frequency=None, bar_style="B", bar_padding=.1, bar_border_colors=None, bar_fill_colors=None, background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
def add_simple_bar_chart(self, data, cursor, width, height, title=None, axis_titles=None, axis_font_size=None, y_axis_limits=None, y_axis_frequency=None, bar_style="B", bar_padding=.1, bar_border_colors=None, bar_fill_colors=None, background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
save_font_size = self.get_font_size()
if axis_font_size is None:
self.set_font_size(8)
else:
self.set_font_size(axis_font_size)

graph = PDFBarChart(self.session, self.page, data, cursor, width, height, axis_titles, y_axis_limits, y_axis_frequency, bar_style, bar_padding, bar_border_colors, bar_fill_colors, background_style, background_size, background_border_color, background_fill_color)
graph = PDFBarChart(self.session, self.page, data, cursor, width, height, title, axis_titles, y_axis_limits, y_axis_frequency, bar_style, bar_padding, bar_border_colors, bar_fill_colors, background_style, background_size, background_border_color, background_fill_color)
self.set_font_size(save_font_size)

def add_pie_chart(self, data, cursor, width, height, data_type="raw", fill_colors=None, labels=False, background_style='S', background_border_color=None, background_fill_color=None, background_size=1):
def add_pie_chart(self, data, cursor, width, height, title=None, data_type="raw", fill_colors=None, labels=False, background_style='S', background_border_color=None, background_fill_color=None, background_size=1):
""" Data type may be "raw" or "percent" """

chart = PDFPieChart(self.session, self.page, data, cursor, width, height, data_type, fill_colors, labels, background_style, background_size, background_border_color, background_fill_color)
chart = PDFPieChart(self.session, self.page, data, cursor, width, height, title, data_type, fill_colors, labels, background_style, background_size, background_border_color, background_fill_color)

def add_table(self, rows, columns, cursor=None):
if cursor is None:
Expand Down
4 changes: 2 additions & 2 deletions pypdflite/pdfobjects/pdfbarchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class PDFBarChart(PDFGraph):
def __init__(self, session, page, data, cursor, width, height, axis_titles=None, y_axis_limits=None, y_axis_frequency=None, bar_style="F", bar_padding=0, bar_border_colors=None, bar_fill_colors=None,
def __init__(self, session, page, data, cursor, width, height, title=None, axis_titles=None, y_axis_limits=None, y_axis_frequency=None, bar_style="F", bar_padding=0, bar_border_colors=None, bar_fill_colors=None,
background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
super(PDFBarChart, self).__init__(session, page, cursor, width, height, background_style, background_size, background_border_color, background_fill_color)
super(PDFBarChart, self).__init__(session, page, cursor, width, height, title, background_style, background_size, background_border_color, background_fill_color)
self.data = data
self.bar_style = bar_style
self.bar_padding = bar_padding
Expand Down
9 changes: 7 additions & 2 deletions pypdflite/pdfobjects/pdffont.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fontref import pdf_character_widths

import copy

CORE_FONTS = {
'courier': 'Courier',
Expand Down Expand Up @@ -28,7 +28,12 @@ def __init__(self, session, family='helvetica', style='', size=12):
self.type = 'Core'

def __repr__(self):
return self.family
return self.font_key

def _copy(self):
new_font = copy.deepcopy(self)
new_font.is_set = False
return new_font

def _set_family(self, family):
if family is not None:
Expand Down
20 changes: 18 additions & 2 deletions pypdflite/pdfobjects/pdfgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@


class PDFGraph(object):
def __init__(self, session, page, cursor, width, height, background_style="S", background_size=1, background_border_color=None, background_fill_color=None, padding=0.1):
def __init__(self, session, page, cursor, width, height, title=None, background_style="S", background_size=1, background_border_color=None, background_fill_color=None, padding=0.1):
self.session = session
self.page = page
self.font = self.session.parent.document.font
self.origin = cursor
self.axis_labels = None
self.base_color = PDFColor(77, 77, 77)
self.padding = (padding * width, padding * height)
self._set_title(title, padding, width, height)
self._draw_background(width, height, background_style, background_size, background_border_color, background_fill_color)
self._pad(width, height)
self._draw_title()

self.default_color_list = [PDFColor(79, 129, 189), PDFColor(192, 80, 77), PDFColor(55, 187, 89),
PDFColor(128, 100, 162), PDFColor(72, 172, 198), PDFColor(247, 150, 70),
PDFColor(208, 146, 167), PDFColor(162, 200, 22), PDFColor(231, 188, 41),
PDFColor(156, 133, 192), PDFColor(243, 164, 71), PDFColor(128, 158, 194)]

def _set_title(self, title, padding, width, height):
self.title = title
if title is None:
self.padding = (padding * width, padding * height)
else:
self.padding = (padding * width, (padding * 1.2 * height))

def _draw_background(self, width, height, style, size, border_color, fill_color):
cursor_end = PDFCursor(self.origin.x + width, self.origin.y - height)
rectangle = PDFRectangle(self.session, self.page, self.origin, cursor_end, border_color, fill_color, style, "solid", size)
Expand All @@ -33,6 +41,14 @@ def _pad(self, width, height):
self.width = width - 2 * self.padding[0]
self.height = height - 2 * self.padding[1]

def _draw_title(self):
if self.title is not None:
save_font = self.font
self.session.parent.document.set_font(save_font.family, "b", save_font.font_size * 1.2)
title_cursor = PDFCursor(self.origin.x + (self.width - self.session.parent.document.font._string_width(self.title))/ 2.0, self.origin.y - self.height - (self.padding[1] * 0.4))
title = PDFText(self.session, self.page, self.title, cursor=title_cursor)
self.session.parent.document.set_font(font=save_font)

def _get_limits_from_data(self, data):
axis_list = [data[0], data[0]]
for item in data[1:]:
Expand Down
4 changes: 2 additions & 2 deletions pypdflite/pdfobjects/pdflinegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class PDFLineGraph(PDFGraph):
def __init__(self, session, page, cursor, data, width, height, x_axis_limits, y_axis_limits, frequency, axis_titles, axis_labels, line_colors, background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
super(PDFLineGraph, self).__init__(session, page, cursor, width, height, background_style, background_size, background_border_color, background_fill_color)
def __init__(self, session, page, cursor, data, width, height, title, x_axis_limits, y_axis_limits, frequency, axis_titles, axis_labels, line_colors, background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
super(PDFLineGraph, self).__init__(session, page, cursor, width, height, title, background_style, background_size, background_border_color, background_fill_color)
self.data = data
self.stroke = "S"
self._set_colors(line_colors)
Expand Down
4 changes: 2 additions & 2 deletions pypdflite/pdfobjects/pdfpiechart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PDFPieChart(PDFGraph):
Create a pie chart from data dict(series labels)
"""
def __init__(self, session, page, data, cursor, width, height, data_type="raw", fill_colors=None, labels=False, background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
super(PDFPieChart, self).__init__(session, page, cursor, width, height, background_style, background_size, background_border_color, background_fill_color)
def __init__(self, session, page, data, cursor, width, height, title, data_type="raw", fill_colors=None, labels=False, background_style="S", background_size=1, background_border_color=None, background_fill_color=None):
super(PDFPieChart, self).__init__(session, page, cursor, width, height, title, background_style, background_size, background_border_color, background_fill_color)
self._parse_data(data, data_type)
self._set_center()
self.stroke = "S"
Expand Down

0 comments on commit 2ffa98b

Please sign in to comment.