1 |
|
2 | G = {};
|
3 |
|
4 |
|
5 |
|
6 | var C3G=function() {
|
7 | this.g = {
|
8 | data: {
|
9 |
|
10 | xs: {},
|
11 | columns: [ ],
|
12 | type: "line",
|
13 | types : {}
|
14 | },
|
15 | axis: {
|
16 | x: {
|
17 | label: 'X',
|
18 | tick: { fit: false, format:d3.format(".2f") }
|
19 | },
|
20 | y: { label: 'Y',
|
21 | tick : { format: d3.format(".2f") }
|
22 | }
|
23 | },
|
24 | bar: { width: { ratio: 0.9 } },
|
25 | };
|
26 | this.varcount = 0;
|
27 | this.xrange(-10, 10);
|
28 | this.step = 1;
|
29 | this.setrange = false;
|
30 | }
|
31 |
|
32 | C3G.prototype.tempvar = function() { return "T"+this.varcount++; }
|
33 |
|
34 | C3G.prototype.xrange = function(xmin, xmax) {
|
35 | this.xmin = xmin;
|
36 | this.xmax = xmax;
|
37 | if (arguments.length == 0)
|
38 | this.setrange = false;
|
39 | else
|
40 | this.setrange = true;
|
41 | }
|
42 |
|
43 | C3G.prototype.plot = function(x,y, options) {
|
44 | var name = options.name || this.tempvar();
|
45 | this.g.data.types[name] = "scatter";
|
46 | this.g.data.xs[name] = name+"x";
|
47 | this.g.data.columns.push([name+"x"].concat(x));
|
48 | this.g.data.columns.push([name].concat(y));
|
49 | }
|
50 |
|
51 | C3G.prototype.curve = function(f, options) {
|
52 | var name = options.name|| this.tempvar();
|
53 | var step = options.step || this.step;
|
54 | var from = options.from || this.xmin;
|
55 | var to = options.to || this.xmax;
|
56 | this.g.data.types[name] = "line";
|
57 | this.g.data.xs[name] = name+"x";
|
58 | var x = R.steps(from, to, step), y=[];
|
59 | var y = R.map(x, f);
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | this.g.data.columns.push([name+"x"].concat(x));
|
72 | this.g.data.columns.push([name].concat(y));
|
73 | }
|
74 |
|
75 | C3G.prototype.hist = function(x, options) {
|
76 | var name = options.name || this.tempvar();
|
77 | var mode = options.mode || "";
|
78 | var step = options.step || this.step;
|
79 | var from = options.from || this.xmin;
|
80 | var to = options.to || this.xmax;
|
81 | this.g.data.types[name] = "bar";
|
82 | this.g.data.xs[name] = name+"x";
|
83 | var xc = R.steps(from+step/2.0, to, step);
|
84 | var n = (to-from)/step + 1;
|
85 | var count = R.fill(Array(n), 0);
|
86 | for (var i in x) {
|
87 | var slot=Math.floor((x[i]-from)/step);
|
88 | if (slot>=0 && slot < n)
|
89 | count[slot]++;
|
90 | }
|
91 |
|
92 | this.g.data.columns.push([name+"x"].concat(xc));
|
93 | var total = R.sum(count);
|
94 | if (mode === "normalized")
|
95 | count = count.map(function(c) { return (1.0/step)*(c/total); });
|
96 |
|
97 | this.g.data.columns.push([name].concat(count));
|
98 | }
|
99 |
|
100 | C3G.prototype.show = function() {
|
101 | if (typeof(module)==="undefined")
|
102 | return c3.generate(this.g);
|
103 | }
|
104 |
|
105 |
|
106 | var VISG=function() {
|
107 |
|
108 | this.options = {
|
109 | width: '95%',
|
110 | height: '95%',
|
111 | style: 'surface',
|
112 | showPerspective: true,
|
113 | showGrid: true,
|
114 | showShadow: false,
|
115 | keepAspectRatio: true,
|
116 | verticalRatio: 0.5
|
117 | };
|
118 | this.data = null;
|
119 | this.graph= null;
|
120 | }
|
121 |
|
122 | VISG.prototype.curve3d = function(f, box) {
|
123 |
|
124 | var data = new vis.DataSet();
|
125 |
|
126 | var counter = 0;
|
127 | var steps = 50;
|
128 | var axisMax = 314;
|
129 | var axisStep = axisMax / steps;
|
130 | for (var x = 0; x < axisMax; x+=axisStep) {
|
131 | for (var y = 0; y < axisMax; y+=axisStep) {
|
132 | var value = f(x,y);
|
133 | data.add({id:counter++,x:x,y:y,z:value,style:value});
|
134 | }
|
135 | }
|
136 | this.graph = new vis.Graph3d(box, data, this.options);
|
137 | }
|
138 |
|
139 |
|
140 | G.newGraph = function(box) {
|
141 | G.c3g = new C3G();
|
142 | G.visg = new VISG();
|
143 | G.box = box;
|
144 |
|
145 | }
|
146 |
|
147 |
|
148 | G.xrange = function(xmin, xmax) { G.c3g.xrange(xmin, xmax); }
|
149 |
|
150 | G.curve = function(f, options) {
|
151 | G.c3g.curve(f, options);
|
152 | G.c3g.show();
|
153 | }
|
154 |
|
155 | G.hist = function(x, options) {
|
156 | G.c3g.hist(x, options);
|
157 |
|
158 | G.c3g.show();
|
159 | }
|
160 |
|
161 | G.plot = function(x,y,options) {
|
162 | G.c3g.plot(x,y,options);
|
163 | G.c3g.show();
|
164 | }
|
165 |
|
166 |
|
167 | G.curve3d = function(f) {
|
168 | G.visg.curve3d(f, G.box);
|
169 | }
|