Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/multiChart.py: 26%
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 multiChart(TemplateMixin, NVD3Chart):
17 """
18 A multiChart is a type of chart which combines several plots of the same or different types.
20 Python example::
22 from nvd3 import multiChart
23 type = "multiChart"
24 chart = multiChart(name=type, x_is_date=False, x_axis_format="AM_PM")
26 xdata = [1,2,3,4,5,6]
27 ydata = [115.5,160.5,108,145.5,84,70.5]
28 ydata2 = [48624,42944,43439,24194,38440,31651]
30 kwargs1 = {'color': 'black'}
31 kwargs2 = {'color': 'red'}
32 extra_serie = {"tooltip": {"y_start": "There is ", "y_end": " calls"}}
33 chart.add_serie(y=ydata, x=xdata, type='line', yaxis=1, name='visits', extra=extra_serie, **kwargs1)
34 extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
35 chart.add_serie(y=ydata2, x=xdata, type='bar', yaxis=2,name='spend', extra=extra_serie, **kwargs2)
36 chart.buildhtml()
39 Javascript rendered to:
41 .. raw:: html
43 <div id="multichart"><svg style="height:450px;"></svg></div>
44 <script>
46 data_multichart=[{"color": "black", "type": "line", "values": [{"y": 115.5, "x": 1}, {"y": 160.5, "x": 2}, {"y": 108, "x": 3}, {"y
47": 145.5, "x": 4}, {"y": 84, "x": 5}, {"y": 70.5, "x": 6}], "key": "visits", "yAxis": 1}, {"color": "red", "type": "bar", "values": [{"y": 486
4824, "x": 1}, {"y": 42944, "x": 2}, {"y": 43439, "x": 3}, {"y": 24194, "x": 4}, {"y": 38440, "x": 5}, {"y": 31651, "x": 6}], "key": "spend", "y
49Axis": 2}];
51 nv.addGraph(function() {
52 var chart = nv.models.multiChart();
54 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
56 var datum = data_multichart;
58 chart.yAxis1
59 .tickFormat(d3.format(',.02f'));
60 chart.yAxis2
61 .tickFormat(d3.format(',.02f'));
62 chart.xAxis
63 .tickFormat(function(d) { return get_am_pm(parseInt(d)); });
65 function get_am_pm(d){
66 if (d > 12) {
67 d = d - 12; return (String(d) + 'PM');
68 }
69 else {
70 return (String(d) + 'AM');
71 }
72 };
74 chart.showLegend(true);
76 d3.select('#multichart svg')
77 .datum(datum)
78 .transition().duration(500)
79 .attr('height', 450)
80 .call(chart);
81 });
82 </script>
84 See the source code of this page, to see the underlying javascript.
85 """
86 CHART_FILENAME = "./multichart.html"
87 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
89 def __init__(self, **kwargs):
90 super(multiChart, self).__init__(**kwargs)
91 self.model = 'multiChart'
93 height = kwargs.get('height', 450)
94 width = kwargs.get('width', None)
96 if kwargs.get('x_is_date', False):
97 self.set_date_flag(True)
98 self.create_x_axis('xAxis',
99 format=kwargs.get('x_axis_format', '%d %b %Y'),
100 date=True)
101 self.set_custom_tooltip_flag(True)
102 else:
103 if kwargs.get('x_axis_format') == 'AM_PM':
104 self.x_axis_format = format = 'AM_PM'
105 else:
106 format = kwargs.get('x_axis_format', 'r')
107 self.create_x_axis('xAxis', format=format,
108 custom_format=kwargs.get('x_custom_format',
109 False))
110 self.create_y_axis(
111 'yAxis1',
112 format=kwargs.get('y1_axis_format', '.02f'),
113 custom_format=kwargs.get('y1_custom_format', False))
115 self.create_y_axis(
116 'yAxis2',
117 format=kwargs.get('y2_axis_format', '.02f'),
118 custom_format=kwargs.get('y2_custom_format', False))
120 # must have a specified height, otherwise it superimposes both chars
121 self.set_graph_height(height)
122 if width:
123 self.set_graph_width(width)