UNPKG

4.65 kBJavaScriptView Raw
1// 考慮: https://plot.ly/javascript/3d-surface-plots/ 互動性好,有 3D
2G = {};
3
4// ------------------ 繪圖物件與函數 C3.js 部份 --------------------------------
5// 注意: C3 的繪圖對像好像是放在 $$.config 裏,預設可能是 chart
6var C3G=function() {
7 this.g = {
8 data: {
9/* x: "x", */
10 xs: {},
11 columns: [ /*["x", 1, 2, 3, 4 ]*/ ],
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
32C3G.prototype.tempvar = function() { return "T"+this.varcount++; }
33
34C3G.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
43C3G.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
51C3G.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 console.log("x=", x, "y=", y);
62 console.log("f=", f, "curve:x=", x);
63 for (var i in x) {
64 if (typeof(f)==="string")
65 y.push(eval(f.replace("x", x[i])));
66 else if (typeof f === "function")
67 y.push(f(x[i]));
68 }
69 console.log("y=", y);
70*/
71 this.g.data.columns.push([name+"x"].concat(x));
72 this.g.data.columns.push([name].concat(y));
73}
74
75C3G.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// console.log("count=", count);
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// console.log("count=", count);
97 this.g.data.columns.push([name].concat(count));
98}
99
100C3G.prototype.show = function() {
101 if (typeof(module)==="undefined")
102 return c3.generate(this.g);
103}
104
105// ------------------ 繪圖物件與函數 vis.js 部份 --------------------------------
106var VISG=function() {
107 // specify options
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
122VISG.prototype.curve3d = function(f, box) {
123 // Create and populate a data table.
124 var data = new vis.DataSet();
125 // create some nice looking data with sin/cos
126 var counter = 0;
127 var steps = 50; // number of datapoints will be steps*steps
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// ---------------------- 繪圖整合函數 ----------------------------------
140G.newGraph = function(box) {
141 G.c3g = new C3G();
142 G.visg = new VISG();
143 G.box = box;
144// G.c3g.show();
145}
146
147// C3 部份
148G.xrange = function(xmin, xmax) { G.c3g.xrange(xmin, xmax); }
149
150G.curve = function(f, options) {
151 G.c3g.curve(f, options);
152 G.c3g.show();
153}
154
155G.hist = function(x, options) {
156 G.c3g.hist(x, options);
157// console.log("x=", x, "G=", G);
158 G.c3g.show();
159}
160
161G.plot = function(x,y,options) {
162 G.c3g.plot(x,y,options);
163 G.c3g.show();
164}
165
166// Vis 部份
167G.curve3d = function(f) {
168 G.visg.curve3d(f, G.box);
169}