From 394ac51416ff0d084eda6e438dc4215fc557067f Mon Sep 17 00:00:00 2001 From: jaraco Date: Sat, 25 Oct 2008 21:32:35 +0000 Subject: [PATCH] Factored Graph subclass CSS handling into superclass, implemented as resource files --- src/svg/charts/bar.css | 78 +++++++++++++++ src/svg/charts/bar.py | 79 +-------------- src/svg/charts/graph.py | 10 ++ src/svg/charts/line.py | 8 -- src/svg/charts/pie.css | 85 ++++++++++++++++ src/svg/charts/pie.py | 85 ---------------- src/svg/charts/plot.css | 193 ++++++++++++++++++++++++++++++++++++ src/svg/charts/plot.py | 192 ----------------------------------- src/svg/charts/schedule.css | 81 +++++++++++++++ src/svg/charts/schedule.py | 78 +-------------- 10 files changed, 450 insertions(+), 439 deletions(-) create mode 100644 src/svg/charts/bar.css create mode 100644 src/svg/charts/pie.css create mode 100644 src/svg/charts/plot.css create mode 100644 src/svg/charts/schedule.css diff --git a/src/svg/charts/bar.css b/src/svg/charts/bar.css new file mode 100644 index 0000000..6088eea --- /dev/null +++ b/src/svg/charts/bar.css @@ -0,0 +1,78 @@ +/* +$Id$ + +default fill styles for multiple datasets (probably only use a single +dataset on this graph though) +*/ +.key1,.fill1{ + fill: #ff0000; + fill-opacity: 0.5; + stroke: none; + stroke-width: 0.5px; +} +.key2,.fill2{ + fill: #0000ff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key3,.fill3{ + fill: #00ff00; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key4,.fill4{ + fill: #ffcc00; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key5,.fill5{ + fill: #00ccff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key6,.fill6{ + fill: #ff00ff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key7,.fill7{ + fill: #00ffff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key8,.fill8{ + fill: #ffff00; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key9,.fill9{ + fill: #cc6666; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key10,.fill10{ + fill: #663399; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key11,.fill11{ + fill: #339900; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key12,.fill12{ + fill: #9966FF; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} diff --git a/src/svg/charts/bar.py b/src/svg/charts/bar.py index 73a8dca..83af4bc 100644 --- a/src/svg/charts/bar.py +++ b/src/svg/charts/bar.py @@ -17,6 +17,8 @@ class Bar(Graph): scale_divisions = None + css_file = 'bar.css' + def __init__(self, fields, *args, **kargs): self.fields = fields super(Bar, self).__init__(*args, **kargs) @@ -71,83 +73,6 @@ class Bar(Graph): bar_gap = int(self.bar_gap) * bar_gap return bar_gap - def get_css(self): - return """\ -/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */ -.key1,.fill1{ - fill: #ff0000; - fill-opacity: 0.5; - stroke: none; - stroke-width: 0.5px; -} -.key2,.fill2{ - fill: #0000ff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key3,.fill3{ - fill: #00ff00; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key4,.fill4{ - fill: #ffcc00; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key5,.fill5{ - fill: #00ccff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key6,.fill6{ - fill: #ff00ff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key7,.fill7{ - fill: #00ffff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key8,.fill8{ - fill: #ffff00; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key9,.fill9{ - fill: #cc6666; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key10,.fill10{ - fill: #663399; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key11,.fill11{ - fill: #339900; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key12,.fill12{ - fill: #9966FF; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -""" - def float_range(start = 0, stop = None, step = 1): "Much like the built-in function range, but accepts floats" while start < stop: diff --git a/src/svg/charts/graph.py b/src/svg/charts/graph.py index 4febcec..a3b169f 100644 --- a/src/svg/charts/graph.py +++ b/src/svg/charts/graph.py @@ -651,6 +651,16 @@ class Graph(object): result = '\n'.join((sheet, self.get_css())) return result + def get_css(self): + css_stream = pkg_resources.resource_stream('svg.charts', self.css_file) + css_string = css_stream.read() + sheet = cssutils.parseString(css_string).cssText + return sheet + + @property + def css_file(self): + return self.__class__.__name__.lower() + '.css' + def _create_element(self, nodeName, attributes={}): "Create an XML node and set the attributes from a dict" node = self._doc.createElement(nodeName) diff --git a/src/svg/charts/line.py b/src/svg/charts/line.py index 9b47d44..f53d65d 100644 --- a/src/svg/charts/line.py +++ b/src/svg/charts/line.py @@ -230,11 +230,3 @@ class Line(Graph): ) prev_sum = list(cum_sum) - - - def get_css(self): - cssutils.log.setLevel(99) # disable log messages - css_stream = pkg_resources.resource_stream('svg.charts', 'line.css') - css_string = css_stream.read() - sheet = cssutils.parseString(css_string).cssText - return sheet \ No newline at end of file diff --git a/src/svg/charts/pie.css b/src/svg/charts/pie.css new file mode 100644 index 0000000..b80cc1a --- /dev/null +++ b/src/svg/charts/pie.css @@ -0,0 +1,85 @@ +/* +$Id$ +*/ + +.dataPointLabel{ + fill: #000000; + text-anchor:middle; + font-size: #{datapoint_font_size}px; + font-family: "Arial", sans-serif; + font-weight: normal; +} + +/* key - MUST match fill styles */ +.key1,.fill1{ + fill: #ff0000; + fill-opacity: 0.7; + stroke: none; + stroke-width: 1px; +} +.key2,.fill2{ + fill: #0000ff; + fill-opacity: 0.7; + stroke: none; + stroke-width: 1px; +} +.key3,.fill3{ + fill-opacity: 0.7; + fill: #00ff00; + stroke: none; + stroke-width: 1px; +} +.key4,.fill4{ + fill-opacity: 0.7; + fill: #ffcc00; + stroke: none; + stroke-width: 1px; +} +.key5,.fill5{ + fill-opacity: 0.7; + fill: #00ccff; + stroke: none; + stroke-width: 1px; +} +.key6,.fill6{ + fill-opacity: 0.7; + fill: #ff00ff; + stroke: none; + stroke-width: 1px; +} +.key7,.fill7{ + fill-opacity: 0.7; + fill: #00ff99; + stroke: none; + stroke-width: 1px; +} +.key8,.fill8{ + fill-opacity: 0.7; + fill: #ffff00; + stroke: none; + stroke-width: 1px; +} +.key9,.fill9{ + fill-opacity: 0.7; + fill: #cc6666; + stroke: none; + stroke-width: 1px; +} +.key10,.fill10{ + fill-opacity: 0.7; + fill: #663399; + stroke: none; + stroke-width: 1px; +} +.key11,.fill11{ + fill-opacity: 0.7; + fill: #339900; + stroke: none; + stroke-width: 1px; +} +.key12,.fill12{ + fill-opacity: 0.7; + fill: #9966FF; + stroke: none; + stroke-width: 1px; +} diff --git a/src/svg/charts/pie.py b/src/svg/charts/pie.py index 4c7be3b..baf6c17 100644 --- a/src/svg/charts/pie.py +++ b/src/svg/charts/pie.py @@ -309,88 +309,3 @@ class Pie(Graph): def round(self, val, to): return round(val,to) - - def get_css(self): - return """\ -.dataPointLabel{ - fill: #000000; - text-anchor:middle; - font-size: #{datapoint_font_size}px; - font-family: "Arial", sans-serif; - font-weight: normal; -} - -/* key - MUST match fill styles */ -.key1,.fill1{ - fill: #ff0000; - fill-opacity: 0.7; - stroke: none; - stroke-width: 1px; -} -.key2,.fill2{ - fill: #0000ff; - fill-opacity: 0.7; - stroke: none; - stroke-width: 1px; -} -.key3,.fill3{ - fill-opacity: 0.7; - fill: #00ff00; - stroke: none; - stroke-width: 1px; -} -.key4,.fill4{ - fill-opacity: 0.7; - fill: #ffcc00; - stroke: none; - stroke-width: 1px; -} -.key5,.fill5{ - fill-opacity: 0.7; - fill: #00ccff; - stroke: none; - stroke-width: 1px; -} -.key6,.fill6{ - fill-opacity: 0.7; - fill: #ff00ff; - stroke: none; - stroke-width: 1px; -} -.key7,.fill7{ - fill-opacity: 0.7; - fill: #00ff99; - stroke: none; - stroke-width: 1px; -} -.key8,.fill8{ - fill-opacity: 0.7; - fill: #ffff00; - stroke: none; - stroke-width: 1px; -} -.key9,.fill9{ - fill-opacity: 0.7; - fill: #cc6666; - stroke: none; - stroke-width: 1px; -} -.key10,.fill10{ - fill-opacity: 0.7; - fill: #663399; - stroke: none; - stroke-width: 1px; -} -.key11,.fill11{ - fill-opacity: 0.7; - fill: #339900; - stroke: none; - stroke-width: 1px; -} -.key12,.fill12{ - fill-opacity: 0.7; - fill: #9966FF; - stroke: none; - stroke-width: 1px; -} -""" diff --git a/src/svg/charts/plot.css b/src/svg/charts/plot.css new file mode 100644 index 0000000..f9de2cf --- /dev/null +++ b/src/svg/charts/plot.css @@ -0,0 +1,193 @@ +/* +$Id$ + +default line styles +*/ +.line1{ + fill: none; + stroke: #ff0000; + stroke-width: 1px; +} +.line2{ + fill: none; + stroke: #0000ff; + stroke-width: 1px; +} +.line3{ + fill: none; + stroke: #00ff00; + stroke-width: 1px; +} +.line4{ + fill: none; + stroke: #ffcc00; + stroke-width: 1px; +} +.line5{ + fill: none; + stroke: #00ccff; + stroke-width: 1px; +} +.line6{ + fill: none; + stroke: #ff00ff; + stroke-width: 1px; +} +.line7{ + fill: none; + stroke: #00ffff; + stroke-width: 1px; +} +.line8{ + fill: none; + stroke: #ffff00; + stroke-width: 1px; +} +.line9{ + fill: none; + stroke: #ccc6666; + stroke-width: 1px; +} +.line10{ + fill: none; + stroke: #663399; + stroke-width: 1px; +} +.line11{ + fill: none; + stroke: #339900; + stroke-width: 1px; +} +.line12{ + fill: none; + stroke: #9966FF; + stroke-width: 1px; +} +/* default fill styles */ +.fill1{ + fill: #cc0000; + fill-opacity: 0.2; + stroke: none; +} +.fill2{ + fill: #0000cc; + fill-opacity: 0.2; + stroke: none; +} +.fill3{ + fill: #00cc00; + fill-opacity: 0.2; + stroke: none; +} +.fill4{ + fill: #ffcc00; + fill-opacity: 0.2; + stroke: none; +} +.fill5{ + fill: #00ccff; + fill-opacity: 0.2; + stroke: none; +} +.fill6{ + fill: #ff00ff; + fill-opacity: 0.2; + stroke: none; +} +.fill7{ + fill: #00ffff; + fill-opacity: 0.2; + stroke: none; +} +.fill8{ + fill: #ffff00; + fill-opacity: 0.2; + stroke: none; +} +.fill9{ + fill: #cc6666; + fill-opacity: 0.2; + stroke: none; +} +.fill10{ + fill: #663399; + fill-opacity: 0.2; + stroke: none; +} +.fill11{ + fill: #339900; + fill-opacity: 0.2; + stroke: none; +} +.fill12{ + fill: #9966FF; + fill-opacity: 0.2; + stroke: none; +} +/* default line styles */ +.key1,.dataPoint1{ + fill: #ff0000; + stroke: none; + stroke-width: 1px; +} +.key2,.dataPoint2{ + fill: #0000ff; + stroke: none; + stroke-width: 1px; +} +.key3,.dataPoint3{ + fill: #00ff00; + stroke: none; + stroke-width: 1px; +} +.key4,.dataPoint4{ + fill: #ffcc00; + stroke: none; + stroke-width: 1px; +} +.key5,.dataPoint5{ + fill: #00ccff; + stroke: none; + stroke-width: 1px; +} +.key6,.dataPoint6{ + fill: #ff00ff; + stroke: none; + stroke-width: 1px; +} +.key7,.dataPoint7{ + fill: #00ffff; + stroke: none; + stroke-width: 1px; +} +.key8,.dataPoint8{ + fill: #ffff00; + stroke: none; + stroke-width: 1px; +} +.key9,.dataPoint9{ + fill: #cc6666; + stroke: none; + stroke-width: 1px; +} +.key10,.dataPoint10{ + fill: #663399; + stroke: none; + stroke-width: 1px; +} +.key11,.dataPoint11{ + fill: #339900; + stroke: none; + stroke-width: 1px; +} +.key12,.dataPoint12{ + fill: #9966FF; + stroke: none; + stroke-width: 1px; +} +.constantLine{ + color: navy; + stroke: navy; + stroke-width: 1px; + stroke-dasharray: 9 1 1; +} diff --git a/src/svg/charts/plot.py b/src/svg/charts/plot.py index 374c268..e6c38e1 100644 --- a/src/svg/charts/plot.py +++ b/src/svg/charts/plot.py @@ -320,195 +320,3 @@ class Plot(Graph): def format(self, x, y): return '(%0.2f, %0.2f)' % (x,y) - - def get_css(self): - return """/* default line styles */ -.line1{ - fill: none; - stroke: #ff0000; - stroke-width: 1px; -} -.line2{ - fill: none; - stroke: #0000ff; - stroke-width: 1px; -} -.line3{ - fill: none; - stroke: #00ff00; - stroke-width: 1px; -} -.line4{ - fill: none; - stroke: #ffcc00; - stroke-width: 1px; -} -.line5{ - fill: none; - stroke: #00ccff; - stroke-width: 1px; -} -.line6{ - fill: none; - stroke: #ff00ff; - stroke-width: 1px; -} -.line7{ - fill: none; - stroke: #00ffff; - stroke-width: 1px; -} -.line8{ - fill: none; - stroke: #ffff00; - stroke-width: 1px; -} -.line9{ - fill: none; - stroke: #ccc6666; - stroke-width: 1px; -} -.line10{ - fill: none; - stroke: #663399; - stroke-width: 1px; -} -.line11{ - fill: none; - stroke: #339900; - stroke-width: 1px; -} -.line12{ - fill: none; - stroke: #9966FF; - stroke-width: 1px; -} -/* default fill styles */ -.fill1{ - fill: #cc0000; - fill-opacity: 0.2; - stroke: none; -} -.fill2{ - fill: #0000cc; - fill-opacity: 0.2; - stroke: none; -} -.fill3{ - fill: #00cc00; - fill-opacity: 0.2; - stroke: none; -} -.fill4{ - fill: #ffcc00; - fill-opacity: 0.2; - stroke: none; -} -.fill5{ - fill: #00ccff; - fill-opacity: 0.2; - stroke: none; -} -.fill6{ - fill: #ff00ff; - fill-opacity: 0.2; - stroke: none; -} -.fill7{ - fill: #00ffff; - fill-opacity: 0.2; - stroke: none; -} -.fill8{ - fill: #ffff00; - fill-opacity: 0.2; - stroke: none; -} -.fill9{ - fill: #cc6666; - fill-opacity: 0.2; - stroke: none; -} -.fill10{ - fill: #663399; - fill-opacity: 0.2; - stroke: none; -} -.fill11{ - fill: #339900; - fill-opacity: 0.2; - stroke: none; -} -.fill12{ - fill: #9966FF; - fill-opacity: 0.2; - stroke: none; -} -/* default line styles */ -.key1,.dataPoint1{ - fill: #ff0000; - stroke: none; - stroke-width: 1px; -} -.key2,.dataPoint2{ - fill: #0000ff; - stroke: none; - stroke-width: 1px; -} -.key3,.dataPoint3{ - fill: #00ff00; - stroke: none; - stroke-width: 1px; -} -.key4,.dataPoint4{ - fill: #ffcc00; - stroke: none; - stroke-width: 1px; -} -.key5,.dataPoint5{ - fill: #00ccff; - stroke: none; - stroke-width: 1px; -} -.key6,.dataPoint6{ - fill: #ff00ff; - stroke: none; - stroke-width: 1px; -} -.key7,.dataPoint7{ - fill: #00ffff; - stroke: none; - stroke-width: 1px; -} -.key8,.dataPoint8{ - fill: #ffff00; - stroke: none; - stroke-width: 1px; -} -.key9,.dataPoint9{ - fill: #cc6666; - stroke: none; - stroke-width: 1px; -} -.key10,.dataPoint10{ - fill: #663399; - stroke: none; - stroke-width: 1px; -} -.key11,.dataPoint11{ - fill: #339900; - stroke: none; - stroke-width: 1px; -} -.key12,.dataPoint12{ - fill: #9966FF; - stroke: none; - stroke-width: 1px; -} -.constantLine{ - color: navy; - stroke: navy; - stroke-width: 1px; - stroke-dasharray: 9 1 1; -} -""" \ No newline at end of file diff --git a/src/svg/charts/schedule.css b/src/svg/charts/schedule.css new file mode 100644 index 0000000..729adf4 --- /dev/null +++ b/src/svg/charts/schedule.css @@ -0,0 +1,81 @@ +/* + +$Id$ + +default fill styles for multiple datasets (probably only use a single dataset +on this graph though) + +*/ + +.key1,.fill1{ + fill: #ff0000; + fill-opacity: 0.5; + stroke: none; + stroke-width: 0.5px; +} +.key2,.fill2{ + fill: #0000ff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key3,.fill3{ + fill: #00ff00; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key4,.fill4{ + fill: #ffcc00; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key5,.fill5{ + fill: #00ccff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key6,.fill6{ + fill: #ff00ff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key7,.fill7{ + fill: #00ffff; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key8,.fill8{ + fill: #ffff00; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key9,.fill9{ + fill: #cc6666; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key10,.fill10{ + fill: #663399; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key11,.fill11{ + fill: #339900; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} +.key12,.fill12{ + fill: #9966FF; + fill-opacity: 0.5; + stroke: none; + stroke-width: 1px; +} diff --git a/src/svg/charts/schedule.py b/src/svg/charts/schedule.py index 12ca1b9..b60266f 100644 --- a/src/svg/charts/schedule.py +++ b/src/svg/charts/schedule.py @@ -242,84 +242,8 @@ class Schedule(Graph): 'class': 'fill%s' % (count+1), # TODO: doublecheck that +1 is correct (that's what's in the Ruby code) }) self.graph.appendChild(rect) - - def get_css(self): - return """\ -/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */ -.key1,.fill1{ - fill: #ff0000; - fill-opacity: 0.5; - stroke: none; - stroke-width: 0.5px; -} -.key2,.fill2{ - fill: #0000ff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key3,.fill3{ - fill: #00ff00; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key4,.fill4{ - fill: #ffcc00; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key5,.fill5{ - fill: #00ccff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key6,.fill6{ - fill: #ff00ff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key7,.fill7{ - fill: #00ffff; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key8,.fill8{ - fill: #ffff00; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key9,.fill9{ - fill: #cc6666; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key10,.fill10{ - fill: #663399; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key11,.fill11{ - fill: #339900; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -.key12,.fill12{ - fill: #9966FF; - fill-opacity: 0.5; - stroke: none; - stroke-width: 1px; -} -""" + def _x_range(self): # ruby version uses teh last data supplied last = -1