1 //@class wso2vis.s.chart.protovis.WedgeChart : wso2vis.s.chart.Chart
  2 //This is the custom wrapper class for axiis bar charts
  3 
  4 //Constructor
  5 wso2vis.s.chart.protovis.WedgeChart = function(canvas, chartTitle, chartDesc) {
  6     wso2vis.s.chart.Chart.call(this, canvas, chartTitle, chartDesc);
  7 
  8     this.labelLength(12)
  9         .thickness(30);
 10 
 11     /* @private */
 12     this.vis = null;
 13 }
 14 
 15 // this makes c.protovis.WedgeChart.prototype inherits from wso2vis.s.chart.Chart
 16 wso2vis.extend(wso2vis.s.chart.protovis.WedgeChart, wso2vis.s.chart.Chart);
 17 
 18 wso2vis.s.chart.protovis.WedgeChart.prototype
 19     .property("dataField")
 20     .property("dataValue")
 21     .property("dataLabel")
 22     .property("labelLength")
 23     .property("thickness");
 24 
 25 //Public function load
 26 //Loads the chart inside the given HTML element
 27 wso2vis.s.chart.protovis.WedgeChart.prototype.load = function (w) {
 28     if ( w !== undefined ) {
 29         this.width(w);
 30     }
 31     /*if ( h !== undefined ) { //not using height for the Wedge
 32         this.height(h);
 33     }*/
 34     //var r = this.width() / 2.5;
 35 
 36     var thisObject = this;
 37     
 38     this.vis = new pv.Panel()
 39         .canvas(function() { return thisObject.divEl(); })
 40         .width(function() { return thisObject.width(); })
 41         .height(function() { return thisObject.height(); })
 42         .overflow('hidden');
 43 
 44     var chart = this.vis.add(pv.Panel)
 45         .width(function() { return (thisObject.width() - thisObject.paddingLeft() - thisObject.paddingRight()); })
 46         .height(function() { return (thisObject.height() - thisObject.paddingTop() - thisObject.paddingBottom()); })
 47         .top(thisObject.paddingTop())
 48         .bottom(thisObject.paddingBottom())
 49         .left(thisObject.paddingLeft())
 50         .right(thisObject.paddingRight());
 51     
 52     var wedge = chart.add(pv.Wedge)
 53         .data(function() { return pv.normalize(thisObject.getData(thisObject)); })
 54         .left((thisObject.width() - thisObject.paddingLeft() - thisObject.paddingRight()) / 2)
 55         .bottom((thisObject.height() - thisObject.paddingTop() - thisObject.paddingBottom()) / 2)
 56         .innerRadius(function() { return (((thisObject.width() - thisObject.paddingLeft() - thisObject.paddingRight()) / 2.5) - thisObject.thickness()); })
 57         .outerRadius(function() { return (thisObject.width() - thisObject.paddingLeft() - thisObject.paddingRight()) / 2.5; })
 58         .angle(function(d) { return (d * 2 * Math.PI); })
 59         .title(function() { 
 60             var dataObj = thisObject.traverseToDataField(thisObject.data, thisObject.dataField());
 61             if( dataObj instanceof Array ) {
 62                 return thisObject.onTooltip(dataObj[this.index]);
 63             }
 64             else {
 65                 return thisObject.onTooltip(dataObj);
 66             }
 67         })
 68         .event("click", function() { 
 69             var dataObj = thisObject.traverseToDataField(thisObject.data, thisObject.dataField());
 70             if( dataObj instanceof Array ) {
 71                 return thisObject.onClick(dataObj[this.index]);
 72             }
 73             else {
 74                 return thisObject.onClick(dataObj);
 75             }
 76         });
 77 
 78     wedge.anchor("outer").add(pv.Label)
 79         .visible(function(d) { return (d > 0.05); })
 80         .textMargin(function(){ return thisObject.thickness() + 5; })
 81         .text(function(d) { var lbl=thisObject.getDataLabel(this.index); return (lbl.length > thisObject.labelLength() ? lbl.substring(0,thisObject.labelLength())+"..." : lbl); })
 82         .font(function() { return thisObject.labelFont(); })
 83         .textStyle(function() { return wedge.fillStyle(); });
 84 
 85     wedge.anchor("center").add(pv.Label)
 86          .visible(function(d) { return (thisObject.marks() && (d > 0.10)); })
 87          .textAngle(0)
 88          .text(function(d) { return (d*100).toFixed() + "%"; })
 89          .textStyle("#fff");
 90 
 91     /* Legend */
 92 /*    var legend = chart.add(pv.Panel)
 93         .top(function() { return thisObject.legendTop(); })
 94         .left(function() { return thisObject.legendLeft(); })
 95         .right(function() { return thisObject.legendRight(); })
 96         .bottom(function() { return thisObject.legendBottom(); })*/
 97         
 98 
 99     chart.add(pv.Dot)
100         .data(function() { return pv.normalize(thisObject.getData(thisObject)); })
101         .visible(function() { return thisObject.legend(); })
102         .fillStyle(function() { return wedge.fillStyle(); })
103         .right(function() { return (thisObject.width() - thisObject.legendX()); })
104         .bottom(function() { return (this.index * 15) + (thisObject.height() - thisObject.legendY()); })
105         .size(20) 
106         .lineWidth(1)
107         .strokeStyle("#000")
108       .anchor("right").add(pv.Label)
109         .text(function() { var lbl=thisObject.getDataLabel(this.index); return (lbl.length > thisObject.labelLength() ? lbl.substring(0,thisObject.labelLength())+"..." : lbl); });
110 
111      this.vis.add(pv.Label)
112         .left(this.width() / 2)
113         .visible(function() { return !(thisObject.title() === ""); })
114         .top(16)
115         .textAlign("center")
116         .text(function() { return thisObject.title(); })
117         .font(function() { return thisObject.titleFont(); });
118 };
119 
120 /**
121 * @private
122 */
123 wso2vis.s.chart.protovis.WedgeChart.prototype.titleSpacing = function () {
124     if(this.title() === "") {
125         return 1;
126     }
127     else {
128         return 0.9;
129     }
130 };
131 
132 /**
133 * @private
134 */
135 wso2vis.s.chart.protovis.WedgeChart.prototype.populateData = function (thisObject) {
136 
137     var _dataField = thisObject.traverseToDataField(thisObject.data, thisObject.dataField());
138     var dataGrpCount = 1;
139 
140     if( _dataField instanceof Array ) {
141         dataGrpCount = _dataField.length;
142     }
143 
144     this.formattedData = pv.range(dataGrpCount).map( genDataMap );
145 
146     function genDataMap(x) {
147         var rootObj;
148         if( _dataField instanceof Array ) {
149             rootObj = _dataField[x];
150         }
151         else {
152             rootObj = _dataField;
153         }
154         return parseInt(thisObject.traverseToDataField(rootObj, thisObject.dataValue()));
155     }
156 };
157 
158 wso2vis.s.chart.protovis.WedgeChart.prototype.getData = function (thisObject) {
159 
160     return thisObject.formattedData;
161 };
162 
163 wso2vis.s.chart.protovis.WedgeChart.prototype.update = function () {
164 
165     this.populateData(this);
166     this.vis.render();
167     if(this.tooltip() === true) {
168         tooltip.init();
169     }
170 };
171 
172 wso2vis.s.chart.protovis.WedgeChart.prototype.getDataLabel = function (i) {
173 
174     if (this.data !== null){
175 
176         var rootObj = this.traverseToDataField(this.data, this.dataField());
177         if( rootObj instanceof Array ) {
178             return  this.traverseToDataField(rootObj[i], this.dataLabel());
179         }
180         else {
181             return  this.traverseToDataField(rootObj, this.dataLabel());
182         }
183     }
184     
185     return i;
186 };
187 
188