forked from ukosuagwu/python-nvd3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscreteBarChart.py
More file actions
138 lines (101 loc) · 4.84 KB
/
Copy pathdiscreteBarChart.py
File metadata and controls
138 lines (101 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Python-nvd3 is a Python wrapper for NVD3 graph library.
NVD3 is an attempt to build re-usable charts and chart components
for d3.js without taking away the power that d3.js gives you.
Project location : https://github.com/areski/python-nvd3
"""
from .NVD3Chart import NVD3Chart, TemplateMixin
class discreteBarChart(TemplateMixin, NVD3Chart):
"""
A discrete bar chart or bar graph is a chart with rectangular bars with
lengths proportional to the values that they represent.
Python example::
from nvd3 import discreteBarChart
chart = discreteBarChart(name='discreteBarChart', height=400, width=400)
xdata = ["A", "B", "C", "D", "E", "F"]
ydata = [3, 4, 0, -3, 5, 7]
chart.add_serie(y=ydata, x=xdata)
chart.buildhtml()
Javascript generated:
.. raw:: html
<div id="discreteBarChart"><svg style="height:450px; width:100%"></svg></div>
<script>
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"}];
nv.addGraph(function() {
var chart = nv.models.discreteBarChart();
chart.margin({top: 30, right: 60, bottom: 20, left: 60});
var datum = data_discreteBarChart;
chart.yAxis
.tickFormat(d3.format(',.0f'));
chart.tooltipContent(function(key, y, e, graph) {
var x = String(graph.point.x);
var y = String(graph.point.y);
var y = String(graph.point.y);
tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
return tooltip_str;
});
d3.select('#discreteBarChart svg')
.datum(datum)
.transition().duration(500)
.attr('width', 400)
.attr('height', 400)
.call(chart);
});
</script>
You can also disable the tooltips by passing ``tooltips=False`` when
creating the bar chart.
Python example::
chart = discreteBarChart(name='discreteBarChart-notooltip', height=400, width=400,
tooltips=False)
.. raw:: html
<div id="discreteBarChart-notooltip"><svg style="height:450px; width:100%"></svg></div>
<script>
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"}];
nv.addGraph(function() {
var chart = nv.models.discreteBarChart();
chart.margin({top: 30, right: 60, bottom: 20, left: 60});
chart.tooltips(false);
var datum = data_discreteBarChart;
chart.yAxis
.tickFormat(d3.format(',.0f'));
chart.tooltipContent(function(key, y, e, graph) {
var x = String(graph.point.x);
var y = String(graph.point.y);
var y = String(graph.point.y);
tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
return tooltip_str;
});
d3.select('#discreteBarChart-notooltip svg')
.datum(datum)
.transition().duration(500)
.attr('width', 400)
.attr('height', 400)
.call(chart);
});
</script>
"""
CHART_FILENAME = "./discretebarchart.html"
template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
def __init__(self, **kwargs):
super(discreteBarChart, self).__init__(**kwargs)
self.model = 'discreteBarChart'
height = kwargs.get('height', 450)
width = kwargs.get('width', None)
if kwargs.get('x_is_date', False):
self.set_date_flag(True)
self.create_x_axis('xAxis',
format=kwargs.get('x_axis_format',
"%d %b %Y %H %S"),
date=True)
else:
self.create_x_axis('xAxis', format=None)
self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))
self.set_custom_tooltip_flag(True)
self.set_graph_height(height)
if width:
self.set_graph_width(width)
tooltips = kwargs.get('tooltips', True)
if not tooltips:
self.chart_attr = {'tooltips': 'false'}