Skip to content

Commit 4e5c0f1

Browse files
committed
feat: add custom tooltip styles to components
1 parent afac081 commit 4e5c0f1

File tree

4 files changed

+152
-141
lines changed

4 files changed

+152
-141
lines changed

src/Chart.vue

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<script>
2+
import Chart from 'chart.js'
3+
import { customTooltips } from '@coreui/coreui-chartjs'
4+
5+
export default {
6+
name: '',
7+
type: '',
8+
props: {
9+
datasets: Array,
10+
labels: [Array, String],
11+
options: Object,
12+
plugins: Array
13+
},
14+
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
15+
data() {
16+
return {
17+
chart: undefined
18+
}
19+
},
20+
computed: {
21+
safeId() {
22+
// as long as this._uid() works there is no need to generate the key
23+
const key = () => Math.random().toString(36).replace('0.', '')
24+
return '__safe_id__' + this._uid || key()
25+
},
26+
computedDatasets() {
27+
return this.datasets
28+
},
29+
computedLabels() {
30+
if (this.labels && typeof this.labels !== 'string') {
31+
return this.labels
32+
}
33+
const emptyLabels = Array(this.datasets[0].data.length).fill('')
34+
35+
if (this.labels === 'indexes') {
36+
return emptyLabels.map((u, i) => i + 1)
37+
} else if (this.labels === 'months') {
38+
return emptyLabels.map((u, i) => this.$options.months[i % 12])
39+
}
40+
return emptyLabels
41+
},
42+
computedData() {
43+
return {
44+
datasets: this.computedDatasets,
45+
labels: this.computedLabels
46+
}
47+
},
48+
customTooltips() {
49+
if (!this.options || !this.options.tooltips) {
50+
return {
51+
tooltips: {
52+
enabled: false,
53+
custom: customTooltips,
54+
intersect: true,
55+
mode: 'index',
56+
position: 'nearest',
57+
callbacks: {
58+
labelColor(tooltipItem, chart) {
59+
function getValue(prop) {
60+
return typeof prop === 'object' ? prop[tooltipItem.index] : prop
61+
}
62+
const dataset = chart.data.datasets[tooltipItem.datasetIndex]
63+
//tooltipLabelColor is coreUI custom prop used only here
64+
const backgroundColor = getValue(
65+
dataset.tooltipLabelColor ||
66+
dataset.pointHoverBackgroundColor ||
67+
dataset.borderColor ||
68+
dataset.backgroundColor
69+
)
70+
return {
71+
backgroundColor
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
},
79+
computedOptions() {
80+
return Object.assign({}, this.options, this.customTooltips || {})
81+
},
82+
chartConfig() {
83+
return {
84+
type: this.$options.type,
85+
data: this.computedData,
86+
options: this.computedOptions || this.options,
87+
plugins: this.plugins
88+
}
89+
}
90+
},
91+
watch: {
92+
chartConfig() {
93+
this.updateChart()
94+
}
95+
},
96+
mounted() {
97+
this.renderChart()
98+
},
99+
methods: {
100+
renderChart() {
101+
this.destroyChart()
102+
this.chart = new Chart(
103+
this.$refs.canvas.getContext('2d'),
104+
this.chartConfig
105+
)
106+
},
107+
updateChart() {
108+
Object.assign(this.chart, this.chartConfig)
109+
this.chart.update()
110+
},
111+
destroyChart() {
112+
if (this.chart) {
113+
this.chart.destroy()
114+
}
115+
}
116+
},
117+
beforeDestroy() {
118+
this.destroyChart()
119+
},
120+
render(h) {
121+
return h(
122+
'div',
123+
[
124+
h(
125+
'canvas', {
126+
attrs: {
127+
id: this.safeId
128+
},
129+
ref: 'canvas'
130+
}
131+
)
132+
]
133+
)
134+
}
135+
}
136+
</script>
137+
138+
<style>
139+
@import "~@coreui/coreui-chartjs/dist/css/coreui-chartjs.css";
140+
</style>

src/generateChartComponent.js

Lines changed: 0 additions & 134 deletions
This file was deleted.

src/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import generateChartComponent from './generateChartComponent'
1+
import Chart from './Chart'
22

3-
const CChartBar = generateChartComponent('CChartBar', 'bar')
4-
const CChartLine = generateChartComponent('CChartLine', 'line')
5-
const CChartDoughnut = generateChartComponent('CChartDoughnut', 'doughnut')
6-
const CChartRadar = generateChartComponent('CChartRadar', 'radar')
7-
const CChartPie = generateChartComponent('CChartPie', 'pie')
8-
const CChartPolarArea = generateChartComponent('CChartPolarArea', 'polarArea')
3+
const CChartBar = Object.assign({}, Chart, { name: 'CChartBar', type: 'bar' })
4+
const CChartLine = Object.assign({}, Chart, { name: 'CChartLine', type: 'line' })
5+
const CChartDoughnut = Object.assign({}, Chart, { name: 'CChartDoughnut', type: 'doughnut' })
6+
const CChartRadar = Object.assign({}, Chart, { name: 'CChartRadar', type: 'radar' })
7+
const CChartPie = Object.assign({}, Chart, { name: 'CChartPie', type: 'pie' })
8+
const CChartPolarArea = Object.assign({}, Chart, { name: 'CChartPolarArea', type: 'polarArea' })
99

1010
export {
1111
CChartBar,

vue.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
css: {
3+
extract: false
4+
}
5+
}

0 commit comments

Comments
 (0)