Skip to content

Commit 46133b5

Browse files
authored
Added Fixed dimensions (#14)
* Added fixed with and height props * Upped version number * Updated travis-ci url
1 parent 59f451e commit 46133b5

File tree

6 files changed

+799
-817
lines changed

6 files changed

+799
-817
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Vue3 ChartJS Wrapper
22

33
[![Coverage Status](https://coveralls.io/repos/github/J-T-McC/vue3-chartjs/badge.svg?branch=main)](https://coveralls.io/github/J-T-McC/vue3-chartjs?branch=main)
4-
[![Build Status](https://travis-ci.com/J-T-McC/vue3-chartjs.svg?branch=main)](https://travis-ci.com/J-T-McC/vue3-chartjs)
4+
[![Build Status](https://travis-ci.com/J-T-McC/vue3-chartjs.svg?branch=main)](https://app.travis-ci.com/github/J-T-McC/vue3-chartjs)
55
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/J-T-McC/vue3-chartjs/pulls)
66
![npm](https://img.shields.io/npm/dt/@j-t-mcc/vue3-chartjs)
77

@@ -26,12 +26,24 @@ npm install chart.js @j-t-mcc/vue3-chartjs
2626

2727
Component props use the same structure as ChartJS arguments and are passed as-is to the instance of ChartJS.
2828

29+
ChartJS charts are responsive by default. If you wish to have a fixed sized chart, you can set the optional `height` and `width` properties, paired with setting responsive to `false` in the `options` property.
30+
2931
```js
3032
props: {
3133
type: {
32-
type: String,
34+
type: String,
3335
required: true
3436
},
37+
height: {
38+
type: Number,
39+
required: false,
40+
default: null
41+
},
42+
width: {
43+
type: Number,
44+
required: false,
45+
default: null
46+
},
3547
data: {
3648
type: Object,
3749
required: true

lib/Vue3ChartJs.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ const Vue3ChartJs = defineComponent({
6161
type: String,
6262
required: true
6363
},
64+
height: {
65+
type: Number,
66+
required: false,
67+
default: null
68+
},
69+
width: {
70+
type: Number,
71+
required: false,
72+
default: null
73+
},
6474
data: {
6575
type: Object,
6676
required: true
@@ -136,9 +146,11 @@ const Vue3ChartJs = defineComponent({
136146
}
137147
},
138148
139-
render () {
149+
render (props) {
140150
return h('canvas', {
141-
ref: 'chartRef'
151+
ref: 'chartRef',
152+
height: props.height,
153+
width: props.width
142154
})
143155
}
144156
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@j-t-mcc/vue3-chartjs",
3-
"version": "1.1.4",
3+
"version": "1.2.0",
44
"author": "Tyson McCarney <info@tysonmccarney.com> (https://tysonmccarney.com)",
55
"description": "Vue3 wrapper for Chart.js",
66
"license": "MIT",

src/App.vue

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<template>
2-
<div style="height:600px;width: 600px;">
3-
4-
<div style="width: 500px">
2+
<div style="width: 100%;height:20%;display: block;">
53
<vue3-chart-js v-bind="{...barChart}" @after-update="afterUpdate"/>
64
</div>
7-
<div style="width: 500px">
5+
<div style="display: block;">
86
<vue3-chart-js ref="chartRef" v-bind="{...localDoughnutChartOptions}" @after-update="afterUpdate"/>
97
</div>
108
<button type="submit" @click="updateChart">Update Doughnut Data</button>
119
<button type="submit" @click="exportChart">Export Chart as PNG</button>
12-
13-
</div>
1410
</template>
1511

1612
<script>
@@ -37,11 +33,18 @@ export default {
3733
}]
3834
},
3935
options: {
36+
maintainAspectRatio: false,
4037
plugins: {
4138
zoom: {
4239
zoom: {
43-
enabled: true,
44-
mode: 'xy',
40+
wheel: {
41+
enabled: true,
42+
mode: 'xy',
43+
},
44+
pinch: {
45+
enabled: true,
46+
mode: 'xy',
47+
}
4548
}
4649
}
4750
}
@@ -51,6 +54,8 @@ export default {
5154
const doughnutChart = {
5255
id: 'doughnut',
5356
type: 'doughnut',
57+
height: 800,
58+
width: 800,
5459
data: {
5560
labels: ['VueJs', 'EmberJs', 'ReactJs', 'AngularJs'],
5661
datasets: [
@@ -66,6 +71,7 @@ export default {
6671
]
6772
},
6873
options: {
74+
responsive: false,
6975
cutout: '10%',
7076
plugins: {}
7177
}

test/wrapper.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ describe('init', () => {
5050
})
5151
})
5252

53+
describe('chart dimensions', () => {
54+
it('it sets fixed height and width', async () => {
55+
const props = getDoughnutProps()
56+
props.options.responsive = false
57+
props.width = props.height = 800
58+
const wrapper = factory(props)
59+
wrapper.vm.render()
60+
expect(wrapper.vm.chartJSState.chart.height).toEqual(800)
61+
expect(wrapper.vm.chartJSState.chart.width).toEqual(800)
62+
})
63+
})
64+
5365
describe('chart reloading', () => {
5466
it('reloads if already exists', async () => {
5567
const wrapper = factory(getDoughnutProps())

0 commit comments

Comments
 (0)