Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/bulletChart.py: 30%
23 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 bulletChart(TemplateMixin, NVD3Chart):
16 '''
17 A bullet chart is a variation of a bar graph
18 used to indicate the value of a single variable
19 in relation to a set of qualitative ranges. It is
20 inspired by a dashboard gauge or thermometer chart.
22 Python example:
24 from nvd3.bulletChart import bulletChart
25 chart = bulletChart.bulletChart(name=type, height=100, width=500)
26 title = 'Revenue',
27 subtitle = 'US$, in thousands'
28 ranges = [150, 225, 300]
29 measures = [220, 270]
30 markers = [250]
31 chart.add_serie(
32 title=title,
33 subtitle=subtitle,
34 ranges=ranges,
35 measures=measures,
36 markers=markers)
37 chart.buildhtml()
39 JavaScript generated:
41 .. raw:: html
43 <!DOCTYPE html>
44 <html lang="en">
45 <head>
46 <meta charset="utf-8" />
47 <link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css" rel="stylesheet" />
48 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
49 <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js"></script>
50 </head>
51 <body>
53 <div id="bulletchart"><svg style="width:500px;height:100px;"></svg></div>
56 <script>
60 data_bulletchart=[{"key": "Serie 1", "values": [{"title": ["Revenue"], "subtitle": "USD, in mill", "ranges": [100, 250, 300], "measures": [220, 280]}]}];
63 function transformedData(){
64 return data_bulletchart[0]['values'][0]
65 }
69 nv.addGraph(function() {
70 var chart = nv.models.bulletChart();
72 d3.select('svg')
73 .datum(transformedData())
74 .transition().duration(1000)
75 .call(chart)
76 ;
78 return chart;
79 });
81 </script>
83 </body>
84 </html>
86 '''
87 CHART_FILENAME = './bulletchart.html'
88 template_chart_nvd3 = NVD3Chart.template_environment.get_template(
89 CHART_FILENAME)
91 def __init__(self, **kwargs):
92 super(bulletChart, self).__init__(**kwargs)
93 self.model = 'bulletChart'
95 height = kwargs.get('height', None)
96 width = kwargs.get('width', 200)
98 if height:
99 self.set_graph_height(height)
100 if width:
101 self.set_graph_width(width)
103 def add_serie(self, ranges, measures, title, subtitle, markers=None,
104 name=None, **kwargs):
105 if not name:
106 name = "Serie %d" % (self.serie_no)
107 if markers:
108 serie = [{
109 'title': title,
110 'subtitle': subtitle,
111 'ranges': ranges,
112 'measures': measures,
113 'markers': markers
114 }]
115 else:
116 serie = [{
117 'title': title,
118 'subtitle': subtitle,
119 'ranges': ranges,
120 'measures': measures,
121 }]
122 data_keyvalue = {'values': serie, 'key': name}
124 self.serie_no += 1
125 self.series.append(data_keyvalue)