UNPKG

3.71 kBJavaScriptView Raw
1G = glab = {
2 dgMap:{}, // (dimension+graph) Map
3}
4
5G.new2D=function() {
6 return { // c3 graph
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
26G.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
37G.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// type : line, spline, step, area, area-spline, area-step, bar, scatter, pie, donut, gauge
46G.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
53G.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
58G.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
63G.ihist=function(g, name, x, type) {
64 G.hist(g, name, x, type, x.min()-0.5, x.max()+0.5, 1);
65}
66
67G.plot =(g, name, x, y)=>G.draw(g, name, x, y, 'scatter');
68
69G.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
77G.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
83G.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/*
99G.chart3D = function(chartName, f) {
100 var g = G.new3D();
101 f(g);
102 var box=document.getElementById(chartName.replace('#',''));
103 new vis.Graph3d(box, g.dataSet, g.options);
104}
105
106G.curve3D = function(g, f) {
107 var g = G.new3D();
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 g.dataSet.add({id:counter++,x:x,y:y,z:value,style:value});
117 }
118 }
119 var box=document.getElementById(chartName.replace('#',''));
120 new vis.Graph3d(box, g.dataSet, g.options);
121}
122*/
123
124// style : surface, grid, bar, bar-color, bar-size, dot, dot-line, dot-color, dot-size, line
125
126G.chart3D = function(chartName, style, f) {
127 var g = G.new3D();
128 // create some nice looking data with sin/cos
129 var counter = 0;
130 var steps = 50; // number of datapoints will be steps*steps
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}