I am working on creating a simplified abstraction using Google charts. I have implemented a chartservice that will act as the abstraction layer, providing options and data-source while handling the rest (data retrieved from a REST API).
Below is the existing code snippet, which currently only caters to one specific case:
createCombo(comboBarLabels: String[], comboBarTypes: String[], options: any, element: any) {
this.overviewService.getOverviewAggBarData().pipe(first()).subscribe(comboRequest => {
for (const index of Object.keys(comboRequest.comboData)) {
comboRequest.comboData[index].unshift(comboBarLabels[index]);
}
const data_array = [comboBarTypes, comboRequest.comboData[0],
comboRequest.comboData[1], comboRequest.comboData[2]];
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(() => {
const data = ChartService.createDataTable(data_array);
const chart = new google.visualization.ComboChart(element);
chart.draw(data, options);
});
});
}
My goal is to replace
this.overviewService.getOverviewAggBarData()
with a conditional function, somewhat like what can be done in Python:
def foo(a, b):
return a + b
a = foo
print(a(1, 2))
Here is the desired pseudo-code:
createCombo(comboBarLabels: String[], comboBarTypes: String[], options: any, element: any, source: any) {
if (source == "OverviewAggBar"){
get_data = this.overviewService.getOverviewAggBarData;
} else {
get_data = this.overviewService.getOverviewPieData;
}
get_data().pipe(first()).subscribe(comboRequest => {
for (const index of Object.keys(comboRequest.comboData)) {
comboRequest.comboData[index].unshift(comboBarLabels[index]);
}
const data_array = [comboBarTypes, comboRequest.comboData[0],
comboRequest.comboData[1], comboRequest.comboData[2]];
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(() => {
const data = ChartService.createDataTable(data_array);
const chart = new google.visualization.ComboChart(element);
chart.draw(data, options);
});
});
}
The objective behind this effort is to simplify the function call process. Abstracting this part away will enable us to create an even more versatile function. Any alternative solutions to achieve the same outcome are highly appreciated!
Issue resolved, presenting the updated code:
createCombo(comboBarLabels: String[], comboBarTypes: String[], options: any, element: any, source: string) {
let getData: any;
if (source === 'getAggData') {
getData = this.overviewService.getOverviewAggBarData.bind(this);
} else {
getData = this.overviewService.getOverviewPieData.bind(this);
}
getData().pipe(first()).subscribe(comboRequest => {
const data_array = [comboBarTypes];
for (const index of Object.keys(comboRequest.comboData)) {
comboRequest.comboData[index].unshift(comboBarLabels[index]);
data_array.push(comboRequest.comboData[index]);
}
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(() => {
const data = ChartService.createDataTable(data_array);
const chart = new google.visualization.ComboChart(element);
chart.draw(data, options);
});
});
}