Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/pieChart.py: 29%

21 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-03 06:25 +0000

1#!/usr/bin/python 

2# -*- coding: utf-8 -*- 

3 

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. 

8 

9Project location : https://github.com/areski/python-nvd3 

10""" 

11 

12from .NVD3Chart import NVD3Chart, TemplateMixin 

13 

14 

15class pieChart(TemplateMixin, NVD3Chart): 

16 

17 """ 

18 A pie chart (or a circle graph) is a circular chart divided into sectors, 

19 illustrating numerical proportion. In chart, the arc length of each sector 

20 is proportional to the quantity it represents. 

21 

22 Python example:: 

23 

24 from nvd3 import pieChart 

25 chart = pieChart(name='pieChart', color_category='category20c', 

26 height=400, width=400) 

27 

28 xdata = ["Orange", "Banana", "Pear", "Kiwi", "Apple", "Strawbery", 

29 "Pineapple"] 

30 ydata = [3, 4, 0, 1, 5, 7, 3] 

31 

32 extra_serie = {"tooltip": {"y_start": "", "y_end": " cal"}} 

33 chart.add_serie(y=ydata, x=xdata, extra=extra_serie) 

34 chart.buildhtml() 

35 

36 Javascript generated: 

37 

38 .. raw:: html 

39 

40 <div id="pieChart"><svg style="height:450px; width:100%"></svg></div> 

41 <script> 

42 

43 

44 data_pieChart=[{"values": [{"value": 3, "label": "Orange"}, 

45 {"value": 4, "label": "Banana"}, 

46 {"value": 0, "label": "Pear"}, 

47 {"value": 1, "label": "Kiwi"}, 

48 {"value": 5, "label": "Apple"}, 

49 {"value": 7, "label": "Strawberry"}, 

50 {"value": 3, "label": "Pineapple"}], 

51 "key": "Serie 1"}]; 

52 

53 nv.addGraph(function() { 

54 var chart = nv.models.pieChart(); 

55 chart.margin({top: 30, right: 60, bottom: 20, left: 60}); 

56 var datum = data_pieChart[0].values; 

57 chart.tooltipContent(function(key, y, e, graph) { 

58 var x = String(key); 

59 var y = String(y) + ' cal'; 

60 

61 tooltip_str = '<center><b>'+x+'</b></center>' + y; 

62 return tooltip_str; 

63 }); 

64 chart.showLegend(true); 

65 chart.showLabels(true); 

66 chart.donut(false); 

67 chart 

68 .x(function(d) { return d.label }) 

69 .y(function(d) { return d.value }); 

70 chart.width(400); 

71 chart.height(400); 

72 

73 d3.select('#pieChart svg') 

74 .datum(datum) 

75 .transition().duration(500) 

76 .attr('width', 400) 

77 .attr('height', 400) 

78 .call(chart); }); 

79 </script> 

80 

81 """ 

82 CHART_FILENAME = "./piechart.html" 

83 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME) 

84 

85 def __init__(self, **kwargs): 

86 super(pieChart, self).__init__(**kwargs) 

87 

88 height = kwargs.get('height', 450) 

89 width = kwargs.get('width', None) 

90 self.donut = kwargs.get('donut', False) 

91 self.donutRatio = kwargs.get('donutRatio', 0.35) 

92 self.color_list = [] 

93 self.create_x_axis('xAxis', format=None) 

94 self.create_y_axis('yAxis', format=None) 

95 # must have a specified height, otherwise it superimposes both chars 

96 if height: 

97 self.set_graph_height(height) 

98 if width: 

99 self.set_graph_width(width) 

100 self.donut = kwargs.get('donut', False) 

101 self.donutRatio = kwargs.get('donutRatio', 0.35) 

102 self.callback = kwargs.get('callback', None)