def on_chart_draw(self, widget, ctx): # This should not happen actually, but be sure. if self.selected_cluster and self.selected_doc: lines = [] for doc in self.selected_cluster.docs: distance = self.selected_doc.distances[doc.name] cut_path = os.path.basename(doc.path) lines.append((cut_path[:20], distance * 100)) # Use this for logarithmic scale: #lines.append((cut_path[:20], math.log10(distance * 100 + 1) / 2.0)) if len(lines) > 1: # This matches the chart background color ctx.set_source_rgb(0.96, 0.96, 0.96) ctx.paint() # Build actual indexed dataset from lines dataSet = ( ('docs', [(i, l[1]) for i, l in enumerate(lines)]), ) # Grid legend scaling CHART_OPTIONS['axis']['x']['ticks'] = [dict(v=i, label=l[0]) for i, l in enumerate(lines)] # Actually draw the chart on the surface provided by ctx chart = LineChart(ctx.get_target(), CHART_OPTIONS) chart.addDataset(dataSet) chart.render() else: # No data to display, so just display a nicely rendererd "NO! alloc = widget.get_allocation() ctx.set_source_rgb(0.5, 0.5, 0.5) # Draw text exactly centered in the widget center, with a # certain offset in height def draw_center_text(text, font_size=15, height_off=0): ctx.set_font_size(font_size) extents = ctx.text_extents(text) ctx.move_to(alloc.width / 2 - extents[2] / 2, alloc.height / 2 - height_off) ctx.show_text(text) # Now you gonna see if your font is good :-) draw_center_text('№', 100, +30) draw_center_text('⦃ only one document in cluster ⦄') # Draw current state ctx.stroke()
def testLine(): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 500) chart = LineChart(surface) dataSet = ( ('myFirstDataset', [[0, 3], [1, 2], [2, 1.414], [3, 2.3]]), ('mySecondDataset', [[0, 1.4], [1, 2.67], [2, 1.34], [3, 1.2]]), ('myThirdDataset', [[0, 0.46], [1, 1.45], [2, 1.0], [3, 1.6]]), ('myFourthDataset', [[0, 0.3], [1, 0.83], [2, 0.7], [3, 0.2]]), ) chart.addDataset(dataSet) chart.render() surface.write_to_png("testline.png")
def plot(self): """Initializes chart (if needed), set data and plots.""" chart = LineChart(self._surface, self._options) chart.addDataset(self._data) chart.render()