def get_series(user, length=4, limit=15): if ":" in user: user = user.replace("_", "+") data, weeks = get_data(user, length) if not data and not weeks: data, weeks = get_data(user, length) if not data and not weeks: data, weeks = get_data(user, length) if not data and not weeks: return None artists = {} for week in data: for artist, plays in week: artists[artist] = [] for week in data: week = dict(week) for artist in artists: plays = week.get(artist, 0) if plays < 2: plays = 0 artists[artist].append(plays) artists = artists.items() artists.sort(key=lambda (x, y): max(y)) artists.reverse() sh, ss, sv = rgb_to_hsv(*hex_to_rgba("ec2d60")[:3]) eh, es, ev = rgb_to_hsv(*hex_to_rgba("0c4da2")[:3]) a = True ad = 0.3 th, ts, tv = (eh - sh) / float(limit), (es - ss) / float(limit), ( ev - sv) / float(limit) series_set = SeriesSet() for artist, data in artists[:15]: series_set.add_series( Series( artist, dict([(datetime.datetime.fromtimestamp(weeks[i][0]), x) for i, x in enumerate(data)]), rgba_to_hex(*(hsv_to_rgb(sh, ss, sv) + (a and 1 or 1 - ad, ))), )) sh += th ss += ts sv += tv a = not a ad += (0.6 / limit) return series_set
def get_series(user, length=4, limit=15): if ":" in user: user = user.replace("_", "+") data, weeks = get_data(user, length) if not data and not weeks: data, weeks = get_data(user, length) if not data and not weeks: data, weeks = get_data(user, length) if not data and not weeks: return None artists = {} for week in data: for artist, plays in week: artists[artist] = [] for week in data: week = dict(week) for artist in artists: plays = week.get(artist, 0) if plays < 2: plays = 0 artists[artist].append(plays) artists = artists.items() artists.sort(key=lambda (x,y):max(y)) artists.reverse() sh, ss, sv = rgb_to_hsv(*hex_to_rgba("ec2d60")[:3]) eh, es, ev = rgb_to_hsv(*hex_to_rgba("0c4da2")[:3]) a = True ad = 0.3 th, ts, tv = (eh-sh)/float(limit), (es-ss)/float(limit), (ev-sv)/float(limit) series_set = SeriesSet() for artist, data in artists[:15]: series_set.add_series(Series( artist, dict([(datetime.datetime.fromtimestamp(weeks[i][0]), x) for i, x in enumerate(data)]), rgba_to_hex(*(hsv_to_rgb(sh, ss, sv) + (a and 1 or 1-ad,))), )) sh += th ss += ts sv += tv a = not a ad += (0.6/limit) return series_set
def render_debug(self, context): """Renders the calculation rectangles""" context.save() for (size, (x1, y1, x2, y2)), title in self.labels: if size: context.set_source_rgba(*hex_to_rgba("#f006")) context.set_line_width(1) else: context.set_source_rgba(*hex_to_rgba("#0f06")) context.set_line_width(0.5) context.rectangle(x1, y1, (x2 - x1), (y2 - y1)) context.stroke() context.restore()
def render_debug(self, context): """Renders the calculation rectangles""" context.save() for (size, (x1, y1, x2, y2)), title in self.labels: if size: context.set_source_rgba(*hex_to_rgba("#f006")) context.set_line_width(1) else: context.set_source_rgba(*hex_to_rgba("#0f06")) context.set_line_width(0.5) context.rectangle(x1, y1, (x2-x1), (y2-y1)) context.stroke() context.restore()
def render(self, context, debug=False): context.set_source_rgba(*hex_to_rgba(self.colour)) context.rectangle(0, 0, self.width, self.height) context.fill()
def render(self, context, debug=False): context.set_source_rgba(*hex_to_rgba(self.colour)) context.rectangle(0,0,self.width,self.height) context.fill()
def stroke(nx=None, ox=None): curve = context.copy_path() # If there's a peak highlight line, we need to draw it if self.peak_highlight: context.save() peak_height, colour = self.peak_highlight context.set_source_rgba(*hex_to_rgba(colour)) # Now, use the curve as a mask for that context.line_to(nx, self.plot_height) context.line_to(ox, self.plot_height) context.clip() # Draw a rectangle at the right height for highlight bottom = (1 - self.y_scale.get_point(peak_height) ) * self.plot_height context.rectangle(0, 0, self.width, bottom) context.fill() context.restore() context.append_path(curve) if prev_style == Series.STYLE_DASHED: context.set_source_rgba(*series.color_as_rgba()) context.set_dash([3, 2], 2) context.stroke() context.set_dash([], 0) elif prev_style == Series.STYLE_LIGHT: r, g, b, a = series.color_as_rgba() context.set_source_rgba(r, g, b, a * 0.5) elif prev_style == Series.STYLE_VLIGHT: r, g, b, a = series.color_as_rgba() context.set_source_rgba(r, g, b, a * 0.4) elif prev_style in [ Series.STYLE_LINETOP, Series.STYLE_DOUBLEFILL, Series.STYLE_WHOLEFILL ]: r, g, b, a = series.color_as_rgba() if self.two_passes and self.first_pass: # Now fill in under the curve context.line_to(nx, self.plot_height) context.line_to(ox, self.plot_height) if series.fill_color: context.set_source_rgba( *series.fill_color_as_rgba()) else: context.set_source_rgba(r, g, b, a * 0.5) context.fill() if prev_style == Series.STYLE_DOUBLEFILL: # Now fill in over the curve context.append_path(curve) context.line_to(nx, 0) context.line_to(ox, 0) if series.fill_color: r, g, b, a = series.fill_color_as_rgba() context.set_source_rgba(r, g, b, a * 0.4) else: context.set_source_rgba(r, g, b, a * 0.20) context.fill() elif prev_style == Series.STYLE_WHOLEFILL: # Now fill in over the curve context.append_path(curve) context.line_to(nx, 0) context.line_to(ox, 0) if series.fill_color: r, g, b, a = series.fill_color_as_rgba() context.set_source_rgba(r, g, b, a) else: context.set_source_rgba(r, g, b, a * 0.5) context.fill() context.append_path(curve) context.set_source_rgba(*series.color_as_rgba()) else: context.set_source_rgba(*series.color_as_rgba()) if self.first_pass: context.new_path() return context.stroke()
def stroke(nx=None, ox=None): curve = context.copy_path() # If there's a peak highlight line, we need to draw it if self.peak_highlight: context.save() peak_height, colour = self.peak_highlight context.set_source_rgba(*hex_to_rgba(colour)) # Now, use the curve as a mask for that context.line_to(nx, self.plot_height) context.line_to(ox, self.plot_height) context.clip() # Draw a rectangle at the right height for highlight bottom = (1 - self.y_scale.get_point(peak_height)) * self.plot_height context.rectangle(0, 0, self.width, bottom) context.fill() context.restore() context.append_path(curve) if prev_style == Series.STYLE_DASHED: context.set_source_rgba(*series.color_as_rgba()) context.set_dash([3, 2], 2) context.stroke() context.set_dash([], 0) elif prev_style == Series.STYLE_LIGHT: r,g,b,a = series.color_as_rgba() context.set_source_rgba(r,g,b,a*0.5) elif prev_style == Series.STYLE_VLIGHT: r,g,b,a = series.color_as_rgba() context.set_source_rgba(r,g,b,a*0.4) elif prev_style in [Series.STYLE_LINETOP, Series.STYLE_DOUBLEFILL, Series.STYLE_WHOLEFILL]: r,g,b,a = series.color_as_rgba() if self.two_passes and self.first_pass: # Now fill in under the curve context.line_to(nx, self.plot_height) context.line_to(ox, self.plot_height) if series.fill_color: context.set_source_rgba(*series.fill_color_as_rgba()) else: context.set_source_rgba(r,g,b,a*0.5) context.fill() if prev_style == Series.STYLE_DOUBLEFILL: # Now fill in over the curve context.append_path(curve) context.line_to(nx, 0) context.line_to(ox, 0) if series.fill_color: r,g,b,a = series.fill_color_as_rgba() context.set_source_rgba(r,g,b,a*0.4) else: context.set_source_rgba(r,g,b,a*0.20) context.fill() elif prev_style == Series.STYLE_WHOLEFILL: # Now fill in over the curve context.append_path(curve) context.line_to(nx, 0) context.line_to(ox, 0) if series.fill_color: r,g,b,a = series.fill_color_as_rgba() context.set_source_rgba(r,g,b,a) else: context.set_source_rgba(r,g,b,a*0.5) context.fill() context.append_path(curve) context.set_source_rgba(*series.color_as_rgba()) else: context.set_source_rgba(*series.color_as_rgba()) if self.first_pass: context.new_path() return context.stroke()
def render(self, context): context.save() major_style = self.style['forcerel grid.major'] minor_style = self.style['forcerel grid.minor'] plot_left = 0 # Render the labels first for linepos, title, ismajor in self.y_scale.get_lines(): if ismajor: this_style = major_style else: this_style = minor_style label_style = this_style.sub('label') context.select_font_face( label_style.get_font(), label_style.get_cairo_font_style(), label_style.get_cairo_font_weight(), ) context.set_font_size( label_style.get_float("font-size") ) y = linepos * self.height x_bearing, y_bearing, width, height = context.text_extents(title)[:4] padding = label_style.get_float("padding") left = padding - x_bearing context.move_to(left - x_bearing, y - height / 2.0 - y_bearing) context.show_text(title) plot_left = max(left + width + padding, plot_left) # Then the lines for linepos, title, ismajor in self.y_scale.get_lines(): if ismajor: this_style = major_style else: this_style = minor_style line_style = this_style.sub('line') context.set_line_width(line_style.get_float("width")) context.set_source_rgba(*line_style.get_color("color")) y = linepos * self.height context.move_to(plot_left, y) context.line_to(self.width, y) context.stroke() plot_width = self.width - plot_left # Work out node locations for node in self.nodeset: node.x = node.x * plot_width + plot_left node.y = self.y_scale.get_point(node.value) * self.height # Draw the links for link in self.nodeset.links: context.set_source_rgba(*hex_to_rgba(link.color)) context.move_to(link.start.x, link.start.y) context.line_to(link.end.x, link.end.y) context.stroke() # Draw nodes r = self.style['forcerel point'].get_float("radius") for node in self.nodeset: context.set_source_rgba(*hex_to_rgba(node.color)) context.move_to(node.x, node.y-r) context.arc(node.x, node.y, r, 0, math.pi * 2) context.fill()
def render(self, context): context.save() major_style = self.style['forcerel grid.major'] minor_style = self.style['forcerel grid.minor'] plot_left = 0 # Render the labels first for linepos, title, ismajor in self.y_scale.get_lines(): if ismajor: this_style = major_style else: this_style = minor_style label_style = this_style.sub('label') context.select_font_face( label_style.get_font(), label_style.get_cairo_font_style(), label_style.get_cairo_font_weight(), ) context.set_font_size(label_style.get_float("font-size")) y = linepos * self.height x_bearing, y_bearing, width, height = context.text_extents( title)[:4] padding = label_style.get_float("padding") left = padding - x_bearing context.move_to(left - x_bearing, y - height / 2.0 - y_bearing) context.show_text(title) plot_left = max(left + width + padding, plot_left) # Then the lines for linepos, title, ismajor in self.y_scale.get_lines(): if ismajor: this_style = major_style else: this_style = minor_style line_style = this_style.sub('line') context.set_line_width(line_style.get_float("width")) context.set_source_rgba(*line_style.get_color("color")) y = linepos * self.height context.move_to(plot_left, y) context.line_to(self.width, y) context.stroke() plot_width = self.width - plot_left # Work out node locations for node in self.nodeset: node.x = node.x * plot_width + plot_left node.y = self.y_scale.get_point(node.value) * self.height # Draw the links for link in self.nodeset.links: context.set_source_rgba(*hex_to_rgba(link.color)) context.move_to(link.start.x, link.start.y) context.line_to(link.end.x, link.end.y) context.stroke() # Draw nodes r = self.style['forcerel point'].get_float("radius") for node in self.nodeset: context.set_source_rgba(*hex_to_rgba(node.color)) context.move_to(node.x, node.y - r) context.arc(node.x, node.y, r, 0, math.pi * 2) context.fill()