解决方案 »

  1.   


    function (require, module, exports) {
        var GeneData,
            __indexOf = [].indexOf || function (item) {
                for (var i = 0, l = this.length; i < l; i++) {
                    if (i in this && this[i] === item)
                        return i
                }
                return-1
            };
        GeneData = function () {
            function GeneData(data, columns) {
                this.data = data;
                this.columns = columns;
                this.columns_by_type_cache = {};
                this._process_data();
                this.set_relative("avg");
                this._store_fc_avg()
            }        GeneData.prototype.set_relative = function (relative) {
                if (relative !== this.relative) {
                    this.relative = relative;
                    return this._calc_fc()
                }
            };
            GeneData.prototype._store_fc_avg = function () {
                var col, cols, d, new_cols, _i, _len, _ref, _results;
                if (this.relative !== "avg") {
                    log_error("Relative is not 'avg' : " + this.relative);
                    return
                }
                if (this.columns_by_type("fc_calc_avg").length !== 0) {
                    log_error("fc_calc_avg already computed");
                    return
                }
                cols = this.columns_by_type("fc_calc");
                new_cols = cols.map(function (c) {
                    var _this = this;
                    return{idx : c.idx + "_avg",
                        name : c.name + "_avg",
                        type : "fc_calc_avg",
                        calc : function (d) {
                            return d[c.idx]
                        }
                    }
                });
                this.columns = this.columns.concat(new_cols);
                _ref = this.data;
                _results = [];
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    d = _ref[_i];
                    _results.push(function () {
                        var _j, _len1, _results1;
                        _results1 = [];
                        for (_j = 0, _len1 = new_cols.length; _j < _len1; _j++) {
                            col = new_cols[_j];
                            _results1.push(d[col.idx] = col.calc(d))
                        }
                        return _results1
                    }())
                }
                return _results
            };
            GeneData.prototype._process_data = function () {
                var c, d, fdr_col, i, _i, _j, _len, _len1, _ref, _ref1, _ref2;
                _ref = this.data;
                for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
                    d = _ref[i];
                    if (d.id == null) {
                        d.id = i
                    }
                    _ref1 = this.columns;
                    for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
                        c = _ref1[_j];
                        if ((_ref2 = c.type) === "fc" || _ref2 === "abs" || _ref2 === "avg" || _ref2 === "fdr" || _ref2 === "count") {
                            d[c.idx] = +d[c.idx]
                        }
                    }
                }
                fdr_col = this.column_by_type("fdr");
                this.data.sort(function (a, b) {
                    return a[fdr_col] - b[fdr_col]
                });
                this._totals = {};
                return null
            };
            GeneData.prototype._calc_fc = function () {
                var col, d, fc_cols, i, new_cols, primary, _fn, _i, _j, _len, _len1, _ref, _ref1, _results, _this = this;
                this.columns_by_type_cache = {};
                primary = this.columns_by_type("primary")[0];
                if (this.relative == null) {
                    this.relative = primary
                }
                fc_cols = this.columns_by_type("fc");
                new_cols = [];
                _ref = this.columns;
                _fn = function (col) {
                    var _ref1, _ref2;
                    if ((_ref1 = col.type) !== "fc_calc") {
                        new_cols.push(col)
                    }
                    if ((_ref2 = col.type) === "fc" || _ref2 === "primary") {
                        return new_cols.push({idx : "_calc_" + i,
                            name : col.name,
                            type : "fc_calc",
                            calc : function (d) {
                                var v1, v2;
                                v1 = col === primary ? 0 : d[col.idx];
                                v2 = _this.relative === primary ? 0 : _this.relative === "avg" ? d3.sum(fc_cols.map(function (c) {
                                    return d[c.idx]
                                })) / (1 + fc_cols.length) : d[_this.relative.idx];
                                return v1 - v2
                            }})
                    }
                };
                for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
                    col = _ref[i];
                    _fn(col)
                }
                this.columns = new_cols;
                _ref1 = this.data;
                _results = [];
                for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
                    d = _ref1[_j];
                    _results.push(function () {
                        var _k, _len2, _ref2, _ref3, _results1;
                        _ref2 = this.columns;
                        _results1 = [];
                        for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
                            col = _ref2[_k];
                            if ((_ref3 = col.type) === "fc_calc") {
                                _results1.push(d[col.idx] = col.calc(d))
                            } else {
                                _results1.push(void 0)
                            }
                        }
                        return _results1
                    }.call(this))
                }
                return _results
            };
            GeneData.prototype.column_by_type = function (type) {
                var col, _i, _len, _ref;
                _ref = this.columns;
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    col = _ref[_i];
                    if (col.type === type) {
                        return col.idx
                    }
                }
                return null
            };
            GeneData.prototype.column_by_idx = function (idx) {
                var col, _i, _len, _ref;
                _ref = this.columns;
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    col = _ref[_i];
                    if (col.idx === idx) {
                        return col
                    }
                }
                return null
            };
            GeneData.prototype.columns_by_type = function (types) {
                var col, res, _i, _len, _ref, _ref1;
                if (this.columns_by_type_cache[types]) {
                    return this.columns_by_type_cache[types]
                }
                if (!(types instanceof Array)) {
                    types = [types]
                }
                res = [];
                _ref = this.columns;
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    col = _ref[_i];
                    if (_ref1 = col.type, __indexOf.call(types, _ref1) >= 0) {
                        res.push(col)
                    }
                }
                this.columns_by_type_cache[types] = res;
                return res
            };
            GeneData.prototype.assoc_column_by_type = function (type, parent_name) {
                var col, res, _i, _len, _ref;
                res = [];
                _ref = this.columns;
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    col = _ref[_i];
                    if (col.type === type && col.parent === parent_name) {
                        res.push(col)
                    }
                }
                return res
            };
            GeneData.prototype.get_total = function (col) {
                if (col.name in this._totals) {
                    return this._totals[col.name]
                }
                this._totals[col.name] = d3.sum(this.data.map(function (d) {
                    return d[col.idx]
                }));
                return this._totals[col.name]
            };
            GeneData.prototype.get_data = function () {
                return this.data
            };
            return GeneData
        }();
        window.GeneData = GeneData
    }遵循一种开发模块的规范,新建了一个对象,里边有若干方法,然后绑定到windows上了。
      

  2.   

    解释一下为什么那么多无满意结贴啊 http://bbs.csdn.net/topics/390834582