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 = x.map(f);
|
60 | this.g.data.columns.push([name+"x"].concat(x));
|
61 | this.g.data.columns.push([name].concat(y));
|
62 | }
|
63 |
|
64 | C3G.prototype.hist = function(x, options) {
|
65 | var name = options.name || this.tempvar();
|
66 | var mode = options.mode || "";
|
67 | var step = options.step || this.step;
|
68 | var from = options.from || this.xmin;
|
69 | var to = options.to || this.xmax;
|
70 | this.g.data.types[name] = "bar";
|
71 | this.g.data.xs[name] = name+"x";
|
72 | var count = R.hist(x, step, from, to);
|
73 | var xc = R.steps(from+step/2.0, to, step);
|
74 | this.g.data.columns.push([name+"x"].concat(xc));
|
75 | var total = R.sum(count);
|
76 | if (mode === "normalized")
|
77 | count = count.map(function(c) { return (1.0/step)*(c/total); });
|
78 | this.g.data.columns.push([name].concat(count));
|
79 | }
|
80 |
|
81 | C3G.prototype.show = function(chartName) {
|
82 | if (typeof(module)==="undefined") {
|
83 | this.g.bindto=chartName;
|
84 | return c3.generate(this.g);
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 | var VISG=function() {
|
90 |
|
91 | this.options = {
|
92 | width: '95%',
|
93 | height: '95%',
|
94 | style: 'surface',
|
95 | showPerspective: true,
|
96 | showGrid: true,
|
97 | showShadow: false,
|
98 | keepAspectRatio: true,
|
99 | verticalRatio: 0.5
|
100 | };
|
101 | this.data = null;
|
102 | this.graph= null;
|
103 | }
|
104 |
|
105 | VISG.prototype.curve3d = function(f, box) {
|
106 |
|
107 | var data = new vis.DataSet();
|
108 |
|
109 | var counter = 0;
|
110 | var steps = 50;
|
111 | var axisMax = 314;
|
112 | var axisStep = axisMax / steps;
|
113 | for (var x = 0; x < axisMax; x+=axisStep) {
|
114 | for (var y = 0; y < axisMax; y+=axisStep) {
|
115 | var value = f(x,y);
|
116 | data.add({id:counter++,x:x,y:y,z:value,style:value});
|
117 | }
|
118 | }
|
119 | this.graph = new vis.Graph3d(box, data, this.options);
|
120 | }
|
121 |
|
122 |
|
123 | G.newGraph = function(box) {
|
124 | G.c3g = new C3G();
|
125 | G.visg = new VISG();
|
126 | G.box = box;
|
127 |
|
128 | }
|
129 |
|
130 |
|
131 | G.xrange = function(xmin, xmax) { G.c3g.xrange(xmin, xmax); }
|
132 |
|
133 | G.curve = function(f, options) {
|
134 | G.c3g.curve(f, options);
|
135 |
|
136 | }
|
137 |
|
138 | G.hist = function(x, options) {
|
139 | G.c3g.hist(x, options);
|
140 |
|
141 | G.c3g.show();
|
142 | }
|
143 |
|
144 | G.plot = function(x,y,options) {
|
145 | G.c3g.plot(x,y,options);
|
146 | G.c3g.show();
|
147 | }
|
148 |
|
149 |
|
150 | G.curve3d = function(f) {
|
151 | G.visg.curve3d(f, G.box);
|
152 | }
|