mirror of
				https://github.com/KevinMidboe/vue-chartjs.git
				synced 2025-10-29 18:00:20 +00:00 
			
		
		
		
	Merge pull request #305 from west-soft-development/extract-identical-code
Refactor: Extract identical code
This commit is contained in:
		| @@ -15,5 +15,8 @@ module.exports = { | |||||||
|     'arrow-parens': 0, |     'arrow-parens': 0, | ||||||
|     // allow debugger during development |     // allow debugger during development | ||||||
|     'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 |     'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 | ||||||
|  |   }, | ||||||
|  |   "globals": { | ||||||
|  |     "LIB_VERSION": true | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,8 +1,10 @@ | |||||||
| 'use strict' | 'use strict' | ||||||
|  | const webpack = require('webpack') | ||||||
| const path = require('path') | const path = require('path') | ||||||
| const utils = require('./utils') | const utils = require('./utils') | ||||||
| const config = require('../config') | const config = require('../config') | ||||||
| const vueLoaderConfig = require('./vue-loader.conf') | const vueLoaderConfig = require('./vue-loader.conf') | ||||||
|  | const npmCfg = require('../package.json') | ||||||
|  |  | ||||||
| function resolve (dir) { | function resolve (dir) { | ||||||
|   return path.join(__dirname, '..', dir) |   return path.join(__dirname, '..', dir) | ||||||
| @@ -72,5 +74,10 @@ module.exports = { | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     ] |     ] | ||||||
|   } |   }, | ||||||
|  |   plugins: [ | ||||||
|  |     new webpack.DefinePlugin({ | ||||||
|  |       LIB_VERSION: JSON.stringify(npmCfg.version) | ||||||
|  |     }) | ||||||
|  |   ] | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										247
									
								
								dist/vue-chartjs.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										247
									
								
								dist/vue-chartjs.js
									
									
									
									
										vendored
									
									
								
							| @@ -12,7 +12,7 @@ | |||||||
