Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/cumulativeLineChart.py: 32%
19 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 cumulativeLineChart(TemplateMixin, NVD3Chart):
16 """
17 A cumulative line chart is used when you have one important grouping representing
18 an ordered set of data and one value to show, summed over time.
20 Python example::
22 from nvd3 import cumulativeLineChart
23 chart = cumulativeLineChart(name='cumulativeLineChart', x_is_date=True)
24 xdata = [1365026400000, 1365026500000, 1365026600000]
25 ydata = [6, 5, 1]
26 y2data = [36, 55, 11]
28 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}}
29 chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie)
31 extra_serie = {"tooltip": {"y_start": "", "y_end": " mins"}}
32 chart.add_serie(name="Serie 2", y=y2data, x=xdata, extra=extra_serie)
33 chart.buildhtml()
35 Javascript generated:
37 .. raw:: html
39 <div id="cumulativeLineChart"><svg style="height:450px; width:100%"></svg></div>
40 <script>
41 data_cumulativeLineChart=[{"values": [{"y": 6, "x": 1365026400000},
42 {"y": 5, "x": 1365026500000},
43 {"y": 1, "x": 1365026600000}],
44 "key": "Serie 1", "yAxis": "1"},
45 {"values": [{"y": 36, "x": 1365026400000},
46 {"y": 55, "x": 1365026500000},
47 {"y": 11, "x": 1365026600000}], "key": "Serie 2", "yAxis": "1"}];
48 nv.addGraph(function() {
49 var chart = nv.models.cumulativeLineChart();
50 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
51 var datum = data_cumulativeLineChart;
53 chart.xAxis
54 .tickFormat(function(d) { return d3.time.format('%d %b %Y')(new Date(parseInt(d))) });
55 chart.yAxis
56 .tickFormat(d3.format(',.1%'));
58 chart.tooltipContent(function(key, y, e, graph) {
59 var x = d3.time.format("%d %b %Y")(new Date(parseInt(graph.point.x)));
60 var y = String(graph.point.y);
61 if(key == 'Serie 1'){
62 var y = 'There are ' + String(e) + ' calls';
63 }if(key == 'Serie 2'){
64 var y = String(e) + ' mins';
65 }
66 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' on ' + x;
67 return tooltip_str;
68 });
69 chart.showLegend(true);
71 d3.select('#cumulativeLineChart svg')
72 .datum(datum)
73 .transition().duration(500)
74 .attr('height', 450)
75 .call(chart); });
76 </script>
78 """
80 CHART_FILENAME = "./cumulativelinechart.html"
81 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
83 def __init__(self, **kwargs):
84 super(cumulativeLineChart, self).__init__(**kwargs)
85 self.model = 'cumulativeLineChart'
87 height = kwargs.get('height', 450)
88 width = kwargs.get('width', None)
90 if kwargs.get('x_is_date', False):
91 self.set_date_flag(True)
92 self.create_x_axis('xAxis',
93 format=kwargs.get('x_axis_format', '%d %b %Y'),
94 date=True)
95 self.set_custom_tooltip_flag(True)
96 else:
97 self.create_x_axis('xAxis', format=kwargs.get(
98 'x_axis_format', '.2f'))
100 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.1%'))
102 self.set_graph_height(height)
103 if width:
104 self.set_graph_width(width)