Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/scatterChart.py: 40%
15 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 scatterChart(TemplateMixin, NVD3Chart):
17 """
18 A scatter plot or scattergraph is a type of mathematical diagram using Cartesian
19 coordinates to display values for two variables for a set of data.
20 The data is displayed as a collection of points, each having the value of one variable
21 determining the position on the horizontal axis and the value of the other variable
22 determining the position on the vertical axis.
24 Python example::
26 from nvd3 import scatterChart
27 chart = scatterChart(name='scatterChart', height=400, width=400)
28 xdata = [3, 4, 0, -3, 5, 7]
29 ydata = [-1, 2, 3, 3, 15, 2]
30 ydata2 = [1, -2, 4, 7, -5, 3]
32 kwargs1 = {'shape': 'circle', 'size': '1'}
33 kwargs2 = {'shape': 'cross', 'size': '10'}
35 extra_serie = {"tooltip": {"y_start": "", "y_end": " call"}}
36 chart.add_serie(name="series 1", y=ydata, x=xdata, extra=extra_serie, **kwargs1)
38 extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
39 chart.add_serie(name="series 2", y=ydata2, x=xdata, extra=extra_serie, **kwargs2)
40 chart.buildhtml()
42 Javascript generated:
44 .. raw:: html
46 <div id="scatterChart"><svg style="height:450px; width:100%"></svg></div>
47 <script>
49 data_scatterChart=[{"values": [{"y": -1, "x": 3, "shape": "circle",
50 "size": "1"}, {"y": 2, "x": 4, "shape": "circle", "size": "1"},
51 {"y": 3, "x": 0, "shape": "circle", "size": "1"},
52 {"y": 3, "x": -3, "shape": "circle", "size": "1"},
53 {"y": 15, "x": 5, "shape": "circle", "size": "1"},
54 {"y": 2, "x": 7, "shape": "circle", "size": "1"}],
55 "key": "series 1", "yAxis": "1"},
56 {"values": [{"y": 1, "x": 3, "shape": "cross", "size": "10"},
57 {"y": -2, "x": 4, "shape": "cross", "size": "10"},
58 {"y": 4, "x": 0, "shape": "cross", "size": "10"},
59 {"y": 7, "x": -3, "shape": "cross", "size": "10"},
60 {"y": -5, "x": 5, "shape": "cross", "size": "10"},
61 {"y": 3, "x": 7, "shape": "cross", "size": "10"}],
62 "key": "series 2", "yAxis": "1"}];
63 nv.addGraph(function() {
64 var chart = nv.models.scatterChart();
66 chart.margin({top: 30, right: 60, bottom: 20, left: 60});
68 var datum = data_scatterChart;
70 chart.xAxis
71 .tickFormat(d3.format(',.02f'));
72 chart.yAxis
73 .tickFormat(d3.format(',.02f'));
75 chart.tooltipContent(function(key, y, e, graph) {
76 var x = String(graph.point.x);
77 var y = String(graph.point.y);
78 if(key == 'series 1'){
79 var y = String(graph.point.y) + ' call';
80 }
81 if(key == 'series 2'){
82 var y = String(graph.point.y) + ' min';
83 }
85 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
86 return tooltip_str;
87 });
89 chart.showLegend(true);
91 chart
92 .showDistX(true)
93 .showDistY(true)
94 .color(d3.scale.category10().range());
96 d3.select('#scatterChart svg')
97 .datum(datum)
98 .transition().duration(500)
99 .attr('width', 400)
100 .attr('height', 400)
101 .call(chart);
102 });
103 </script>
105 """
107 CHART_FILENAME = "./scatterchart.html"
108 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
110 def __init__(self, **kwargs):
111 super(scatterChart, self).__init__(**kwargs)
112 self.model = 'scatterChart'
113 height = kwargs.get('height', 450)
114 width = kwargs.get('width', None)
115 self.create_x_axis('xAxis', format=kwargs.get('x_axis_format', '.02f'),
116 label=kwargs.get('x_axis_label', None))
117 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.02f'),
118 label=kwargs.get('y_axis_label', None))
119 self.set_graph_height(height)
120 if width:
121 self.set_graph_width(width)