Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/stackedAreaChart.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 stackedAreaChart(TemplateMixin, NVD3Chart):
16 """
17 The stacked area chart is identical to the area chart, except the areas are stacked
18 on top of each other, rather than overlapping. This can make the chart much easier to read.
20 Python example::
22 from nvd3 import stackedAreaChart
23 chart = stackedAreaChart(name='stackedAreaChart', height=400, width=400)
25 xdata = [100, 101, 102, 103, 104, 105, 106,]
26 ydata = [6, 11, 12, 7, 11, 10, 11]
27 ydata2 = [8, 20, 16, 12, 20, 28, 28]
29 extra_serie = {"tooltip": {"y_start": "There is ", "y_end": " min"}}
30 chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie)
31 chart.add_serie(name="Serie 2", y=ydata2, x=xdata, extra=extra_serie)
32 chart.buildhtml()
34 Javascript generated:
36 .. raw:: html
38 <div id="stackedAreaChart"><svg style="height:450px; width:100%"></svg></div>
39 <script>
42 data_stackedAreaChart=[{"values": [{"y": 6, "x": 100}, {"y": 11, "x": 101}, {"y": 12, "x": 102}, {"y": 7, "x": 103}, {"y": 11, "x": 104}, {"y": 10, "x": 105}, {"y": 11, "x": 106}], "key": "Serie 1", "yAxis": "1"}, {"values": [{"y": 8, "x": 100}, {"y": 20, "x": 101}, {"y": 16, "x": 102}, {"y": 12, "x": 103}, {"y": 20, "x": 104}, {"y": 28, "x": 105}, {"y": 28, "x": 106}], "key": "Serie 2", "yAxis": "1"}];
43 nv.addGraph(function() {
44 var chart = nv.models.stackedAreaChart();
45 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
46 var datum = data_stackedAreaChart;
47 chart.xAxis
48 .tickFormat(d3.format(',.2f'));
49 chart.yAxis
50 .tickFormat(d3.format(',.2f'));
52 chart.tooltipContent(function(key, y, e, graph) {
53 var x = String(graph.point.x);
54 var y = String(graph.point.y);
55 if(key == 'Serie 1'){
56 var y = 'There is ' + String(graph.point.y) + ' min';
57 }
58 if(key == 'Serie 2'){
59 var y = 'There is ' + String(graph.point.y) + ' min';
60 }
62 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
63 return tooltip_str;
64 });
65 chart.showLegend(true);
66 d3.select('#stackedAreaChart svg')
67 .datum(datum)
68 .transition().duration(500)
69 .attr('width', 400)
70 .attr('height', 400)
71 .call(chart);
72 });
73 </script>
75 """
77 CHART_FILENAME = "./stackedareachart.html"
78 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
80 def __init__(self, **kwargs):
81 super(stackedAreaChart, self).__init__(**kwargs)
82 height = kwargs.get('height', 450)
83 width = kwargs.get('width', None)
84 self.model = 'stackedAreaChart'
86 if kwargs.get('x_is_date', False):
87 self.set_date_flag(True)
88 self.create_x_axis('xAxis',
89 format=kwargs.get('x_axis_format', '%d %b %Y'),
90 date=True)
91 self.set_custom_tooltip_flag(True)
92 else:
93 self.create_x_axis('xAxis', format=kwargs.get('x_axis_format',
94 '.2f'))
95 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.2f'))
97 self.set_graph_height(height)
98 if width:
99 self.set_graph_width(width)