示例#1
0
chart_width = display.width - 50
chart_height = display.height - 50

font = terminalio.FONT

line_color = 0xFFFFFF

# Setup the first bitmap and sparkline
# This sparkline has no background bitmap
# mySparkline1 uses a vertical y range between 0 to 10 and will contain a
# maximum of 40 items
sparkline1 = Sparkline(
    width=chart_width,
    height=chart_height,
    max_items=40,
    y_min=0,
    y_max=10,
    x=40,
    y=30,
    color=line_color,
)

# Label the y-axis range

text_xoffset = -10
text_label1a = label.Label(font=font,
                           text=str(sparkline1.y_top),
                           color=line_color)  # yTop label
text_label1a.anchor_point = (1, 0.5)  # set the anchorpoint at right-center
text_label1a.anchored_position = (
    sparkline1.x + text_xoffset,
    sparkline1.y,
##########################################

# Baseline size of the sparkline chart, in pixels.
chart_width = 50
chart_height = 50

font = terminalio.FONT

# Setup the first bitmap and sparkline
# This sparkline has no background bitmap
# sparkline1 uses a vertical y range between -1 to +1.25 and will contain a maximum of 40 items
sparkline1 = Sparkline(
    width=chart_width,
    height=chart_height,
    max_items=40,
    y_min=-1,
    y_max=1.25,
    x=10,
    y=10,
)

# Label the y-axis range
text_label1a = label.Label(font=font,
                           text=str(sparkline1.y_top),
                           color=0xFFFFFF)  # y_top label
text_label1a.anchor_point = (0, 0.5)  # set the anchorpoint
text_label1a.anchored_position = (
    10 + chart_width,
    10,
)  # set the text anchored position to the upper right of the graph
示例#3
0
class SparkLineView(View):
    def __init__(self, display, layout_json):
        self.json = layout_json
        if "view_type" not in layout_json:
            raise MissingTypeError
        if layout_json["view_type"] != "SparkLine":
            raise IncorrectTypeError(
                "view_type '{}' does not match Layout Class 'SparkLine'".
                format(layout_json["view_type"]))
        self._display = display
        if "attributes" in layout_json:
            _missing_attrs = []
            for attribute in REQUIRED_ATTRIBUTES:
                if attribute not in layout_json['attributes']:
                    _missing_attrs.append(attribute)
            if len(_missing_attrs) > 0:
                raise MissingRequiredAttributesError(
                    "Missing required attributes: {}".format(_missing_attrs))

            _color = 0xFFFFFF
            if "color" in layout_json["attributes"]:
                _outline = int(layout_json["attributes"]["color"], 16)

            _width = 0
            if "width" in layout_json["attributes"]:
                _width = self.keyword_compiler(
                    layout_json["attributes"]["width"])

            _height = 0
            if "height" in layout_json["attributes"]:
                _height = self.keyword_compiler(
                    layout_json["attributes"]["height"])

            _y_min = None
            if "y_min" in layout_json["attributes"]:
                _y_min = self.keyword_compiler(
                    layout_json["attributes"]["y_min"])

            _x_min = None
            if "x_min" in layout_json["attributes"]:
                _x_min = self.keyword_compiler(
                    layout_json["attributes"]["x_min"])

            _max_items = 10
            if "max_items" in layout_json["attributes"]:
                _max_items = self.keyword_compiler(
                    layout_json["attributes"]["max_items"])

            _x = 0
            if "x" in layout_json["attributes"]:
                _x = self.keyword_compiler(layout_json["attributes"]["x"], {
                    "WIDTH": _width,
                    "HEIGHT": _height
                })

            _y = 0
            if "y" in layout_json["attributes"]:
                _y = self.keyword_compiler(layout_json["attributes"]["y"], {
                    "WIDTH": _width,
                    "HEIGHT": _height
                })

            _points = None
            if "points" in layout_json["attributes"]:
                _points = layout_json["attributes"]["points"]

            self.sparkline = Sparkline(_width,
                                       _height,
                                       _max_items,
                                       y_min=_y_min,
                                       y_max=_x_min,
                                       x=_x,
                                       y=_y,
                                       color=_color)

            if _points != None:
                for _point in _points:
                    self.sparkline.add_value(_point)

            self.view = self.sparkline
        else:
            raise MissingAttributesError()