我在应用程序中使用ember-charts来呈现来自同一模型的多个不同图表。Ember-charts 采用特定方式格式化的数据;如下代码中的对象数组。
我一直在使用computed properties
将数据格式化为ember-charts
喜欢的格式。
这行得通。
但考虑到会有很多图表,并且格式化数据的方式非常相似,我对重复代码的数量不满意,并且想知道是否有人有更好的方法来格式化 ember-charts 的数据。
我正在使用Ember 2.4
和ember-cli-mirage
现在来生成我的模型。
控制器:
import Ember from 'ember';
export default Ember.Controller.extend({
sqByU: Ember.computed("model.[]", function() {
let arr = [], model = this.get('model');
model.content.map(function(item) {
let d = item._data;
arr.pushObjects([
{ "label": 'Manufacturing', "group": d.location, "value": d.manufacturing },
{ "label": 'Warehouse', "group": d.location, "value": d.warehouse },
{ "label": 'Research & Development', "group": d.location, "value": d.rAndD },
{ "label": 'Mechanical Support', "group": d.location, "value": d.mechSupport },
{ "label": 'Office', "group": d.location, "value": d.office }
]);
});
return arr;
}),
annualFacilityCost: Ember.computed("model.[]", function() {
let arr = [], model = this.get('model');
model.content.map(function(item) {
let d = item._data;
let facilCost = d.opexSpend/(d.manufacturing + d.warehouse + d.rAndD + d.mechSupport + d.office);
arr.pushObjects([
{"label": 'IFMA Benchmark', "group": d.location, "value": d.ifmaBenchmark },
{"label": 'Facility Cost', "group": d.location, "value": facilCost }
]);
});
return arr;
}),
});
模板:
<div class="col-md-4">
{{#box-reg title="Square Foot by Utilization"}}
{{vertical-bar-chart maxBarThickness=100 selectedSeedColor="rgb(13,71,161)" stackBars=true data=sqByU }}
{{/box-reg}}
</div>
<div class="col-md-4">
{{#box-reg title="BSC Total Annual Facility Cost/SF"}}
{{vertical-bar-chart maxBarThickness=100 selectedSeedColor="rgb(13,71,161)" data=annualFacilityCost }}
{{/box-reg}}
</div>