Debian Codegraph @

src/igraph/drawing/cairo/histogram.py

raw
1 """This module provides implementation for a Cairo-specific histogram drawer"""
2
3 from igraph.drawing.cairo.base import AbstractCairoDrawer
4
5 __all__ = ("CairoHistogramDrawer",)
6
7
8 class CairoHistogramDrawer(AbstractCairoDrawer):
9 """Default Cairo drawer object for histograms"""
10
11 def __init__(self, context):
12 """Constructs the vertex drawer and associates it to the given
13 palette.
14
15 @param context: the context on which we will draw
16 """
17 super().__init__(context, bbox=None)
18
19 def draw(self, histogram, **kwds):
20 """TODO"""
21 from igraph.drawing.cairo.coord import DescartesCoordinateSystem
22
23 context = self.context
24
25 bbox = self.bbox = kwds.pop("bbox", None)
26 if bbox is None:
27 raise ValueError("bbox is required for Cairo plots")
28
29 xmin = kwds.get("min", self._min)
30 ymin = 0
31 xmax = kwds.get("max", self._max)
32 ymax = kwds.get("max_value", max(self._bins))
33 width = self._bin_width
34
35 coord_system = DescartesCoordinateSystem(
36 context,
37 bbox,
38 (xmin, ymin, xmax, ymax),
39 )
40
41 # Draw the boxes
42 context.set_line_width(1)
43 context.set_source_rgb(1.0, 0.0, 0.0)
44 x = self._min
45 for value in self._bins:
46 top_left_x, top_left_y = coord_system.local_to_context(x, value)
47 x += width
48 bottom_right_x, bottom_right_y = coord_system.local_to_context(x, 0)
49 context.rectangle(
50 top_left_x,
51 top_left_y,
52 bottom_right_x - top_left_x,
53 bottom_right_y - top_left_y,
54 )
55 context.fill()
56
57 # Draw the axes
58 coord_system.draw()