Author: croberts
Date: 2012-02-24 19:25:52 +0000 (Fri, 24 Feb 2012)
New Revision: 5227
Added:
trunk/cumin/python/cumin/charts.py
Log:
Adding a slimmed down version of charts.py back so that we can maintain the slotvis for
the time being.
Added: trunk/cumin/python/cumin/charts.py
===================================================================
--- trunk/cumin/python/cumin/charts.py (rev 0)
+++ trunk/cumin/python/cumin/charts.py 2012-02-24 19:25:52 UTC (rev 5227)
@@ -0,0 +1,139 @@
+import logging
+
+from cairo import *
+from random import random
+from time import time
+from math import sqrt, pi, ceil
+
+from formats import *
+from util import *
+
+log = logging.getLogger("cumin.chart")
+
+
+class HeatMapChart(object):
+ def __init__(self, width=400, height=400):
+ self.width = width # final width
+ self.height = height
+ self.max_width = width # max width
+ self.max_height = height
+ self.cols = 0 # columns
+ self.rows = 0
+ self.max_size = 28 # size of each slot
+ self.surface = None
+ self.gap = 3
+
+ def plot_colored_shape(self, interior, shape, width, height):
+ surface = ImageSurface(FORMAT_ARGB32, int(width), int(height))
+ cr = Context(surface)
+ cr.set_line_width(1)
+
+ cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
+ cr.rectangle(0, 0, width - 1, height - 1)
+ cr.fill()
+
+ cr.set_line_width(2)
+ self._plot_shape(cr, interior, shape, 0, 0, width, height)
+ return surface
+
+ def plot_colored_rect(self, interior, width, height):
+ surface = ImageSurface(FORMAT_ARGB32, int(width), int(height))
+ cr = Context(surface)
+ cr.set_line_width(1)
+
+ cr.set_source_rgb(*interior)
+ cr.rectangle(0, 0, width - 1, height - 1)
+ cr.fill()
+ return surface
+
+ def plot_state(self, state, width, height):
+ surface = ImageSurface(FORMAT_ARGB32, int(width), int(height))
+ cr = Context(surface)
+ cr.set_line_width(2)
+
+ self._plot_square(cr, (0.7, 0.7, 0.7), state, 0, 0, width, height)
+ return surface
+
+ def _plot_square(self, cr, colors, state, x, y, width, height):
+ if state == "Unclaimed":
+ shape = "box"
+ elif state == "Claimed":
+ shape = "solid"
+ elif state in ("Owner", "Unavailable"):
+ shape = "diagonal"
+ elif state in ("Matched", "Preempting",
"Preempting/Matched"):
+ shape = "filled_triangle"
+ else:
+ shape = "box"
+
+ self._plot_shape(cr, colors, shape, x, y, width, height)
+
+ def _plot_shape(self, cr, colors, shape, x, y, width, height):
+ cr.set_source_rgb(*colors)
+ #cr.move_to(x, y)
+ cr.rectangle(x, y, width - self.gap, height - self.gap)
+
+ if shape == "box": # leave empty
+ cr.stroke()
+ elif shape == "solid": # solid fill
+ cr.fill()
+ elif shape == "diagonal": # diagonal line
+ cr.move_to(x, y)
+ cr.line_to(x + width - self.gap, y + height - self.gap)
+ cr.stroke()
+ elif shape == "filled_triangle": # triangle
+ cr.stroke()
+ cr.move_to(x + width - self.gap, y)
+ cr.line_to(x, y + height - self.gap)
+ cr.line_to(x + width - self.gap, y + height - self.gap)
+ cr.close_path()
+ cr.fill()
+
+ def plot_shapes(self, shape_info, zl):
+ count = len(shape_info)
+ slot_size = self.slot_size(count, zl)
+ x = 0
+ y = 0
+ col = 0
+ self.rows = 1
+ # the width and height depend on the number of slots
+ self.surface = ImageSurface(FORMAT_ARGB32, int(self.width), int(self.height))
+ cr = Context(self.surface)
+
+ cr.set_line_width( zl == 1 and 1 or 2 )
+
+ for slot in shape_info:
+ interior = slot["color"]
+ shape = slot["shape"]
+
+ if col >= self.cols:
+ x = 0
+ y = y + slot_size
+ col = 0
+ self.rows = self.rows + 1
+
+ self._plot_shape(cr, interior, shape, x, y, slot_size, slot_size)
+
+ x = x + slot_size
+ col = col + 1
+ return slot_size
+
+ def slot_size(self, count, zoom=1):
+ sq = sqrt(count)
+ cols = int(sq)
+ if sq > cols:
+ cols = cols + 1
+ size = int(self.width * zoom / cols)
+ if size < 2:
+ size = 2
+ if size > self.max_size * zoom:
+ size = self.max_size * zoom
+
+ self.width = int((size * cols) + 1)
+ self.height = int((ceil(count * 1.0 / cols) * size) + 1)
+ self.cols = cols
+ return size
+
+ def write(self, writer):
+ self.surface.write_to_png(writer)
+
Property changes on: trunk/cumin/python/cumin/charts.py
___________________________________________________________________
Added: svn:mime-type
+ text/plain