Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/discreteBarChart.py: 27%
22 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-03 06:25 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-03 06:25 +0000
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
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.
9Project location : https://github.com/areski/python-nvd3
10"""
12from .NVD3Chart import NVD3Chart, TemplateMixin
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.
21 Python example::
23 from nvd3 import discreteBarChart
24 chart = discreteBarChart(name='discreteBarChart', height=400, width=400)
26 xdata = ["A", "B", "C", "D", "E", "F"]
27 ydata = [3, 4, 0, -3, 5, 7]
29 chart.add_serie(y=ydata, x=xdata)
30 chart.buildhtml()
32 Javascript generated:
34 .. raw:: html
36 <div id="discreteBarChart"><svg style="height:450px; width:100%"></svg></div>
37 <script>
38 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"}];
40 nv.addGraph(function() {
41 var chart = nv.models.discreteBarChart();
43 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
45 var datum = data_discreteBarChart;
46 chart.yAxis
47 .tickFormat(d3.format(',.0f'));
48 chart.tooltipContent(function(key, y, e, graph) {
49 var x = String(graph.point.x);
50 var y = String(graph.point.y);
51 var y = String(graph.point.y);
53 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
54 return tooltip_str;
55 });
57 d3.select('#discreteBarChart svg')
58 .datum(datum)
59 .transition().duration(500)
60 .attr('width', 400)
61 .attr('height', 400)
62 .call(chart);
63 });
64 </script>
67 You can also disable the tooltips by passing ``tooltips=False`` when
68 creating the bar chart.
70 Python example::
72 chart = discreteBarChart(name='discreteBarChart-notooltip', height=400, width=400,
73 tooltips=False)
75 .. raw:: html
77 <div id="discreteBarChart-notooltip"><svg style="height:450px; width:100%"></svg></div>
78 <script>
79 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"}];
81 nv.addGraph(function() {
82 var chart = nv.models.discreteBarChart();
84 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
85 chart.tooltips(false);
86 var datum = data_discreteBarChart;
87 chart.yAxis
88 .tickFormat(d3.format(',.0f'));
89 chart.tooltipContent(function(key, y, e, graph) {
90 var x = String(graph.point.x);
91 var y = String(graph.point.y);
92 var y = String(graph.point.y);
94 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
95 return tooltip_str;
96 });
98 d3.select('#discreteBarChart-notooltip svg')
99 .datum(datum)
100 .transition().duration(500)
101 .attr('width', 400)
102 .attr('height', 400)
103 .call(chart);
104 });
105 </script>
107 """
108 CHART_FILENAME = "./discretebarchart.html"
109 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
111 def __init__(self, **kwargs):
112 super(discreteBarChart, self).__init__(**kwargs)
113 self.model = 'discreteBarChart'
114 height = kwargs.get('height', 450)
115 width = kwargs.get('width', None)
117 if kwargs.get('x_is_date', False):
118 self.set_date_flag(True)
119 self.create_x_axis('xAxis',
120 format=kwargs.get('x_axis_format',
121 "%d %b %Y %H %S"),
122 date=True)
123 else:
124 self.create_x_axis('xAxis', format=None)
126 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))
128 self.set_custom_tooltip_flag(True)
130 self.set_graph_height(height)
131 if width:
132 self.set_graph_width(width)
134 tooltips = kwargs.get('tooltips', True)
136 if not tooltips:
137 self.chart_attr = {'tooltips': 'false'}