From 3a41cd071fbe087c228b86dffd821723ff1b844b Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 1 Nov 2017 19:11:47 +0100 Subject: [PATCH] refactor(charts.js): use key value map for handling chart types --- src/scripts/charts.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/scripts/charts.js b/src/scripts/charts.js index 6e1fdf8..75a7e13 100644 --- a/src/scripts/charts.js +++ b/src/scripts/charts.js @@ -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]); } }