Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add list2charts files and add table chart formatter
- Loading branch information
1 parent
3bb5a4a
commit fd77d3e
Showing
20 changed files
with
3,007 additions
and
1,533 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,47 @@ | ||
'use strict'; | ||
|
||
const { processPipeline } = require('./pipeline'); | ||
|
||
class LineChart { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
constructor(dataHandlingProps, options = {}) { | ||
this._dataHandlingProps = dataHandlingProps; | ||
this._options = options; | ||
} | ||
|
||
setData(data) { | ||
this._data = data; | ||
} | ||
|
||
parse() { | ||
return { | ||
data: this.tryToSort(this._parseData()), | ||
options: this._parseOptions() | ||
}; | ||
} | ||
|
||
_parseData() { | ||
const { pipeline } = this._dataHandlingProps; | ||
|
||
return pipeline ? processPipeline(this._data, pipeline) : this._data; | ||
} | ||
|
||
_parseOptions() { | ||
return this._options; | ||
} | ||
|
||
tryToSort(data) { | ||
if(!this._dataHandlingProps.sortData || typeof this._dataHandlingProps.sortData !== 'function') | ||
return data; | ||
|
||
const [headers, ...realData] = data; | ||
|
||
return [ | ||
headers, | ||
...realData.sort(([key1], [key2]) => this._dataHandlingProps.sortData(key1, key2)) | ||
]; | ||
} | ||
|
||
} | ||
|
||
module.exports = LineChart; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,38 @@ | ||
'use strict'; | ||
|
||
const identity = x => x; | ||
|
||
const getTitle = ({ source, title, titleMapper }) => { | ||
return (titleMapper || identity)(title || source); | ||
This comment has been minimized.
Sorry, something went wrong.
gastonpereyra
|
||
}; | ||
|
||
/** | ||
* Maps an array of source definitions to an array of strings representing the titles. | ||
* A source definition is an object with the following properties: | ||
* - title {string} | ||
* - source {string} | ||
* - mapper {callable}. Receives a string and must return another | ||
* | ||
* @param {array<object>} sources The source definitions | ||
* @return {array<string>} The titles. | ||
*/ | ||
const getTitles = sources => sources.map(getTitle); | ||
|
||
const getValue = (row, { source, valueMapper }) => (valueMapper || identity)(row[source], row); | ||
|
||
/** | ||
* Maps a row and an array of source definitions to an array of values. | ||
* A source definition is an object with the following properties: | ||
* - source {string} | ||
* - valueMapper {callable}. Receives a string and must return another | ||
* | ||
* @param {object} row The row with all the data | ||
* @param {array<object>} sources The source definitions | ||
* @return {array<any>} The values. | ||
*/ | ||
const getValues = (row, sources) => sources.map(source => getValue(row, source)); | ||
|
||
module.exports = { | ||
getTitles, | ||
getValues | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
'use strict'; | ||
|
||
const FormatDataToGoogleChart = require('./format-data-to-google-chart'); | ||
const FormatDataToGoogleChartError = require('./format-data-to-google-chart-error'); | ||
const PieChart = require('./pie-chart'); | ||
const LineChart = require('./line-chart'); | ||
const TableChart = require('./table-chart'); | ||
|
||
module.exports = { | ||
FormatDataToGoogleChart, | ||
FormatDataToGoogleChartError | ||
PieChart, | ||
LineChart, | ||
TableChart | ||
}; |
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,31 @@ | ||
'use strict'; | ||
|
||
const BaseChart = require('./base-chart'); | ||
const { getTitles, getValues } = require('./helpers'); | ||
|
||
class LineChart extends BaseChart { | ||
|
||
_parseData() { | ||
|
||
const data = super._parseData(); | ||
|
||
return [ | ||
getTitles([ | ||
this._dataHandlingProps.label, | ||
...this._dataHandlingProps.values | ||
]), | ||
// Data | ||
...data.map(row => this._parseRow(row)) | ||
]; | ||
} | ||
|
||
_parseRow(row) { | ||
return getValues(row, [ | ||
this._dataHandlingProps.label, | ||
...this._dataHandlingProps.values | ||
]); | ||
} | ||
|
||
} | ||
|
||
module.exports = LineChart; |
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,31 @@ | ||
'use strict'; | ||
|
||
const BaseChart = require('./base-chart'); | ||
const { getTitles, getValues } = require('./helpers'); | ||
|
||
class PieChart extends BaseChart { | ||
|
||
_parseData() { | ||
|
||
const data = super._parseData(); | ||
|
||
return [ | ||
getTitles([ | ||
this._dataHandlingProps.label, | ||
this._dataHandlingProps.value | ||
]), | ||
// Data | ||
...data.map(row => this._parseRow(row)) | ||
]; | ||
} | ||
|
||
_parseRow(row) { | ||
return getValues(row, [ | ||
this._dataHandlingProps.label, | ||
this._dataHandlingProps.value | ||
]); | ||
} | ||
|
||
} | ||
|
||
module.exports = PieChart; |
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,49 @@ | ||
'use strict'; | ||
|
||
const aggregate = (rows, { | ||
aggregate: aggregationField, | ||
target = aggregationField, | ||
aggregationOperation = 'count', | ||
groupTitleTarget, | ||
groupSelector | ||
}) => { | ||
|
||
const indexedAggregation = rows.reduce((aggregated, row) => { | ||
|
||
const groupName = groupSelector(row); | ||
|
||
if(!aggregated[groupName]) { | ||
aggregated[groupName] = { | ||
[groupTitleTarget]: groupName, | ||
[target]: 0 | ||
}; | ||
} | ||
|
||
if(aggregationOperation === 'sum') | ||
aggregated[groupName][target] += row[aggregationField]; | ||
else // Default is 'count' | ||
aggregated[groupName][target]++; | ||
|
||
return aggregated; | ||
}, {}); | ||
|
||
return Object.values(indexedAggregation); | ||
}; | ||
|
||
const processPipelineStep = (rows, step) => { | ||
if(step.map) | ||
return rows.map(row => step.map(row)); | ||
|
||
if(step.aggregate) | ||
return aggregate(rows, step); | ||
|
||
return rows; | ||
}; | ||
|
||
const processPipeline = (rows, pipeline) => pipeline.reduce((processedRows, step) => { | ||
return processPipelineStep(processedRows, step); | ||
}, rows); | ||
|
||
module.exports = { | ||
processPipeline | ||
}; |
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,24 @@ | ||
'use strict'; | ||
|
||
const BaseChart = require('./base-chart'); | ||
const { getTitles, getValues } = require('./helpers'); | ||
|
||
class TableChart extends BaseChart { | ||
|
||
_parseData() { | ||
const data = super._parseData(); | ||
|
||
const titles = getTitles(this._dataHandlingProps.values); | ||
|
||
return [ | ||
titles.map(label => ({ label })), | ||
...data.map(row => this._parseRow(row)) | ||
]; | ||
} | ||
|
||
_parseRow(row) { | ||
return getValues(row, this._dataHandlingProps.values); | ||
} | ||
} | ||
|
||
module.exports = TableChart; |
Oops, something went wrong.
🤔 Viendo como se requiere después, llamaría esta clase
BaseChart