blob: 6063421d862a2f21a7d034004c255b0d167cb451 [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright 2011 The ChromiumOS Authors. All rights reserved.
# 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))
def AddColumn(self, name, column_type):
self.columns.append((column_type, name))
def AddRow(self, 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(
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