Roo/bootstrap/Graph.js
[roojs1] / Roo / bootstrap / Graph.js
1     /*
2  * - LGPL
3  *
4  * Graph
5  * 
6  */
7
8
9 /**
10  * @class Roo.bootstrap.Graph
11  * @extends Roo.bootstrap.Component
12  * Bootstrap Graph class
13 > Prameters
14  -sm {number} sm 4
15  -md {number} md 5
16  @cfg {String} graphtype  bar | vbar | pie
17  @cfg {number} g_x coodinator | centre x (pie)
18  @cfg {number} g_y coodinator | centre y (pie)
19  @cfg {number} g_r radius (pie)
20  @cfg {number} g_height height of the chart (respected by all elements in the set)
21  @cfg {number} g_width width of the chart (respected by all elements in the set)
22  @cfg {String} title The title of this graph
23  -{Array}  values
24  -opts (object) options for the chart 
25      o {
26      o type (string) type of endings of the bar. Default: 'square'. Other options are: 'round', 'sharp', 'soft'.
27      o gutter (number)(string) default '20%' (WHAT DOES IT DO?)
28      o vgutter (number)
29      o colors (array) colors be used repeatedly to plot the bars. If multicolumn bar is used each sequence of bars with use a different color.
30      o stacked (boolean) whether or not to tread values as in a stacked bar chart
31      o to
32      o stretch (boolean)
33      o }
34  -opts (object) options for the pie
35      o{
36      o cut
37      o startAngle (number)
38      o endAngle (number)
39      } 
40  *
41  * @constructor
42  * Create a new Input
43  * @param {Object} config The config object
44  */
45
46 Roo.bootstrap.Graph = function(config){
47     Roo.bootstrap.Graph.superclass.constructor.call(this, config);
48     
49     this.addEvents({
50         // img events
51         /**
52          * @event click
53          * The img click event for the img.
54          * @param {Roo.EventObject} e
55          */
56         "click" : true
57     });
58 };
59
60 Roo.extend(Roo.bootstrap.Graph, Roo.bootstrap.Component,  {
61     
62     sm: 4,
63     md: 5,
64     graphtype: 'bar',
65     g_height: 250,
66     g_width: 400,
67     g_x: 50,
68     g_y: 50,
69     g_r: 30,
70     opts:{
71         //g_colors: this.colors,
72         g_type: 'soft',
73         g_gutter: '20%'
74
75     },
76     title : '',
77
78     getAutoCreate : function(){
79         
80         var cfg = {
81             tag: 'div',
82             html : null
83         }
84         
85         
86         return  cfg;
87     },
88
89     onRender : function(ct,position){
90         Roo.bootstrap.Graph.superclass.onRender.call(this,ct,position);
91         this.raphael = Raphael(this.el.dom);
92
93                     // data1 = [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55], [12, 20, 30]],
94                     // data2 = [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55], [12, 20, 30]],
95                     // data3 = [[55, 20, 13, 32, 5, 1, 2, 10], [10, 2, 1, 5, 32, 13, 20, 55], [12, 20, 30]],
96                     // txtattr = { font: "12px 'Fontin Sans', Fontin-Sans, sans-serif" };
97                 /*
98                 r.text(160, 10, "Single Series Chart").attr(txtattr);
99                 r.text(480, 10, "Multiline Series Chart").attr(txtattr);
100                 r.text(160, 250, "Multiple Series Stacked Chart").attr(txtattr);
101                 r.text(480, 250, 'Multiline Series Stacked Vertical Chart. Type "round"').attr(txtattr);
102                 
103                 r.barchart(10, 10, 300, 220, [[55, 20, 13, 32, 5, 1, 2, 10]], 0, {type: "sharp"});
104                 r.barchart(330, 10, 300, 220, data1);
105                 r.barchart(10, 250, 300, 220, data2, {stacked: true});
106                 r.barchart(330, 250, 300, 220, data3, {stacked: true, type: "round"});
107                 */
108                 
109                 // var xdata = [55, 20, 13, 32, 5, 1, 2, 10,5 , 10];
110                 // r.barchart(30, 30, 560, 250,  xdata, {
111                 //    labels : [55, 20, 13, 32, 5, 1, 2, 10,5 , 10],
112                 //     axis : "0 0 1 1",
113                 //     axisxlabels :  xdata
114                 //     //yvalues : cols,
115                    
116                 // });
117         var xdata = [55, 20, 13, 32, 5, 1, 2, 10,5 , 10];
118         
119         this.load(null,xdata,{
120                 axis : "0 0 1 1",
121                 axisxlabels :  xdata
122                 });
123
124     },
125
126     load : function(graphtype,xdata,opts){
127         this.raphael.clear();
128         if(!graphtype) {
129             graphtype = this.graphtype;
130         }
131         if(!opts){
132             opts = this.opts;
133         }
134         var r = this.raphael,
135             fin = function () {
136                 this.flag = r.popup(this.bar.x, this.bar.y, this.bar.value || "0").insertBefore(this);
137             },
138             fout = function () {
139                 this.flag.animate({opacity: 0}, 300, function () {this.remove();});
140             };
141
142         switch(graphtype){
143             case 'bar':
144                 this.raphael.barchart(this.g_x,this.g_y,this.g_width,this.g_height,xdata,opts).hover(fin,fout);
145                 this.title = this.title || 'This is Vertical Barchart';
146                 break;
147             case 'hbar':
148                 this.raphael.hbarchart(this.g_x,this.g_y,this.g_width,this.g_height,xdata,opts).hover(fin,fout);
149                 this.title = this.title || 'This is Horizontal Barchart';
150                 break;
151             case 'pie':
152                 opts = { legend: ["%% - Enterprise Users", "% - ddd","Chrome Users"], legendpos: "west", 
153                 href: ["http://raphaeljs.com", "http://g.raphaeljs.com"]};
154             
155                 this.raphael.piechart(this.g_x,this.g_y,this.g_r,xdata,opts);
156                 
157                 this.title = this.title || 'This is Piechart';
158                 break;
159
160         }
161         this.raphael.text(150,50,this.title).attr({ font: "20px 'Fontin Sans', Fontin-Sans, sans-serif" });
162     },
163     
164     setTitle: function(str) {
165         this.title = str;
166     },
167     
168     initEvents: function() {
169         
170         if(!this.href){
171             this.el.on('click', this.onClick, this);
172         }
173     },
174     
175     onClick : function(e)
176     {
177         Roo.log('img onclick');
178         this.fireEvent('click', this, e);
179     }
180    
181 });
182
183