for i in range(total_ticks + 1): x_start = sparkline1.x - 5 x_end = sparkline1.x y_both = int(round(sparkline1.y + (i * (chart_height) / (total_ticks)))) if y_both > sparkline1.y + chart_height - 1: y_both = sparkline1.y + chart_height - 1 my_group.append(Line(x_start, y_both, x_end, y_both, color=line_color)) # Set the display to show my_group that contains the sparkline and other graphics display.show(my_group) # Start the main loop while True: # Turn off auto_refresh to prevent partial updates of the screen during updates # of the sparkline drawing display.auto_refresh = False # add_value: add a new value to a sparkline # Note: The y-range for mySparkline1 is set to 0 to 10, so all these random # values (between 0 and 10) will fit within the visible range of this sparkline sparkline1.add_value(random.uniform(0, 10)) # Turn on auto_refresh for the display display.auto_refresh = True # The display seems to be less jittery if a small sleep time is provided # You can adjust this to see if it has any effect time.sleep(0.01)
# sparklines display.show(my_group) i = 0 # This is a counter for changing the random values for mySparkline3 # Start the main loop while True: # Turn off auto_refresh to prevent partial updates of the screen during updates # of the sparklines display.auto_refresh = False # add_value: add a new value to a sparkline # Note: The y-range for sparkline1 is set to -1 to 1.25, so all these random # values (between 0 and 1) will fit within the visible range of this sparkline sparkline1.add_value(random.uniform(0, 1)) # Note: For sparkline2, the y-axis range is set from 0 to 1. # With the random values set between -1 and +2, the values will sometimes # be out of the y-range. This example shows how the fixed y-range (0 to 1) # will "clip" values (it will not display them) that are above or below the # y-range. sparkline2.add_value(random.uniform(-1, 2)) # sparkline3 is set autoranging for both the top and bottom of the Y-axis # In this example, for 15 values, this adds points in the range from 0 to 1. # Then, for the next 15 values, it adds points in the range of 0 to 10. # This is to highlight the autoranging of the y-axis. # Notice how the y-axis labels show that the y-scale is changing. #
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()