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.4.3, created at 2024-03-03 06:16 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-03 06:16 +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()
31 print(chart.content)
33 Javascript generated:
35 .. include:: ./examples/discreteBarChart.html
39 You can also disable the tooltips by passing ``tooltips=False`` when
40 creating the bar chart.
42 Python example::
44 chart = discreteBarChart(name='discreteBarChart-notooltip', height=400, width=400,
45 tooltips=False)
47 .. raw:: html
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"}];
53 nv.addGraph(function() {
54 var chart = nv.models.discreteBarChart();
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);
66 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
67 return tooltip_str;
68 });
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>
79 """
80 CHART_FILENAME = "./discretebarchart.html"
81 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
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)
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)
98 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))
100 self.set_custom_tooltip_flag(True)
102 self.set_graph_height(height)
103 if width:
104 self.set_graph_width(width)
106 tooltips = kwargs.get('tooltips', True)
108 if not tooltips:
109 self.chart_attr = {'tooltips': 'false'}