【问题标题】:Translating EmberJS D3-Charts to AngularJS将 EmberJS D3 图表转换为 AngularJS
【发布时间】:2014-02-12 21:01:38
【问题描述】:

Ember Charts 是一个基于D3 的非常漂亮的图表库。我使用 Angular 作为我的主要库,我想将它移植到 Angular。我的问题是如何将一些 Ember.compute 属性转换为 Angular 风格的观察者。

因此,以 trim 函数为例,Embers 的实现如下所示:

  Ember.Charts.Helpers = Ember.Namespace.create({
    groupBy: function(obj, getter) {
        var group, index, key, result, value, _i, _ref;
        result = {};
        for (index = _i = 0, _ref = obj.length; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) {
            value = obj[index];
            key = getter(value, index);
            group = result[key] || (result[key] = []);
            group.push(value);
        }
        return result;
    },
    LabelTrimmer: Ember.Object.extend({
        getLabelSize: function(d, selection) {
            return 100;
        },
        getLabelText: function(d, selection) {
            return d.label;
        },
        trim: Ember.computed(function() {
            var getLabelSize, getLabelText;
            getLabelSize = this.get('getLabelSize');
            getLabelText = this.get('getLabelText');
            return function(selection) {
                return selection.text(function(d) {
                    var bbW, charWidth, label, numChars, textLabelWidth;
                    bbW = this.getBBox().width;
                    label = getLabelText(d, selection);
                    if (!label) {
                        return '';
                    }
                    charWidth = bbW / label.length;
                    textLabelWidth = getLabelSize(d, selection) - 4 * charWidth;
                    numChars = Math.floor(textLabelWidth / charWidth);
                    if (numChars - 3 <= 0) {
                        return '...';
                    }
                    else if (bbW > textLabelWidth) {
                        return label.slice(0, numChars - 3) + '...';
                    }
                    else {
                        return label;
                    }
                });
            };
        }).property('getLabelSize', 'getLabelText')
    })
});

我将其转换为 Angular factory,例如:

return app.factory('Charts.Helpers', function () {

    var factory = {

        groupBy: function (obj, getter) {
            var group, index, key, result, value, _i, _ref, result = {};

            for (index = _i = 0, _ref = obj.length; 0 <= _ref ? _i < _ref : _i > _ref; index = 0 <= _ref ? ++_i : --_i) {
                value = obj[index];
                key = getter(value, index);
                group = result[key] || (result[key] = []);
                group.push(value);
            }

            return result;
        },

        LabelTrimmer: {

            getLabelSize: function (d, selection) {
                return 100;
            },

            getLabelText: function (d, selection) {
                return d.label;
            },

            trim: function (selection){
                return selection.text(function (d) {
                    var bbW, charWidth, label, numChars, textLabelWidth;
                    bbW = this.getBBox().width;
                    label = factory.getLabelText(d, selection);

                    if (!label) {
                        return '';
                    }

                    charWidth = bbW / label.length;
                    textLabelWidth = factory.getLabelSize(d, selection) - 4 * charWidth;
                    numChars = Math.floor(textLabelWidth / charWidth);

                    if (numChars - 3 <= 0) {
                        return '...';
                    }
                    else if (bbW > textLabelWidth) {
                        return label.slice(0, numChars - 3) + '...';
                    }
                    else {
                        return label;
                    }
                });
            }
        }
    };

    return factory;
});

但我不确定如何使用trim 函数来监听这些属性的变化。有任何反馈、想法,这是愚蠢的移植吗?

【问题讨论】:

    标签: javascript angularjs ember.js d3.js


    【解决方案1】:

    最终使用dc-js 并使用angular dc-js directive。更灵活和更多图表。想知道如何模拟角度计算。

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2015-09-17
      相关资源
      最近更新 更多