def get_dimstr_bounds(dim, scale, x, y): """Return the bounds rect of the string for the given dimension.""" dim_scaled = dim * inch * scale dim_str = String(x + dim_scaled / 2, y - 3, dimstr(dim) + '"', textAnchor='middle', fontSize=9) result = dim_str.getBounds() return result
def getStringWidth(text): dummyChar = String(0, 0, text) # returns: # (x0, y0, x1, y1) # x0 and y0 start point # x1 and y1 end point bounds = dummyChar.getBounds() xPosStart = bounds[0] xPosEnd = bounds[2] return xPosEnd - xPosStart
def add_vdimstr(arrow, dim, scale, x, y, boundsln_len): """Add a measurement label to the given vertical dimension arrow.""" dim_scaled = dim * inch * scale dim_str = String(x + boundsln_len / 2 + 2, y + dim_scaled / 2 - 4, dimstr(dim) + '"', textAnchor='end', fontSize=9) bnds = dim_str.getBounds() whiteout_r = Rect(bnds[0], bnds[1], bnds[2] - bnds[0], dim_str.fontSize, fillColor=colors.white, strokeColor=colors.white) arrow.add(whiteout_r) arrow.add(dim_str) return arrow
def add_hdimstr(arrow, dim, scale, x, y): """Add a measurement label to the given horizontal dimension arrow.""" dim_scaled = dim * inch * scale dim_str = String(x + dim_scaled / 2, y - 3, dimstr(dim) + '"', textAnchor='middle', fontSize=9) bounds_rect = dim_str.getBounds() whiteout_rect = Rect(bounds_rect[0], bounds_rect[1], bounds_rect[2] - bounds_rect[0], bounds_rect[3] - bounds_rect[1], fillColor=colors.white, strokeColor=colors.white) arrow.add(whiteout_rect) arrow.add(dim_str) return arrow
def add_vdimstr_iso(arrow, dim, scale, x, y, boundsln_len): """Add a measurement label to the given isometric vert dimension arrow.""" dim_scaled = dim * inch * scale off = math.sqrt((boundsln_len / 2)**2 / 2) dim_str = String(x - off - 2, y + dim_scaled / 2 - 4, dimstr(dim) + '"', textAnchor='start', fontSize=9) bnds = dim_str.getBounds() whiteout_r = Rect(bnds[0], bnds[1], bnds[2] - bnds[0], dim_str.fontSize, fillColor=colors.white, strokeColor=colors.white) arrow.add(whiteout_r) arrow.add(dim_str) return arrow
def add_ddimstr_iso(arrow, dim, scale, x, y, boundsln_len): """Add a measurement label to the given isometric depth dimension arrow.""" dim_scaled = dim * inch * scale # `iso45' is divided by 2 below because that is how it's calculated # in the isometric_view, above. iso45 = math.sin(math.radians(45)) * dim_scaled / 2 xmid, ymid = x + iso45 / 2, y + iso45 / 2 dim_str = String(xmid - boundsln_len / 2 - 4, ymid - 4, dimstr(dim) + '"', textAnchor='start', fontSize=9) bnds = dim_str.getBounds() whiteout_r = Rect(bnds[0], bnds[1], bnds[2] - bnds[0], dim_str.fontSize, fillColor=colors.white, strokeColor=colors.white) arrow.add(whiteout_r) arrow.add(dim_str) return arrow
def genChart(width, height): chart = VerticalBarChart() chart.data = [(12, 11, 9, 6, 2, 22, 7)] # Vertical axis - Y axis chart.valueAxis.valueMin = 0 chart.valueAxis.valueMax = 25 chart.valueAxis.visible = 0 # hide # Horizontal axis - X axis chart.categoryAxis.categoryNames = [ 'Asia', 'Africa', 'North America', 'South America', 'Antarctica', 'Europe', 'Australia' ] # We could hide X axis like we did with Y axis: # example: chart.categoryAxis.visible = 0 # Hidding the X axis will hide all the elements of X axis # and we want to see the labels! # example: chart.categoryAxis.labels.visible = 1 # by default they are visible # So we just hide what we don't want to see! chart.categoryAxis.visibleAxis = 0 # hide line chart.categoryAxis.visibleTicks = 0 # hide line ticks chart.categoryAxis.labels.angle = 90 # rotate labels chart.categoryAxis.labels.dx = -30 # adjust labels position at x chart.categoryAxis.labels.dy = 30 # adjust labels position at y # Numbers above each bar chart.barLabelFormat = '%s' # contains the provived string chart.barLabels.nudge = 15 # space between the bar and the text chart.barLabels.angle = -20 # rotate the text chart.barLabels.fontName = 'barText' # registered font at report.py line 91 chart.barLabels.fontSize = 20 chart.barLabels.fillColor = colors.red chart.barWidth = 5 chart.barSpacing = 8 # space between each bar # NOTE: bar list doesn't work like we could expect, # the first element apparently sets the configuration for all! chart.bars[0].fillColor = colors.toColor('hsl(240, 100%, 20%)') chart.bars[0].strokeColor = colors.white chart.x = width * 5 / 100 # starts at 5% of width space chart.y = height * 5 / 100 # starts at 5% of height space chart.width = width * 90 / 100 # uses 90% of width chart.height = height * 80 / 100 # uses 80% of height titleShadow = String( 19, height * 60 / 100 - 1, # x and y start point 'Average JC attacks by continent', fontSize=20, fontName='chartTitle', # registered font at report.py line 94 fillColor=colors.red) title = String( 20, height * 60 / 100, # x and y start point 'Average JC attacks by continent', fontSize=20, fontName='chartTitle', # registered font at report.py line 94 fillColor=colors.orange) bounds = title.getBounds() # returns a tuple with (x1,y1,x2,y2) rectangle = Rect( bounds[0] - 3, bounds[1], # x1 and y1 bounds[2] - bounds[0] + 3, 20 # x2 and y2 ) rectangle.fillColor = colors.black drawing = Drawing() drawing.add(rectangle) drawing.add(titleShadow) drawing.add(title) drawing.add(chart) # Bars bottom 'labels' # For a better understand of this coordinates please # see code guide charWidth = utils.getStringWidth('A') dataLen = len(chart.data[0]) # 7 elements startPoint = chart.getBounds()[0] barAndLabelWidth = chart.width / dataLen # total width / 7 elements center = barAndLabelWidth / 2 - charWidth / 2 yPos = chart.y - 10 # Example: # drawing.add(String(startPoint + barAndLabelWidth * 0 + center, chart.y - 10, 'A')) # drawing.add(String(startPoint + barAndLabelWidth * 1 + center, chart.y - 10, 'B')) # ... # drawing.add(String(startPoint + barAndLabelWidth * 6 + center, chart.y - 10, 'G')) for index, char in enumerate('ABCDEFG', start=0): xPos = startPoint + barAndLabelWidth * index + center drawing.add(String(xPos, yPos, char)) return drawing