Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/lineChart.py: 95%
22 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 lineChart(TemplateMixin, NVD3Chart):
17 """
18 A line chart or line graph is a type of chart which displays information
19 as a series of data points connected by straight line segments.
21 Python example::
23 from nvd3 import lineChart
24 chart = lineChart(name="lineChart", x_is_date=False, x_axis_format="AM_PM")
26 xdata = range(24)
27 ydata = [0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 4, 3, 3, 5, 7, 5, 3, 16, 6, 9, 15, 4, 12]
28 ydata2 = [9, 8, 11, 8, 3, 7, 10, 8, 6, 6, 9, 6, 5, 4, 3, 10, 0, 6, 3, 1, 0, 0, 0, 1]
30 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}}
31 chart.add_serie(y=ydata, x=xdata, name='sine', extra=extra_serie, **kwargs1)
32 extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
33 chart.add_serie(y=ydata2, x=xdata, name='cose', extra=extra_serie, **kwargs2)
34 chart.buildhtml()
36 Javascript rendered to:
38 .. raw:: html
40 <div id="lineChart"><svg style="height:450px; width:100%"></svg></div>
41 <script>
43 data_lineChart=[{"values": [{"y": 0, "x": 0}, {"y": 0, "x": 1}, {"y": 1, "x": 2}, {"y": 1, "x": 3}, {"y": 0, "x": 4}, {"y": 0, "x": 5}, {"y": 0, "x": 6}, {"y": 0, "x": 7}, {"y": 1, "x": 8}, {"y": 0, "x": 9}, {"y": 0, "x": 10}, {"y": 4, "x": 11}, {"y": 3, "x": 12}, {"y": 3, "x": 13}, {"y": 5, "x": 14}, {"y": 7, "x": 15}, {"y": 5, "x": 16}, {"y": 3, "x": 17}, {"y": 16, "x": 18}, {"y": 6, "x": 19}, {"y": 9, "x": 20}, {"y": 15, "x": 21}, {"y": 4, "x": 22}, {"y": 12, "x": 23}], "key": "sine", "yAxis": "1"}, {"values": [{"y": 9, "x": 0}, {"y": 8, "x": 1}, {"y": 11, "x": 2}, {"y": 8, "x": 3}, {"y": 3, "x": 4}, {"y": 7, "x": 5}, {"y": 10, "x": 6}, {"y": 8, "x": 7}, {"y": 6, "x": 8}, {"y": 6, "x": 9}, {"y": 9, "x": 10}, {"y": 6, "x": 11}, {"y": 5, "x": 12}, {"y": 4, "x": 13}, {"y": 3, "x": 14}, {"y": 10, "x": 15}, {"y": 0, "x": 16}, {"y": 6, "x": 17}, {"y": 3, "x": 18}, {"y": 1, "x": 19}, {"y": 0, "x": 20}, {"y": 0, "x": 21}, {"y": 0, "x": 22}, {"y": 1, "x": 23}], "key": "cose", "yAxis": "1"}];
45 nv.addGraph(function() {
46 var chart = nv.models.lineChart();
47 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
48 var datum = data_lineChart;
49 chart.xAxis
50 .tickFormat(function(d) { return get_am_pm(parseInt(d)); });
51 chart.yAxis
52 .tickFormat(d3.format(',.02f'));
54 chart.tooltipContent(function(key, y, e, graph) {
55 var x = String(graph.point.x);
56 var y = String(graph.point.y);
57 if(key == 'sine'){
58 var y = 'There is ' + String(graph.point.y) + ' calls';
59 }
60 if(key == 'cose'){
61 var y = String(graph.point.y) + ' min';
62 }
64 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
65 return tooltip_str;
66 });
67 chart.showLegend(true);
68 function get_am_pm(d){
69 if (d > 12) {
70 d = d - 12; return (String(d) + 'PM');
71 }
72 else {
73 return (String(d) + 'AM');
74 }
75 };
77 d3.select('#lineChart svg')
78 .datum(datum)
79 .transition().duration(500)
80 .attr('height', 200)
81 .call(chart);
82 return chart;
83 });
84 </script>
86 See the source code of this page, to see the underlying javascript.
87 """
88 CHART_FILENAME = "./linechart.html"
89 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
91 def __init__(self, **kwargs):
92 super(lineChart, self).__init__(**kwargs)
93 self.model = 'lineChart'
95 height = kwargs.get('height', 450)
96 width = kwargs.get('width', None)
98 if kwargs.get('x_is_date', False):
99 self.set_date_flag(True)
100 self.create_x_axis('xAxis',
101 format=kwargs.get('x_axis_format', '%d %b %Y'),
102 date=True)
103 self.set_custom_tooltip_flag(True)
104 else:
105 if kwargs.get('x_axis_format') == 'AM_PM':
106 self.x_axis_format = format = 'AM_PM'
107 else:
108 format = kwargs.get('x_axis_format', 'r')
109 self.create_x_axis('xAxis', format=format,
110 custom_format=kwargs.get('x_custom_format',
111 False))
112 self.create_y_axis(
113 'yAxis',
114 format=kwargs.get('y_axis_format', '.02f'),
115 custom_format=kwargs.get('y_custom_format', False))
117 # must have a specified height, otherwise it superimposes both chars
118 self.set_graph_height(height)
119 if width:
120 self.set_graph_width(width)