UNPKG

4.21 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 = x.map(f);
60 this.g.data.columns.push([name+"x"].concat(x));
61 this.g.data.columns.push([name].concat(y));
62}
63
64C3G.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
81C3G.prototype.show = function(chartName) {
82 if (typeof(module)==="undefined") {
83 this.g.bindto=chartName;
84 return c3.generate(this.g);
85 }
86}
87
88// ------------------ 繪圖物件與函數 vis.js 部份 --------------------------------
89var VISG=function() {
90 // specify options
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
105VISG.prototype.curve3d = function(f, box) {
106 // Create and populate a data table.
107 var data = new vis.DataSet();
108 // create some nice looking data with sin/cos
109 var counter = 0;
110 var steps = 50; // number of datapoints will be steps*steps
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// ---------------------- 繪圖整合函數 ----------------------------------
123G.newGraph = function(box) {
124 G.c3g = new C3G();
125 G.visg = new VISG();
126 G.box = box;
127// G.c3g.show();
128}
129
130// C3 部份
131G.xrange = function(xmin, xmax) { G.c3g.xrange(xmin, xmax); }
132
133G.curve = function(f, options) {
134 G.c3g.curve(f, options);
135// G.c3g.show();
136}
137
138G.hist = function(x, options) {
139 G.c3g.hist(x, options);
140// console.log("x=", x, "G=", G);
141 G.c3g.show();
142}
143
144G.plot = function(x,y,options) {
145 G.c3g.plot(x,y,options);
146 G.c3g.show();
147}
148
149// Vis 部份
150G.curve3d = function(f) {
151 G.visg.curve3d(f, G.box);
152}