aboutsummaryrefslogtreecommitdiff
path: root/crosperf/column_chart.py
blob: 6ed99bf0bceb6036aa9bea8cb0e5aa8143356fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
# Copyright 2011 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Module to draw column chart."""


class ColumnChart(object):
    """class to draw column chart."""

    def __init__(self, title, width, height):
        self.title = title
        self.chart_div = "".join(t for t in title if t.isalnum())
        self.width = width
        self.height = height
        self.columns = []
        self.rows = []
        self.series = []

    def AddSeries(self, column_name, series_type, color):
        for i in range(len(self.columns)):
            if column_name == self.columns[i][1]:
                self.series.append((i - 1, series_type, color))
                break

    def AddColumn(self, name, column_type):
        self.columns.append((column_type, name))

    def AddRow(self, row):
        self.rows.append(row)

    def GetJavascript(self):
        res = "var data = new google.visualization.DataTable();\n"
        for column in self.columns:
            res += "data.addColumn('%s', '%s');\n" % column
        res += "data.addRows(%s);\n" % len(self.rows)
        for row in range(len(self.rows)):
            for column in range(len(self.columns)):
                val = self.rows[row][column]
                if isinstance(val, str):
                    val = "'%s'" % val
                res += "data.setValue(%s, %s, %s);\n" % (row, column, val)

        series_javascript = ""
        for series in self.series:
            series_javascript += "%s: {type: '%s', color: '%s'}, " % series

        chart_add_javascript = """
var chart_%s = new google.visualization.ComboChart(
  document.getElementById('%s'));
chart_%s.draw(data, {width: %s, height: %s, title: '%s', legend: 'none',
  seriesType: "bars", lineWidth: 0, pointSize: 5, series: {%s},
  vAxis: {minValue: 0}})
"""

        res += chart_add_javascript % (
            self.chart_div,
            self.chart_div,
            self.chart_div,
            self.width,
            self.height,
            self.title,
            series_javascript,
        )
        return res

    def GetDiv(self):
        return "<div id='%s' class='chart'></div>" % self.chart_div