Changeset 140
- Timestamp:
- 02/03/09 16:08:23 (3 years ago)
- Location:
- trunk/pycha
- Files:
-
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/pycha/__init__.py
r130 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. -
trunk/pycha/bar.py
r112 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. … … 18 18 from pycha.chart import Chart, uniqueIndices 19 19 from pycha.color import hex2rgb 20 20 21 21 22 class BarChart(Chart): … … 96 97 drawBar(bar) 97 98 cx.restore() 99 98 100 99 101 class VerticalBarChart(BarChart): … … 138 140 xval, yval = item 139 141 y = (((xval - self.minxval) * self.xscale) 140 + self.barMargin + (i * self.barWidthForSet))142 + self.barMargin + (i * self.barWidthForSet)) 141 143 h = self.barWidthForSet 142 144 w = abs(yval) * self.yscale … … 155 157 offset = (self.minxdelta * self.xscale) / 2 156 158 tmp = self.xticks 157 self.xticks = [(1.0 - tick[0], tick[1]) for tick in self.yticks ]159 self.xticks = [(1.0 - tick[0], tick[1]) for tick in self.yticks] 158 160 self.yticks = [(tick[0] + offset, tick[1]) for tick in tmp] 159 161 … … 185 187 cx.stroke() 186 188 189 187 190 class Rect(object): 191 188 192 def __init__(self, x, y, w, h, xval, yval, name): 189 193 self.x, self.y, self.w, self.h = x, y, w, h -
trunk/pycha/chart.py
r138 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. … … 24 24 DEFAULT_COLOR) 25 25 26 26 27 class Chart(object): 27 28 … … 132 133 self.clean() 133 134 134 135 # update methods136 135 def _update(self, options={}): 137 136 """Update all the information needed to render the chart""" … … 154 153 self.minxval, self.maxxval = self.options.axis.x.range 155 154 else: 156 xdata = [pair[0] for pair in reduce(lambda a, b: a+b, stores)]155 xdata = [pair[0] for pair in reduce(lambda a, b: a+b, stores)] 157 156 self.minxval = float(min(xdata)) 158 157 self.maxxval = float(max(xdata)) … … 170 169 self.minyval, self.maxyval = self.options.axis.y.range 171 170 else: 172 ydata = [pair[1] for pair in reduce(lambda a, b: a+b, stores)]171 ydata = [pair[1] for pair in reduce(lambda a, b: a+b, stores)] 173 172 self.minyval = float(min(ydata)) 174 173 self.maxyval = float(max(ydata)) … … 228 227 roughSeparation = self.xrange / self.options.axis.x.tickCount 229 228 i = j = 0 230 while i < len(uniqx) and j < self.options.axis.x.tickCount:229 while i < len(uniqx) and j < self.options.axis.x.tickCount: 231 230 if (uniqx[i] - self.minxval) >= (j * roughSeparation): 232 231 pos = self.xscale * (uniqx[i] - self.minxval) … … 264 263 self.yticks.append((pos, round(yval, prec))) 265 264 266 # render methods267 265 def _renderBackground(self, cx): 268 266 """Renders the background area of the chart""" … … 330 328 cx.stroke() 331 329 332 label = unicode(tick[1])330 label = unicode(tick[1]) 333 331 extents = cx.text_extents(label) 334 332 labelWidth = extents[2] … … 387 385 388 386 def _getTickSize(self, cx, ticks, rotate): 389 tickExtents = [cx.text_extents(unicode(tick[1]))[2:4] for tick in ticks] 387 tickExtents = [cx.text_extents(unicode(tick[1]))[2:4] 388 for tick in ticks] 390 389 tickWidth = tickHeight = 0.0 391 390 if tickExtents: … … 399 398 cosRadians = math.cos(radians) 400 399 maxHeight = maxWidth * sinRadians + maxHeight * cosRadians 401 maxWidth = maxWidth * cosRadians + maxHeight * sinRadians400 maxWidth = maxWidth * cosRadians + maxHeight * sinRadians 402 401 tickWidth += maxWidth 403 402 tickHeight += maxHeight 404 403 return tickWidth, tickHeight 405 404 406 def _renderAxisLabel(self, cx, tickWidth, tickHeight, label, x, y, vertical=False): 405 def _renderAxisLabel(self, cx, tickWidth, tickHeight, label, x, y, 406 vertical=False): 407 407 cx.new_path() 408 408 cx.select_font_face(self.options.axis.labelFont, … … 570 570 cx.restore() 571 571 572 572 573 def uniqueIndices(arr): 573 574 """Return a list with the indexes of the biggest element of arr""" 574 575 return range(max([len(a) for a in arr])) 575 576 577 576 578 class Area(object): 577 579 """Simple rectangle to hold an area coordinates and dimensions""" 580 578 581 def __init__(self, x, y, w, h, origin=0.0): 579 582 self.x, self.y, self.w, self.h = x, y, w, h … … 584 587 return msg % (self.x, self.y, self.w, self.h, self.origin) 585 588 589 586 590 class Option(dict): 587 591 """Useful dict that allow attribute-like access to its keys""" 592 588 593 def __getattr__(self, name): 589 594 if name in self.keys(): … … 595 600 """Recursive merge with other Option or dict object""" 596 601 for key, value in other.items(): 597 if self.has_key(key):602 if key in self: 598 603 if isinstance(self[key], Option): 599 604 self[key].merge(other[key]) 600 605 else: 601 606 self[key] = other[key] 607 602 608 603 609 DEFAULT_OPTIONS = Option( -
trunk/pycha/color.py
r96 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. … … 18 18 DEFAULT_COLOR = '#3c581a' 19 19 20 20 21 def clamp(minValue, maxValue, value): 21 22 """Make sure value is between minValue and maxValue""" … … 25 26 return maxValue 26 27 return value 28 27 29 28 30 def hex2rgb(hexstring, digits=2): … … 43 45 return r / top, g / top, b / top 44 46 47 45 48 def lighten(r, g, b, amount): 46 49 """Return a lighter version of the color (r, g, b)""" … … 48 51 clamp(0.0, 1.0, g + amount), 49 52 clamp(0.0, 1.0, b + amount)) 53 50 54 51 55 def generateColorscheme(masterColor, keys): … … 63 67 for i, key in enumerate(keys)]) 64 68 69 65 70 def defaultColorscheme(keys): 66 71 """Return the default color scheme (derived from a dark green)""" 67 72 return generateColorscheme(DEFAULT_COLOR, keys) 73 68 74 69 75 def getColorscheme(color, keys): … … 72 78 """ 73 79 return generateColorscheme(colorSchemes.get(color, color), keys) 80 74 81 75 82 # default colors for color schemes … … 80 87 grey='#444444', 81 88 black='#000000', 82 darkcyan='#305755' 89 darkcyan='#305755', 83 90 ) -
trunk/pycha/line.py
r109 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. … … 18 18 from pycha.chart import Chart 19 19 from pycha.color import hex2rgb 20 20 21 21 22 class LineChart(Chart): … … 41 42 def _renderChart(self, cx): 42 43 """Renders a line chart""" 44 43 45 def preparePath(storeName): 44 46 cx.new_path() … … 48 50 # Go to the (0,0) coordinate to start drawing the area 49 51 #cx.move_to(self.area.x, self.area.y + self.area.h) 50 cx.move_to(self.area.x,51 self.area.y + (1.0 - self.area.origin) * self.area.h)52 offset = (1.0 - self.area.origin) * self.area.h 53 cx.move_to(self.area.x, self.area.y + offset) 52 54 53 55 for point in self.points: … … 79 81 cx.set_line_width(self.options.stroke.width) 80 82 if self.options.shouldFill: 83 81 84 def drawLine(storeName): 82 85 if self.options.stroke.shadow: … … 109 112 cx.restore() 110 113 114 111 115 class Point(object): 116 112 117 def __init__(self, x, y, xval, yval, name): 113 118 self.x, self.y = x, y -
trunk/pycha/pie.py
r139 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. … … 22 22 from pycha.chart import Chart, Option 23 23 from pycha.color import hex2rgb 24 24 25 25 26 class PieChart(Chart): … … 139 140 normalisedAngle = slice.getNormalisedAngle() 140 141 141 labelx = self.centerx + math.sin(normalisedAngle) * (self.radius + 10) 142 labely = self.centery - math.cos(normalisedAngle) * (self.radius + 10) 142 big_radius = self.radius + 10 143 labelx = self.centerx + math.sin(normalisedAngle) * big_radius 144 labely = self.centery - math.cos(normalisedAngle) * big_radius 143 145 144 146 label = tick[1] … … 168 170 169 171 class Slice(object): 172 170 173 def __init__(self, name, fraction, xval, yval, angle): 171 174 self.name = name -
trunk/pycha/scatter.py
r109 r140 1 # Copyright (c) 2007-2008by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>1 # Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 2 2 # 3 3 # This file is part of PyCha. … … 18 18 from pycha.line import LineChart 19 19 20 20 21 class ScatterplotChart(LineChart): 21 22 22 23 def _renderChart(self, cx): 23 24 """Renders a scatterplot""" 25 24 26 def drawSymbol(point, size=2): 25 27 ox = point.x * self.area.w + self.area.x 26 28 oy = point.y * self.area.h + self.area.y 27 cx.move_to(ox-size, oy )28 cx.line_to(ox+size, oy )29 cx.move_to(ox , oy-size)30 cx.line_to(ox , oy+size)29 cx.move_to(ox-size, oy) 30 cx.line_to(ox+size, oy) 31 cx.move_to(ox, oy-size) 32 cx.line_to(ox, oy+size) 31 33 32 34 def preparePath(storeName, size=2):
