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 discreteBarChart(TemplateMixin, NVD3Chart):
16 """
17 A discrete bar chart or bar graph is a chart with rectangular bars with
18 lengths proportional to the values that they represent.
19
20
21 Python example::
22
23 from nvd3 import discreteBarChart
24 chart = discreteBarChart(name='discreteBarChart', height=400, width=400)
25
26 xdata = ["A", "B", "C", "D", "E", "F"]
27 ydata = [3, 4, 0, -3, 5, 7]
28
29 chart.add_serie(y=ydata, x=xdata)
30 chart.buildhtml()
31 print(chart.content)
32
33 Javascript generated:
34
35 .. include:: ./examples/discreteBarChart.html
36
37
38
39 You can also disable the tooltips by passing ``tooltips=False`` when
40 creating the bar chart.
41
42 Python example::
43
44 chart = discreteBarChart(name='discreteBarChart-notooltip', height=400, width=400,
45 tooltips=False)
46
47 .. raw:: html
48
49 <div id="discreteBarChart-notooltip"><svg style="height:450px; width:100%"></svg></div>
50 <script>
51 data_discreteBarChart=[{"values": [{"y": 3, "x": "A"}, {"y": 4, "x": "B"}, {"y": 0, "x": "C"}, {"y": -3, "x": "D"}, {"y": 5, "x": "E"}, {"y": 7, "x": "F"}], "key": "Serie 1", "yAxis": "1"}];
52
53 nv.addGraph(function() {
54 var chart = nv.models.discreteBarChart();
55
56 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
57 chart.tooltips(false);
58 var datum = data_discreteBarChart;
59 chart.yAxis
60 .tickFormat(d3.format(',.0f'));
61 chart.tooltipContent(function(key, y, e, graph) {
62 var x = String(graph.point.x);
63 var y = String(graph.point.y);
64 var y = String(graph.point.y);
65
66 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
67 return tooltip_str;
68 });
69
70 d3.select('#discreteBarChart-notooltip svg')
71 .datum(datum)
72 .transition().duration(500)
73 .attr('width', 400)
74 .attr('height', 400)
75 .call(chart);
76 });
77 </script>
78
79 """
80 CHART_FILENAME = "./discretebarchart.html"
81 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
82
83 def __init__(self, **kwargs):
84 super(discreteBarChart, self).__init__(**kwargs)
85 self.model = 'discreteBarChart'
86 height = kwargs.get('height', 450)
87 width = kwargs.get('width', None)
88
89 if kwargs.get('x_is_date', False):
90 self.set_date_flag(True)
91 self.create_x_axis('xAxis',
92 format=kwargs.get('x_axis_format',
93 "%d %b %Y %H %S"),
94 date=True)
95 else:
96 self.create_x_axis('xAxis', format=None)
97
98 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))
99
100 self.set_custom_tooltip_flag(True)
101
102 self.set_graph_height(height)
103 if width:
104 self.set_graph_width(width)
105
106 tooltips = kwargs.get('tooltips', True)
107
108 if not tooltips:
109 self.chart_attr = {'tooltips': 'false'}
110