| 		exports["VueChartJs"] = factory(require("chart.js")); | 		exports["VueChartJs"] = factory(require("chart.js")); | ||||||
| 	else | 	else | ||||||
| 		root["VueChartJs"] = factory(root["Chart"]); | 		root["VueChartJs"] = factory(root["Chart"]); | ||||||
| })(typeof self !== 'undefined' ? self : this, function(__WEBPACK_EXTERNAL_MODULE_4__) { | })(typeof self !== 'undefined' ? self : this, function(__WEBPACK_EXTERNAL_MODULE_1__) { | ||||||
| return /******/ (function(modules) { // webpackBootstrap | return /******/ (function(modules) { // webpackBootstrap | ||||||
| /******/ 	// The module cache | /******/ 	// The module cache | ||||||
| /******/ 	var installedModules = {}; | /******/ 	var installedModules = {}; | ||||||
| @@ -85,27 +85,89 @@ return /******/ (function(modules) { // webpackBootstrap | |||||||
| "use strict"; | "use strict"; | ||||||
| Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | ||||||
|  |  | ||||||
| // EXTERNAL MODULE: ./src/mixins/reactiveData.js |  | ||||||
| var reactiveData = __webpack_require__(1); |  | ||||||
| var reactiveData_default = /*#__PURE__*/__webpack_require__.n(reactiveData); |  | ||||||
|  |  | ||||||
| // EXTERNAL MODULE: ./src/mixins/reactiveProp.js |  | ||||||
| var reactiveProp = __webpack_require__(2); |  | ||||||
| var reactiveProp_default = /*#__PURE__*/__webpack_require__.n(reactiveProp); |  | ||||||
|  |  | ||||||
| // CONCATENATED MODULE: ./src/mixins/index.js | // CONCATENATED MODULE: ./src/mixins/index.js | ||||||
|  | function dataHandler(newData, oldData) { | ||||||
|  |   if (oldData) { | ||||||
|  |     var chart = this.$data._chart; | ||||||
|  |     var newDatasetLabels = newData.datasets.map(function (dataset) { | ||||||
|  |       return dataset.label; | ||||||
|  |     }); | ||||||
|  |     var oldDatasetLabels = oldData.datasets.map(function (dataset) { | ||||||
|  |       return dataset.label; | ||||||
|  |     }); | ||||||
|  |     var oldLabels = JSON.stringify(oldDatasetLabels); | ||||||
|  |     var newLabels = JSON.stringify(newDatasetLabels); | ||||||
|  |  | ||||||
|  |     if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { | ||||||
|  |       newData.datasets.forEach(function (dataset, i) { | ||||||
|  |         var oldDatasetKeys = Object.keys(oldData.datasets[i]); | ||||||
|  |         var newDatasetKeys = Object.keys(dataset); | ||||||
|  |         var deletionKeys = oldDatasetKeys.filter(function (key) { | ||||||
|  |           return key !== '_meta' && newDatasetKeys.indexOf(key) === -1; | ||||||
|  |         }); | ||||||
|  |         deletionKeys.forEach(function (deletionKey) { | ||||||
|  |           delete chart.data.datasets[i][deletionKey]; | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         for (var attribute in dataset) { | ||||||
|  |           if (dataset.hasOwnProperty(attribute)) { | ||||||
|  |             chart.data.datasets[i][attribute] = dataset[attribute]; | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |       }); | ||||||
|  |  | ||||||
|  |       if (newData.hasOwnProperty('labels')) { | ||||||
|  |         chart.data.labels = newData.labels; | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       if (newData.hasOwnProperty('xLabels')) { | ||||||
|  |         chart.data.xLabels = newData.xLabels; | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       if (newData.hasOwnProperty('yLabels')) { | ||||||
|  |         chart.data.yLabels = newData.yLabels; | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       chart.update(); | ||||||
|  |     } else { | ||||||
|  |       chart.destroy(); | ||||||
|  |       this.renderChart(this.chartData, this.options); | ||||||
|  |     } | ||||||
|  |   } else { | ||||||
|  |     if (this.$data._chart) { | ||||||
|  |       this.$data._chart.destroy(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     this.renderChart(this.chartData, this.options); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var reactiveData = { | ||||||
|  |   data: function data() { | ||||||
|  |     return { | ||||||
|  |       chartData: null | ||||||
|  |     }; | ||||||
|  |   }, | ||||||
|  |   watch: { | ||||||
|  |     'chartData': dataHandler | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  | var reactiveProp = { | ||||||
|  |   props: { | ||||||
|  |     chartData: { | ||||||
|  |       required: true | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   watch: { | ||||||
|  |     'chartData': dataHandler | ||||||
|  |   } | ||||||
|  | }; | ||||||
| /* harmony default export */ var mixins = ({ | /* harmony default export */ var mixins = ({ | ||||||
|   reactiveData: reactiveData_default.a, |   reactiveData: reactiveData, | ||||||
|   reactiveProp: reactiveProp_default.a |   reactiveProp: reactiveProp | ||||||
| }); | }); | ||||||
| // EXTERNAL MODULE: ./package.json |  | ||||||
| var package_0 = __webpack_require__(3); |  | ||||||
| var package_default = /*#__PURE__*/__webpack_require__.n(package_0); |  | ||||||
|  |  | ||||||
| // EXTERNAL MODULE: external {"root":"Chart","commonjs":"chart.js","commonjs2":"chart.js","amd":"chart.js"} | // EXTERNAL MODULE: external {"root":"Chart","commonjs":"chart.js","commonjs2":"chart.js","amd":"chart.js"} | ||||||
| var external___root___Chart___commonjs___chart_js___commonjs2___chart_js___amd___chart_js__ = __webpack_require__(4); | var external___root___Chart___commonjs___chart_js___commonjs2___chart_js___amd___chart_js__ = __webpack_require__(1); | ||||||
| var external___root___Chart___commonjs___chart_js___commonjs2___chart_js___amd___chart_js___default = /*#__PURE__*/__webpack_require__.n(external___root___Chart___commonjs___chart_js___commonjs2___chart_js___amd___chart_js__); | var external___root___Chart___commonjs___chart_js___commonjs2___chart_js___amd___chart_js___default = /*#__PURE__*/__webpack_require__.n(external___root___Chart___commonjs___chart_js___commonjs2___chart_js___amd___chart_js__); | ||||||
|  |  | ||||||
| // CONCATENATED MODULE: ./src/BaseCharts.js | // CONCATENATED MODULE: ./src/BaseCharts.js | ||||||
| @@ -214,9 +276,8 @@ var Scatter = generateChart('scatter-chart', 'scatter'); | |||||||
| /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "mixins", function() { return mixins; }); | /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "mixins", function() { return mixins; }); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| var VueCharts = { | var VueCharts = { | ||||||
|   version: package_default.a.version, |   version: LIB_VERSION, | ||||||
|   Bar: Bar, |   Bar: Bar, | ||||||
|   HorizontalBar: HorizontalBar, |   HorizontalBar: HorizontalBar, | ||||||
|   Doughnut: Doughnut, |   Doughnut: Doughnut, | ||||||
| @@ -235,155 +296,7 @@ var VueCharts = { | |||||||
| /* 1 */ | /* 1 */ | ||||||
| /***/ (function(module, exports) { | /***/ (function(module, exports) { | ||||||
|  |  | ||||||
| module.exports = { | module.exports = __WEBPACK_EXTERNAL_MODULE_1__; | ||||||
|   data: function data() { |  | ||||||
|     return { |  | ||||||
|       chartData: null |  | ||||||
|     }; |  | ||||||
|   }, |  | ||||||
|   watch: { |  | ||||||
|     'chartData': { |  | ||||||
|       handler: function handler(newData, oldData) { |  | ||||||
|         if (oldData) { |  | ||||||
|           var chart = this.$data._chart; |  | ||||||
|           var newDatasetLabels = newData.datasets.map(function (dataset) { |  | ||||||
|             return dataset.label; |  | ||||||
|           }); |  | ||||||
|           var oldDatasetLabels = oldData.datasets.map(function (dataset) { |  | ||||||
|             return dataset.label; |  | ||||||
|           }); |  | ||||||
|           var oldLabels = JSON.stringify(oldDatasetLabels); |  | ||||||
|           var newLabels = JSON.stringify(newDatasetLabels); |  | ||||||
|  |  | ||||||
|           if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { |  | ||||||
|             newData.datasets.forEach(function (dataset, i) { |  | ||||||
|               var oldDatasetKeys = Object.keys(oldData.datasets[i]); |  | ||||||
|               var newDatasetKeys = Object.keys(dataset); |  | ||||||
|               var deletionKeys = oldDatasetKeys.filter(function (key) { |  | ||||||
|                 return key !== '_meta' && newDatasetKeys.indexOf(key) === -1; |  | ||||||
|               }); |  | ||||||
|               deletionKeys.forEach(function (deletionKey) { |  | ||||||
|                 delete chart.data.datasets[i][deletionKey]; |  | ||||||
|               }); |  | ||||||
|  |  | ||||||
|               for (var attribute in dataset) { |  | ||||||
|                 if (dataset.hasOwnProperty(attribute)) { |  | ||||||
|                   chart.data.datasets[i][attribute] = dataset[attribute]; |  | ||||||
|                 } |  | ||||||
|               } |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('labels')) { |  | ||||||
|               chart.data.labels = newData.labels; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('xLabels')) { |  | ||||||
|               chart.data.xLabels = newData.xLabels; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('yLabels')) { |  | ||||||
|               chart.data.yLabels = newData.yLabels; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             chart.update(); |  | ||||||
|           } else { |  | ||||||
|             chart.destroy(); |  | ||||||
|             this.renderChart(this.chartData, this.options); |  | ||||||
|           } |  | ||||||
|         } else { |  | ||||||
|           if (this.$data._chart) { |  | ||||||
|             this.$data._chart.destroy(); |  | ||||||
|           } |  | ||||||
|  |  | ||||||
|           this.renderChart(this.chartData, this.options); |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| /***/ }), |  | ||||||
| /* 2 */ |  | ||||||
| /***/ (function(module, exports) { |  | ||||||
|  |  | ||||||
| module.exports = { |  | ||||||
|   props: { |  | ||||||
|     chartData: { |  | ||||||
|       required: true |  | ||||||
|     } |  | ||||||
|   }, |  | ||||||
|   watch: { |  | ||||||
|     'chartData': { |  | ||||||
|       handler: function handler(newData, oldData) { |  | ||||||
|         if (oldData) { |  | ||||||
|           var chart = this.$data._chart; |  | ||||||
|           var newDatasetLabels = newData.datasets.map(function (dataset) { |  | ||||||
|             return dataset.label; |  | ||||||
|           }); |  | ||||||
|           var oldDatasetLabels = oldData.datasets.map(function (dataset) { |  | ||||||
|             return dataset.label; |  | ||||||
|           }); |  | ||||||
|           var oldLabels = JSON.stringify(oldDatasetLabels); |  | ||||||
|           var newLabels = JSON.stringify(newDatasetLabels); |  | ||||||
|  |  | ||||||
|           if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { |  | ||||||
|             newData.datasets.forEach(function (dataset, i) { |  | ||||||
|               var oldDatasetKeys = Object.keys(oldData.datasets[i]); |  | ||||||
|               var newDatasetKeys = Object.keys(dataset); |  | ||||||
|               var deletionKeys = oldDatasetKeys.filter(function (key) { |  | ||||||
|                 return key !== '_meta' && newDatasetKeys.indexOf(key) === -1; |  | ||||||
|               }); |  | ||||||
|               deletionKeys.forEach(function (deletionKey) { |  | ||||||
|                 delete chart.data.datasets[i][deletionKey]; |  | ||||||
|               }); |  | ||||||
|  |  | ||||||
|               for (var attribute in dataset) { |  | ||||||
|                 if (dataset.hasOwnProperty(attribute)) { |  | ||||||
|                   chart.data.datasets[i][attribute] = dataset[attribute]; |  | ||||||
|                 } |  | ||||||
|               } |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('labels')) { |  | ||||||
|               chart.data.labels = newData.labels; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('xLabels')) { |  | ||||||
|               chart.data.xLabels = newData.xLabels; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('yLabels')) { |  | ||||||
|               chart.data.yLabels = newData.yLabels; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             chart.update(); |  | ||||||
|           } else { |  | ||||||
|             chart.destroy(); |  | ||||||
|             this.renderChart(this.chartData, this.options); |  | ||||||
|           } |  | ||||||
|         } else { |  | ||||||
|           if (this.$data._chart) { |  | ||||||
|             this.$data._chart.destroy(); |  | ||||||
|           } |  | ||||||
|  |  | ||||||
|           this.renderChart(this.chartData, this.options); |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| /***/ }), |  | ||||||
| /* 3 */ |  | ||||||
| /***/ (function(module, exports) { |  | ||||||
|  |  | ||||||
| module.exports = {"name":"vue-chartjs","version":"3.1.1","description":"Vue.js wrapper for chart.js for creating beautiful charts.","author":"Jakub Juszczak <jakub@posteo.de>","homepage":"http://vue-chartjs.org","license":"MIT","contributors":[{"name":"Thorsten Lünborg","web":"https://github.com/LinusBorg"},{"name":"Juan Carlos Alonso","web":"https://github.com/jcalonso"}],"maintainers":[{"name":"Jakub Juszczak","email":"jakub@posteo.de","web":"http://www.jakubjuszczak.de"}],"repository":{"type":"git","url":"git+ssh://git@github.com:apertureless/vue-chartjs.git"},"bugs":{"url":"https://github.com/apertureless/vue-chartjs/issues"},"keywords":["ChartJs","Vue","Visualisation","Wrapper","Charts"],"main":"dist/vue-chartjs.js","unpkg":"dist/vue-chartjs.min.js","module":"es/index.js","jsnext:main":"es/index.js","files":["src","dist","es"],"scripts":{"dev":"node build/dev-server.js","build":"yarn run release && yarn run build:es","build:es":"cross-env BABEL_ENV=es babel src --out-dir es","unit":"karma start test/unit/karma.conf.js --single-run","e2e":"node test/e2e/runner.js","test":"npm run unit","lint":"eslint --ext .js,.vue src test/unit/specs test/e2e/specs","release":"webpack --progress --hide-modules --config  ./build/webpack.release.js && cross-env NODE_ENV=production webpack --progress --hide-modules --config  ./build/webpack.release.min.js","prepublishOnly":"yarn run lint && yarn run test && yarn run build"},"dependencies":{},"peerDependencies":{"chart.js":"2.7.x"},"devDependencies":{"@babel/cli":"^7.0.0-beta.31","@babel/core":"^7.0.0-beta.31","@babel/preset-env":"^7.0.0-beta.31","@babel/preset-stage-2":"^7.0.0-beta.31","babel-loader":"8.0.0-beta.0","chai":"^3.5.0","chart.js":"2.7.0","chromedriver":"^2.28.0","connect-history-api-fallback":"^1.1.0","cross-env":"^5.1.1","cross-spawn":"^5.1.0","css-loader":"^0.28.0","eslint":"^3.19.0","eslint-config-standard":"^10.2.1","eslint-friendly-formatter":"^2.0.7","eslint-loader":"^1.7.1","eslint-plugin-html":"^2.0.1","eslint-plugin-import":"^2.2.0","eslint-plugin-node":"^4.2.2","eslint-plugin-promise":"^3.5.0","eslint-plugin-standard":"^3.0.1","eventsource-polyfill":"^0.9.6","express":"^4.15.2","extract-text-webpack-plugin":"^3.0.1","file-loader":"^0.10.1","friendly-errors-webpack-plugin":"^1.6.1","function-bind":"^1.0.2","html-webpack-plugin":"^2.28.0","http-proxy-middleware":"^0.17.4","inject-loader":"^3.0.0","isparta":"^4.0.0","jasmine-core":"^2.5.2","json-loader":"^0.5.4","karma":"^1.5.0","karma-coverage":"^1.1.1","karma-jasmine":"^1.0.2","karma-mocha":"^1.2.0","karma-phantomjs-launcher":"^1.0.4","karma-phantomjs-shim":"^1.4.0","karma-sinon-chai":"^1.2.0","karma-sourcemap-loader":"^0.3.7","karma-spec-reporter":"0.0.30","karma-webpack":"2","lolex":"^1.6.0","mocha":"^3.1.0","opn":"^5.1.0","ora":"^1.2.0","phantomjs-prebuilt":"^2.1.13","portfinder":"^1.0.13","selenium-server":"^3.3.1","shelljs":"^0.7.7","sinon":"^2.1.0","sinon-chai":"^2.9.0","url-loader":"^0.5.8","vue":"2.5.2","vue-hot-reload-api":"2.1.0","vue-html-loader":"^1.2.4","vue-loader":"^13.3.0","vue-style-loader":"3.0.1","vue-template-compiler":"2.5.2","webpack":"^3.7.1","webpack-dev-middleware":"^1.10.1","webpack-hot-middleware":"^2.17.1","webpack-merge":"^4.1.0"},"engines":{"node":">=6.9.0","npm":">= 3.0.0"},"browserify":{"transform":["babelify"]},"greenkeeper":{"ignore":["extract-text-webpack-plugin","karma-webpack","webpack","webpack-merge"]}} |  | ||||||
|  |  | ||||||
| /***/ }), |  | ||||||
| /* 4 */ |  | ||||||
| /***/ (function(module, exports) { |  | ||||||
|  |  | ||||||
| module.exports = __WEBPACK_EXTERNAL_MODULE_4__; |  | ||||||
|  |  | ||||||
| /***/ }) | /***/ }) | ||||||
| /******/ ]); | /******/ ]); | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								dist/vue-chartjs.min.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/vue-chartjs.min.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -1,5 +1,5 @@ | |||||||
| import { Bar } from '../BaseCharts' | import { Bar } from '../BaseCharts' | ||||||
| import reactiveData from '../mixins/reactiveData' | import { reactiveData } from '../mixins' | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|   extends: Bar, |   extends: Bar, | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { Bar } from '../BaseCharts' | import { Bar } from '../BaseCharts' | ||||||
| import reactiveProp from '../mixins/reactiveProp' | import { reactiveProp } from '../mixins' | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|   extends: Bar, |   extends: Bar, | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import mixins from './mixins/index.js' | import mixins from './mixins/index.js' | ||||||
| import npmCfg from '../package.json' |  | ||||||
| import { | import { | ||||||
|   Bar, |   Bar, | ||||||
|   HorizontalBar, |   HorizontalBar, | ||||||
| @@ -13,7 +13,7 @@ import { | |||||||
| } from './BaseCharts' | } from './BaseCharts' | ||||||
|  |  | ||||||
| const VueCharts = { | const VueCharts = { | ||||||
|   version: npmCfg.version, |   version: LIB_VERSION, | ||||||
|   Bar, |   Bar, | ||||||
|   HorizontalBar, |   HorizontalBar, | ||||||
|   Doughnut, |   Doughnut, | ||||||
|   | |||||||
| @@ -1,5 +1,89 @@ | |||||||
| import reactiveData from './reactiveData.js' | function dataHandler (newData, oldData) { | ||||||
| import reactiveProp from './reactiveProp.js' |   if (oldData) { | ||||||
|  |     let chart = this.$data._chart | ||||||
|  |  | ||||||
|  |     // Get new and old DataSet Labels | ||||||
|  |     let newDatasetLabels = newData.datasets.map((dataset) => { | ||||||
|  |       return dataset.label | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     let oldDatasetLabels = oldData.datasets.map((dataset) => { | ||||||
|  |       return dataset.label | ||||||
|  |     }) | ||||||
|  |  | ||||||
|  |     // Stringify 'em for easier compare | ||||||
|  |     const oldLabels = JSON.stringify(oldDatasetLabels) | ||||||
|  |     const newLabels = JSON.stringify(newDatasetLabels) | ||||||
|  |  | ||||||
|  |     // Check if Labels are equal and if dataset length is equal | ||||||
|  |     if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { | ||||||
|  |       newData.datasets.forEach((dataset, i) => { | ||||||
|  |         // Get new and old dataset keys | ||||||
|  |         const oldDatasetKeys = Object.keys(oldData.datasets[i]) | ||||||
|  |         const newDatasetKeys = Object.keys(dataset) | ||||||
|  |  | ||||||
|  |         // Get keys that aren't present in the new data | ||||||
|  |         const deletionKeys = oldDatasetKeys.filter((key) => { | ||||||
|  |           return key !== '_meta' && newDatasetKeys.indexOf(key) === -1 | ||||||
|  |         }) | ||||||
|  |  | ||||||
|  |         // Remove outdated key-value pairs | ||||||
|  |         deletionKeys.forEach((deletionKey) => { | ||||||
|  |           delete chart.data.datasets[i][deletionKey] | ||||||
|  |         }) | ||||||
|  |  | ||||||
|  |         // Update attributes individually to avoid re-rendering the entire chart | ||||||
|  |         for (const attribute in dataset) { | ||||||
|  |           if (dataset.hasOwnProperty(attribute)) { | ||||||
|  |             chart.data.datasets[i][attribute] = dataset[attribute] | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |       }) | ||||||
|  |  | ||||||
|  |       if (newData.hasOwnProperty('labels')) { | ||||||
|  |         chart.data.labels = newData.labels | ||||||
|  |       } | ||||||
|  |       if (newData.hasOwnProperty('xLabels')) { | ||||||
|  |         chart.data.xLabels = newData.xLabels | ||||||
|  |       } | ||||||
|  |       if (newData.hasOwnProperty('yLabels')) { | ||||||
|  |         chart.data.yLabels = newData.yLabels | ||||||
|  |       } | ||||||
|  |       chart.update() | ||||||
|  |     } else { | ||||||
|  |       chart.destroy() | ||||||
|  |       this.renderChart(this.chartData, this.options) | ||||||
|  |     } | ||||||
|  |   } else { | ||||||
|  |     if (this.$data._chart) { | ||||||
|  |       this.$data._chart.destroy() | ||||||
|  |     } | ||||||
|  |     this.renderChart(this.chartData, this.options) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export const reactiveData = { | ||||||
|  |   data () { | ||||||
|  |     return { | ||||||
|  |       chartData: null | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   watch: { | ||||||
|  |     'chartData': dataHandler | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export const reactiveProp = { | ||||||
|  |   props: { | ||||||
|  |     chartData: { | ||||||
|  |       required: true | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   watch: { | ||||||
|  |     'chartData': dataHandler | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|   reactiveData, |   reactiveData, | ||||||
|   | |||||||
| @@ -1,74 +0,0 @@ | |||||||
| module.exports = { |  | ||||||
|   data () { |  | ||||||
|     return { |  | ||||||
|       chartData: null |  | ||||||
|     } |  | ||||||
|   }, |  | ||||||
|   watch: { |  | ||||||
|     'chartData': { |  | ||||||
|       handler (newData, oldData) { |  | ||||||
|         if (oldData) { |  | ||||||
|           let chart = this.$data._chart |  | ||||||
|  |  | ||||||
|           // Get new and old DataSet Labels |  | ||||||
|           let newDatasetLabels = newData.datasets.map((dataset) => { |  | ||||||
|             return dataset.label |  | ||||||
|           }) |  | ||||||
|  |  | ||||||
|           let oldDatasetLabels = oldData.datasets.map((dataset) => { |  | ||||||
|             return dataset.label |  | ||||||
|           }) |  | ||||||
|  |  | ||||||
|           // Stringify 'em for easier compare |  | ||||||
|           const oldLabels = JSON.stringify(oldDatasetLabels) |  | ||||||
|           const newLabels = JSON.stringify(newDatasetLabels) |  | ||||||
|  |  | ||||||
|           // Check if Labels are equal and if dataset length is equal |  | ||||||
|           if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { |  | ||||||
|             newData.datasets.forEach((dataset, i) => { |  | ||||||
|               // Get new and old dataset keys |  | ||||||
|               const oldDatasetKeys = Object.keys(oldData.datasets[i]) |  | ||||||
|               const newDatasetKeys = Object.keys(dataset) |  | ||||||
|  |  | ||||||
|               // Get keys that aren't present in the new data |  | ||||||
|               const deletionKeys = oldDatasetKeys.filter((key) => { |  | ||||||
|                 return key !== '_meta' && newDatasetKeys.indexOf(key) === -1 |  | ||||||
|               }) |  | ||||||
|  |  | ||||||
|               // Remove outdated key-value pairs |  | ||||||
|               deletionKeys.forEach((deletionKey) => { |  | ||||||
|                 delete chart.data.datasets[i][deletionKey] |  | ||||||
|               }) |  | ||||||
|  |  | ||||||
|               // Update attributes individually to avoid re-rendering the entire chart |  | ||||||
|               for (const attribute in dataset) { |  | ||||||
|                 if (dataset.hasOwnProperty(attribute)) { |  | ||||||
|                   chart.data.datasets[i][attribute] = dataset[attribute] |  | ||||||
|                 } |  | ||||||
|               } |  | ||||||
|             }) |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('labels')) { |  | ||||||
|               chart.data.labels = newData.labels |  | ||||||
|             } |  | ||||||
|             if (newData.hasOwnProperty('xLabels')) { |  | ||||||
|               chart.data.xLabels = newData.xLabels |  | ||||||
|             } |  | ||||||
|             if (newData.hasOwnProperty('yLabels')) { |  | ||||||
|               chart.data.yLabels = newData.yLabels |  | ||||||
|             } |  | ||||||
|             chart.update() |  | ||||||
|           } else { |  | ||||||
|             chart.destroy() |  | ||||||
|             this.renderChart(this.chartData, this.options) |  | ||||||
|           } |  | ||||||
|         } else { |  | ||||||
|           if (this.$data._chart) { |  | ||||||
|             this.$data._chart.destroy() |  | ||||||
|           } |  | ||||||
|           this.renderChart(this.chartData, this.options) |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @@ -1,74 +0,0 @@ | |||||||
| module.exports = { |  | ||||||
|   props: { |  | ||||||
|     chartData: { |  | ||||||
|       required: true |  | ||||||
|     } |  | ||||||
|   }, |  | ||||||
|   watch: { |  | ||||||
|     'chartData': { |  | ||||||
|       handler (newData, oldData) { |  | ||||||
|         if (oldData) { |  | ||||||
|           let chart = this.$data._chart |  | ||||||
|  |  | ||||||
|           // Get new and old DataSet Labels |  | ||||||
|           let newDatasetLabels = newData.datasets.map((dataset) => { |  | ||||||
|             return dataset.label |  | ||||||
|           }) |  | ||||||
|  |  | ||||||
|           let oldDatasetLabels = oldData.datasets.map((dataset) => { |  | ||||||
|             return dataset.label |  | ||||||
|           }) |  | ||||||
|  |  | ||||||
|           // Stringify 'em for easier compare |  | ||||||
|           const oldLabels = JSON.stringify(oldDatasetLabels) |  | ||||||
|           const newLabels = JSON.stringify(newDatasetLabels) |  | ||||||
|  |  | ||||||
|           // Check if Labels are equal and if dataset length is equal |  | ||||||
|           if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { |  | ||||||
|             newData.datasets.forEach((dataset, i) => { |  | ||||||
|               // Get new and old dataset keys |  | ||||||
|               const oldDatasetKeys = Object.keys(oldData.datasets[i]) |  | ||||||
|               const newDatasetKeys = Object.keys(dataset) |  | ||||||
|  |  | ||||||
|               // Get keys that aren't present in the new data |  | ||||||
|               const deletionKeys = oldDatasetKeys.filter((key) => { |  | ||||||
|                 return key !== '_meta' && newDatasetKeys.indexOf(key) === -1 |  | ||||||
|               }) |  | ||||||
|  |  | ||||||
|               // Remove outdated key-value pairs |  | ||||||
|               deletionKeys.forEach((deletionKey) => { |  | ||||||
|                 delete chart.data.datasets[i][deletionKey] |  | ||||||
|               }) |  | ||||||
|  |  | ||||||
|               // Update attributes individually to avoid re-rendering the entire chart |  | ||||||
|               for (const attribute in dataset) { |  | ||||||
|                 if (dataset.hasOwnProperty(attribute)) { |  | ||||||
|                   chart.data.datasets[i][attribute] = dataset[attribute] |  | ||||||
|                 } |  | ||||||
|               } |  | ||||||
|             }) |  | ||||||
|  |  | ||||||
|             if (newData.hasOwnProperty('labels')) { |  | ||||||
|               chart.data.labels = newData.labels |  | ||||||
|             } |  | ||||||
|             if (newData.hasOwnProperty('xLabels')) { |  | ||||||
|               chart.data.xLabels = newData.xLabels |  | ||||||
|             } |  | ||||||
|             if (newData.hasOwnProperty('yLabels')) { |  | ||||||
|               chart.data.yLabels = newData.yLabels |  | ||||||
|             } |  | ||||||
|             chart.update() |  | ||||||
|           } else { |  | ||||||
|             chart.destroy() |  | ||||||
|             this.renderChart(this.chartData, this.options) |  | ||||||
|           } |  | ||||||
|         } else { |  | ||||||
|           if (this.$data._chart) { |  | ||||||
|             this.$data._chart.destroy() |  | ||||||
|           } |  | ||||||
|           this.renderChart(this.chartData, this.options) |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user