prewikka/master: OpenSource Graphical Statistics implementation
[email protected] Mon, 11 Jan 2010 17:36:01 +0100 (CET)
| Newsgroups | gmane.comp.security.ids.prelude.cvs |
|---|---|
| Message-ID | <[email protected]> |
commit 3b67ac1361c66f62557acb665d462dfa4a2631c0 Author: Yoann Vandoorselaere <[email protected]> Date: Wed Jan 6 17:22:12 2010 +0100 OpenSource Graphical Statistics implementation This implement a set of basic statistics for Prewikka, based on the (provided) Cairoplot rendering engine. This initial implementation provides Categorizations, Sources, Targets, Analyzers, and Timeline statistics. ======================================== prewikka/Chart.py | 398 ++++++++ prewikka/Core.py | 4 +- prewikka/MyConfigParser.py | 40 +- prewikka/cairoplot.py | 2265 +++++++++++++++++++++++++++++++++++++++++ prewikka/templates/Stats.tmpl | 167 +++ prewikka/utils.py | 26 + prewikka/views/__init__.py | 14 +- prewikka/views/stats.py | 850 ++++++++++++++++ setup.py | 1 + 9 files changed, 3725 insertions(+), 40 deletions(-) ======================================== diff --git a/prewikka/Chart.py b/prewikka/Chart.py new file mode 100644 index 0000000..073eb93 --- /dev/null +++ b/prewikka/Chart.py @@ -0,0 +1,398 @@ +# Copyright (C) 2005-2009 PreludeIDS Technologies. All Rights Reserved. +# Author: Nicolas Delon <[email protected]> +# Author: Yoann Vandoorselaere <[email protected]> +# +# This file is part of the Prewikka program. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + + +import stat +import time +import base64 +import os, os.path +import glob, tempfile + +from prewikka import utils, siteconfig, cairoplot +from preludedb import PreludeDBError + +from xml.dom.minidom import parse, parseString + +RED_STD = "c1292e" +ORANGE_STD = "F29324" +YELLOW_STD = "f8e930" +GREEN_STD = "7ab41d" +BLUE_STD = "528fc8" + +COLOR_MAP = "528fc8", "7ab41d", "f8e930", "F29324", "c1292e", "874b94", "212483", \ + "487118", "ea752c", "8C0A14", "5F3269", "196d38" + + + +def userToHex(user): + if not user: + return "" + + hval = "" + for i in user: + hval += hex(ord(i)).replace("0x", "") + + return hval + + +class ChartCommon: + def __init__(self, width=800, height=450): + self._filename = None + self._values = [ ] + self._labels = [ ] + self._has_title = False + self._support_link = False + self._width = width + self._height = height + self._color_map = COLOR_MAP + self._names_map = {} + self._color_map_idx = 0 + + def hex2rgb(self, color): + r = int(color[0:2], 16) + g = int(color[2:4], 16) + b = int(color[4:6], 16) + + return (r/255.0, g/255.0, b/255.0) + + def getWidth(self): + return self._width + + def getHeight(self): + return self._height + + def _colorMap2str(self): + if type(self._color_map) not in (list, tuple): + return None + + str = "" + for color in self._color_map: + if len(str): + str += "," + + str += color + + return str + + def getItemColor(self, str): + if isinstance(self._color_map, dict): + return self._color_map[str] + + color = self._color_map[self._color_map_idx % len(self._color_map)] + self._color_map_idx += 1 + + return color + + def setColorMap(self, plist): + self._color_map = plist + + def getFilename(self): + return self._filename + + def getHref(self): + return self._href + + def setTitle(self, title): + self.addTitle(title, "", 14) + self._has_title = True + + def setValues(self, values): + self._values = values + + def setLabels(self, labels): + self._labels = labels + + def addLabelValuePair(self, label, value, link=None): + self._labels.append(label) + if self._support_link: + self._values.append((value, utils.escape_html_string(link))) + else: + self._values.append(value) + + def _remove_old_chart_files(self, pathname, expire): + directory = pathname + "_*" + files = glob.glob(directory) + + used = None + prev = None + + for f in files: + mtime = os.stat(f)[stat.ST_MTIME] + now = time.time() + + if not expire or (now - mtime) > (2 * expire): + os.remove(f) + + def _getFilename(self, name, expire = None, user = None, uid=None, gid=None, suffix=".png"): + old_mask = os.umask(0) + basename = base64.urlsafe_b64encode(name) + pathname = os.path.join(siteconfig.htdocs_dir, "generated_images") + + user = base64.urlsafe_b64encode(user) + pathname = os.path.normpath(os.path.join(pathname, user)) + + try: + os.mkdir(pathname, 0755) + except: pass + if uid != None and gid != None: + os.lchown(pathname, uid, gid) + + self._remove_old_chart_files(os.path.join(pathname, basename), expire) + + fd, self._filename = tempfile.mkstemp(prefix = basename + "_", suffix = suffix, dir = pathname) + if uid != None and gid != None: + os.lchown(self._filename, uid, gid) + + os.chmod(self._filename, 0644) + + self._href = "prewikka/generated_images/%s" % (user or "") + "/" + os.path.basename(self._filename) + os.umask(old_mask) + + return self._filename + +class TimelineChartCommon(ChartCommon): + def getType(self): + return "None" + + def __init__(self, width, height): + ChartCommon.__init__(self, width, height) + self._got_value = False + self._color_map_idx = 0 + self._assigned_colors = {} + self._multiple_values = False + self._total = [] + + def enableMultipleValues(self, names_and_colors={}): + self._multiple_values = True + self._names_and_colors = names_and_colors + + self._values = utils.OrderedDict() + for name in self._names_and_colors.keys(): + self._values[name] = [] + + def getItemColor(self, name): + if not self._multiple_values: + return ChartCommon.getItemColor(self, name) + + if self._names_and_colors.has_key(name): + return self._names_and_colors[name] + + if self._assigned_colors.has_key(name): + return self._assigned_colors[name] + + color = self._assigned_colors[name] = ChartCommon.getItemColor(self, name) + return color + + def _itemFromValue(self, value): + if isinstance(value, tuple): + return value + return value, None + + if self._support_link: + return value[0], utils.escape_html_string(value[1]) + else: + return value[0] + + def addLabelValuesPair(self, label, values, total_link): + empty = True + for i in values.values(): + if i != 0: + empty = False + break + + if not self._got_value and empty: + # do not add 0 only values at the beginning of the chart + return + + if self._support_link and total_link: + total_link = utils.escape_html_string(total_link) + + self._labels.append(label) + + clen = 0 + if self._values: + clen = len(self._values.values()[0]) + + total = 0 + for name in values.keys(): + if not self._values.has_key(name): + if clen > 0: + self._values[name] = [(0, None) for i in range(0, clen)] + else: + self._values[name] = [] + + value = self._itemFromValue(values[name]) + self._values[name].append(value) + + total += value[0] + + self._total.append((total, total_link)) + + for name in self._values.keys(): + if not values.has_key(name): + self._values[name].append(self._itemFromValue(0)) + + self._got_value = True + + def addLabelValuePair(self, label, values, link=None): + if self._multiple_values or isinstance(values, dict): + if not isinstance(self._values, dict): + self._values = utils.OrderedDict() + + self.addLabelValuesPair(label, values, link) + else: + ChartCommon.addLabelValuePair(self, label, values, link) + + +class CairoDistributionChart(ChartCommon): + def getType(self): + return "None" + + def render(self, name, expire=None, user=None, suffix=".png", uid=None, gid=None): + fname = self._getFilename(name, expire, user, uid, gid); + + color = [] + idx = 0 + data = {} + total = 0 + + for l, v in zip(self._labels, self._values): + total += v + data[str(l)] = v + + item_color = self.getItemColor(str(l)) + if item_color: + color.append(self.hex2rgb(item_color)) + else: + color.append(self.hex2rgb(self._color_map[idx % len(self._color_map)])) + + idx += 1 + + other = 0 + if data: + share = 100.0 / total + + for key in data.keys(): + if data[key] * share < 1: + other += data[key] + else: + nkey = key + ", %.1f%% (%d)" % (share * data[key], data[key]) + data[nkey] = data[key] + + data.pop(key) + + if other: + data["Other, %.1f%% (%d)" % (share * other, other)] = other + + cairoplot.pie_plot(fname, data, self._width, self._height, gradient = True, shadow = True, colors=color) + + +class CairoTimelineChart(TimelineChartCommon): + def render(self, name, expire=None, user=None, suffix=".png", uid=None, gid=None): + fname = self._getFilename(name, expire, user, uid, gid); + + colors = [] + legend = [] + values = {} + for name in self._values.keys(): + nname = name[0:min(len(name), 25)] + if not values.has_key(nname): + values[nname] = [] + + for item in self._values[name]: + values[nname].append(item[0]) + + colors.append(self.hex2rgb(self.getItemColor(name))) + cairoplot.dot_line_plot(fname, values, self._width, self._height, border=0, axis=True, grid=True, + x_labels = self._labels, series_legend=True, series_colors=colors) + + +class CairoStackedTimelineChart(TimelineChartCommon): + def render(self, name, expire=None, user=None, suffix=".png", uid=None, gid=None): + fname = self._getFilename(name, expire, user, uid, gid); + + colors = [] + legend = [] + labels = [] + data = [] + minval = 0 + maxval = 0 + + values_items = self._values.items() + + for i in xrange(0, len(self._labels)): + l = [] + total = 0 + for name, values in values_items: + l.append(values[i]) + total += values[i] + + minval = min(minval, total) + maxval = max(maxval, total) + data.append(l) + + l = minval + increment = maxval / 20.0 + for i in xrange(0, 20+1): + labels.append("%.1f" % l) + l += increment + + idx = 0 + + for name, color in self._names_and_colors.values(): + if self._values.has_key(name): + if color: + colors.append(self.hex2rgb(color)) + else: + colors.append(self.hex2rgb(COLOR_MAP[idx % len(COLOR_MAP)])) + idx += 1 + legend.append(name) + + cairoplot.vertical_bar_plot(fname, data, self._width, self._height, border=0, series_labels=legend, display_values=True, grid=True, rounded_corners=False, stack=True, + three_dimension=False, y_labels=labels, x_labels = self._labels, colors=colors) + + +class CairoWorldChart(CairoDistributionChart): + def needCountryCode(self): + return False + +class TimelineChart(object): + def __new__(cls, width, height): + o = CairoTimelineChart(width, height) + o.isFlash = False + return o + +class StackedTimelineChart(object): + def __new__(cls, width, height): + o = CairoStackedTimelineChart(width, height) + o.isFlash = False + return o + +class WorldChart(object): + def __new__(cls, width, height): + o = CairoWorldChart(width, height) + o.isFlash = False + return o + +class DistributionChart(object): + def __new__(cls, width, height): + o = CairoDistributionChart(width, height) + o.isFlash = True + return o diff --git a/prewikka/Core.py b/prewikka/Core.py index 530feb6..b15a931 100644 --- a/prewikka/Core.py +++ b/prewikka/Core.py @@ -169,7 +169,7 @@ class Core: self._view_to_tab = { } self._view_to_section = { } - for section, tabs in (prewikka.views.events_section, prewikka.views.agents_section, + for section, tabs in (prewikka.views.events_section, prewikka.views.agents_section, prewikka.views.stats_section, prewikka.views.settings_section, prewikka.views.about_section): for tab, views in tabs: for view in views: @@ -218,7 +218,7 @@ class Core: def _setupDataSet(self, dataset, request, user, view=None, parameters={}): init_dataset(dataset, self._env.config, request) - sections = prewikka.views.events_section, prewikka.views.agents_section, prewikka.views.settings_section, \ + sections = prewikka.views.events_section, prewikka.views.agents_section, prewikka.views.stats_section, prewikka.views.settings_section, \ prewikka.views.about_section section_to_tabs = { } diff --git a/prewikka/MyConfigParser.py b/prewikka/MyConfigParser.py index 380e6e4..1ca0a46 100644 --- a/prewikka/MyConfigParser.py +++ b/prewikka/MyConfigParser.py @@ -36,41 +36,9 @@ class ParseError(Error): return "parse error in \"%s\" at %s line %d" % (self.line.rstrip(), self.filename, self.lineno) - -class OrderedDict(dict): - def __init__(self): - dict.__init__(self) - self.ordered_key_list = [ ] - - def __delitem__(self, key): - dict.__delitem__(self, key) - self.ordered_key_list.remove(key) - - def __setitem__(self, key, value): - dict.__setitem__(self, key, value) - if not key in self.ordered_key_list: - self.ordered_key_list.append(key) - - def values(self): - return map(lambda k: self[k], self.ordered_key_list) - - def keys(self): - return self.ordered_key_list - - def items(self): - return map(lambda key: (key, self[key]), self.ordered_key_list) - - def copy(self): - new = OrderedDict() - for key in self.keys(): - new[key] = self[key] - return new - - - -class ConfigParserSection(OrderedDict): +class ConfigParserSection(utils.OrderedDict): def __init__(self, name): - OrderedDict.__init__(self) + utils.OrderedDict.__init__(self) self.name = name def __nonzero__(self): @@ -116,8 +84,8 @@ class MyConfigParser: def __init__(self, filename): self.filename = filename - self._sections = OrderedDict() - self._root_section = OrderedDict() + self._sections = utils.OrderedDict() + self._root_section = utils.OrderedDict() self._current_section = self._root_section def load(self): diff --git a/prewikka/cairoplot.py b/prewikka/cairoplot.py new file mode 100644 index 0000000..e81559d --- /dev/null +++ b/prewikka/cairoplot.py @@ -0,0 +1,2265 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# CairoPlot.py +# +# Copyright (c) 2008 Rodrigo Moreira Araújo +# +# Author: Rodrigo Moreiro Araujo <[email protected]> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License +# as published by the Free Software Foundation; either version 2 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA + +#Contributor: João S. O. Bueno + +#TODO: review BarPlot Code +#TODO: x_label colision problem on Horizontal Bar Plot +#TODO: y_label's eat too much space on HBP + + +__version__ = 1.1 + +import cairo +import math +import random + +HORZ = 0 +VERT = 1 +NORM = 2 + +COLORS = {"red" : (1.0,0.0,0.0,1.0), "lime" : (0.0,1.0,0.0,1.0), "blue" : (0.0,0.0,1.0,1.0), + "maroon" : (0.5,0.0,0.0,1.0), "green" : (0.0,0.5,0.0,1.0), "navy" : (0.0,0.0,0.5,1.0), + "yellow" : (1.0,1.0,0.0,1.0), "magenta" : (1.0,0.0,1.0,1.0), "cyan" : (0.0,1.0,1.0,1.0), + "orange" : (1.0,0.5,0.0,1.0), "white" : (1.0,1.0,1.0,1.0), "black" : (0.0,0.0,0.0,1.0), + "gray" : (0.5,0.5,0.5,1.0), "light_gray" : (0.9,0.9,0.9,1.0), + "transparent" : (0.0,0.0,0.0,0.0)} + +THEMES = {"black_red" : [(0.0,0.0,0.0,1.0), (1.0,0.0,0.0,1.0)], + "red_green_blue" : [(1.0,0.0,0.0,1.0), (0.0,1.0,0.0,1.0), (0.0,0.0,1.0,1.0)], + "red_orange_yellow" : [(1.0,0.2,0.0,1.0), (1.0,0.7,0.0,1.0), (1.0,1.0,0.0,1.0)], + "yellow_orange_red" : [(1.0,1.0,0.0,1.0), (1.0,0.7,0.0,1.0), (1.0,0.2,0.0,1.0)], + "rainbow" : [(1.0,0.0,0.0,1.0), (1.0,0.5,0.0,1.0), (1.0,1.0,0.0,1.0), (0.0,1.0,0.0,1.0), (0.0,0.0,1.0,1.0), (0.3, 0.0, 0.5,1.0), (0.5, 0.0, 1.0, 1.0)]} + +def colors_from_theme( theme, series_length, mode = 'solid' ): + colors = [] + if theme not in THEMES.keys() : + raise Exception, "Theme not defined" + color_steps = THEMES[theme] + n_colors = len(color_steps) + if series_length <= n_colors: + colors = [color + tuple([mode]) for color in color_steps[0:n_colors]] + else: + iterations = [(series_length - n_colors)/(n_colors - 1) for i in color_steps[:-1]] + over_iterations = (series_length - n_colors) % (n_colors - 1) + for i in range(n_colors - 1): + if over_iterations <= 0: + break + iterations[i] += 1 + over_iterations -= 1 + for index,color in enumerate(color_steps[:-1]): + colors.append(color + tuple([mode])) + if iterations[index] == 0: + continue + next_color = color_steps[index+1] + color_step = ((next_color[0] - color[0])/(iterations[index] + 1), + (next_color[1] - color[1])/(iterations[index] + 1), + (next_color[2] - color[2])/(iterations[index] + 1), + (next_color[3] - color[3])/(iterations[index] + 1)) + for i in range( iterations[index] ): + colors.append((color[0] + color_step[0]*(i+1), + color[1] + color_step[1]*(i+1), + color[2] + color_step[2]*(i+1), + color[3] + color_step[3]*(i+1), + mode)) + colors.append(color_steps[-1] + tuple([mode])) + return colors + + +def other_direction(direction): + "explicit is better than implicit" + if direction == HORZ: + return VERT + else: + return HORZ + +#Class definition + +class Plot(object): + def __init__(self, + surface=None, + data=None, + width=640, + height=480, + background=None, + border = 0, + x_labels = None, + y_labels = None, + series_colors = None): + random.seed(2) + self.create_surface(surface, width, height) + self.dimensions = {} + self.dimensions[HORZ] = width + self.dimensions[VERT] = height + self.context = cairo.Context(self.surface) + self.labels={} + self.labels[HORZ] = x_labels + self.labels[VERT] = y_labels + self.load_series(data, x_labels, y_labels, series_colors) + self.font_size = 10 + self.set_background (background) + self.border = border + self.borders = {} + self.line_color = (0.5, 0.5, 0.5) + self.line_width = 0.5 + self.label_color = (0.0, 0.0, 0.0) + self.grid_color = (0.8, 0.8, 0.8) + + def create_surface(self, surface, width=None, height=None): + self.filename = None + if isinstance(surface, cairo.Surface): + self.surface = surface + return + if not type(surface) in (str, unicode): + raise TypeError("Surface should be either a Cairo surface or a filename, not %s" % surface) + sufix = surface.rsplit(".")[-1].lower() + self.filename = surface + if sufix == "png": + self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + elif sufix == "ps": + self.surface = cairo.PSSurface(surface, width, height) + elif sufix == "pdf": + self.surface = cairo.PSSurface(surface, width, height) + else: + if sufix != "svg": + self.filename += ".svg" + self.surface = cairo.SVGSurface(self.filename, width, height) + + def commit(self): + try: + self.context.show_page() + if self.filename and self.filename.endswith(".png"): + self.surface.write_to_png(self.filename) + else: + self.surface.finish() + except cairo.Error: + pass + + def load_series (self, data, x_labels=None, y_labels=None, series_colors=None): + #FIXME: implement Series class for holding series data, + # labels and presentation properties + + #data can be a list, a list of lists or a dictionary with + #each item as a labeled data series. + #we should (for the time being) create a list of lists + #and set labels for teh series rom teh values provided. + + self.series_labels = [] + self.data = [] + #dictionary + if hasattr(data, "keys"): + self.series_labels = data.keys() + for key in self.series_labels: + self.data.append(data[key]) + #lists of lists: + elif max([hasattr(item,'__delitem__') for item in data]) : + self.data = data + self.series_labels = range(len(data)) + #list + else: + self.data = [data] + self.series_labels = None + #TODO: allow user passed series_widths + self.series_widths = [1.0 for series in self.data] + self.process_colors( series_colors ) + + def process_colors( self, series_colors, length = None, mode = 'solid' ): + #series_colors might be None, a theme, a string of colors names or a string of color tuples + if length is None : + length = len( self.data ) + #no colors passed + if not series_colors: + #Randomize colors + self.series_colors = [ [random.random() for i in range(3)] + [1.0, mode] for series in range( length ) ] + else: + #Just theme pattern + if not hasattr( series_colors, "__iter__" ): + theme = series_colors + self.series_colors = colors_from_theme( theme.lower(), length ) + #Theme pattern and mode + elif not hasattr(series_colors, '__delitem__') and not hasattr( series_colors[0], "__iter__" ): + theme = series_colors[0] + mode = series_colors[1] + self.series_colors = colors_from_theme( theme.lower(), length, mode ) + #List + else: + self.series_colors = series_colors + for index, color in enumerate( self.series_colors ): + #element is a color name + if not hasattr(color, "__iter__"): + self.series_colors[index] = COLORS[color.lower()] + tuple([mode]) + #element is rgb tuple instead of rgba + elif len( color ) == 3 : + self.series_colors[index] += (1.0,mode) + #element has 4 elements, might be rgba tuple or rgb tuple with mode + elif len( color ) == 4 : + #last element is mode + if not hasattr(color[3], "__iter__"): + self.series_colors[index] += tuple([color[3]]) + self.series_colors[index][3] = 1.0 + #last element is alpha + else: + self.series_colors[index] += tuple([mode]) + + def get_width(self): + return self.surface.get_width() + + def get_height(self): + return self.surface.get_height() + + def set_background(self, background): + if background is None: + self.background = (0.0,0.0,0.0,0.0) + elif type(background) in (cairo.LinearGradient, tuple): + self.background = background + elif not hasattr(background,"__iter__"): + colors = background.split(" ") + if len(colors) == 1 and colors[0] in COLORS: + self.background = COLORS[background] + elif len(colors) > 1: + self.background = cairo.LinearGradient(self.dimensions[HORZ] / 2, 0, self.dimensions[HORZ] / 2, self.dimensions[VERT]) + for index,color in enumerate(colors): + self.background.add_color_stop_rgba(float(index)/(len(colors)-1),*COLORS[color]) + else: + raise TypeError ("Background should be either cairo.LinearGradient or a 3-tuple, not %s" % type(background)) + + def render_background(self): + if isinstance(self.background, cairo.LinearGradient): + self.context.set_source(self.background) + else: + self.context.set_source_rgba(*self.background) + self.context.rectangle(0,0, self.dimensions[HORZ], self.dimensions[VERT]) + self.context.fill() + + def render_bounding_box(self): + self.context.set_source_rgba(*self.line_color) + self.context.set_line_width(self.line_width) + self.context.rectangle(self.border, self.border, + self.dimensions[HORZ] - 2 * self.border, + self.dimensions[VERT] - 2 * self.border) + self.context.stroke() + + def render(self): + pass + +class ScatterPlot( Plot ): + def __init__(self, + surface=None, + data=None, + errorx=None, + errory=None, + width=640, + height=480, + background=None, + border=0, + axis = False, + dash = False, + discrete = False, + dots = 0, + grid = False, + series_legend = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + z_bounds = None, + x_title = None, + y_title = None, + series_colors = None, + circle_colors = None ): + + self.bounds = {} + self.bounds[HORZ] = x_bounds + self.bounds[VERT] = y_bounds + self.bounds[NORM] = z_bounds + self.titles = {} + self.titles[HORZ] = x_title + self.titles[VERT] = y_title + self.max_value = {} + self.axis = axis + self.discrete = discrete + self.dots = dots + self.grid = grid + self.series_legend = series_legend + self.variable_radius = False + self.x_label_angle = math.pi / 2.5 + self.circle_colors = circle_colors + + Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors) + + self.dash = None + if dash: + if hasattr(dash, "keys"): + self.dash = [dash[key] for key in self.series_labels] + elif max([hasattr(item,'__delitem__') for item in data]) : + self.dash = dash + else: + self.dash = [dash] + + self.load_errors(errorx, errory) + + def convert_list_to_tuple(self, data): + #Data must be converted from lists of coordinates to a single + # list of tuples + out_data = zip(*data) + if len(data) == 3: + self.variable_radius = True + return out_data + + def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): + #Dictionary with lists + if hasattr(data, "keys") : + if hasattr( data.values()[0][0], "__delitem__" ) : + for key in data.keys() : + data[key] = self.convert_list_to_tuple(data[key]) + elif len(data.values()[0][0]) == 3: + self.variable_radius = True + #List + elif hasattr(data[0], "__delitem__") : + #List of lists + if hasattr(data[0][0], "__delitem__") : + for index,value in enumerate(data) : + data[index] = self.convert_list_to_tuple(value) + #List + elif type(data[0][0]) != type((0,0)): + data = self.convert_list_to_tuple(data) + #Three dimensional data + elif len(data[0][0]) == 3: + self.variable_radius = True + #List with three dimensional tuples + elif len(data[0]) == 3: + self.variable_radius = True + Plot.load_series(self, data, x_labels, y_labels, series_colors) + self.calc_boundaries() + self.calc_labels() + + def load_errors(self, errorx, errory): + self.errors = None + if errorx == None and errory == None: + return + self.errors = {} + self.errors[HORZ] = None + self.errors[VERT] = None + #asimetric errors + if errorx and hasattr(errorx[0], "__delitem__"): + self.errors[HORZ] = errorx + #simetric errors + elif errorx: + self.errors[HORZ] = [errorx] + #asimetric errors + if errory and hasattr(errory[0], "__delitem__"): + self.errors[VERT] = errory + #simetric errors + elif errory: + self.errors[VERT] = [errory] + + def calc_labels(self): + if not self.labels[HORZ]: + amplitude = self.bounds[HORZ][1] - self.bounds[HORZ][0] + if amplitude % 10: #if horizontal labels need floating points + self.labels[HORZ] = ["%.2lf" % (float(self.bounds[HORZ][0] + (amplitude * i / 10.0))) for i in range(11) ] + else: + self.labels[HORZ] = ["%d" % (int(self.bounds[HORZ][0] + (amplitude * i / 10.0))) for i in range(11) ] + if not self.labels[VERT]: + amplitude = self.bounds[VERT][1] - self.bounds[VERT][0] + if amplitude % 10: #if vertical labels need floating points + self.labels[VERT] = ["%.2lf" % (float(self.bounds[VERT][0] + (amplitude * i / 10.0))) for i in range(11) ] + else: + self.labels[VERT] = ["%d" % (int(self.bounds[VERT][0] + (amplitude * i / 10.0))) for i in range(11) ] + + def calc_extents(self, direction): + self.context.set_font_size(self.font_size * 0.8) + self.max_value[direction] = max(self.context.text_extents(item)[2] for item in self.labels[direction]) + self.borders[other_direction(direction)] = self.max_value[direction] + self.border + 20 + + def calc_boundaries(self): + #HORZ = 0, VERT = 1, NORM = 2 + min_data_value = [0,0,0] + max_data_value = [0,0,0] + for serie in self.data : + for tuple in serie : + for index, item in enumerate(tuple) : + if item > max_data_value[index]: + max_data_value[index] = item + elif item < min_data_value[index]: + min_data_value[index] = item + + if not self.bounds[HORZ]: + self.bounds[HORZ] = (min_data_value[HORZ], max_data_value[HORZ]) + if not self.bounds[VERT]: + self.bounds[VERT] = (min_data_value[VERT], max_data_value[VERT]) + if not self.bounds[NORM]: + self.bounds[NORM] = (min_data_value[NORM], max_data_value[NORM]) + + def calc_all_extents(self): + self.calc_extents(HORZ) + self.calc_extents(VERT) + + self.plot_height = self.dimensions[VERT] - 2 * self.borders[VERT] + self.plot_width = self.dimensions[HORZ] - 2* self.borders[HORZ] + + self.plot_top = self.dimensions[VERT] - self.borders[VERT] + + def calc_steps(self): + #Calculates all the x, y, z and color steps + series_amplitude = [self.bounds[index][1] - self.bounds[index][0] for index in range(3)] + + if series_amplitude[HORZ]: + self.horizontal_step = float (self.plot_width) / series_amplitude[HORZ] + else: + self.horizontal_step = 0.00 + + if series_amplitude[VERT]: + self.vertical_step = float (self.plot_height) / series_amplitude[VERT] + else: + self.vertical_step = 0.00 + + if series_amplitude[NORM]: + if self.variable_radius: + self.z_step = float (self.bounds[NORM][1]) / series_amplitude[NORM] + if self.circle_colors: + self.circle_color_step = tuple([float(self.circle_colors[1][i]-self.circle_colors[0][i])/series_amplitude[NORM] for i in range(4)]) + else: + self.z_step = 0.00 + self.circle_color_step = ( 0.0, 0.0, 0.0, 0.0 ) + + def get_circle_color(self, value): + return tuple( [self.circle_colors[0][i] + value*self.circle_color_step[i] for i in range(4)] ) + + def render(self): + self.calc_all_extents() + self.calc_steps() + self.render_background() + self.render_bounding_box() + if self.axis: + self.render_axis() + if self.grid: + self.render_grid() + self.render_labels() + self.render_plot() + if self.errors: + self.render_errors() + if self.series_legend and self.series_labels: + self.render_legend() + + def render_axis(self): + #Draws both the axis lines and their titles + cr = self.context + cr.set_source_rgba(*self.line_color) + cr.move_to(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) + cr.line_to(self.borders[HORZ], self.borders[VERT]) + cr.stroke() + + cr.move_to(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) + cr.line_to(self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT]) + cr.stroke() + + cr.set_source_rgba(*self.label_color) + self.context.set_font_size( 1.2 * self.font_size ) + if self.titles[HORZ]: + title_width,title_height = cr.text_extents(self.titles[HORZ])[2:4] + cr.move_to( self.dimensions[HORZ]/2 - title_width/2, self.borders[VERT] - title_height/2 ) + cr.show_text( self.titles[HORZ] ) + + if self.titles[VERT]: + title_width,title_height = cr.text_extents(self.titles[VERT])[2:4] + cr.move_to( self.dimensions[HORZ] - self.borders[HORZ] + title_height/2, self.dimensions[VERT]/2 - title_width/2) + cr.rotate( math.pi/2 ) + cr.show_text( self.titles[VERT] ) + cr.rotate( -math.pi/2 ) + + def render_grid(self): + cr = self.context + horizontal_step = float( self.plot_height ) / ( len( self.labels[VERT] ) - 1 ) + vertical_step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 ) + + x = self.borders[HORZ] + vertical_step + y = self.plot_top - horizontal_step + + for label in self.labels[HORZ][:-1]: + cr.set_source_rgba(*self.grid_color) + cr.move_to(x, self.dimensions[VERT] - self.borders[VERT]) + cr.line_to(x, self.borders[VERT]) + cr.stroke() + x += vertical_step + for label in self.labels[VERT][:-1]: + cr.set_source_rgba(*self.grid_color) + cr.move_to(self.borders[HORZ], y) + cr.line_to(self.dimensions[HORZ] - self.borders[HORZ], y) + cr.stroke() + y -= horizontal_step + + def render_labels(self): + self.context.set_font_size(self.font_size * 0.8) + self.render_horz_labels() + self.render_vert_labels() + + def render_horz_labels(self): + cr = self.context + step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 ) + x = self.borders[HORZ] + for item in self.labels[HORZ]: + cr.set_source_rgba(*self.label_color) + width = cr.text_extents(item)[2] + cr.move_to(x, self.dimensions[VERT] - self.borders[VERT] + 5) + cr.rotate(self.x_label_angle) + cr.show_text(item) + cr.rotate(-self.x_label_angle) + x += step + + def render_vert_labels(self): + cr = self.context + step = ( self.plot_height ) / ( len( self.labels[VERT] ) - 1 ) + y = self.plot_top + for item in self.labels[VERT]: + cr.set_source_rgba(*self.label_color) + width = cr.text_extents(item)[2] + cr.move_to(self.borders[HORZ] - width - 5,y) + cr.show_text(item) + y -= step + + def render_legend(self): + cr = self.context + cr.set_font_size(self.font_size) + cr.set_line_width(self.line_width) + + widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2]) + tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3]) + max_width = self.context.text_extents(widest_word)[2] + max_height = self.context.text_extents(tallest_word)[3] * 1.1 + + color_box_height = max_height / 2 + color_box_width = color_box_height * 2 + + #Draw a bounding box + bounding_box_width = max_width + color_box_width + 15 + bounding_box_height = (len(self.series_labels)+0.5) * max_height + cr.set_source_rgba(1,1,1) + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - bounding_box_width, self.borders[VERT], + bounding_box_width, bounding_box_height) + cr.fill() + + cr.set_source_rgba(*self.line_color) + cr.set_line_width(self.line_width) + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - bounding_box_width, self.borders[VERT], + bounding_box_width, bounding_box_height) + cr.stroke() + + for idx,key in enumerate(self.series_labels): + #Draw color box + cr.set_source_rgba(*self.series_colors[idx][:4]) + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - max_width - color_box_width - 10, + self.borders[VERT] + color_box_height + (idx*max_height) , + color_box_width, color_box_height) + cr.fill() + + cr.set_source_rgba(0, 0, 0) + cr.rectangle(self.dimensions[HORZ] - self.borders[HORZ] - max_width - color_box_width - 10, + self.borders[VERT] + color_box_height + (idx*max_height), + color_box_width, color_box_height) + cr.stroke() + + #Draw series labels + cr.set_source_rgba(0, 0, 0) + cr.move_to(self.dimensions[HORZ] - self.borders[HORZ] - max_width - 5, self.borders[VERT] + ((idx+1)*max_height)) + cr.show_text(key) + + def render_errors(self): + cr = self.context + cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) + cr.clip() + radius = self.dots + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step + for index, serie in enumerate(self.data): + cr.set_source_rgba(*self.series_colors[index][:4]) + for number, tuple in enumerate(serie): + x = x0 + self.horizontal_step * tuple[0] + y = self.dimensions[VERT] - y0 - self.vertical_step * tuple[1] + if self.errors[HORZ]: + cr.move_to(x, y) + x1 = x - self.horizontal_step * self.errors[HORZ][0][number] + cr.line_to(x1, y) + cr.line_to(x1, y - radius) + cr.line_to(x1, y + radius) + cr.stroke() + if self.errors[HORZ] and len(self.errors[HORZ]) == 2: + cr.move_to(x, y) + x1 = x + self.horizontal_step * self.errors[HORZ][1][number] + cr.line_to(x1, y) + cr.line_to(x1, y - radius) + cr.line_to(x1, y + radius) + cr.stroke() + if self.errors[VERT]: + cr.move_to(x, y) + y1 = y + self.vertical_step * self.errors[VERT][0][number] + cr.line_to(x, y1) + cr.line_to(x - radius, y1) + cr.line_to(x + radius, y1) + cr.stroke() + if self.errors[VERT] and len(self.errors[VERT]) == 2: + cr.move_to(x, y) + y1 = y - self.vertical_step * self.errors[VERT][1][number] + cr.line_to(x, y1) + cr.line_to(x - radius, y1) + cr.line_to(x + radius, y1) + cr.stroke() + + + def render_plot(self): + cr = self.context + if self.discrete: + cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) + cr.clip() + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step + radius = self.dots + for number, serie in enumerate (self.data): + cr.set_source_rgba(*self.series_colors[number][:4]) + for tuple in serie : + if self.variable_radius: + radius = tuple[2]*self.z_step + if self.circle_colors: + cr.set_source_rgba( *self.get_circle_color( tuple[2]) ) + x = x0 + self.horizontal_step*tuple[0] + y = y0 + self.vertical_step*tuple[1] + cr.arc(x, self.dimensions[VERT] - y, radius, 0, 2*math.pi) + cr.fill() + else: + cr.rectangle(self.borders[HORZ], self.borders[VERT], self.plot_width, self.plot_height) + cr.clip() + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step + radius = self.dots + for number, serie in enumerate (self.data): + last_tuple = None + cr.set_source_rgba(*self.series_colors[number][:4]) + for tuple in serie : + x = x0 + self.horizontal_step*tuple[0] + y = y0 + self.vertical_step*tuple[1] + if self.dots: + if self.variable_radius: + radius = tuple[2]*self.z_step + cr.arc(x, self.dimensions[VERT] - y, radius, 0, 2*math.pi) + cr.fill() + if last_tuple : + old_x = x0 + self.horizontal_step*last_tuple[0] + old_y = y0 + self.vertical_step*last_tuple[1] + cr.move_to( old_x, self.dimensions[VERT] - old_y ) + cr.line_to( x, self.dimensions[VERT] - y) + cr.set_line_width(self.series_widths[number]) + + # Display line as dash line + if self.dash and self.dash[number]: + s = self.series_widths[number] + cr.set_dash([s*3, s*3], 0) + + cr.stroke() + cr.set_dash([]) + last_tuple = tuple + +class DotLinePlot(ScatterPlot): + def __init__(self, + surface=None, + data=None, + width=640, + height=480, + background=None, + border=0, + axis = False, + dash = False, + dots = 0, + grid = False, + series_legend = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + x_title = None, + y_title = None, + series_colors = None): + + ScatterPlot.__init__(self, surface, data, None, None, width, height, background, border, + axis, dash, False, dots, grid, series_legend, x_labels, y_labels, + x_bounds, y_bounds, None, x_title, y_title, series_colors, None ) + + + def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): + Plot.load_series(self, data, x_labels, y_labels, series_colors) + for serie in self.data : + for index,value in enumerate(serie): + serie[index] = (index, value) + + self.calc_boundaries() + self.calc_labels() + +class FunctionPlot(ScatterPlot): + def __init__(self, + surface=None, + data=None, + width=640, + height=480, + background=None, + border=0, + axis = False, + discrete = False, + dots = 0, + grid = False, + series_legend = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + x_title = None, + y_title = None, + series_colors = None, + step = 1): + + self.function = data + self.step = step + self.discrete = discrete + + data, x_bounds = self.load_series_from_function( self.function, x_bounds ) + + ScatterPlot.__init__(self, surface, data, None, None, width, height, background, border, + axis, False, discrete, dots, grid, series_legend, x_labels, y_labels, + x_bounds, y_bounds, None, x_title, y_title, series_colors, None ) + + def load_series(self, data, x_labels = None, y_labels = None, series_colors=None): + Plot.load_series(self, data, x_labels, y_labels, series_colors) + for serie in self.data : + for index,value in enumerate(serie): + serie[index] = (self.bounds[HORZ][0] + self.step*index, value) + + self.calc_boundaries() + self.calc_labels() + + def load_series_from_function( self, function, x_bounds ): + #TODO: Add the possibility for the user to define multiple functions with different discretization parameters + + #This function converts a function, a list of functions or a dictionary + #of functions into its corresponding array of data + data = None + #if no bounds are provided + if x_bounds == None: + x_bounds = (0,10) + + if hasattr(function, "keys"): #dictionary: + data = {} + for key in function.keys(): + data[ key ] = [] + i = x_bounds[0] + while i <= x_bounds[1] : + data[ key ].append( function[ key ](i) ) + i += self.step + elif hasattr(function, "__delitem__"): #list of functions + data = [] + for index,f in enumerate( function ) : + data.append( [] ) + i = x_bounds[0] + while i <= x_bounds[1] : + data[ index ].append( f(i) ) + i += self.step + else: #function + data = [] + i = x_bounds[0] + while i <= x_bounds[1] : + data.append( function(i) ) + i += self.step + + return data, x_bounds + + def calc_labels(self): + if not self.labels[HORZ]: + self.labels[HORZ] = [] + i = self.bounds[HORZ][0] + while i<=self.bounds[HORZ][1]: + self.labels[HORZ].append(str(i)) + i += float(self.bounds[HORZ][1] - self.bounds[HORZ][0])/10 + ScatterPlot.calc_labels(self) + + def render_plot(self): + if not self.discrete: + ScatterPlot.render_plot(self) + else: + last = None + cr = self.context + for number, series in enumerate (self.data): + cr.set_source_rgba(*self.series_colors[number][:4]) + x0 = self.borders[HORZ] - self.bounds[HORZ][0]*self.horizontal_step + y0 = self.borders[VERT] - self.bounds[VERT][0]*self.vertical_step + for tuple in series: + x = x0 + self.horizontal_step * tuple[0] + y = y0 + self.vertical_step * tuple[1] + cr.move_to(x, self.dimensions[VERT] - y) + cr.line_to(x, self.plot_top) + cr.set_line_width(self.series_widths[number]) + cr.stroke() + if self.dots: + cr.new_path() + cr.arc(x, self.dimensions[VERT] - y, 3, 0, 2.1 * math.pi) + cr.close_path() + cr.fill() + +class BarPlot(Plot): + def __init__(self, + surface = None, + data = None, + width = 640, + height = 480, + background = "white light_gray", + border = 0, + display_values = False, + grid = False, + rounded_corners = False, + stack = False, + three_dimension = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + series_colors = None, + main_dir = None): + + self.bounds = {} + self.bounds[HORZ] = x_bounds + self.bounds[VERT] = y_bounds + self.display_values = display_values + self.grid = grid + self.rounded_corners = rounded_corners + self.stack = stack + self.three_dimension = three_dimension + self.x_label_angle = math.pi / 2.5 + self.main_dir = main_dir + self.max_value = {} + self.plot_dimensions = {} + self.steps = {} + self.value_label_color = (0.5,0.5,0.5,1.0) + + Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors) + + def load_series(self, data, x_labels = None, y_labels = None, series_colors = None): + Plot.load_series(self, data, x_labels, y_labels, series_colors) + self.calc_boundaries() + + def process_colors(self, series_colors): + #Data for a BarPlot might be a List or a List of Lists. + #On the first case, colors must be generated for all bars, + #On the second, colors must be generated for each of the inner lists. + if hasattr(self.data[0], '__getitem__'): + length = max(len(series) for series in self.data) + else: + length = len( self.data ) + + Plot.process_colors( self, series_colors, length, 'linear') + + def calc_boundaries(self): + if not self.bounds[self.main_dir]: + if self.stack: + max_data_value = max(sum(serie) for serie in self.data) + else: + max_data_value = max(max(serie) for serie in self.data) + self.bounds[self.main_dir] = (0, max_data_value) + if not self.bounds[other_direction(self.main_dir)]: + self.bounds[other_direction(self.main_dir)] = (0, len(self.data)) + + def calc_extents(self, direction): + self.max_value[direction] = 0 + if self.labels[direction]: + widest_word = max(self.labels[direction], key = lambda item: self.context.text_extents(item)[2]) + self.max_value[direction] = self.context.text_extents(widest_word)[3 - direction] + self.borders[other_direction(direction)] = (2-direction)*self.max_value[direction] + self.border + direction*(5) + else: + self.borders[other_direction(direction)] = self.border + + def calc_horz_extents(self): + self.calc_extents(HORZ) + + def calc_vert_extents(self): + self.calc_extents(VERT) + + def calc_all_extents(self): + self.calc_horz_extents() + self.calc_vert_extents() + other_dir = other_direction(self.main_dir) + self.value_label = 0 + if self.display_values: + if self.stack: + self.value_label = self.context.text_extents(str(max(sum(serie) for serie in self.data)))[2 + self.main_dir] + else: + self.value_label = self.context.text_extents(str(max(max(serie) for serie in self.data)))[2 + self.main_dir] + if self.labels[self.main_dir]: + self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - 2*self.borders[self.main_dir] - self.value_label + else: + self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - self.borders[self.main_dir] - 1.2*self.border - self.value_label + self.plot_dimensions[other_dir] = self.dimensions[other_dir] - self.borders[other_dir] - self.border + self.plot_top = self.dimensions[VERT] - self.borders[VERT] + + def calc_steps(self): + other_dir = other_direction(self.main_dir) + self.series_amplitude = self.bounds[self.main_dir][1] - self.bounds[self.main_dir][0] + if self.series_amplitude: + self.steps[self.main_dir] = float(self.plot_dimensions[self.main_dir])/self.series_amplitude + else: + self.steps[self.main_dir] = 0.00 + series_length = len(self.data) + self.steps[other_dir] = float(self.plot_dimensions[other_dir])/(series_length + 0.1*(series_length + 1)) + self.space = 0.1*self.steps[other_dir] + + def render(self): + self.calc_all_extents() + self.calc_steps() + self.render_background() + self.render_bounding_box() + if self.grid: + self.render_grid() + if self.three_dimension: + self.render_ground() + if self.display_values: + self.render_values() + self.render_labels() + self.render_plot() + if self.series_labels: + self.render_legend() + + def draw_3d_rectangle_front(self, x0, y0, x1, y1, shift): + self.context.rectangle(x0-shift, y0+shift, x1-x0, y1-y0) + + def draw_3d_rectangle_side(self, x0, y0, x1, y1, shift): + self.context.move_to(x1-shift,y0+shift) + self.context.line_to(x1, y0) + self.context.line_to(x1, y1) + self.context.line_to(x1-shift, y1+shift) + self.context.line_to(x1-shift, y0+shift) + self.context.close_path() + + def draw_3d_rectangle_top(self, x0, y0, x1, y1, shift): + self.context.move_to(x0-shift,y0+shift) + self.context.line_to(x0, y0) + self.context.line_to(x1, y0) + self.context.line_to(x1-shift, y0+shift) + self.context.line_to(x0-shift, y0+shift) + self.context.close_path() + + def draw_round_rectangle(self, x0, y0, x1, y1): + self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) + self.context.line_to(x1-5, y0) + self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) + self.context.line_to(x1, y1-5) + self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) + self.context.line_to(x0+5, y1) + self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) + self.context.line_to(x0, y0+5) + self.context.close_path() + + def render_ground(self): + self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) + self.context.fill() + + self.draw_3d_rectangle_side (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) + self.context.fill() + + self.draw_3d_rectangle_top (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) + self.context.fill() + + def render_labels(self): + self.context.set_font_size(self.font_size * 0.8) + if self.labels[HORZ]: + self.render_horz_labels() + if self.labels[VERT]: + self.render_vert_labels() + + def render_legend(self): + cr = self.context + cr.set_font_size(self.font_size) + cr.set_line_width(self.line_width) + + widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2]) + tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3]) + max_width = self.context.text_extents(widest_word)[2] + max_height = self.context.text_extents(tallest_word)[3] * 1.1 + 5 + + color_box_height = max_height / 2 + color_box_width = color_box_height * 2 + + #Draw a bounding box + bounding_box_width = max_width + color_box_width + 15 + bounding_box_height = (len(self.series_labels)+0.5) * max_height + cr.set_source_rgba(1,1,1) + cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border, + bounding_box_width, bounding_box_height) + cr.fill() + + cr.set_source_rgba(*self.line_color) + cr.set_line_width(self.line_width) + cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border, + bounding_box_width, bounding_box_height) + cr.stroke() + + for idx,key in enumerate(self.series_labels): + #Draw color box + cr.set_source_rgba(*self.series_colors[idx][:4]) + cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, + self.border + color_box_height + (idx*max_height) , + color_box_width, color_box_height) + cr.fill() + + cr.set_source_rgba(0, 0, 0) + cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, + self.border + color_box_height + (idx*max_height), + color_box_width, color_box_height) + cr.stroke() + + #Draw series labels + cr.set_source_rgba(0, 0, 0) + cr.move_to(self.dimensions[HORZ] - self.border - max_width - 5, self.border + ((idx+1)*max_height)) + cr.show_text(key) + + +class HorizontalBarPlot(BarPlot): + def __init__(self, + surface = None, + data = None, + width = 640, + height = 480, + background = "white light_gray", + border = 0, + display_values = False, + grid = False, + rounded_corners = False, + stack = False, + three_dimension = False, + series_labels = None, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + series_colors = None): + + BarPlot.__init__(self, surface, data, width, height, background, border, + display_values, grid, rounded_corners, stack, three_dimension, + x_labels, y_labels, x_bounds, y_bounds, series_colors, HORZ) + self.series_labels = series_labels + + def calc_vert_extents(self): + self.calc_extents(VERT) + if self.labels[HORZ] and not self.labels[VERT]: + self.borders[HORZ] += 10 + + def draw_rectangle_bottom(self, x0, y0, x1, y1): + self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) + self.context.line_to(x0, y0+5) + self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) + self.context.line_to(x1, y0) + self.context.line_to(x1, y1) + self.context.line_to(x0+5, y1) + self.context.close_path() + + def draw_rectangle_top(self, x0, y0, x1, y1): + self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) + self.context.line_to(x1, y1-5) + self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) + self.context.line_to(x0, y1) + self.context.line_to(x0, y0) + self.context.line_to(x1, y0) + self.context.close_path() + + def draw_rectangle(self, index, length, x0, y0, x1, y1): + if length == 1: + BarPlot.draw_rectangle(self, x0, y0, x1, y1) + elif index == 0: + self.draw_rectangle_bottom(x0, y0, x1, y1) + elif index == length-1: + self.draw_rectangle_top(x0, y0, x1, y1) + else: + self.context.rectangle(x0, y0, x1-x0, y1-y0) + + #TODO: Review BarPlot.render_grid code + def render_grid(self): + self.context.set_source_rgba(0.8, 0.8, 0.8) + if self.labels[HORZ]: + self.context.set_font_size(self.font_size * 0.8) + step = (self.dimensions[HORZ] - 2*self.borders[HORZ] - self.value_label)/(len(self.labels[HORZ])-1) + x = self.borders[HORZ] + next_x = 0 + for item in self.labels[HORZ]: + width = self.context.text_extents(item)[2] + if x - width/2 > next_x and x - width/2 > self.border: + self.context.move_to(x, self.border) + self.context.line_to(x, self.dimensions[VERT] - self.borders[VERT]) + self.context.stroke() + next_x = x + width/2 + x += step + else: + lines = 11 + horizontal_step = float(self.plot_dimensions[HORZ])/(lines-1) + x = self.borders[HORZ] + for y in xrange(0, lines): + self.context.move_to(x, self.border) + self.context.line_to(x, self.dimensions[VERT] - self.borders[VERT]) + self.context.stroke() + x += horizontal_step + + def render_horz_labels(self): + step = (self.dimensions[HORZ] - 2*self.borders[HORZ])/(len(self.labels[HORZ])-1) + x = self.borders[HORZ] + next_x = 0 + + for item in self.labels[HORZ]: + self.context.set_source_rgba(*self.label_color) + width = self.context.text_extents(item)[2] + if x - width/2 > next_x and x - width/2 > self.border: + self.context.move_to(x - width/2, self.dimensions[VERT] - self.borders[VERT] + self.max_value[HORZ] + 3) + self.context.show_text(item) + next_x = x + width/2 + x += step + + def render_vert_labels(self): + series_length = len(self.labels[VERT]) + step = (self.plot_dimensions[VERT] - (series_length + 1)*self.space)/(len(self.labels[VERT])) + y = self.border + step/2 + self.space + + for item in self.labels[VERT]: + self.context.set_source_rgba(*self.label_color) + width, height = self.context.text_extents(item)[2:4] + self.context.move_to(self.borders[HORZ] - width - 5, y + height/2) + self.context.show_text(item) + y += step + self.space + self.labels[VERT].reverse() + + def render_values(self): + self.context.set_source_rgba(*self.value_label_color) + self.context.set_font_size(self.font_size * 0.8) + if self.stack: + for i,series in enumerate(self.data): + value = sum(series) + height = self.context.text_extents(str(value))[3] + x = self.borders[HORZ] + value*self.steps[HORZ] + 2 + y = self.borders[VERT] + (i+0.5)*self.steps[VERT] + (i+1)*self.space + height/2 + self.context.move_to(x, y) + self.context.show_text(str(value)) + else: + for i,series in enumerate(self.data): + inner_step = self.steps[VERT]/len(series) + y0 = self.border + i*self.steps[VERT] + (i+1)*self.space + for number,key in enumerate(series): + height = self.context.text_extents(str(key))[3] + self.context.move_to(self.borders[HORZ] + key*self.steps[HORZ] + 2, y0 + 0.5*inner_step + height/2, ) + self.context.show_text(str(key)) + y0 += inner_step + + def render_plot(self): + if self.stack: + for i,series in enumerate(self.data): + x0 = self.borders[HORZ] + y0 = self.borders[VERT] + i*self.steps[VERT] + (i+1)*self.space + for number,key in enumerate(series): + if self.series_colors[number][4] in ('radial','linear') : + linear = cairo.LinearGradient( key*self.steps[HORZ]/2, y0, key*self.steps[HORZ]/2, y0 + self.steps[VERT] ) + color = self.series_colors[number] + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) + linear.add_color_stop_rgba(1.0, *color[:4]) + self.context.set_source(linear) + elif self.series_colors[number][4] == 'solid': + self.context.set_source_rgba(*self.series_colors[number][:4]) + if self.rounded_corners: + self.draw_rectangle(number, len(series), x0, y0, x0+key*self.steps[HORZ], y0+self.steps[VERT]) + self.context.fill() + else: + self.context.rectangle(x0, y0, key*self.steps[HORZ], self.steps[VERT]) + self.context.fill() + x0 += key*self.steps[HORZ] + else: + for i,series in enumerate(self.data): + inner_step = self.steps[VERT]/len(series) + x0 = self.borders[HORZ] + y0 = self.border + i*self.steps[VERT] + (i+1)*self.space + for number,key in enumerate(series): + linear = cairo.LinearGradient(key*self.steps[HORZ]/2, y0, key*self.steps[HORZ]/2, y0 + inner_step) + color = self.series_colors[number] + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) + linear.add_color_stop_rgba(1.0, *color[:4]) + self.context.set_source(linear) + if self.rounded_corners and key != 0: + BarPlot.draw_round_rectangle(self,x0, y0, x0 + key*self.steps[HORZ], y0 + inner_step) + self.context.fill() + else: + self.context.rectangle(x0, y0, key*self.steps[HORZ], inner_step) + self.context.fill() + y0 += inner_step + +class VerticalBarPlot(BarPlot): + def __init__(self, + surface = None, + data = None, + width = 640, + height = 480, + background = "white light_gray", + border = 0, + display_values = False, + grid = False, + rounded_corners = False, + stack = False, + three_dimension = False, + series_labels = None, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + series_colors = None): + + BarPlot.__init__(self, surface, data, width, height, background, border, + display_values, grid, rounded_corners, stack, three_dimension, + x_labels, y_labels, x_bounds, y_bounds, series_colors, VERT) + self.series_labels = series_labels + + def calc_vert_extents(self): + self.calc_extents(VERT) + if self.labels[VERT] and not self.labels[HORZ]: + self.borders[VERT] += 10 + + def draw_rectangle_bottom(self, x0, y0, x1, y1): + self.context.move_to(x1,y1) + self.context.arc(x1-5, y1-5, 5, 0, math.pi/2) + self.context.line_to(x0+5, y1) + self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi) + self.context.line_to(x0, y0) + self.context.line_to(x1, y0) + self.context.line_to(x1, y1) + self.context.close_path() + + def draw_rectangle_top(self, x0, y0, x1, y1): + self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2) + self.context.line_to(x1-5, y0) + self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0) + self.context.line_to(x1, y1) + self.context.line_to(x0, y1) + self.context.line_to(x0, y0) + self.context.close_path() + + def draw_rectangle(self, index, length, x0, y0, x1, y1): + if length == 1: + BarPlot.draw_rectangle(self, x0, y0, x1, y1) + elif index == 0: + self.draw_rectangle_bottom(x0, y0, x1, y1) + elif index == length-1: + self.draw_rectangle_top(x0, y0, x1, y1) + else: + self.context.rectangle(x0, y0, x1-x0, y1-y0) + + def render_grid(self): + self.context.set_source_rgba(0.8, 0.8, 0.8) + if self.labels[VERT]: + lines = len(self.labels[VERT]) + vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1) + y = self.borders[VERT] + self.value_label + else: + lines = 11 + vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1) + y = 1.2*self.border + self.value_label + for x in xrange(0, lines): + self.context.move_to(self.borders[HORZ], y) + self.context.line_to(self.dimensions[HORZ] - self.border, y) + self.context.stroke() + y += vertical_step + + def render_ground(self): + self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) + self.context.fill() + + self.draw_3d_rectangle_side (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) + self.context.fill() + + self.draw_3d_rectangle_top (self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], + self.dimensions[HORZ] - self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT] + 5, 10) + self.context.fill() + + def render_horz_labels(self): + series_length = len(self.labels[HORZ]) + step = float (self.plot_dimensions[HORZ] - (series_length + 1)*self.space)/len(self.labels[HORZ]) + x = self.borders[HORZ] + step/2 + self.space + next_x = 0 + + for item in self.labels[HORZ]: + self.context.set_source_rgba(*self.label_color) + width = self.context.text_extents(item)[2] + if x - width/2 > next_x and x - width/2 > self.borders[HORZ]: + self.context.move_to(x - width/2, self.dimensions[VERT] - self.borders[VERT] + self.max_value[HORZ] + 3) + self.context.show_text(item) + next_x = x + width/2 + x += step + self.space + + def render_vert_labels(self): + self.context.set_source_rgba(*self.label_color) + y = self.borders[VERT] + self.value_label + step = (self.dimensions[VERT] - 2*self.borders[VERT] - self.value_label)/(len(self.labels[VERT]) - 1) + self.labels[VERT].reverse() + for item in self.labels[VERT]: + width, height = self.context.text_extents(item)[2:4] + self.context.move_to(self.borders[HORZ] - width - 5, y + height/2) + self.context.show_text(item) + y += step + self.labels[VERT].reverse() + + def render_values(self): + self.context.set_source_rgba(*self.value_label_color) + self.context.set_font_size(self.font_size * 0.8) + if self.stack: + for i,series in enumerate(self.data): + value = sum(series) + width = self.context.text_extents(str(value))[2] + x = self.borders[HORZ] + (i+0.5)*self.steps[HORZ] + (i+1)*self.space - width/2 + y = value*self.steps[VERT] + 2 + self.context.move_to(x, self.plot_top-y) + self.context.show_text(str(value)) + else: + for i,series in enumerate(self.data): + inner_step = self.steps[HORZ]/len(series) + x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space + for number,key in enumerate(series): + width = self.context.text_extents(str(key))[2] + self.context.move_to(x0 + 0.5*inner_step - width/2, self.plot_top - key*self.steps[VERT] - 2) + self.context.show_text(str(key)) + x0 += inner_step + + def render_plot(self): + if self.stack: + for i,series in enumerate(self.data): + x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space + y0 = 0 + for number,key in enumerate(series): + if self.series_colors[number][4] in ('linear','radial'): + linear = cairo.LinearGradient( x0, key*self.steps[VERT]/2, x0 + self.steps[HORZ], key*self.steps[VERT]/2 ) + color = self.series_colors[number] + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) + linear.add_color_stop_rgba(1.0, *color[:4]) + self.context.set_source(linear) + elif self.series_colors[number][4] == 'solid': + self.context.set_source_rgba(*self.series_colors[number][:4]) + if self.rounded_corners: + self.draw_rectangle(number, len(series), x0, self.plot_top - y0 - key*self.steps[VERT], x0 + self.steps[HORZ], self.plot_top - y0) + self.context.fill() + else: + self.context.rectangle(x0, self.plot_top - y0 - key*self.steps[VERT], self.steps[HORZ], key*self.steps[VERT]) + self.context.fill() + y0 += key*self.steps[VERT] + else: + for i,series in enumerate(self.data): + inner_step = self.steps[HORZ]/len(series) + y0 = self.borders[VERT] + x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space + for number,key in enumerate(series): + if self.series_colors[number][4] == 'linear': + linear = cairo.LinearGradient( x0, key*self.steps[VERT]/2, x0 + inner_step, key*self.steps[VERT]/2 ) + color = self.series_colors[number] + linear.add_color_stop_rgba(0.0, 3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) + linear.add_color_stop_rgba(1.0, *color[:4]) + self.context.set_source(linear) + elif self.series_colors[number][4] == 'solid': + self.context.set_source_rgba(*self.series_colors[number][:4]) + if self.rounded_corners and key != 0: + BarPlot.draw_round_rectangle(self, x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top) + self.context.fill() + elif self.three_dimension: + self.draw_3d_rectangle_front(x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top, 5) + self.context.fill() + self.draw_3d_rectangle_side(x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top, 5) + self.context.fill() + self.draw_3d_rectangle_top(x0, self.plot_top - key*self.steps[VERT], x0+inner_step, self.plot_top, 5) + self.context.fill() + else: + self.context.rectangle(x0, self.plot_top - key*self.steps[VERT], inner_step, key*self.steps[VERT]) + self.context.fill() + + x0 += inner_step + +class StreamChart(VerticalBarPlot): + def __init__(self, + surface = None, + data = None, + width = 640, + height = 480, + background = "white light_gray", + border = 0, + grid = False, + series_legend = None, + x_labels = None, + x_bounds = None, + y_bounds = None, + series_colors = None): + + VerticalBarPlot.__init__(self, surface, data, width, height, background, border, + False, grid, False, True, False, + None, x_labels, None, x_bounds, y_bounds, series_colors) + + def calc_steps(self): + other_dir = other_direction(self.main_dir) + self.series_amplitude = self.bounds[self.main_dir][1] - self.bounds[self.main_dir][0] + if self.series_amplitude: + self.steps[self.main_dir] = float(self.plot_dimensions[self.main_dir])/self.series_amplitude + else: + self.steps[self.main_dir] = 0.00 + series_length = len(self.data) + self.steps[other_dir] = float(self.plot_dimensions[other_dir])/series_length + + def render_legend(self): + pass + + def ground(self, index): + sum_values = sum(self.data[index]) + return -0.5*sum_values + + def calc_angles(self): + middle = self.plot_top - self.plot_dimensions[VERT]/2.0 + self.angles = [tuple([0.0 for x in range(len(self.data)+1)])] + for x_index in range(1, len(self.data)-1): + t = [] + x0 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] + y0 = middle - self.ground(x_index-1)*self.steps[VERT] + y2 = middle - self.ground(x_index+1)*self.steps[VERT] + t.append(math.atan(float(y0-y2)/(x0-x2))) + for data_index in range(len(self.data[x_index])): + x0 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] + y0 = middle - self.ground(x_index-1)*self.steps[VERT] - self.data[x_index-1][data_index]*self.steps[VERT] + y2 = middle - self.ground(x_index+1)*self.steps[VERT] - self.data[x_index+1][data_index]*self.steps[VERT] + + for i in range(0,data_index): + y0 -= self.data[x_index-1][i]*self.steps[VERT] + y2 -= self.data[x_index+1][i]*self.steps[VERT] + + if data_index == len(self.data[0])-1 and False: + self.context.set_source_rgba(0.0,0.0,0.0,0.3) + self.context.move_to(x0,y0) + self.context.line_to(x2,y2) + self.context.stroke() + self.context.arc(x0,y0,2,0,2*math.pi) + self.context.fill() + t.append(math.atan(float(y0-y2)/(x0-x2))) + self.angles.append(tuple(t)) + self.angles.append(tuple([0.0 for x in range(len(self.data)+1)])) + + def render_plot(self): + self.calc_angles() + middle = self.plot_top - self.plot_dimensions[VERT]/2.0 + p = 0.4*self.steps[HORZ] + for data_index in range(len(self.data[0])-1,-1,-1): + self.context.set_source_rgba(*self.series_colors[data_index][:4]) + + #draw the upper line + for x_index in range(len(self.data)-1) : + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] + y1 = middle - self.ground(x_index)*self.steps[VERT] - self.data[x_index][data_index]*self.steps[VERT] + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] + y2 = middle - self.ground(x_index + 1)*self.steps[VERT] - self.data[x_index + 1][data_index]*self.steps[VERT] + + for i in range(0,data_index): + y1 -= self.data[x_index][i]*self.steps[VERT] + y2 -= self.data[x_index+1][i]*self.steps[VERT] + + if x_index == 0: + self.context.move_to(x1,y1) + + ang1 = self.angles[x_index][data_index+1] + ang2 = self.angles[x_index+1][data_index+1] + math.pi + self.context.curve_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1), + x2+p*math.cos(ang2),y2+p*math.sin(ang2), + x2,y2) + + for x_index in range(len(self.data)-1,0,-1) : + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] + y1 = middle - self.ground(x_index)*self.steps[VERT] + x2 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] + y2 = middle - self.ground(x_index - 1)*self.steps[VERT] + + for i in range(0,data_index): + y1 -= self.data[x_index][i]*self.steps[VERT] + y2 -= self.data[x_index-1][i]*self.steps[VERT] + + if x_index == len(self.data)-1: + self.context.line_to(x1,y1+2) + + #revert angles by pi degrees to take the turn back + ang1 = self.angles[x_index][data_index] + math.pi + ang2 = self.angles[x_index-1][data_index] + self.context.curve_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1), + x2+p*math.cos(ang2),y2+p*math.sin(ang2), + x2,y2+2) + + self.context.close_path() + self.context.fill() + + if False: + self.context.move_to(self.borders[HORZ] + 0.5*self.steps[HORZ], middle) + for x_index in range(len(self.data)-1) : + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] + y1 = middle - self.ground(x_index)*self.steps[VERT] - self.data[x_index][data_index]*self.steps[VERT] + x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ] + y2 = middle - self.ground(x_index + 1)*self.steps[VERT] - self.data[x_index + 1][data_index]*self.steps[VERT] + + for i in range(0,data_index): + y1 -= self.data[x_index][i]*self.steps[VERT] + y2 -= self.data[x_index+1][i]*self.steps[VERT] + + ang1 = self.angles[x_index][data_index+1] + ang2 = self.angles[x_index+1][data_index+1] + math.pi + self.context.set_source_rgba(1.0,0.0,0.0) + self.context.arc(x1+p*math.cos(ang1),y1+p*math.sin(ang1),2,0,2*math.pi) + self.context.fill() + self.context.set_source_rgba(0.0,0.0,0.0) + self.context.arc(x2+p*math.cos(ang2),y2+p*math.sin(ang2),2,0,2*math.pi) + self.context.fill() + '''self.context.set_source_rgba(0.0,0.0,0.0,0.3) + self.context.arc(x2,y2,2,0,2*math.pi) + self.context.fill()''' + self.context.move_to(x1,y1) + self.context.line_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1)) + self.context.stroke() + self.context.move_to(x2,y2) + self.context.line_to(x2+p*math.cos(ang2),y2+p*math.sin(ang2)) + self.context.stroke() + if False: + for x_index in range(len(self.data)-1,0,-1) : + x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ] + y1 = middle - self.ground(x_index)*self.steps[VERT] + x2 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ] + y2 = middle - self.ground(x_index - 1)*self.steps[VERT] + + for i in range(0,data_index): + y1 -= self.data[x_index][i]*self.steps[VERT] + y2 -= self.data[x_index-1][i]*self.steps[VERT] + + #revert angles by pi degrees to take the turn back + ang1 = self.angles[x_index][data_index] + math.pi + ang2 = self.angles[x_index-1][data_index] + self.context.set_source_rgba(0.0,1.0,0.0) + self.context.arc(x1+p*math.cos(ang1),y1+p*math.sin(ang1),2,0,2*math.pi) + self.context.fill() + self.context.set_source_rgba(0.0,0.0,1.0) + self.context.arc(x2+p*math.cos(ang2),y2+p*math.sin(ang2),2,0,2*math.pi) + self.context.fill() + '''self.context.set_source_rgba(0.0,0.0,0.0,0.3) + self.context.arc(x2,y2,2,0,2*math.pi) + self.context.fill()''' + self.context.move_to(x1,y1) + self.context.line_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1)) + self.context.stroke() + self.context.move_to(x2,y2) + self.context.line_to(x2+p*math.cos(ang2),y2+p*math.sin(ang2)) + self.context.stroke() + #break + + #self.context.arc(self.dimensions[HORZ]/2, self.dimensions[VERT]/2,50,0,3*math.pi/2) + #self.context.fill() + + +class PiePlot(Plot): + def __init__ (self, + surface = None, + data = None, + width = 640, + height = 480, + background = "white light_gray", + gradient = False, + shadow = False, + colors = None): + + Plot.__init__( self, surface, data, width, height, background, series_colors = colors ) + self.center = (self.dimensions[HORZ]/2, self.dimensions[VERT]/2) + self.total = sum(self.data) + self.radius = min(self.dimensions[HORZ]/3,self.dimensions[VERT]/3) + self.gradient = gradient + self.shadow = shadow + + def load_series(self, data, x_labels=None, y_labels=None, series_colors=None): + Plot.load_series(self, data, x_labels, y_labels, series_colors) + self.data = sorted(self.data) + + def draw_piece(self, angle, next_angle): + self.context.move_to(self.center[0],self.center[1]) + self.context.line_to(self.center[0] + self.radius*math.cos(angle), self.center[1] + self.radius*math.sin(angle)) + self.context.arc(self.center[0], self.center[1], self.radius, angle, next_angle) + self.context.line_to(self.center[0], self.center[1]) + self.context.close_path() + + def render(self): + self.render_background() + self.render_bounding_box() + if self.shadow: + self.render_shadow() + self.render_plot() + self.render_series_labels() + + def render_shadow(self): + horizontal_shift = 3 + vertical_shift = 3 + self.context.set_source_rgba(0, 0, 0, 0.5) + self.context.arc(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.radius, 0, 2*math.pi) + self.context.fill() + + def render_series_labels(self): + angle = 0 + next_angle = 0 + x0,y0 = self.center + cr = self.context + for number,key in enumerate(self.series_labels): + next_angle = angle + 2.0*math.pi*self.data[number]/self.total + cr.set_source_rgba(*self.series_colors[number][:4]) + w = cr.text_extents(key)[2] + if (angle + next_angle)/2 < math.pi/2 or (angle + next_angle)/2 > 3*math.pi/2: + cr.move_to(x0 + (self.radius+10)*math.cos((angle+next_angle)/2), y0 + (self.radius+10)*math.sin((angle+next_angle)/2) ) + else: + cr.move_to(x0 + (self.radius+10)*math.cos((angle+next_angle)/2) - w, y0 + (self.radius+10)*math.sin((angle+next_angle)/2) ) + cr.show_text(key) + angle = next_angle + + def render_plot(self): + angle = 0 + next_angle = 0 + x0,y0 = self.center + cr = self.context + for number,series in enumerate(self.data): + next_angle = angle + 2.0*math.pi*series/self.total + if self.gradient or self.series_colors[number][4] in ('linear','radial'): + gradient_color = cairo.RadialGradient(self.center[0], self.center[1], 0, self.center[0], self.center[1], self.radius) + gradient_color.add_color_stop_rgba(0.3, *self.series_colors[number][:4]) + gradient_color.add_color_stop_rgba(1, self.series_colors[number][0]*0.7, + self.series_colors[number][1]*0.7, + self.series_colors[number][2]*0.7, + self.series_colors[number][3]) + cr.set_source(gradient_color) + else: + cr.set_source_rgba(*self.series_colors[number][:4]) + + self.draw_piece(angle, next_angle) + cr.fill() + + cr.set_source_rgba(1.0, 1.0, 1.0) + self.draw_piece(angle, next_angle) + cr.stroke() + + angle = next_angle + +class DonutPlot(PiePlot): + def __init__ (self, + surface = None, + data = None, + width = 640, + height = 480, + background = "white light_gray", + gradient = False, + shadow = False, + colors = None, + inner_radius=-1): + + Plot.__init__( self, surface, data, width, height, background, series_colors = colors ) + + self.center = ( self.dimensions[HORZ]/2, self.dimensions[VERT]/2 ) + self.total = sum( self.data ) + self.radius = min( self.dimensions[HORZ]/3,self.dimensions[VERT]/3 ) + self.inner_radius = inner_radius*self.radius + + if inner_radius == -1: + self.inner_radius = self.radius/3 + + self.gradient = gradient + self.shadow = shadow + + def draw_piece(self, angle, next_angle): + self.context.move_to(self.center[0] + (self.inner_radius)*math.cos(angle), self.center[1] + (self.inner_radius)*math.sin(angle)) + self.context.line_to(self.center[0] + self.radius*math.cos(angle), self.center[1] + self.radius*math.sin(angle)) + self.context.arc(self.center[0], self.center[1], self.radius, angle, next_angle) + self.context.line_to(self.center[0] + (self.inner_radius)*math.cos(next_angle), self.center[1] + (self.inner_radius)*math.sin(next_angle)) + self.context.arc_negative(self.center[0], self.center[1], self.inner_radius, next_angle, angle) + self.context.close_path() + + def render_shadow(self): + horizontal_shift = 3 + vertical_shift = 3 + self.context.set_source_rgba(0, 0, 0, 0.5) + self.context.arc(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.inner_radius, 0, 2*math.pi) + self.context.arc_negative(self.center[0] + horizontal_shift, self.center[1] + vertical_shift, self.radius, 0, -2*math.pi) + self.context.fill() + +class GanttChart (Plot) : + def __init__(self, + surface = None, + data = None, + width = 640, + height = 480, + x_labels = None, + y_labels = None, + colors = None): + self.bounds = {} + self.max_value = {} + Plot.__init__(self, surface, data, width, height, x_labels = x_labels, y_labels = y_labels, series_colors = colors) + + def load_series(self, data, x_labels=None, y_labels=None, series_colors=None): + Plot.load_series(self, data, x_labels, y_labels, series_colors) + self.calc_boundaries() + + def calc_boundaries(self): + self.bounds[HORZ] = (0,len(self.data)) + for item in self.data: + if hasattr(item, "__delitem__"): + for sub_item in item: + end_pos = max(sub_item) + else: + end_pos = max(item) + self.bounds[VERT] = (0,end_pos) + + def calc_extents(self, direction): + self.max_value[direction] = 0 + if self.labels[direction]: + self.max_value[direction] = max(self.context.text_extents(item)[2] for item in self.labels[direction]) + else: + self.max_value[direction] = self.context.text_extents( str(self.bounds[direction][1] + 1) )[2] + + def calc_horz_extents(self): + self.calc_extents(HORZ) + self.borders[HORZ] = 100 + self.max_value[HORZ] + + def calc_vert_extents(self): + self.calc_extents(VERT) + self.borders[VERT] = self.dimensions[VERT]/(self.bounds[HORZ][1] + 1) + + def calc_steps(self): + self.horizontal_step = (self.dimensions[HORZ] - self.borders[HORZ])/(len(self.labels[VERT])) + self.vertical_step = self.borders[VERT] + + def render(self): + self.calc_horz_extents() + self.calc_vert_extents() + self.calc_steps() + self.render_background() + + self.render_labels() + self.render_grid() + self.render_plot() + + def render_background(self): + cr = self.context + cr.set_source_rgba(255,255,255) + cr.rectangle(0,0,self.dimensions[HORZ], self.dimensions[VERT]) + cr.fill() + for number,item in enumerate(self.data): + linear = cairo.LinearGradient(self.dimensions[HORZ]/2, self.borders[VERT] + number*self.vertical_step, + self.dimensions[HORZ]/2, self.borders[VERT] + (number+1)*self.vertical_step) + linear.add_color_stop_rgba(0,1.0,1.0,1.0,1.0) + linear.add_color_stop_rgba(1.0,0.9,0.9,0.9,1.0) + cr.set_source(linear) + cr.rectangle(0,self.borders[VERT] + number*self.vertical_step,self.dimensions[HORZ],self.vertical_step) + cr.fill() + + def render_grid(self): + cr = self.context + cr.set_source_rgba(0.7, 0.7, 0.7) + cr.set_dash((1,0,0,0,0,0,1)) + cr.set_line_width(0.5) + for number,label in enumerate(self.labels[VERT]): + h = cr.text_extents(label)[3] + cr.move_to(self.borders[HORZ] + number*self.horizontal_step, self.vertical_step/2 + h) + cr.line_to(self.borders[HORZ] + number*self.horizontal_step, self.dimensions[VERT]) + cr.stroke() + + def render_labels(self): + self.context.set_font_size(0.02 * self.dimensions[HORZ]) + + self.render_horz_labels() + self.render_vert_labels() + + def render_horz_labels(self): + cr = self.context + labels = self.labels[HORZ] + if not labels: + labels = [str(i) for i in range(1, self.bounds[HORZ][1] + 1) ] + for number,label in enumerate(labels): + if label != None: + cr.set_source_rgba(0.5, 0.5, 0.5) + w,h = cr.text_extents(label)[2], cr.text_extents(label)[3] + cr.move_to(40,self.borders[VERT] + number*self.vertical_step + self.vertical_step/2 + h/2) + cr.show_text(label) + + def render_vert_labels(self): + cr = self.context + labels = self.labels[VERT] + if not labels: + labels = [str(i) for i in range(1, self.bounds[VERT][1] + 1) ] + for number,label in enumerate(labels): + w,h = cr.text_extents(label)[2], cr.text_extents(label)[3] + cr.move_to(self.borders[HORZ] + number*self.horizontal_step - w/2, self.vertical_step/2) + cr.show_text(label) + + def render_rectangle(self, x0, y0, x1, y1, color): + self.draw_shadow(x0, y0, x1, y1) + self.draw_rectangle(x0, y0, x1, y1, color) + + def draw_rectangular_shadow(self, gradient, x0, y0, w, h): + self.context.set_source(gradient) + self.context.rectangle(x0,y0,w,h) + self.context.fill() + + def draw_circular_shadow(self, x, y, radius, ang_start, ang_end, mult, shadow): + gradient = cairo.RadialGradient(x, y, 0, x, y, 2*radius) + gradient.add_color_stop_rgba(0, 0, 0, 0, shadow) + gradient.add_color_stop_rgba(1, 0, 0, 0, 0) + self.context.set_source(gradient) + self.context.move_to(x,y) + self.context.line_to(x + mult[0]*radius,y + mult[1]*radius) + self.context.arc(x, y, 8, ang_start, ang_end) + self.context.line_to(x,y) + self.context.close_path() + self.context.fill() + + def draw_rectangle(self, x0, y0, x1, y1, color): + cr = self.context + middle = (x0+x1)/2 + linear = cairo.LinearGradient(middle,y0,middle,y1) + linear.add_color_stop_rgba(0,3.5*color[0]/5.0, 3.5*color[1]/5.0, 3.5*color[2]/5.0,1.0) + linear.add_color_stop_rgba(1,*color[:4]) + cr.set_source(linear) + + cr.arc(x0+5, y0+5, 5, 0, 2*math.pi) + cr.arc(x1-5, y0+5, 5, 0, 2*math.pi) + cr.arc(x0+5, y1-5, 5, 0, 2*math.pi) + cr.arc(x1-5, y1-5, 5, 0, 2*math.pi) + cr.rectangle(x0+5,y0,x1-x0-10,y1-y0) + cr.rectangle(x0,y0+5,x1-x0,y1-y0-10) + cr.fill() + + def draw_shadow(self, x0, y0, x1, y1): + shadow = 0.4 + h_mid = (x0+x1)/2 + v_mid = (y0+y1)/2 + h_linear_1 = cairo.LinearGradient(h_mid,y0-4,h_mid,y0+4) + h_linear_2 = cairo.LinearGradient(h_mid,y1-4,h_mid,y1+4) + v_linear_1 = cairo.LinearGradient(x0-4,v_mid,x0+4,v_mid) + v_linear_2 = cairo.LinearGradient(x1-4,v_mid,x1+4,v_mid) + + h_linear_1.add_color_stop_rgba( 0, 0, 0, 0, 0) + h_linear_1.add_color_stop_rgba( 1, 0, 0, 0, shadow) + h_linear_2.add_color_stop_rgba( 0, 0, 0, 0, shadow) + h_linear_2.add_color_stop_rgba( 1, 0, 0, 0, 0) + v_linear_1.add_color_stop_rgba( 0, 0, 0, 0, 0) + v_linear_1.add_color_stop_rgba( 1, 0, 0, 0, shadow) + v_linear_2.add_color_stop_rgba( 0, 0, 0, 0, shadow) + v_linear_2.add_color_stop_rgba( 1, 0, 0, 0, 0) + + self.draw_rectangular_shadow(h_linear_1,x0+4,y0-4,x1-x0-8,8) + self.draw_rectangular_shadow(h_linear_2,x0+4,y1-4,x1-x0-8,8) + self.draw_rectangular_shadow(v_linear_1,x0-4,y0+4,8,y1-y0-8) + self.draw_rectangular_shadow(v_linear_2,x1-4,y0+4,8,y1-y0-8) + + self.draw_circular_shadow(x0+4, y0+4, 4, math.pi, 3*math.pi/2, (-1,0), shadow) + self.draw_circular_shadow(x1-4, y0+4, 4, 3*math.pi/2, 2*math.pi, (0,-1), shadow) + self.draw_circular_shadow(x0+4, y1-4, 4, math.pi/2, math.pi, (0,1), shadow) + self.draw_circular_shadow(x1-4, y1-4, 4, 0, math.pi/2, (1,0), shadow) + + def render_plot(self): + for number,item in enumerate(self.data): + if hasattr(item, "__delitem__") : + for space in item: + self.render_rectangle(self.borders[HORZ] + space[0]*self.horizontal_step, + self.borders[VERT] + number*self.vertical_step + self.vertical_step/4.0, + self.borders[HORZ] + space[1]*self.horizontal_step, + self.borders[VERT] + number*self.vertical_step + 3.0*self.vertical_step/4.0, + self.series_colors[number]) + else: + space = item + self.render_rectangle(self.borders[HORZ] + space[0]*self.horizontal_step, + self.borders[VERT] + number*self.vertical_step + self.vertical_step/4.0, + self.borders[HORZ] + space[1]*self.horizontal_step, + self.borders[VERT] + number*self.vertical_step + 3.0*self.vertical_step/4.0, + self.series_colors[number]) + +# Function definition + +def scatter_plot(name, + data = None, + errorx = None, + errory = None, + width = 640, + height = 480, + background = "white light_gray", + border = 0, + axis = False, + dash = False, + discrete = False, + dots = False, + grid = False, + series_legend = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + z_bounds = None, + x_title = None, + y_title = None, + series_colors = None, + circle_colors = None): + + ''' + - Function to plot scatter data. + + - Parameters + + data - The values to be ploted might be passed in a two basic: + list of points: [(0,0), (0,1), (0,2)] or [(0,0,1), (0,1,4), (0,2,1)] + lists of coordinates: [ [0,0,0] , [0,1,2] ] or [ [0,0,0] , [0,1,2] , [1,4,1] ] + Notice that these kinds of that can be grouped in order to form more complex data + using lists of lists or dictionaries; + series_colors - Define color values for each of the series + circle_colors - Define a lower and an upper bound for the circle colors for variable radius + (3 dimensions) series + ''' + + plot = ScatterPlot( name, data, errorx, errory, width, height, background, border, + axis, dash, discrete, dots, grid, series_legend, x_labels, y_labels, + x_bounds, y_bounds, z_bounds, x_title, y_title, series_colors, circle_colors ) + plot.render() + plot.commit() + +def dot_line_plot(name, + data, + width, + height, + background = "white light_gray", + border = 0, + axis = False, + dash = False, + dots = False, + grid = False, + series_legend = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + x_title = None, + y_title = None, + series_colors = None): + ''' + - Function to plot graphics using dots and lines. + + dot_line_plot (name, data, width, height, background = "white light_gray", border = 0, axis = False, grid = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None) + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; + data - The list, list of lists or dictionary holding the data to be plotted; + width, height - Dimensions of the output image; + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. + If left None, a gray to white gradient will be generated; + border - Distance in pixels of a square border into which the graphics will be drawn; + axis - Whether or not the axis are to be drawn; + dash - Boolean or a list or a dictionary of booleans indicating whether or not the associated series should be drawn in dashed mode; + dots - Whether or not dots should be drawn on each point; + grid - Whether or not the gris is to be drawn; + series_legend - Whether or not the legend is to be drawn; + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; + x_title - Whether or not to plot a title over the x axis. + y_title - Whether or not to plot a title over the y axis. + + - Examples of use + + data = [0, 1, 3, 8, 9, 0, 10, 10, 2, 1] + CairoPlot.dot_line_plot('teste', data, 400, 300) + + data = { "john" : [10, 10, 10, 10, 30], "mary" : [0, 0, 3, 5, 15], "philip" : [13, 32, 11, 25, 2] } + x_labels = ["jan/2008", "feb/2008", "mar/2008", "apr/2008", "may/2008" ] + CairoPlot.dot_line_plot( 'test', data, 400, 300, axis = True, grid = True, + series_legend = True, x_labels = x_labels ) + ''' + plot = DotLinePlot( name, data, width, height, background, border, + axis, dash, dots, grid, series_legend, x_labels, y_labels, + x_bounds, y_bounds, x_title, y_title, series_colors ) + plot.render() + plot.commit() + +def function_plot(name, + data, + width, + height, + background = "white light_gray", + border = 0, + axis = True, + dots = False, + discrete = False, + grid = False, + series_legend = False, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + x_title = None, + y_title = None, + series_colors = None, + step = 1): + + ''' + - Function to plot functions. + + function_plot(name, data, width, height, background = "white light_gray", border = 0, axis = True, grid = False, dots = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None, step = 1, discrete = False) + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; + data - The list, list of lists or dictionary holding the data to be plotted; + width, height - Dimensions of the output image; + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. + If left None, a gray to white gradient will be generated; + border - Distance in pixels of a square border into which the graphics will be drawn; + axis - Whether or not the axis are to be drawn; + grid - Whether or not the gris is to be drawn; + dots - Whether or not dots should be shown at each point; + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; + step - the horizontal distance from one point to the other. The smaller, the smoother the curve will be; + discrete - whether or not the function should be plotted in discrete format. + + - Example of use + + data = lambda x : x**2 + CairoPlot.function_plot('function4', data, 400, 300, grid = True, x_bounds=(-10,10), step = 0.1) + ''' + + plot = FunctionPlot( name, data, width, height, background, border, + axis, discrete, dots, grid, series_legend, x_labels, y_labels, + x_bounds, y_bounds, x_title, y_title, series_colors, step ) + plot.render() + plot.commit() + +def pie_plot( name, data, width, height, background = "white light_gray", gradient = False, shadow = False, colors = None ): + + ''' + - Function to plot pie graphics. + + pie_plot(name, data, width, height, background = "white light_gray", gradient = False, colors = None) + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; + data - The list, list of lists or dictionary holding the data to be plotted; + width, height - Dimensions of the output image; + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. + If left None, a gray to white gradient will be generated; + gradient - Whether or not the pie color will be painted with a gradient; + shadow - Whether or not there will be a shadow behind the pie; + colors - List of slices colors. + + - Example of use + + teste_data = {"john" : 123, "mary" : 489, "philip" : 890 , "suzy" : 235} + CairoPlot.pie_plot("pie_teste", teste_data, 500, 500) + ''' + + plot = PiePlot( name, data, width, height, background, gradient, shadow, colors ) + plot.render() + plot.commit() + +def donut_plot(name, data, width, height, background = "white light_gray", gradient = False, shadow = False, colors = None, inner_radius = -1): + + ''' + - Function to plot donut graphics. + + donut_plot(name, data, width, height, background = "white light_gray", gradient = False, inner_radius = -1) + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; + data - The list, list of lists or dictionary holding the data to be plotted; + width, height - Dimensions of the output image; + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. + If left None, a gray to white gradient will be generated; + shadow - Whether or not there will be a shadow behind the donut; + gradient - Whether or not the donut color will be painted with a gradient; + colors - List of slices colors; + inner_radius - The radius of the donut's inner circle. + + - Example of use + + teste_data = {"john" : 123, "mary" : 489, "philip" : 890 , "suzy" : 235} + CairoPlot.donut_plot("donut_teste", teste_data, 500, 500) + ''' + + plot = DonutPlot(name, data, width, height, background, gradient, shadow, colors, inner_radius) + plot.render() + plot.commit() + +def gantt_chart(name, pieces, width, height, x_labels, y_labels, colors): + + ''' + - Function to generate Gantt Charts. + + gantt_chart(name, pieces, width, height, x_labels, y_labels, colors): + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtim; + pieces - A list defining the spaces to be drawn. The user must pass, for each line, the index of its start and the index of its end. If a line must have two or more spaces, they must be passed inside a list; + width, height - Dimensions of the output image; + x_labels - A list of names for each of the vertical lines; + y_labels - A list of names for each of the horizontal spaces; + colors - List containing the colors expected for each of the horizontal spaces + + - Example of use + + pieces = [ (0.5,5.5) , [(0,4),(6,8)] , (5.5,7) , (7,8)] + x_labels = [ 'teste01', 'teste02', 'teste03', 'teste04'] + y_labels = [ '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009', '0010' ] + colors = [ (1.0, 0.0, 0.0), (1.0, 0.7, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0) ] + CairoPlot.gantt_chart('gantt_teste', pieces, 600, 300, x_labels, y_labels, colors) + ''' + + plot = GanttChart(name, pieces, width, height, x_labels, y_labels, colors) + plot.render() + plot.commit() + +def vertical_bar_plot(name, + data, + width, + height, + background = "white light_gray", + border = 0, + display_values = False, + grid = False, + rounded_corners = False, + stack = False, + three_dimension = False, + series_labels = None, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + colors = None): + #TODO: Fix docstring for vertical_bar_plot + ''' + - Function to generate vertical Bar Plot Charts. + + bar_plot(name, data, width, height, background, border, grid, rounded_corners, three_dimension, + x_labels, y_labels, x_bounds, y_bounds, colors): + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtime; + data - The list, list of lists or dictionary holding the data to be plotted; + width, height - Dimensions of the output image; + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. + If left None, a gray to white gradient will be generated; + border - Distance in pixels of a square border into which the graphics will be drawn; + grid - Whether or not the gris is to be drawn; + rounded_corners - Whether or not the bars should have rounded corners; + three_dimension - Whether or not the bars should be drawn in pseudo 3D; + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; + colors - List containing the colors expected for each of the bars. + + - Example of use + + data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + CairoPlot.vertical_bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False) + ''' + + plot = VerticalBarPlot(name, data, width, height, background, border, + display_values, grid, rounded_corners, stack, three_dimension, + series_labels, x_labels, y_labels, x_bounds, y_bounds, colors) + plot.render() + plot.commit() + +def horizontal_bar_plot(name, + data, + width, + height, + background = "white light_gray", + border = 0, + display_values = False, + grid = False, + rounded_corners = False, + stack = False, + three_dimension = False, + series_labels = None, + x_labels = None, + y_labels = None, + x_bounds = None, + y_bounds = None, + colors = None): + + #TODO: Fix docstring for horizontal_bar_plot + ''' + - Function to generate Horizontal Bar Plot Charts. + + bar_plot(name, data, width, height, background, border, grid, rounded_corners, three_dimension, + x_labels, y_labels, x_bounds, y_bounds, colors): + + - Parameters + + name - Name of the desired output file, no need to input the .svg as it will be added at runtime; + data - The list, list of lists or dictionary holding the data to be plotted; + width, height - Dimensions of the output image; + background - A 3 element tuple representing the rgb color expected for the background or a new cairo linear gradient. + If left None, a gray to white gradient will be generated; + border - Distance in pixels of a square border into which the graphics will be drawn; + grid - Whether or not the gris is to be drawn; + rounded_corners - Whether or not the bars should have rounded corners; + three_dimension - Whether or not the bars should be drawn in pseudo 3D; + x_labels, y_labels - lists of strings containing the horizontal and vertical labels for the axis; + x_bounds, y_bounds - tuples containing the lower and upper value bounds for the data to be plotted; + colors - List containing the colors expected for each of the bars. + + - Example of use + + data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + CairoPlot.bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False) + ''' + + plot = HorizontalBarPlot(name, data, width, height, background, border, + display_values, grid, rounded_corners, stack, three_dimension, + series_labels, x_labels, y_labels, x_bounds, y_bounds, colors) + plot.render() + plot.commit() + +def stream_chart(name, + data, + width, + height, + background = "white light_gray", + border = 0, + grid = False, + series_legend = None, + x_labels = None, + x_bounds = None, + y_bounds = None, + colors = None): + + #TODO: Fix docstring for horizontal_bar_plot + plot = StreamChart(name, data, width, height, background, border, + grid, series_legend, x_labels, x_bounds, y_bounds, colors) + plot.render() + plot.commit() diff --git a/prewikka/templates/Stats.tmpl b/prewikka/templates/Stats.tmpl new file mode 100644 index 0000000..09d6287 --- /dev/null +++ b/prewikka/templates/Stats.tmpl @@ -0,0 +1,167 @@ +#extends prewikka.templates.ClassicLayout + +#block head_extra_content +<script type="text/javascript"> + +#set $fcnt = 0 +#for $chart in $charts + var chart_${fcnt} = null; + #set $fcnt += 1 +#end for + +function changeLinkUnit(unit) { + var str = "&timeline_type=" + unit; + + if ( unit == "custom" ) { + \$("input[type=text]").each(function() { + str += "&" + \$(this).attr("name") + "=" + \$(this).attr("value"); + }); + } + + \$("#topmenu").find("a").each(function() { + \$(this).attr("href", \$(this).attr("href") + str); + }); +} + + +\$(document).ready(function() { + \$("select[name=timeline_type]").change(function() { + if ( \$("select[name=timeline_type] option:selected").attr("value") == "custom" ) + \$("input[type=text]").each(function() { \$(this).removeAttr("disabled") }); + else + \$("input[type=text]").each(function() { \$(this).attr("disabled", "disabled") }); + }); + + changeLinkUnit(\$("select[name=timeline_type] option:selected").attr("value")); +}); + +</script> + +#end block + + +#def gen_std($chart, $map_index) +<fieldset> +<legend>$chart.title</legend> + +<table width="100%"> + <tr> + <td style="vertical-align: top;"> + +#filter CleanOutput + <img style="padding-top: 2px;" src='$chart.chart.getHref()' alt="Chart"/> + </td> + </tr> +</table> +</fieldset> +#end filter +#end def + + + +#block main_content +#filter CleanOutput +#set $fcnt = 0 +#set $map_index = 0; + +<h2>$period</h2> + +#if $current_filter +<h2>Filter: $current_filter</h2> +#end if + +<br/><br/> + +<table style="width: 100%;"> +#end filter + +#for $chart in $charts + <tr><td id="td_$fcnt"> + $gen_std($chart, $map_index) +<br/><br/> + </td></tr> + +#set $fcnt = $fcnt + 1 +#set $map_index += 1 +#end for + +</table> +#end block + + + + +#def layout_start_hook +<form action="?" method="get"> +#for $name, $value in $hidden_parameters + <input type="hidden" name="$name" value="$value"/> +#end for +#end def + + +#def layout_end_hook +</form> +#end def + + +#block menu_extra_content +#filter CleanOutput + +<table id="timeline"> + <tr> + <th>$_("Filter:")</th> + <td colspan="2"> + <select name="filter" class="filter_control_select"> + <option value=""> </option> + #for $fltr in $filters + #if $fltr == $current_filter + #set $selected = "selected=\"selected\"" + #else + #set $selected = "" + #end if + <option value="$fltr" $selected>$fltr</option> + #end for + </select> + </td> + </tr> + <tr> + <th>$_("Time:")</th> + <td colspan="2"> + <select name="timeline_type"> + <option value="hour" $timeline_hour_selected>$_("Hour")</option> + <option value="day" $timeline_day_selected>$_("Day")</option> + <option value="month" $timeline_month_selected>$_("Month")</option> + <option value="custom" $timeline_custom_selected>$_("Custom")</option> + </select> + </td> + </tr> + <tr> + <th>$_("From:")</th> + <td colspan="2"><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="4" name="from_year" value="$from_year"/><b>/</b><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="from_month" value="$from_month"/><b>/</b><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="from_day" value="$from_day"/></td> + </tr> + <tr> + <th></th> + <td colspan="2"><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="from_hour" value="$from_hour"/><b>:</b><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="from_min" value="$from_min"/></td> + </tr> + <tr> + <th>$_("To:")</th> + <td colspan="2"><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="4" name="to_year" value="$to_year"/><b>/</b><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="to_month" value="$to_month"/><b>/</b><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="to_day" value="$to_day"/></td> + </tr> + <tr> + <th></th> + <td colspan="2"><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="to_hour" value="$to_hour"/><b>:</b><input type="text" #if not $timeline_custom_selected# disabled="disabled" #end if# size="2" name="to_min" value="$to_min"/></td> + </tr> + +<tr> + <td colspan="3" style="text-align: center"> + +<br style="line-height: 5px;" /> + +<div> + <input id="form_apply" type="submit" name="apply" value="$_("Apply")" /> <input type="submit" name="_save" value="$_("Save")" /> +</div> + +</table> + +#end filter +#end block diff --git a/prewikka/utils.py b/prewikka/utils.py index 21d384d..820f16b 100644 --- a/prewikka/utils.py +++ b/prewikka/utils.py @@ -195,3 +195,29 @@ def toUnicode(text): pass return unicode(text, "ISO-8859-1") + + + +class OrderedDict(dict): + def __init__(self, *args, **kwargs): + dict.__init__(self, *args, **kwargs) + self._order = dict.keys(self) + + def __setitem__(self, key, value): + dict.__setitem__(self, key, value) + if key in self._order: + self._order.remove(key) + self._order.append(key) + + def __delitem__(self, key): + dict.__delitem__(self, key) + self._order.remove(key) + + def keys(self): + return self._order[:] + + def items(self): + return [(key,self[key]) for key in self._order] + + def values(self): + return [ self[key] for key in self._order] diff --git a/prewikka/views/__init__.py b/prewikka/views/__init__.py index a750a49..2fe4d87 100644 --- a/prewikka/views/__init__.py +++ b/prewikka/views/__init__.py @@ -20,7 +20,7 @@ from prewikka.views import \ messagelisting, alertlisting, heartbeatlisting, messagesummary, messagedetails, sensor, \ - commands, filter, usermanagement, misc + commands, filter, usermanagement, stats, misc objects = alertlisting.AlertListing(), \ alertlisting.CorrelationAlertListing(), \ @@ -36,7 +36,9 @@ objects = alertlisting.AlertListing(), \ usermanagement.UserListing(), \ usermanagement.UserAddForm(), usermanagement.UserDelete(), \ usermanagement.UserSettingsDisplay(), usermanagement.UserSettingsModify(), usermanagement.UserSettingsAdd(), \ - misc.About() + misc.About(), \ + stats.StatsSummary(), stats.CategorizationStats(), stats.SourceStats(), stats.TargetStats(), stats.AnalyzerStats(), \ + stats.TimelineStats() @@ -48,6 +50,14 @@ events_section = (_("Events"), [(_("Alerts"), ["alert_listing", "sensor_alert_li agents_section = (_("Agents"), [(_("Agents"), ["sensor_listing", "sensor_messages_delete", "heartbeat_analyze"]), (_("Heartbeats"), ["heartbeat_listing", "sensor_heartbeat_listing"] )]) +stats_section = (_("Statistics"), [ + (_("Categorizations"), ["stats_categorization" ]), + (_("Sources"), [ "stats_source" ]), + (_("Targets"), [ "stats_target" ]), + (_("Analyzers"), [ "stats_analyzer" ]), + (_("Timeline"), [ "stats_timeline" ])]) + + settings_section = (_("Settings"), [ (_("Filters"), ["filter_edition"]), diff --git a/prewikka/views/stats.py b/prewikka/views/stats.py new file mode 100644 index 0000000..feb038a --- /dev/null +++ b/prewikka/views/stats.py @@ -0,0 +1,850 @@ +# Copyright (C) 2005-2009 PreludeIDS Technologies. All Rights Reserved. +# Author: Nicolas Delon <[email protected]> +# Author: Yoann Vandoorselaere <[email protected]> +# +# This file is part of the Prewikka program. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +import sys +import time +import copy +import urllib +import datetime + +from prewikka import User, view, Chart, utils, resolve + +try: + import GeoIP + geoip = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) +except: + geoip = None + + +DEFAULT_WIDTH = 800 +DEFAULT_HEIGHT = 450 + + +class DistributionStatsParameters(view.Parameters): + def register(self): + self.optional("timeline_type", str, default="hour", save=True) + self.optional("from_year", int, save=True) + self.optional("from_month", int, save=True) + self.optional("from_day", int, save=True) + self.optional("from_hour", int, save=True) + self.optional("from_min", int, save=True) + self.optional("to_year", int, save=True) + self.optional("to_month", int, save=True) + self.optional("to_day", int, save=True) + self.optional("to_hour", int, save=True) + self.optional("to_min", int, save=True) + self.optional("filter", str, save=True) + self.optional("idmef_filter", str) + self.optional("apply", str) + + def normalize(self, view_name, user): + do_save = self.has_key("_save") + + view.Parameters.normalize(self, view_name, user) + + if do_save and not self.has_key("filter"): + user.delConfigValue(view_name, "filter") + + +class StatsSummary(view.View): + view_name = "stats_summary" + view_template = "StatsSummary" + view_permission = [ ] + view_parameters = view.Parameters + + def render(self): + pass + + + + +class DistributionStats(view.View): + view_template = "Stats" + view_permissions = [ User.PERM_IDMEF_VIEW ] + view_parameters = DistributionStatsParameters + + def _getNameFromMap(self, name, names_and_colors): + if names_and_colors.has_key(name): + return names_and_colors[name][0] + + return name + + def _namesAndColors2ColorMap(self, names_and_colors): + d = utils.OrderedDict() + for name, color in names_and_colors.values(): + d[name] = color + + return d + + def _getBaseURL(self): + start = long(time.mktime(self._period_start)) + + if self.parameters["timeline_type"] in ("month", "day", "hour"): + unit = self.parameters["timeline_type"] + value = 1 + else: + delta = long(time.mktime(self._period_end)) - start + if delta > 3600: + unit = "day" + value = delta / (24 * 3600) + 1 + else: + unit = "hour" + value = delta / 3600 + 1 + + filter_str = "" + if self.parameters.has_key("filter"): + filter_str = "&" + urllib.urlencode({"filter": self.parameters["filter"]}) + + return utils.create_link("alert_listing", { "timeline_unit": unit, + "timeline_value": value, + "timeline_start": start }) + filter_str + + + def _addDistributionChart(self, title, value_name, width, height, path, criteria, sub_url_handler, limit=-1, dns=False, names_and_colors={}): + base_url = self._getBaseURL() + chart = { "title": title, "value_name": value_name, "data": [ ] } + + distribution = Chart.DistributionChart(width, height) + if names_and_colors: + distribution.setColorMap(self._namesAndColors2ColorMap(names_and_colors)) + + chart["chart"] = distribution + chart["render"] = (distribution, title, base_url) + + results = self.env.idmef_db.getValues([ path + "/group_by", "count(%s)/order_desc" % path ], + criteria=criteria + [ path ], limit=limit) + if results: + total = reduce(lambda x, y: x + y, [ count for value, count in results ]) + chart["total"] = total + for value, count in results: + if dns: + v = resolve.AddressResolve(value) + else: + v = self._getNameFromMap(value, names_and_colors) + + chart["data"].append((v, base_url + "&" + sub_url_handler(value), count, "%.1f" % (count / float(total) * 100))) + distribution.addLabelValuePair(v, count, base_url + "&" + sub_url_handler(value)) + + distribution.render(title, user = self.user.login) + self.dataset["charts"].append(chart) + + def _processTimeCriteria(self): + now = time.time() + self._period_end = time.localtime(now) + + if self.parameters["timeline_type"] == "hour": + self.dataset["timeline_hour_selected"] = "selected=\"selected\"" + self._period_start = time.localtime(now - 3600) + + elif self.parameters["timeline_type"] == "day": + self.dataset["timeline_day_selected"] = "selected=\"selected\"" + tm = time.localtime(now - 24 * 3600) + self._period_start = time.localtime(now - 24 * 3600) + + elif self.parameters["timeline_type"] == "month": + self.dataset["timeline_month_selected"] = "selected=\"selected\"" + tm = list(time.localtime(now)) + tm[1] -= 1 + self._period_start = time.localtime(time.mktime(tm)) + + else: + self.dataset["timeline_custom_selected"] = "selected=\"selected\"" + self._period_start = time.struct_time((self.parameters["from_year"], self.parameters["from_month"], + self.parameters["from_day"], self.parameters["from_hour"], + self.parameters["from_min"], 0, 0, 0, -1)) + self._period_end = time.struct_time((self.parameters["to_year"], self.parameters["to_month"], + self.parameters["to_day"], self.parameters["to_hour"], + self.parameters["to_min"], 0, 0, 0, -1)) + + self.dataset["from_year"] = "%.4d" % self._period_start.tm_year + self.dataset["from_month"] = "%.2d" % self._period_start.tm_mon + self.dataset["from_day"] = "%.2d" % self._period_start.tm_mday + self.dataset["from_hour"] = "%.2d" % self._period_start.tm_hour + self.dataset["from_min"] = "%.2d" % self._period_start.tm_min + + self.dataset["to_year"] = "%.4d" % self._period_end.tm_year + self.dataset["to_month"] = "%.2d" % self._period_end.tm_mon + self.dataset["to_day"] = "%.2d" % self._period_end.tm_mday + self.dataset["to_hour"] = "%.2d" % self._period_end.tm_hour + self.dataset["to_min"] = "%.2d" % self._period_end.tm_min + + criteria = [ "alert.create_time >= '%d-%d-%d %d:%d:%d' && alert.create_time < '%d-%d-%d %d:%d:%d'" % \ + (self._period_start.tm_year, self._period_start.tm_mon, self._period_start.tm_mday, + self._period_start.tm_hour, self._period_start.tm_min, self._period_start.tm_sec, + self._period_end.tm_year, self._period_end.tm_mon, self._period_end.tm_mday, + self._period_end.tm_hour, self._period_end.tm_min, self._period_end.tm_sec) ] + + return criteria + + def _processFilterCriteria(self): + c = [ ] + if self.parameters.has_key("idmef_filter"): + c.append(unicode(self.parameters["idmef_filter"])) + + self.dataset["current_filter"] = self.parameters.get("filter", "") + if self.parameters.has_key("filter"): + f = self.env.db.getAlertFilter(self.user.login, self.parameters["filter"]) + if f: + c.append(unicode(f)) + + return c + + def _processCriteria(self): + criteria = [ ] + criteria += self._processTimeCriteria() + criteria += self._processFilterCriteria() + + return criteria + + def render(self): + self.dataset["hidden_parameters"] = [ ("view", self.view_name) ] + self.dataset["charts"] = [ ] + self.dataset["filters"] = self.env.db.getAlertFilterNames(self.user.login) + self.dataset["timeline_hour_selected"] = "" + self.dataset["timeline_day_selected"] = "" + self.dataset["timeline_month_selected"] = "" + self.dataset["timeline_custom_selected"] = "" + + def _setPeriod(self): + tm = time.localtime() + + period = "from %s/%s/%s %s:%s to %s/%s/%s %s:%s" % \ + (self.dataset["from_year"], self.dataset["from_month"], self.dataset["from_day"], + self.dataset["from_hour"], self.dataset["from_min"], + self.dataset["to_year"], self.dataset["to_month"], self.dataset["to_day"], + self.dataset["to_hour"], self.dataset["to_min"]) + + if self.parameters["timeline_type"] == "month": + self.dataset["period"] = "Period: current month (%s)" % period + elif self.parameters["timeline_type"] == "day": + self.dataset["period"] = "Period: today (%s)" % period + elif self.parameters["timeline_type"] == "hour": + self.dataset["period"] = "Period: current hour (%s)" % period + else: + self.dataset["period"] = "Period: %s" % period + + + +class GenericTimelineStats(DistributionStats): + def _getAlertCount(self, criteria, link): + d = {} + + results = self.env.idmef_db.getValues(self._getSelection(), criteria) + if not results: + return d + + for name, count in results: + d[self._getNameFromMap(name, self._names_and_colors)] = (count, link) + + return d + + def _newTimeline(self, width, height, stacked=False): + if stacked: + timeline = Chart.StackedTimelineChart(width, height) + else: + timeline = Chart.TimelineChart(width, height) + + if not self.parameters.has_key("idmef_filter"): + timeline.enableMultipleValues(self._namesAndColors2ColorMap(self._names_and_colors)) + + return timeline + + def _getTimeCrit(self, start, step): + tm1 = start #time.localtime(start) + tm2 = start+step #time.localtime(start + step) + + c = [ "alert.create_time >= '%d-%d-%d %d:%d:%d' && alert.create_time < '%d-%d-%d %d:%d:%d'" % \ + (tm1.year, tm1.month, tm1.day, tm1.hour, tm1.minute, tm1.second, + tm2.year, tm2.month, tm2.day, tm2.hour, tm2.minute, tm2.second) ] + + return c + + def _getStep(self, type, absolute=False): + start = None + + if type == "custom": + type = self.getCustomUnit() + start = datetime.datetime(*self._period_start[:6]) + end = datetime.datetime(*self._period_end[:6]) + else: + end = datetime.datetime.today() + + if type == "hour": + if not start: + start = end - datetime.timedelta(minutes=60) + step = datetime.timedelta(minutes=1) + label_tm_index = "%Hh%M" + zoom_view = "alert_listing" + timeline_type = "min" + timeline_unit = "" + + elif type == "day": + if not start: + start = end - datetime.timedelta(hours=24) + step = datetime.timedelta(hours=1) + label_tm_index = "%d/%Hh" + zoom_view = "stats_timeline" + timeline_type = "custom" + timeline_unit = "hour" + + elif type == "month": + if not start: + start = end - datetime.timedelta(days=31) + step = datetime.timedelta(days=1) + label_tm_index = "%m/%d" + zoom_view = "stats_timeline" + timeline_type = "custom" + timeline_unit = "day" + + elif type == "year": + if not start: + start = end - datetime.timedelta(days=365) + step = datetime.timedelta(days=31) + label_tm_index = "%m/%d" + zoom_view = "stats_timeline" + timeline_type = "custom" + timeline_unit = "day" + + return start, end, step, label_tm_index, zoom_view, timeline_type, timeline_unit + + + def _setTimelineZoom(self, base_parameters, start, end): + #tm = time.localtime(start) + base_parameters["from_year"] = start.year + base_parameters["from_month"] = start.month + base_parameters["from_day"] = start.day + base_parameters["from_hour"] = start.hour + base_parameters["from_min"] = start.minute + + #tm = time.localtime(end) + base_parameters["to_year"] = end.year + base_parameters["to_month"] = end.month + base_parameters["to_day"] = end.day + base_parameters["to_hour"] = end.hour + base_parameters["to_min"] = end.minute + + def _generateTimeline(self, width, height): + start, end, step, format, zoom_view, timeline_type, timeline_time = self._getStep(self.parameters["timeline_type"]) + timeline = self._newTimeline(width, height) + + if timeline_type != "custom": + base_parameters = { "timeline_unit": "min" } + else: + base_parameters = { "timeline_type": timeline_type } + + self.dataset["timeline_user_type"] = self.parameters.get("timeline_type") + + while start < end: + c = self._getTimeCrit(start, step) + self._criteria + + if timeline_type != "custom": + base_parameters["timeline_start"] = long(time.mktime(start.timetuple())) #long(start) + else: + self._setTimelineZoom(base_parameters, start, start + step) + + link = utils.create_link(zoom_view, base_parameters) + count = self._getAlertCount(c, link) + label = start.strftime(format) + + start += step + timeline.addLabelValuePair(label, count, link) + + return timeline + + def getCustomUnit(self): + start = long(time.mktime(self._period_start)) + delta = long(time.mktime(self._period_end)) - start + + if delta > 86400: + unit = "month" + elif delta > 3600: + unit = "day" + else: + unit = "hour" + + return unit + + def _getSelection(self): + return ("%s/group_by" % self._path, "count(%s)/order_desc" % self._path) + + def _addTimelineChart(self, title, value_name, width, height, path, criteria, limit=-1, names_and_colors={}, allow_stacked=False, value_callback=None, zoom_type=None): + self._path = path + self._limit = limit + self._value_callback = value_callback + self._criteria = criteria + self._zoom_type = zoom_type + self._names_and_colors = names_and_colors + + base_url = self._getBaseURL() + chart = { "title": title, "value_name": value_name, "data": [ ] } + + if limit > 0: + res = self.env.idmef_db.getValues(self._getSelection(), criteria = criteria, limit=self._limit) + + c = "" + for name, count in res: + if c: + c += " || " + c += "%s = '%s'" % (self._path, utils.escape_criteria(name)) + + if c: + criteria.append(c) + + timeline = self._generateTimeline(width, height) + timeline.render(title, user = self.user.login) + + chart["chart"] = timeline + self.dataset["charts"].append(chart) + self.dataset["zoom"] = self.parameters.get("zoom", None) + + +class CategorizationStats(DistributionStats, GenericTimelineStats): + view_name = "stats_categorization" + + def _renderClassifications(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Classifications"), _("Classification"), width, height, + "alert.classification.text", + criteria, + lambda value: utils.urlencode({"classification_object_0": "alert.classification.text", + "classification_value_0": value}), + 10) + + def _renderReferences(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Alert References"), _("References"), width, height, + "alert.classification.reference.name", + criteria, + lambda value: utils.urlencode({"classification_object_0": "alert.classification.reference.name", + "classification_value_0": value}), + 10) + + def _renderImpactSeverities(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + _severity_maps = utils.OrderedDict() + _severity_maps["high"] = (_("High"), Chart.RED_STD) + _severity_maps["medium"] = (_("Medium"), Chart.ORANGE_STD) + _severity_maps["low"] = (_("Low"), Chart.GREEN_STD) + _severity_maps["info"] = (_("Informational"), Chart.BLUE_STD) + _severity_maps[None] = (_("N/a"), "000000") + + self._addDistributionChart(_("Severities"), _("Severity"), width, height, + "alert.assessment.impact.severity", + criteria, + lambda value: utils.urlencode({"classification_object_0": "alert.assessment.impact.severity", + "classification_value_0": value}), names_and_colors=_severity_maps) + + def _renderImpactTypes(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Alert Impact Types"), _("Impact Types"), width, height, + "alert.assessment.impact.type", + criteria, + lambda value: utils.urlencode({"classification_object_0": "alert.assessment.impact.type", + "classification_value_0": value})) + + def _renderClassificationsTrend(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + GenericTimelineStats._addTimelineChart(self, "Top 10 Classifications Trend", None, width, height, + "alert.classification.text", criteria, limit = 10, zoom_type="classifications_trend") + + def render(self): + DistributionStats.render(self) + + self.dataset["title"] = "Alerts categorization" + + criteria = self._processCriteria() + + self._setPeriod() + + self._renderClassificationsTrend(criteria) + self._renderClassifications(criteria) + self._renderReferences(criteria) + self._renderImpactSeverities(criteria) + self._renderImpactTypes(criteria) + + + +class SourceStats(DistributionStats, GenericTimelineStats): + view_name = "stats_source" + + def _countryDistributionChart(self, criteria, width, height): + + base_url = self._getBaseURL() + distribution = Chart.WorldChart(width, height) + + chart = { "title": _("Top Source Country"), "value_name": _("Country"), "data": [ ], "chart": distribution } + + results = self.env.idmef_db.getValues([ "alert.source.node.address.address/group_by", + "count(alert.source.node.address.address)"], + criteria=criteria, limit=-1) + + if results: + total = reduce(lambda x, y: x + y, [ count for value, count in results ]) + chart["total"] = total + + merge = { } + for value, count in results: + if not value: + continue + + if distribution.needCountryCode(): + nvalue = geoip.country_code_by_addr(value) + else: + nvalue = geoip.country_name_by_addr(value) + if not nvalue: + nvalue = "Unknown" + + if not merge.has_key(nvalue): + url_index = 0 + merge[nvalue] = (0, 0, nvalue, "") + else: + url_index = merge[nvalue][1] + + encode = "&" + utils.urlencode({"source_object_%d" % url_index: "alert.source.node.address.address", + "source_value_%d" % url_index: value}) + merge[nvalue] = (merge[nvalue][0] + count, url_index + 1, nvalue, merge[nvalue][3] + encode) + + s = [ t[1] for t in merge.items() ] + s.sort() + s.reverse() + results = s #[0:10] + + for item in results: + distribution.addLabelValuePair(item[2], item[0]) + chart["data"].append((item[2], base_url + item[3], item[0], "%.1f" % (item[0] / float(total) * 100))) + + distribution.render("Top 10 Source Country", user = self.user.login) + chart["filename"] = distribution.getHref() + chart["type"] = distribution.getType() + self.dataset["charts"].append(chart) + + + def _renderCountry(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + if geoip is not None: + self._countryDistributionChart(criteria, width, height) + + def _renderAddresses(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Source Addresses"), _("Address"), width, height, + "alert.source.node.address.address", + criteria, + lambda value: utils.urlencode({"source_object_0": "alert.source.node.address.address", + "source_value_0": value}), + 10, dns=True) + + def _renderUsers(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Source Users"), _("User"), width, height, + "alert.source.user.user_id.name", + criteria, + lambda value: utils.urlencode({"source_object_0": "alert.source.user.user_id.name", + "source_value_0": value}), + 10) + + def _renderSourcesTrend(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + GenericTimelineStats._addTimelineChart(self, "Top 10 Sources Trend", None, DEFAULT_WIDTH, DEFAULT_HEIGHT, + "alert.source.node.address.address", criteria, 10, zoom_type="sources_trend") + + def render(self): + DistributionStats.render(self) + + self.dataset["title"] = "Top Alert Sources" + + criteria = self._processCriteria() + + self._setPeriod() + + self._renderCountry(criteria) + self._renderSourcesTrend(criteria) + self._renderAddresses(criteria) + self._renderUsers(criteria) + + resolve.process(self.env.dns_max_delay) + +class TargetStats(DistributionStats): + view_name = "stats_target" + + def _renderPorts(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + base_url = self._getBaseURL() + title = "Top 10 Targeted Ports" + distribution = Chart.DistributionChart(width, height) + chart = { "title": title, "value_name": "Port", "data": [ ], "chart": distribution } + + criteria = criteria[:] + [ "(alert.target.service.iana_protocol_number == 6 ||" + "alert.target.service.iana_protocol_number == 17 ||" + "alert.target.service.iana_protocol_name =* 'tcp' ||" + "alert.target.service.iana_protocol_name =* 'udp' ||" + "alert.target.service.protocol =* 'udp' ||" + "alert.target.service.protocol =* 'tcp')" ] + + results = self.env.idmef_db.getValues([ "alert.target.service.port/group_by", + "alert.target.service.iana_protocol_number/group_by", + "alert.target.service.iana_protocol_name/group_by", + "alert.target.service.protocol/group_by", + "count(alert.target.service.port)/order_desc" ], + criteria=criteria, limit=10) + if not results: + return + + merge = { "TCP": { }, "UDP": { } } + + for port, iana_protocol_number, iana_protocol_name, protocol, count in results: + if not port: + continue + + if iana_protocol_number: + protocol = utils.protocol_number_to_name(iana_protocol_number) + + elif iana_protocol_name: + protocol = iana_protocol_name + + protocol = protocol.upper() + if not merge.has_key(protocol): + continue + + if not merge[protocol].has_key(port): + merge[protocol][port] = 0 + + merge[protocol][port] += count + + results = [ ] + + for protocol, values in merge.items(): + for port, count in values.items(): + results.append((port, protocol, count)) + + results.sort(lambda x, y: int(y[2] - x[2])) + + total = reduce(lambda x, y: x + y, [ count for port, protocol, count in results ]) + chart["total"] = total + + for port, protocol, count in results: + name = "%d (%s)" % (port, protocol) + chart["data"].append((name, base_url + "&" + "target_object_0=alert.target.service.port&target_value_0=%d" % port, + count, "%.1f" % (count / float(total) * 100))) + + distribution.addLabelValuePair(name, count, base_url + "&" + "target_object_0=alert.target.service.port&target_value_0=%d" % port) + + distribution.render(title, user = self.user.login) + self.dataset["charts"].append(chart) + + def _renderAddresses(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Targeted Addresses"), _("Address"), width, height, + "alert.target.node.address.address", + criteria, + lambda value: utils.urlencode({"target_object_0": "alert.target.node.address.address", + "target_value_0": value}), + 10, dns=True) + + def _renderUsers(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Targeted Users"), _("User"), width, height, + "alert.target.user.user_id.name", + criteria, + lambda value: utils.urlencode({"target_object_0": "alert.target.user.user_id.name", + "target_value_0": value}), + 10) + + def render(self): + DistributionStats.render(self) + + self.dataset["title"] = "Top Alert Targets" + + criteria = self._processCriteria() + + self._setPeriod() + + self._renderAddresses(criteria) + self._renderPorts(criteria) + self._renderUsers(criteria) + + resolve.process(self.env.dns_max_delay) + + +class AnalyzerStats(DistributionStats, GenericTimelineStats): + view_name = "stats_analyzer" + + def _renderAnalyzers(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + base_url = self._getBaseURL() + title = "Top 10 analyzers" + distribution = Chart.DistributionChart(width, height) + chart = { "title": title, "value_name": "Analyzer", "data": [ ], "chart": distribution } + + results = self.env.idmef_db.getValues([ "alert.analyzer(-1).name/group_by", "alert.analyzer(-1).node.name/group_by", + "count(alert.analyzer(-1).name)/order_desc" ], + criteria=criteria + [ "alert.analyzer(-1).name" ], limit=10) + if results: + total = reduce(lambda x, y: x + y, [ row[-1] for row in results ]) + chart["total"] = total + for analyzer_name, node_name, count in results: + if node_name: + value = "%s on %s" % (analyzer_name, node_name) + else: + value = analyzer_name + + analyzer_criteria = utils.urlencode({ "analyzer_object_0": "alert.analyzer(-1).name", + "analyzer_value_0": analyzer_name }) + + chart["data"].append((value, + base_url + "&" + analyzer_criteria, + count, + "%.1f" % (count / float(total) * 100))) + + distribution.addLabelValuePair(value, count, base_url + "&" + analyzer_criteria) + + distribution.render(title, user = self.user.login) + self.dataset["charts"].append(chart) + + def _renderModels(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Analyzer Models"), _("Model"), width, height, + "alert.analyzer(-1).model", + criteria, + lambda value: utils.urlencode({ "analyzer_object_0": "alert.analyzer(-1).model", + "analyzer_value_0": value }), + 10) + + def _renderClasses(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Analyzer Classes"), _("Class"), width, height, + "alert.analyzer(-1).class", + criteria, + lambda value: utils.urlencode({ "analyzer_object_0": "alert.analyzer(-1).class", + "analyzer_value_0": value }), + 10) + + def _renderNodeAddresses(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Top 10 Analyzer Node Addresses"), _("Address"), width, height, + "alert.analyzer(-1).node.address.address", + criteria, + lambda value: utils.urlencode({ "analyzer_object_0": "alert.analyzer(-1).node.address.address", + "analyzer_value_0": value }), + 10) + + def _renderNodeLocations(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + self._addDistributionChart(_("Analyzer Locations"), _("Location"), width, height, + "alert.analyzer(-1).node.location", + criteria, + lambda value: utils.urlencode({ "analyzer_object_0": "alert.analyzer(-1).node.location", + "analyzer_value_0": value })) + + def _renderClassesTrend(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + GenericTimelineStats._addTimelineChart(self, "Top 10 Analyzer Classes Trend", None, width, height, + "alert.analyzer(-1).class", criteria, limit = 10, zoom_type="analyzer_classes_trend") + + def render(self): + DistributionStats.render(self) + + self.dataset["title"] = "Top Analyzers" + + criteria = self._processCriteria() + + self._setPeriod() + + self._renderClassesTrend(criteria) + self._renderAnalyzers(criteria) + self._renderModels(criteria) + self._renderClasses(criteria) + self._renderNodeAddresses(criteria) + self._renderNodeLocations(criteria) + + + +class RiskStats(GenericTimelineStats): + def _renderRisk(self): + tmap = { "hour": "day", "day": "month", "month": "year", "custom": "custom" } + start, end, step, format, zoom_view, timeline_type, timeline_time = self._getStep(tmap[self.parameters["timeline_type"]]) + + i = 0 + total = 0 + total_score = 0.0 + score_table = { "info": 0.5, "low": 1, "n/a": 1, "medium": 1.5, "high": 2 } + + while start < end: + c = self._getTimeCrit(start, step) + res = self.env.idmef_db.getValues([ "alert.assessment.impact.severity/group_by", "count(alert.create_time)/order_desc" ], criteria=c) + + gscore = 0.0 + for severity, count in res: + total += count + score = score_table[severity or "n/a"] * count + gscore += score + + start += step + total_score += gscore + + if gscore or total > 0: + i += 1 + + avg = share = slice = 0 + if total_score: + avg = float(total_score) / i + share = 100 / float(avg * 2) + slice = (avg * 2) / 3 + + gauge = Chart.FlashVerticalGaugeChart() + gauge.addLabelValuePair("Low", 0, Chart.GREEN_STD) + gauge.addLabelValuePair("Moderate", 50, Chart.YELLOW_STD) + gauge.addLabelValuePair("High", 100, Chart.RED_STD) + gauge.setPointer(min(gscore * share, 100), 0) + + gauge.render("Risk Evaluation", user = self.user.login) + self.dataset["charts"].append({"chart": gauge}) + + +class TimelineStats(GenericTimelineStats, AnalyzerStats, CategorizationStats, SourceStats): + view_name = "stats_timeline" + + def _renderTimelineChart(self, criteria, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): + _severity_maps = utils.OrderedDict() + _severity_maps["high"] = (_("High"), Chart.RED_STD) + _severity_maps["medium"] = (_("Medium"), Chart.ORANGE_STD) + _severity_maps["low"] = (_("Low"), Chart.GREEN_STD) + _severity_maps["info"] = (_("Informational"), Chart.BLUE_STD) + _severity_maps[None] = (_("N/a"), "000000") + + GenericTimelineStats._addTimelineChart(self, "Timeline", None, width, height, + "alert.assessment.impact.severity", criteria, names_and_colors=_severity_maps) + + def render(self): + DistributionStats.render(self) + self.dataset["title"] = "Timeline" + + criteria = self._processCriteria() + self._setPeriod() + + type = self.parameters.get("type", None) + if type == "analyzer_classes_trend": + AnalyzerStats._renderClassesTrend(self, criteria) + + elif type == "classifications_trend": + CategorizationStats._renderClassificationsTrend(self, criteria) + + elif type == "sources_trend": + SourceStats._renderSourcesTrend(self, criteria) + else: + self._renderTimelineChart(criteria) + + + + +class AnalyzerTrendStats(GenericTimelineStats, AnalyzerStats): + view_name = "stats_analyzer_trend" + + def render(self): + DistributionStats.render(self) + self.dataset["title"] = "Timeline" + + criteria = self._processCriteria() + self._setPeriod() + + title = "Top 10 Analyzer Trend " + self.dataset["period"] + AnalyzerStats._renderClassesTrend(self, criteria, width, height) + diff --git a/setup.py b/setup.py index cf351b5..686ce23 100644 --- a/setup.py +++ b/setup.py @@ -189,6 +189,7 @@ class my_install(install): self.install_conf() self.init_siteconfig() install.run(self) + self.mkpath(self.prefix + "/share/prewikka/htdocs/generated_images") for dir in ("/", "share/prewikka", _______________________________________________ Prelude-cvslog site list [email protected] http://lists.prelude-ids.org/mailman/listinfo/prelude-cvslog