Skip to content

Commit

Permalink
feat: make chart reactive to all properties (#523)
Browse files Browse the repository at this point in the history
* feat: make chart update automatically...

...for any property change. Disables ts-ignore rule as a workaround for vuejs/vetur#1707 (comment)

* chore: update wrapper for zchart

* Format code with prettier

* chore: skip ts-ignore check

* Format code with prettier

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
yash-deepsource and deepsource-autofix[bot] authored Mar 13, 2023
1 parent f6c7b3b commit af3d330
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 48 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ module.exports = {
requireLast: false
}
}
]
],
// ? Need to use @ts-ignore for working around https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
'@typescript-eslint/ban-ts-ignore': 'off'
},

overrides: [
Expand Down
43 changes: 35 additions & 8 deletions src/components/ZChart/ZChart.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import '../../assets/css/layout.css'
import '../../assets/css/chart.css'
import ZChart from './ZChart.vue'

const issueData = {
labels: ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
datasets: [
function getRandomDatasets() {
return [
{
name: 'Active Issues',
values: Array.from({ length: 12 }, () => Math.floor(10 + Math.random() * 80))
Expand All @@ -15,9 +14,24 @@ const issueData = {
name: 'Resolved Issues',
values: Array.from({ length: 12 }, () => Math.floor(10 + Math.random() * 75))
}
],
]
}

function getRandomMarkers() {
return [
{
label: 'Another Threshold',
value: Math.floor(10 + Math.random() * 80),
options: { stroke: 'vanilla-400', lineType: 'dashed' }
}
]
}

const issueData = {
labels: ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
datasets: getRandomDatasets(),
yRegions: [{ label: 'Threshold', start: 0, end: 35, options: { fill: 'ink-300' } }],
yMarkers: [{ label: 'Another Threshold', value: 45, options: { stroke: 'vanilla-400', lineType: 'dashed' } }]
yMarkers: getRandomMarkers()
}

export default {
Expand All @@ -36,9 +50,22 @@ export const LineChart = () => ({
yMarkers: issueData.yMarkers
}
},
template: `<div class="padding-container">
<z-chart :dataSets="dataSets" :labels="labels" :yRegions=yRegions :yMarkers=yMarkers type="line" :yAxisMax="100" :yAxisMin="0"></z-chart>
</div>`
methods: {
changeData() {
// skipcq JS-0295
// @ts-ignore ref https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
this.dataSets = getRandomDatasets()
// skipcq JS-0295
// @ts-ignore ref https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
this.yMarkers = getRandomMarkers()
}
},
template: `
<div class="padding-container">
<z-chart :dataSets="dataSets" :labels="labels" :yRegions=yRegions :yMarkers=yMarkers type="line" :yAxisMax="100" :yAxisMin="0" />
<button class="text-vanilla-100 bg-ink-200 rounded-md px-2 py-1" @click="changeData">Change data</button>
</div>
`
})

export const CustomColors = () => ({
Expand Down
83 changes: 44 additions & 39 deletions src/components/ZChart/ZChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,50 +173,17 @@ export default Vue.extend({
this.themeColors = this.getThemeColors(baseColors)
},
watch: {
chartType() {
this.initChart()
},
labels() {
this.updateChart()
},
dataSets() {
this.updateChart()
},
yMarkers() {
this.updateChart()
chartData: {
handler: function () {
this.updateChart()
}
// deep: true
}
},
methods: {
initChart() {
const chartOptions = {
data: {
labels: this.labels,
datasets: this.dataSets,
yMarkers: this.yMarkers ? this.markers : undefined,
yRegions: this.yRegions ? this.regions : undefined
},
tooltipOptions: this.tooltipOptions,
barOptions: this.barOptions,
lineOptions: Object.assign(DEFAULT_LINE_OPTIONS, this.lineOptions),
axisOptions: {
...Object.assign(DEFAULT_AXIS_OPTIONS, this.axisOptions),
yAxisRange: {
min: this.yAxisMin,
max: this.yAxisMax
}
},
maxSlices: this.maxSlices,
showLegend: this.showLegend,
animate: this.animate,
disableEntryAnimation: !this.animate,
maxLegendPoints: this.maxLegendPoints
}
this.chart = new Chart(this.$refs[this.wrapperName], {
type: this.chartType,
colors: this.palette,
height: this.height,
title: this.title,
...chartOptions
...this.chartData
}) as ChartInterface
},
updateChart() {
Expand Down Expand Up @@ -260,6 +227,7 @@ export default Vue.extend({
}
return []
},
/** @return {Array<Marker>} */
markers(): Array<Marker> {
return (this.yMarkers as Array<Marker>).map((marker) => {
if (!marker.options) {
Expand Down Expand Up @@ -288,6 +256,43 @@ export default Vue.extend({
},
chartType(): string {
return this.type
},
chartData() {
return {
// skipcq JS-0295
// @ts-ignore ref https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
type: this.chartType,
// skipcq JS-0295
// @ts-ignore ref https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
colors: this.palette,
height: this.height,
title: this.title,
data: {
labels: this.labels,
datasets: this.dataSets,
// skipcq JS-0295
// @ts-ignore ref https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
yMarkers: this.yMarkers ? this.markers : undefined,
// skipcq JS-0295
// @ts-ignore ref https://github.com/vuejs/vetur/issues/1707#issuecomment-686851677
yRegions: this.yRegions ? this.regions : undefined
},
tooltipOptions: this.tooltipOptions,
barOptions: this.barOptions,
lineOptions: Object.assign(DEFAULT_LINE_OPTIONS, this.lineOptions),
axisOptions: {
...Object.assign(DEFAULT_AXIS_OPTIONS, this.axisOptions),
yAxisRange: {
min: this.yAxisMin,
max: this.yAxisMax
}
},
maxSlices: this.maxSlices,
showLegend: this.showLegend,
animate: this.animate,
disableEntryAnimation: !this.animate,
maxLegendPoints: this.maxLegendPoints
}
}
},
mounted() {
Expand Down

0 comments on commit af3d330

Please sign in to comment.