def _GetRescaledData(self, data, data_max): # Use the extended encoding if we don't have too many data points if data_max: rescaled_max = (len(data) > 1500 and \ SimpleData.max_value() or ExtendedData.max_value()) scaling_factor = float(rescaled_max) / float(data_max) else: scaling_factor = 0 scaled_data = [] for point in data: scaled_data.append(int(float(point) * scaling_factor)) return scaled_data
def GetHtml(self): if self.IsEmpty(): return "" # Determine top 10 addresses top_addresses = \ [(count, address) for (address, count) in self.__all_addresses.items()] top_addresses.sort(reverse=True) top_addresses = [address for (count, address) in top_addresses] if len(top_addresses) > 10: top_addresses = top_addresses[0:10] top_addresses.reverse() # Collect lines for each address bucket_lines = {} for bucket in self.__buckets: sum = 0 for address in top_addresses: sum += bucket.get(address, 0) sum = float(sum) fraction_sum = 0 for address in top_addresses: if sum == 0: fraction = 0 else: fraction = bucket.get(address, 0) / sum fraction_sum += fraction # Make sure everything adds up to 1.0 if address == top_addresses[-1]: fraction_sum = 1.0 if address not in bucket_lines: bucket_lines[address] = [] bucket_lines[address].append( round(fraction_sum * ExtendedData.max_value())) # Smooth lines for address, points in bucket_lines.items(): smoothed = [] window = [] window_sum = 0 for i in xrange(0, len(points)): if i < self.__min_bucket or i > self.__max_bucket: smoothed.append(0) else: point = points[i] if len(window) == Distribution._BUCKET_SIZE: window_sum -= window.pop(0) window.append(point) window_sum += point smoothed.append(round(window_sum / len(window))) bucket_lines[address] = smoothed # Generate chart chart = SimpleLineChart(450, 250) data_index = 0 colors = [] legend = [] top_addresses.reverse() for address in top_addresses: data = bucket_lines[address] chart.add_data(data) color = _FILL_COLORS[data_index % len(_FILL_COLORS)] chart.add_fill_range(color, data_index, data_index + 1) data_index += 1 colors.append(color) legend.append((color, self.__address_names[address], address)) # Another set of points to make sure we will to the bottom chart.add_data([0, 0]) chart.set_colours(colors) chart.set_axis_labels(Axis.BOTTOM, MONTH_NAMES) t = Template( file="templates/distribution.tmpl", searchList={ "id": self.id, "chart": chart, # We don't use the legend feature of the chart API since that would # make the URL longer than its limits "legend": legend, "class": self.__css_class, }) return unicode(t)
def GetHtml(self): if self.IsEmpty(): return "" # Determine top 10 addresses top_addresses = \ [(count, address) for (address, count) in self.__all_addresses.items()] top_addresses.sort(reverse=True) top_addresses = [address for (count, address) in top_addresses] if len(top_addresses) > 10: top_addresses = top_addresses[0:10] top_addresses.reverse() # Collect lines for each address bucket_lines = {} for bucket in self.__buckets: sum = 0 for address in top_addresses: sum += bucket.get(address, 0) sum = float(sum) fraction_sum = 0 for address in top_addresses: if sum == 0: fraction = 0 else: fraction = bucket.get(address, 0)/sum fraction_sum += fraction # Make sure everything adds up to 1.0 if address == top_addresses[-1]: fraction_sum = 1.0 if address not in bucket_lines: bucket_lines[address] = [] bucket_lines[address].append(round( fraction_sum * ExtendedData.max_value())) # Smooth lines for address, points in bucket_lines.items(): smoothed = [] window = [] window_sum = 0 for i in xrange(0, len(points)): if i < self.__min_bucket or i > self.__max_bucket: smoothed.append(0) else: point = points[i] if len(window) == Distribution._BUCKET_SIZE: window_sum -= window.pop(0) window.append(point) window_sum += point smoothed.append(round(window_sum/len(window))) bucket_lines[address] = smoothed # Generate chart chart = SimpleLineChart(450, 250) data_index = 0 colors = [] legend = [] top_addresses.reverse() for address in top_addresses: data = bucket_lines[address] chart.add_data(data) color = _FILL_COLORS[data_index % len(_FILL_COLORS)] chart.add_fill_range( color, data_index, data_index + 1) data_index += 1 colors.append(color) legend.append((color, self.__address_names[address], address)) # Another set of points to make sure we will to the bottom chart.add_data([0, 0]) chart.set_colours(colors) chart.set_axis_labels(Axis.BOTTOM, MONTH_NAMES) t = Template( file="templates/distribution.tmpl", searchList = { "id": self.id, "chart": chart, # We don't use the legend feature of the chart API since that would # make the URL longer than its limits "legend": legend, "class": self.__css_class, }) return unicode(t)