1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4"""
5Python-nvd3 is a Python wrapper for NVD3 graph library.
6NVD3 is an attempt to build re-usable charts and chart components
7for d3.js without taking away the power that d3.js gives you.
8
9Project location : https://github.com/areski/python-nvd3
10"""
11
12from .NVD3Chart import NVD3Chart, TemplateMixin
13
14
15class linePlusBarChart(TemplateMixin, NVD3Chart):
16
17 """
18 A linePlusBarChart Chart is a type of chart which displays information
19 as a series of data points connected by straight line segments
20 and with some series with rectangular bars with lengths proportional
21 to the values that they represent.
22
23 Python example::
24
25 from nvd3 import linePlusBarChart
26 chart = linePlusBarChart(name="linePlusBarChart",
27 width=500, height=400, x_axis_format="%d %b %Y",
28 x_is_date=True, focus_enable=True,
29 yaxis2_format="function(d) { return d3.format(',0.3f')(d) }")
30
31 xdata = [1338501600000, 1345501600000, 1353501600000]
32 ydata = [6, 5, 1]
33 y2data = [0.002, 0.003, 0.004]
34
35 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"},
36 "date_format": "%d %b %Y %H:%S" }
37 chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie,
38 bar=True)
39
40 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " min"}}
41 chart.add_serie(name="Serie 2", y=y2data, x=xdata, extra=extra_serie)
42 chart.buildhtml()
43 print(chart.content)
44
45 Note that in case you have two data serie with extreme different numbers,
46 that you would like to format in different ways,
47 you can pass a keyword *yaxis1_format* or *yaxis2_format* when
48 creating the graph.
49
50 In the example above the graph created presents the values of the second
51 data series with three digits right of the decimal point.
52
53 Javascript generated:
54
55 .. include:: ./examples/linePlusBarChart.html
56
57 """
58 CHART_FILENAME = "./lineplusbarchart.html"
59 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
60
61 def __init__(self, **kwargs):
62 super(linePlusBarChart, self).__init__(**kwargs)
63 self.model = 'linePlusBarChart'
64
65 height = kwargs.get('height', 450)
66 width = kwargs.get('width', None)
67 self.yaxis1_format = kwargs.get('yaxis1_format',
68 "function(d) { return d3.format(',f')(d) }")
69 self.yaxis2_format = kwargs.get('yaxis2_format',
70 "function(d) { return d3.format(',f')(d) }")
71
72 if kwargs.get('x_is_date', False):
73 self.set_date_flag(True)
74 self.create_x_axis('xAxis',
75 format=kwargs.get('x_axis_format',
76 '%d %b %Y %H %S'),
77 date=True)
78 self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format',
79 '%d %b %Y %H %S'),
80 date=True)
81 self.set_custom_tooltip_flag(True)
82 else:
83 self.create_x_axis('xAxis', format=kwargs.get('x_axis_format',
84 '.2f'))
85 self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format',
86 '.2f'))
87
88 self.create_y_axis('y1Axis', format=self.yaxis1_format,
89 custom_format=True)
90 self.create_y_axis('y2Axis', format=self.yaxis2_format,
91 custom_format=True)
92
93 self.set_graph_height(height)
94 if width:
95 self.set_graph_width(width)