diff --git a/src/js/charts/BaseChart.js b/src/js/charts/BaseChart.js index 6c266e7..3452fe8 100644 --- a/src/js/charts/BaseChart.js +++ b/src/js/charts/BaseChart.js @@ -6,9 +6,12 @@ import { BASE_MEASURES, getExtraHeight, getExtraWidth, getTopOffset, getLeftOffs import { getColor, isValidColor } from '../utils/colors'; import { runSMILAnimation } from '../utils/animation'; import { downloadFile, prepareForExport } from '../utils/export'; +import { deepClone } from '../utils/helpers' export default class BaseChart { constructor(parent, options) { + // deepclone options to avoid making changes to orignal object + options = deepClone(options) this.parent = typeof parent === 'string' ? document.querySelector(parent) diff --git a/src/js/utils/helpers.js b/src/js/utils/helpers.js index 592760d..d1493f7 100644 --- a/src/js/utils/helpers.js +++ b/src/js/utils/helpers.js @@ -115,3 +115,29 @@ export function round(d) { // https://www.jacklmoore.com/notes/rounding-in-javascript/ return Number(Math.round(d + 'e4') + 'e-4'); } + +/** + * Creates a deep clone of an object + * @param {Object} candidate Any Object + */ + export function deepClone(candidate) { + let cloned, value, key; + + if (candidate instanceof Date) { + return new Date(candidate.getTime()); + } + + if (typeof candidate !== "object" || candidate === null) { + return candidate; + } + + cloned = Array.isArray(candidate) ? [] : {}; + + for (key in candidate) { + value = candidate[key]; + + cloned[key] = deepClone(value); + } + + return cloned; + } \ No newline at end of file