refactor(charts.js): use key value map for handling chart types

This commit is contained in:
Moritz 2017-11-01 19:11:47 +01:00 committed by GitHub
parent 38a9428059
commit 3a41cd071f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,15 +14,24 @@ import Heatmap from './charts/Heatmap';
// );
// }
const chartTypes = {
line: LineChart,
bar: BarChart,
scatter: ScatterChart,
percentage: PercentageChart,
heatmap: Heatmap
};
function getChartByType(chartType = 'line', options) {
if (!chartTypes[chartType]) {
return new LineChart(options);
}
return new chartTypes[chartType](options);
}
export default class Chart {
constructor(args) {
switch (args.type) {
case 'line': return new LineChart(arguments[0]);
case 'bar': return new BarChart(arguments[0]);
case 'scatter': return new ScatterChart(arguments[0]);
case 'percentage': return new PercentageChart(arguments[0]);
case 'heatmap': return new Heatmap(arguments[0]);
default: return new LineChart(arguments[0]);
}
return getChartByType(args.type, arguments[0]);
}
}