-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
423d595
commit 548d675
Showing
57 changed files
with
1,249 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Component.extend({ | ||
rows: null, | ||
columns: null, | ||
initialOptions: null, | ||
defaultOptions: { | ||
unusedAttrsVertical: true, | ||
menuLimit: 200, | ||
renderers: Ember.$.extend( | ||
Ember.$.pivotUtilities.renderers, | ||
Ember.$.pivotUtilities.c3_renderers | ||
) | ||
}, | ||
|
||
options: Ember.computed('initialOptions', 'defaultOptions', function() { | ||
let deserialized = this.get('initialOptions'); | ||
let options = this.get('defaultOptions'); | ||
// Attach refresh handler | ||
return Ember.$.extend({ onRefresh: this.refreshHandler(this) }, deserialized, options); | ||
}), | ||
|
||
didInsertElement() { | ||
this._super(...arguments); | ||
let { rows, options } = this.getProperties('rows', 'options'); | ||
this.$('.pivot-table-container').pivotUI(rows, options); | ||
}, | ||
|
||
refreshHandler(context) { | ||
return (configuration) => { | ||
let copy = JSON.parse(JSON.stringify(configuration)); | ||
// Deletes functions and defaults: http://nicolas.kruchten.com/pivottable/examples/onrefresh.html | ||
delete copy.aggregators; | ||
delete copy.renderers; | ||
delete copy.rendererOptions; | ||
delete copy.localeStrings; | ||
context.sendAction('onRefresh', copy); | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright 2016, Yahoo Inc. | ||
* Licensed under the terms of the Apache License, Version 2.0. | ||
* See the LICENSE file associated with the project for terms. | ||
*/ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Component.extend({ | ||
classNames: ['records-charter'], | ||
model: null, | ||
columns: null, | ||
rows: null, | ||
chartType: 'bar', | ||
simpleMode: true, | ||
|
||
cannotModeSwitch: Ember.computed.alias('model.isRaw').readOnly(), | ||
canModeSwitch: Ember.computed.not('cannotModeSwitch').readOnly(), | ||
notSimpleMode: Ember.computed.not('simpleMode').readOnly(), | ||
pivotMode: Ember.computed.or('notSimpleMode', 'cannotModeSwitch').readOnly(), | ||
pivotOptions: Ember.computed('model.pivotOptions', function() { | ||
return JSON.parse(this.get('model.pivotOptions')); | ||
}).readOnly(), | ||
|
||
sampleRow: Ember.computed('rows', 'columns', function() { | ||
let typicalRow = { }; | ||
let rows = this.get('rows'); | ||
this.get('columns').forEach(column => { | ||
for (let row of rows) { | ||
let value = row[column]; | ||
if (!Ember.isEmpty(value)) { | ||
typicalRow[column] = value; | ||
break; | ||
} | ||
} | ||
}); | ||
return typicalRow; | ||
}), | ||
|
||
independentColumns: Ember.computed('model', 'sampleRow', 'columns', function() { | ||
let { columns, sampleRow } = this.getProperties('columns', 'sampleRow'); | ||
let isDistribution = this.get('model.isDistribution'); | ||
if (isDistribution) { | ||
return Ember.A(columns.filter(c => this.isAny(c, 'Quantile', 'Range'))); | ||
} | ||
// Pick all string columns | ||
return Ember.A(columns.filter(c => this.isType(sampleRow, c, 'string'))); | ||
}), | ||
|
||
dependentColumns: Ember.computed('model', 'sampleRow', 'columns', function() { | ||
let { columns, sampleRow } = this.getProperties('columns', 'sampleRow'); | ||
let isDistribution = this.get('model.isDistribution'); | ||
if (isDistribution) { | ||
return Ember.A(columns.filter(c => this.isAny(c, 'Count', 'Value', 'Probability'))); | ||
} | ||
// Pick all number columns | ||
return Ember.A(columns.filter(c => this.isType(sampleRow, c, 'number'))); | ||
}), | ||
|
||
options: Ember.computed('dependentColumns', function() { | ||
let numberOfColumns = this.get('dependentColumns.length'); | ||
if (numberOfColumns === 1) { | ||
return { }; | ||
} | ||
// Everything else, 2 axes | ||
return { | ||
scales: { | ||
yAxes: [{ | ||
position: 'left', | ||
id: '0' | ||
}, { | ||
position: 'right', | ||
id: '1' | ||
}] | ||
} | ||
}; | ||
}), | ||
|
||
labels: Ember.computed('independentColumns', 'rows', function() { | ||
// Only one independent column for now | ||
let rows = this.get('rows'); | ||
// [ [field1 values...], [field2 values...], ...] | ||
let valuesList = this.get('independentColumns').map(field => this.getFieldValues(field, rows)); | ||
// valuesList won't be empty because all non-Raw aggregations will have at least one string field | ||
return this.zip(valuesList); | ||
}), | ||
|
||
datasets: Ember.computed('dependentColumns', 'rows', function() { | ||
let dependentColumns = this.get('dependentColumns'); | ||
let rows = this.get('rows'); | ||
return dependentColumns.map((c, i) => this.dataset(c, rows, i)); | ||
}), | ||
|
||
data: Ember.computed('labels', 'datasets', function() { | ||
return { | ||
labels: this.get('labels'), | ||
datasets: this.get('datasets') | ||
}; | ||
}), | ||
|
||
dataset(column, rows, index) { | ||
let values = this.getFieldValues(column, rows); | ||
let dataset = { | ||
label: column, | ||
data: values, | ||
backgroundColor: this.randomColors(values.length) | ||
}; | ||
// Add yAxisID only if we have more than one dataset. More than 2 => Add the first y-axis | ||
if (index === 1) { | ||
dataset.yAxisID = '1'; | ||
} else if (index > 1) { | ||
dataset.yAxisID = '0'; | ||
} | ||
return dataset; | ||
}, | ||
|
||
randomUpto(size) { | ||
return Math.floor(Math.random() * size); | ||
}, | ||
|
||
randomColor() { | ||
let red = this.randomUpto(255); | ||
let green = this.randomUpto(255); | ||
let blue = this.randomUpto(255); | ||
return `rgb(${red},${green},${blue})`; | ||
}, | ||
|
||
randomColors(size) { | ||
let color = this.randomColor(); | ||
return new Array(size).fill(color); | ||
}, | ||
|
||
isType(row, field, type) { | ||
return Ember.isEqual(Ember.typeOf(row[field]), type); | ||
}, | ||
|
||
isAny(field, ...values) { | ||
for (let value of values) { | ||
if (Ember.isEqual(field, value)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
|
||
zip(arrayOfArrays, delimiter = '/') { | ||
let zipped = arrayOfArrays[0].map((_, i) => arrayOfArrays.map(a => a[i])); | ||
return zipped.map(a => a.reduce((p, c) => `${p}${delimiter}${c}`), ''); | ||
}, | ||
|
||
getFieldValues(field, rows) { | ||
return rows.map(row => row[field]); | ||
}, | ||
|
||
actions: { | ||
toggleMode() { | ||
this.toggleProperty('simpleMode'); | ||
}, | ||
|
||
saveOptions(options) { | ||
let model = this.get('model'); | ||
model.set('pivotOptions', JSON.stringify(options)); | ||
model.save(); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.