Changeset 15168

Show
Ignore:
Timestamp:
09/07/08 23:00:27 (3 months ago)
Author:
elazutkin
Message:

gfx: fixing degenerated pies (no series, empty series, all non-positive,
only one value, only one positive value), a test case was added as well.
Fixes #7234. Fixes #6982. !strict

Location:
dojox/trunk/charting
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • dojox/trunk/charting/plot2d/Pie.js

    r13816 r15168  
    5757                        return this.run ? this.run.data.length : 0; 
    5858                }, 
    59                  
     59 
    6060                // events 
    6161                plotEvent: function(o){ 
     
    9393                        }); 
    9494                }, 
    95                  
     95 
    9696                render: function(dim, offsets){ 
    9797                        if(!this.dirty){ return this; } 
     
    9999                        this.cleanGroup(); 
    100100                        var s = this.group, color, t = this.chart.theme; 
     101 
     102                        if(!this.run || !this.run.data.length){ 
     103                                return this; 
     104                        } 
    101105 
    102106                        // calculate the geometry 
     
    107111                                size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0, 
    108112                                taFontColor = "fontColor" in this.opt ? this.opt.fontColor : t.axis.fontColor, 
    109                                 start = 0, step, sum, slices, labels, shift, labelR, 
     113                                start = 0, step, filteredRun, slices, labels, shift, labelR, 
    110114                                run = this.run.data, 
    111115                                events = this.events(); 
    112116                        if(typeof run[0] == "number"){ 
    113                                 sum = df.foldl1(run, "+"); 
    114                                 slices = dojo.map(run, function(x){ return x / sum; }); 
     117                                filteredRun = df.map(run, "Math.max(x, 0)"); 
     118                                if(df.every(filteredRun, "<= 0")){ 
     119                                        return this; 
     120                                } 
     121                                slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0)); 
    115122                                if(this.opt.labels){ 
    116123                                        labels = dojo.map(slices, function(x){ 
    117                                                 return this._getLabel(x * 100) + "%"; 
     124                                                return x > 0 ? this._getLabel(x * 100) + "%" : ""; 
    118125                                        }, this); 
    119126                                } 
    120127                        }else{ 
    121                                 sum = df.foldl1(run, function(a, b){ return {y: a.y + b.y}; }).y; 
    122                                 slices = df.map(run, function(x){ return x.y / sum; }); 
     128                                filteredRun = df.map(run, "Math.max(x.y, 0)"); 
     129                                if(df.every(filteredRun, "<= 0")){ 
     130                                        return this; 
     131                                } 
     132                                slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0)); 
    123133                                if(this.opt.labels){ 
    124134                                        labels = dojo.map(slices, function(x, i){ 
     135                                                if(x <= 0){ return ""; } 
    125136                                                var v = run[i]; 
    126137                                                return "text" in v ? v.text : this._getLabel(x * 100) + "%"; 
     
    147158                                }; 
    148159 
    149                         this.dyn = [];                   
    150                         if(!this.run || !run.length){ 
    151                                 return this; 
    152                         } 
    153                         if(run.length == 1){ 
    154                                 // need autogenerated color 
    155                                 color = new dojo.Color(t.next("color")); 
    156                                 var shape = s.createCircle(circle). 
    157                                                 setFill(dc.augmentFill(t.run.fill, color)). 
    158                                                 setStroke(dc.augmentStroke(t.series.stroke, color)); 
    159                                 this.dyn.push({color: color, fill: shape.getFill(), stroke: shape.getStroke()}); 
    160                                 if(this.opt.labels){ 
    161                                         // draw the label 
    162                                         var elem = da.createText[this.opt.htmlLabels && dojox.gfx.renderer != "vml" ? "html" : "gfx"] 
    163                                                                         (this.chart, s, circle.cx, circle.cy + size / 2, "middle", 
    164                                                                                 "100%", taFont, taFontColor); 
    165                                         if(this.opt.htmlLabels){ this.htmlElements.push(elem); } 
    166                                 } 
    167                                 return this; 
    168                         } 
     160                        this.dyn = []; 
    169161                        // draw slices 
    170                         dojo.forEach(slices, function(x, i){ 
     162                        dojo.some(slices, function(slice, i){ 
     163                                if(slice <= 0){ 
     164                                        // degenerated slice 
     165                                        return false;   // continue 
     166                                } 
     167                                var v = run[i]; 
     168                                if(slice >= 1){ 
     169                                        // whole pie 
     170                                        var color, fill, stroke; 
     171                                        if(typeof v == "object"){ 
     172                                                color  = "color"  in v ? v.color  : new dojo.Color(t.next("color")); 
     173                                                fill   = "fill"   in v ? v.fill   : dc.augmentFill(t.series.fill, color); 
     174                                                stroke = "stroke" in v ? v.stroke : dc.augmentStroke(t.series.stroke, color); 
     175                                        }else{ 
     176                                                color  = new dojo.Color(t.next("color")); 
     177                                                fill   = dc.augmentFill(t.series.fill, color); 
     178                                                stroke = dc.augmentStroke(t.series.stroke, color); 
     179                                        } 
     180                                        var shape = s.createCircle(circle).setFill(fill).setStroke(stroke); 
     181                                        this.dyn.push({color: color, fill: fill, stroke: stroke}); 
     182 
     183                                        if(events){ 
     184                                                var o = { 
     185                                                        element: "slice", 
     186                                                        index:   i, 
     187                                                        run:     this.run, 
     188                                                        plot:    this, 
     189                                                        shape:   shape, 
     190                                                        x:       i, 
     191                                                        y:       typeof v == "number" ? v : v.y, 
     192                                                        cx:      circle.cx, 
     193                                                        cy:      circle.cy, 
     194                                                        cr:      r 
     195                                                }; 
     196                                                this._connectEvents(shape, o); 
     197                                        } 
     198 
     199                                        return true;    // stop iteration 
     200                                } 
    171201                                // calculate the geometry of the slice 
    172                                 var end = start + x * 2 * Math.PI, v = run[i]; 
     202                                var end = start + slice * 2 * Math.PI; 
    173203                                if(i + 1 == slices.length){ 
    174204                                        end = 2 * Math.PI; 
     
    199229                                                setStroke(stroke); 
    200230                                this.dyn.push({color: color, fill: fill, stroke: stroke}); 
    201                                  
     231 
    202232                                if(events){ 
    203233                                        var o = { 
     
    215245                                        this._connectEvents(shape, o); 
    216246                                } 
    217                                  
     247 
    218248                                start = end; 
     249 
     250                                return false;   // continue 
    219251                        }, this); 
    220252                        // draw labels 
    221253                        if(this.opt.labels){ 
    222254                                start = 0; 
    223                                 dojo.forEach(slices, function(slice, i){ 
     255                                dojo.some(slices, function(slice, i){ 
     256                                        if(slice <= 0){ 
     257                                                // degenerated slice 
     258                                                return false;   // continue 
     259                                        } 
     260                                        if(slice >= 1){ 
     261                                                // whole pie 
     262                                                var v = run[i], elem = da.createText[this.opt.htmlLabels && dojox.gfx.renderer != "vml" ? "html" : "gfx"] 
     263                                                                        (this.chart, s, circle.cx, circle.cy + size / 2, "middle", 
     264                                                                                labels[i], taFont, (typeof v == "object" && "fontColor" in v) ? v.fontColor : taFontColor); 
     265                                                if(this.opt.htmlLabels){ this.htmlElements.push(elem); } 
     266                                                return true;    // stop iteration 
     267                                        } 
    224268                                        // calculate the geometry of the slice 
    225269                                        var end = start + slice * 2 * Math.PI, v = run[i]; 
     
    233277                                        var elem = da.createText[this.opt.htmlLabels && dojox.gfx.renderer != "vml" ? "html" : "gfx"] 
    234278                                                                        (this.chart, s, x, y, "middle", 
    235                                                                                 labels[i], taFont,  
    236                                                                                 (typeof v == "object" && "fontColor" in v)  
     279                                                                                labels[i], taFont, 
     280                                                                                (typeof v == "object" && "fontColor" in v) 
    237281                                                                                        ? v.fontColor : taFontColor); 
    238282                                        if(this.opt.htmlLabels){ this.htmlElements.push(elem); } 
    239283                                        start = end; 
     284                                        return false;   // continue 
    240285                                }, this); 
    241286                        } 
    242287                        return this; 
    243288                }, 
    244                  
     289 
    245290                // utilities 
    246291                _getLabel: function(number){ 
  • dojox/trunk/charting/tests/test_pie2d.html

    r13562 r15168  
    2626dojo.require("dojox.charting.themes.PlotKit.green"); 
    2727dojo.require("dojox.charting.themes.PlotKit.red"); 
     28dojo.require("dojox.charting.themes.Adobebricks"); 
     29dojo.require("dojox.charting.themes.Algae"); 
    2830 
    2931makeObjects = function(){ 
     
    3133        chart1.setTheme(dojox.charting.themes.PlotKit.blue); 
    3234        chart1.addPlot("default", { 
    33                 type: "Pie",  
    34                 font: "normal normal bold 12pt Tahoma",  
    35                 fontColor: "white",  
     35                type: "Pie", 
     36                font: "normal normal bold 12pt Tahoma", 
     37                fontColor: "white", 
    3638                labelOffset: 40 
    3739        }); 
     
    4244        chart2.setTheme(dojox.charting.themes.PlotKit.blue); 
    4345        chart2.addPlot("default", { 
    44                 type: "Pie",  
    45                 font: "normal normal bold 12pt Tahoma",  
    46                 fontColor: "black",  
     46                type: "Pie", 
     47                font: "normal normal bold 12pt Tahoma", 
     48                fontColor: "black", 
    4749                labelOffset: -25, 
    4850                precision: 0 
     
    5456        chart3.setTheme(dojox.charting.themes.PlotKit.green); 
    5557        chart3.addPlot("default", { 
    56                 type: "Pie",  
    57                 font: "normal normal bold 10pt Tahoma",  
    58                 fontColor: "white",  
    59                 labelOffset: 25,  
     58                type: "Pie", 
     59                font: "normal normal bold 10pt Tahoma", 
     60                fontColor: "white", 
     61                labelOffset: 25, 
    6062                radius: 90 
    6163        }); 
     
    6668        chart4.setTheme(dojox.charting.themes.PlotKit.green); 
    6769        chart4.addPlot("default", { 
    68                 type: "Pie",  
    69                 font: "normal normal bold 10pt Tahoma",  
    70                 fontColor: "black",  
    71                 labelOffset: -25,  
     70                type: "Pie", 
     71                font: "normal normal bold 10pt Tahoma", 
     72                fontColor: "black", 
     73                labelOffset: -25, 
    7274                radius: 90 
    7375        }); 
     
    7880        chart5.setTheme(dojox.charting.themes.PlotKit.red); 
    7981        chart5.addPlot("default", { 
    80                 type: "Pie",  
    81                 font: "normal normal bold 14pt Tahoma",  
    82                 fontColor: "white",  
     82                type: "Pie", 
     83                font: "normal normal bold 14pt Tahoma", 
     84                fontColor: "white", 
    8385                labelOffset: 40 
    8486        }); 
     
    8991        chart6.setTheme(dojox.charting.themes.PlotKit.red); 
    9092        chart6.addPlot("default", { 
    91                 type: "Pie",  
    92                 font: "normal normal bold 14pt Tahoma",  
    93                 fontColor: "white",  
     93                type: "Pie", 
     94                font: "normal normal bold 14pt Tahoma", 
     95                fontColor: "white", 
    9496                labelOffset: 40 
    9597        }); 
     
    101103        ]); 
    102104        chart6.render(); 
     105 
     106        var chart7 = new dojox.charting.Chart2D("test7"); 
     107        chart7.setTheme(dojox.charting.themes.Adobebricks); 
     108        chart7.addPlot("default", { 
     109                type: "Pie", 
     110                font: "normal normal bold 12pt Tahoma", 
     111                fontColor: "white", 
     112                radius: 80 
     113        }); 
     114        chart7.addSeries("Series A", [4]); 
     115        chart7.render(); 
     116 
     117        var chart8 = new dojox.charting.Chart2D("test8"); 
     118        chart8.setTheme(dojox.charting.themes.Algae); 
     119        chart8.addPlot("default", { 
     120                type: "Pie", 
     121                font: "normal normal bold 12pt Tahoma", 
     122                fontColor: "white", 
     123                radius: 80 
     124        }); 
     125        chart8.addSeries("Series A", [ 
     126                {y: -1, text: "Red", color: "red"}, 
     127                {y: 5, text: "Green", color: "green"}, 
     128                {y: 0, text: "Blue", color: "blue"}, 
     129                {y: 0, text: "Other", color: "white", fontColor: "black"} 
     130        ]); 
     131        chart8.render(); 
    103132}; 
    104133 
     
    124153        <td><div id="test6" style="width: 300px; height: 300px;"></div></td> 
    125154</tr></table> 
     155<p>7: Degenerated pie with 1 element.</p> 
     156<div id="test7" style="width: 200px; height: 200px;"></div> 
     157<p>8: Degenerated pie with 1 positive elements (out of 5).</p> 
     158<div id="test8" style="width: 200px; height: 200px;"></div> 
    126159<p>That's all Folks!</p> 
    127160</body>