|
4 | 4 | # See the bottom for a code example using the `sparkline` Class.
|
5 | 5 |
|
6 | 6 | # # File: display_shapes_sparkline.py
|
7 |
| -# A sparkline is a scrolling line graph, where any values added to sparkline using `add_value` are plotted. |
| 7 | +# A sparkline is a scrolling line graph, where any values added to sparkline using ` |
| 8 | +# add_value` are plotted. |
8 | 9 | #
|
9 |
| -# The `sparkline` class creates an element suitable for adding to the display using `display.show(mySparkline)` |
| 10 | +# The `sparkline` class creates an element suitable for adding to the display using |
| 11 | +# `display.show(mySparkline)` |
10 | 12 | # or adding to a `displayio.Group` to be displayed.
|
11 | 13 | #
|
12 |
| -# When creating the sparkline, identify the number of `max_items` that will be included in the graph. |
13 |
| -# When additional elements are added to the sparkline and the number of items has exceeded max_items, |
14 |
| -# any excess values are removed from the left of the graph, and new values are added to the right. |
| 14 | +# When creating the sparkline, identify the number of `max_items` that will be |
| 15 | +# included in the graph. When additional elements are added to the sparkline and |
| 16 | +# the number of items has exceeded max_items, any excess values are removed from |
| 17 | +# the left of the graph, and new values are added to the right. |
15 | 18 |
|
16 | 19 | import displayio
|
17 | 20 | from adafruit_display_shapes.line import Line
|
18 | 21 |
|
19 | 22 |
|
20 | 23 | class Sparkline(displayio.Group):
|
| 24 | + # pylint: disable=invalid-name |
| 25 | + """ A sparkline graph. |
| 26 | +
|
| 27 | + : param width: Width of the sparkline graph in pixels |
| 28 | + : param height: Height of the sparkline graph in pixels |
| 29 | + : param max_items: Maximum number of values housed in the sparkline |
| 30 | + : param yMin: Lower range for the y-axis. Set to None for autorange. |
| 31 | + : param yMax: Upper range for the y-axis. Set to None for autorange. |
| 32 | + : param x: X-position on the screen, in pixels |
| 33 | + : param y: Y-position on the screen, in pixels |
| 34 | + : param color: Line color, the default value is 0xFFFFFF (WHITE) |
| 35 | + """ |
| 36 | + |
21 | 37 | def __init__(
|
22 | 38 | self,
|
23 | 39 | width,
|
@@ -48,6 +64,11 @@ def __init__(
|
48 | 64 | ) # self is a group of lines
|
49 | 65 |
|
50 | 66 | def add_value(self, value):
|
| 67 | + """ Add a value to the sparkline. |
| 68 | + |
| 69 | + : param value: The value to be added to the sparkline |
| 70 | + """ |
| 71 | + |
51 | 72 | if value is not None:
|
52 | 73 | if (
|
53 | 74 | len(self._spark_list) >= self._max_items
|
@@ -77,16 +98,20 @@ def _plotLine(self, x1, last_value, x2, value, yBottom, yTop):
|
77 | 98 | y1 = int(self.height * (yTop - last_value) / (yTop - yBottom))
|
78 | 99 | self.append(Line(x1, y1, x2, y2, self.color)) # plot the line
|
79 | 100 |
|
| 101 | + # pylint: disable=invalid-name, too-many-branches |
| 102 | + |
80 | 103 | def update(self):
|
81 |
| - # What to do if there is 0 or 1 element? |
| 104 | + """Update the drawing of the sparkline |
| 105 | +
|
| 106 | + """ |
82 | 107 |
|
83 | 108 | # get the y range
|
84 |
| - if self.yMin == None: |
| 109 | + if self.yMin is None: |
85 | 110 | self.yBottom = min(self._spark_list)
|
86 | 111 | else:
|
87 | 112 | self.yBottom = self.yMin
|
88 | 113 |
|
89 |
| - if self.yMax == None: |
| 114 | + if self.yMax is None: |
90 | 115 | self.yTop = max(self._spark_list)
|
91 | 116 | else:
|
92 | 117 | self.yTop = self.yMax
|
|
0 commit comments