mirror of
				https://github.com/KevinMidboe/vue-chartjs.git
				synced 2025-10-29 18:00:20 +00:00 
			
		
		
		
	✨ Add scatter chart
This commit is contained in:
		
							
								
								
									
										69
									
								
								src/BaseCharts/Scatter.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/BaseCharts/Scatter.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | import Vue from 'vue' | ||||||
|  | import Chart from 'chart.js' | ||||||
|  | import { mergeOptions } from '../helpers/options' | ||||||
|  |  | ||||||
|  | export default Vue.extend({ | ||||||
|  |   render: function (createElement) { | ||||||
|  |     return createElement( | ||||||
|  |       'div', | ||||||
|  |       [ | ||||||
|  |         createElement( | ||||||
|  |           'canvas', { | ||||||
|  |             attrs: { | ||||||
|  |               id: this.chartId, | ||||||
|  |               width: this.width, | ||||||
|  |               height: this.height | ||||||
|  |             }, | ||||||
|  |             ref: 'canvas' | ||||||
|  |           } | ||||||
|  |         ) | ||||||
|  |       ] | ||||||
|  |     ) | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   props: { | ||||||
|  |     chartId: { | ||||||
|  |       default: 'scatter-chart', | ||||||
|  |       type: String | ||||||
|  |     }, | ||||||
|  |     width: { | ||||||
|  |       default: 400, | ||||||
|  |       type: Number | ||||||
|  |     }, | ||||||
|  |     height: { | ||||||
|  |       default: 400, | ||||||
|  |       type: Number | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   data () { | ||||||
|  |     return { | ||||||
|  |       defaultOptions: { | ||||||
|  |         scales: { | ||||||
|  |           xAxes: [{ | ||||||
|  |             type: 'linear', | ||||||
|  |             position: 'bottom' | ||||||
|  |           }] | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   methods: { | ||||||
|  |     renderChart (data, options) { | ||||||
|  |       let chartOptions = mergeOptions(this.defaultOptions, options) | ||||||
|  |  | ||||||
|  |       this._chart = new Chart( | ||||||
|  |         this.$refs.canvas.getContext('2d'), { | ||||||
|  |           type: 'scatter', | ||||||
|  |           data: data, | ||||||
|  |           options: chartOptions | ||||||
|  |         } | ||||||
|  |       ) | ||||||
|  |       this._chart.generateLegend() | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   beforeDestroy () { | ||||||
|  |     this._chart.destroy() | ||||||
|  |   } | ||||||
|  | }) | ||||||
| @@ -18,6 +18,8 @@ | |||||||
|     <polar-area-example></polar-area-example> |     <polar-area-example></polar-area-example> | ||||||
|     <h1 style="text-align:center;margin:40px 0;">Bubblechart</h1> |     <h1 style="text-align:center;margin:40px 0;">Bubblechart</h1> | ||||||
|     <bubble-example></bubble-example> |     <bubble-example></bubble-example> | ||||||
|  |     <h1 style="text-align:center;margin:40px 0;">Scatter Chart</h1> | ||||||
|  |     <scatter-example></scatter-example> | ||||||
|   </div> |   </div> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| @@ -31,6 +33,7 @@ | |||||||
|   import BubbleExample from './BubbleExample' |   import BubbleExample from './BubbleExample' | ||||||
|   import ReactiveExample from './ReactiveExample' |   import ReactiveExample from './ReactiveExample' | ||||||
|   import ReactivePropExample from './ReactivePropExample' |   import ReactivePropExample from './ReactivePropExample' | ||||||
|  |   import ScatterExample from './ScatterExample' | ||||||
|  |  | ||||||
|   export default { |   export default { | ||||||
|     components: { |     components: { | ||||||
| @@ -42,7 +45,8 @@ | |||||||
|       PolarAreaExample, |       PolarAreaExample, | ||||||
|       BubbleExample, |       BubbleExample, | ||||||
|       ReactiveExample, |       ReactiveExample, | ||||||
|       ReactivePropExample |       ReactivePropExample, | ||||||
|  |       ScatterExample | ||||||
|     }, |     }, | ||||||
|     data () { |     data () { | ||||||
|       return { |       return { | ||||||
|   | |||||||
							
								
								
									
										53
									
								
								src/examples/ScatterExample.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/examples/ScatterExample.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | |||||||
|  | import Scatter from '../BaseCharts/Scatter' | ||||||
|  |  | ||||||
|  | export default Scatter.extend({ | ||||||
|  |   mounted () { | ||||||
|  |     this.renderChart({ | ||||||
|  |       labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], | ||||||
|  |       datasets: [{ | ||||||
|  |         label: 'Scatter Dataset 1', | ||||||
|  |         fill: false, | ||||||
|  |         borderColor: '#f87979', | ||||||
|  |         backgroundColor: '#f87979', | ||||||
|  |         data: [{ | ||||||
|  |           x: -2, | ||||||
|  |           y: 4 | ||||||
|  |         }, { | ||||||
|  |           x: -1, | ||||||
|  |           y: 1 | ||||||
|  |         }, { | ||||||
|  |           x: 0, | ||||||
|  |           y: 0 | ||||||
|  |         }, { | ||||||
|  |           x: 1, | ||||||
|  |           y: 1 | ||||||
|  |         }, { | ||||||
|  |           x: 2, | ||||||
|  |           y: 4 | ||||||
|  |         }] | ||||||
|  |       }, | ||||||
|  |       { | ||||||
|  |         label: 'Scatter Dataset 2', | ||||||
|  |         fill: false, | ||||||
|  |         borderColor: '#7acbf9', | ||||||
|  |         backgroundColor: '#7acbf9', | ||||||
|  |         data: [{ | ||||||
|  |           x: -2, | ||||||
|  |           y: -4 | ||||||
|  |         }, { | ||||||
|  |           x: -1, | ||||||
|  |           y: -1 | ||||||
|  |         }, { | ||||||
|  |           x: 0, | ||||||
|  |           y: 1 | ||||||
|  |         }, { | ||||||
|  |           x: 1, | ||||||
|  |           y: -1 | ||||||
|  |         }, { | ||||||
|  |           x: 2, | ||||||
|  |           y: -4 | ||||||
|  |         }] | ||||||
|  |       }] | ||||||
|  |     }, {responsive: true, maintainAspectRatio: false}) | ||||||
|  |   } | ||||||
|  | }) | ||||||
		Reference in New Issue
	
	Block a user