1 | G = glab = {
|
2 | dgMap:{},
|
3 | }
|
4 |
|
5 | G.new2D=function() {
|
6 | return {
|
7 | data: {
|
8 | xs: {},
|
9 | columns: [],
|
10 | type: "line",
|
11 | types : {}
|
12 | },
|
13 | axis: {
|
14 | x: {
|
15 | label: 'X',
|
16 | tick: { fit: false, format:d3.format(".2f") }
|
17 | },
|
18 | y: { label: 'Y',
|
19 | tick : { format: d3.format(".2f") }
|
20 | }
|
21 | },
|
22 | bar: { width: { ratio: 0.9 } },
|
23 | }
|
24 | }
|
25 |
|
26 | G.show = function(chartName, dg) {
|
27 | var g = dg.graph;
|
28 | if (dg.dim === '2D') {
|
29 | g.bindto=chartName;
|
30 | return c3.generate(g);
|
31 | } else {
|
32 | var box=document.getElementById(chartName.replace('#',''));
|
33 | new vis.Graph3d(box, g.dataSet, g.options);
|
34 | }
|
35 | }
|
36 |
|
37 | G.chart2D = function(chartName, f) {
|
38 | var g = G.new2D();
|
39 | f(g);
|
40 | var dg = {dim:'2D', graph:g};
|
41 | G.dgMap[chartName] = dg;
|
42 | return G.show(chartName, dg);
|
43 | }
|
44 |
|
45 |
|
46 | G.draw = function(g, name, x, y, type) {
|
47 | g.data.types[name] = type;
|
48 | g.data.xs[name] = name+"x";
|
49 | g.data.columns.push([name+"x"].concat(x));
|
50 | g.data.columns.push([name].concat(y));
|
51 | }
|
52 |
|
53 | G.curve = function(g, name, f, from=-10, to=10, step=0.1) {
|
54 | var rg = R.G.curve(f, from, to, step);
|
55 | G.draw(g, name, rg.x, rg.y, 'line');
|
56 | }
|
57 |
|
58 | G.hist = function(g, name, x, type, from, to, step=1) {
|
59 | var rh = R.G.hist(x, from, to, step);
|
60 | G.draw(g, name, rh.xc, rh.bins, type || 'bar');
|
61 | }
|
62 |
|
63 | G.ihist=function(g, name, x, type) {
|
64 | G.hist(g, name, x, type, x.min()-0.5, x.max()+0.5, 1);
|
65 | }
|
66 |
|
67 | G.plot =(g, name, x, y)=>G.draw(g, name, x, y, 'scatter');
|
68 |
|
69 | G.pie =function(g, countMap) {
|
70 | g.data.type = 'pie';
|
71 | for (var name in countMap) {
|
72 | var count = countMap[name];
|
73 | g.data.columns.push([name, count]);
|
74 | }
|
75 | }
|
76 |
|
77 | G.timeSeries =function(g, columns) {
|
78 | g.data.x = 'x';
|
79 | g.axis.x = { type:'timeseries', tick:{format:'%Y-%m-%d'}};
|
80 | g.data.columns = columns;
|
81 | }
|
82 |
|
83 | G.new3D = function() {
|
84 | return {
|
85 | dataSet:new vis.DataSet(),
|
86 | options:{
|
87 | width: '95%',
|
88 | height: '95%',
|
89 | style: 'surface',
|
90 | showPerspective: true,
|
91 | showGrid: true,
|
92 | showShadow: false,
|
93 | keepAspectRatio: true,
|
94 | verticalRatio: 0.5
|
95 | }
|
96 | }
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | G.chart3D = function(chartName, style, f) {
|
127 | var g = G.new3D();
|
128 |
|
129 | var counter = 0;
|
130 | var steps = 50;
|
131 | var axisMax = 314;
|
132 | var axisStep = axisMax / steps;
|
133 | for (var x = 0; x < axisMax; x+=axisStep) {
|
134 | for (var y = 0; y < axisMax; y+=axisStep) {
|
135 | var value = f(x,y);
|
136 | g.dataSet.add({id:counter++,x:x,y:y,z:value,style:value});
|
137 | }
|
138 | }
|
139 | g.options.style=style;
|
140 | G.dgMap[chartName] = {dim:'3D',graph:g};
|
141 | var box=document.getElementById(chartName.replace('#',''));
|
142 | new vis.Graph3d(box, g.dataSet, g.options);
|
143 | }
|