2023-02-28 08:29:30 +00:00
|
|
|
<template>
|
|
|
|
<DocSectionText v-bind="$attrs">
|
|
|
|
<p>
|
|
|
|
A chart is configured with 3 properties; <i>type</i>, <i>data</i> and <i>options</i>. Chart type is defined using the <i>type</i> property that accepts <i>pie</i>, <i>doughtnut</i>, <i>line</i>, <i>bar</i>, <i>radar</i> and
|
|
|
|
<i>polarArea</i> as a value. The <i>data</i> defines datasets represented with the chart and the <i>options</i> provide numerous customization options to customize the presentation.
|
|
|
|
</p>
|
|
|
|
</DocSectionText>
|
|
|
|
<div class="card">
|
|
|
|
<Chart type="bar" :data="chartData" :options="chartOptions" />
|
|
|
|
</div>
|
|
|
|
<DocSectionCode :code="code" :dependencies="{ 'chart.js': '3.3.2' }" component="Chart" />
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-10-13 20:06:19 +00:00
|
|
|
import EventBus from '@/layouts/AppEventBus';
|
|
|
|
|
2023-02-28 08:29:30 +00:00
|
|
|
export default {
|
2024-03-29 07:44:47 +00:00
|
|
|
redrawListener: null,
|
2023-02-28 08:29:30 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2023-10-13 19:52:02 +00:00
|
|
|
chartData: null,
|
|
|
|
chartOptions: null,
|
2023-02-28 08:29:30 +00:00
|
|
|
code: {
|
2023-09-22 12:54:14 +00:00
|
|
|
basic: `
|
2023-10-15 09:38:39 +00:00
|
|
|
<Chart type="bar" :data="chartData" :options="chartOptions" />
|
|
|
|
`,
|
2023-09-22 12:54:14 +00:00
|
|
|
options: `
|
|
|
|
<template>
|
2023-02-28 08:29:30 +00:00
|
|
|
<div class="card">
|
|
|
|
<Chart type="bar" :data="chartData" :options="chartOptions" />
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2023-10-13 19:52:02 +00:00
|
|
|
chartData: null,
|
|
|
|
chartOptions: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.chartData = this.setChartData();
|
|
|
|
this.chartOptions = this.setChartOptions();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
setChartData() {
|
|
|
|
return {
|
2023-02-28 08:29:30 +00:00
|
|
|
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: 'Sales',
|
|
|
|
data: [540, 325, 702, 620],
|
2024-01-29 13:22:29 +00:00
|
|
|
backgroundColor: ['rgba(249, 115, 22, 0.2)', 'rgba(6, 182, 212, 0.2)', 'rgb(107, 114, 128, 0.2)', 'rgba(139, 92, 246, 0.2)'],
|
|
|
|
borderColor: ['rgb(249, 115, 22)', 'rgb(6, 182, 212)', 'rgb(107, 114, 128)', 'rgb(139, 92, 246)'],
|
2023-02-28 08:29:30 +00:00
|
|
|
borderWidth: 1
|
|
|
|
}
|
|
|
|
]
|
2023-10-13 19:52:02 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
setChartOptions() {
|
|
|
|
const documentStyle = getComputedStyle(document.documentElement);
|
|
|
|
const textColor = documentStyle.getPropertyValue('--text-color');
|
2024-03-26 21:24:49 +00:00
|
|
|
const textColorSecondary = documentStyle.getPropertyValue('--text-secondary-color');
|
|
|
|
const surfaceBorder = documentStyle.getPropertyValue('--border-color');
|
2023-10-13 19:52:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
color: textColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-02-28 08:29:30 +00:00
|
|
|
scales: {
|
2023-10-13 19:52:02 +00:00
|
|
|
x: {
|
|
|
|
ticks: {
|
|
|
|
color: textColorSecondary
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: surfaceBorder
|
|
|
|
}
|
|
|
|
},
|
2023-02-28 08:29:30 +00:00
|
|
|
y: {
|
2023-10-13 19:52:02 +00:00
|
|
|
beginAtZero: true,
|
|
|
|
ticks: {
|
|
|
|
color: textColorSecondary
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: surfaceBorder
|
|
|
|
}
|
2023-02-28 08:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 19:52:02 +00:00
|
|
|
};
|
|
|
|
}
|
2023-02-28 08:29:30 +00:00
|
|
|
}
|
|
|
|
};
|
2023-10-15 09:38:39 +00:00
|
|
|
<\/script>
|
|
|
|
`,
|
2023-09-22 12:54:14 +00:00
|
|
|
composition: `
|
|
|
|
<template>
|
2023-02-28 08:29:30 +00:00
|
|
|
<div class="card">
|
|
|
|
<Chart type="bar" :data="chartData" :options="chartOptions" />
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
2023-10-13 19:52:02 +00:00
|
|
|
import { ref, onMounted } from "vue";
|
2023-02-28 08:29:30 +00:00
|
|
|
|
2023-10-13 19:52:02 +00:00
|
|
|
onMounted(() => {
|
|
|
|
chartData.value = setChartData();
|
|
|
|
chartOptions.value = setChartOptions();
|
2023-02-28 08:29:30 +00:00
|
|
|
});
|
2023-10-13 19:52:02 +00:00
|
|
|
|
|
|
|
const chartData = ref();
|
|
|
|
const chartOptions = ref();
|
|
|
|
|
|
|
|
const setChartData = () => {
|
|
|
|
return {
|
|
|
|
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: 'Sales',
|
|
|
|
data: [540, 325, 702, 620],
|
2024-01-29 13:22:29 +00:00
|
|
|
backgroundColor: ['rgba(249, 115, 22, 0.2)', 'rgba(6, 182, 212, 0.2)', 'rgb(107, 114, 128, 0.2)', 'rgba(139, 92, 246 0.2)'],
|
|
|
|
borderColor: ['rgb(249, 115, 22)', 'rgb(6, 182, 212)', 'rgb(107, 114, 128)', 'rgb(139, 92, 246)'],
|
2023-10-13 19:52:02 +00:00
|
|
|
borderWidth: 1
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const setChartOptions = () => {
|
|
|
|
const documentStyle = getComputedStyle(document.documentElement);
|
|
|
|
const textColor = documentStyle.getPropertyValue('--text-color');
|
2024-03-26 21:24:49 +00:00
|
|
|
const textColorSecondary = documentStyle.getPropertyValue('--text-secondary-color');
|
|
|
|
const surfaceBorder = documentStyle.getPropertyValue('--border-color');
|
2023-10-13 19:52:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
color: textColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
ticks: {
|
|
|
|
color: textColorSecondary
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: surfaceBorder
|
|
|
|
}
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
beginAtZero: true,
|
|
|
|
ticks: {
|
|
|
|
color: textColorSecondary
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: surfaceBorder
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 08:29:30 +00:00
|
|
|
}
|
2023-10-13 19:52:02 +00:00
|
|
|
};
|
|
|
|
}
|
2023-10-15 09:38:39 +00:00
|
|
|
<\/script>
|
|
|
|
`
|
2023-02-28 08:29:30 +00:00
|
|
|
}
|
|
|
|
};
|
2023-10-13 19:52:02 +00:00
|
|
|
},
|
2023-10-13 20:06:19 +00:00
|
|
|
beforeUnmount() {
|
2024-03-29 07:44:47 +00:00
|
|
|
EventBus.off('dark-mode-toggle-complete', this.redrawListener);
|
|
|
|
EventBus.off('theme-palette-change', this.redrawListener);
|
2023-10-13 20:06:19 +00:00
|
|
|
},
|
2023-10-13 19:52:02 +00:00
|
|
|
mounted() {
|
|
|
|
this.chartData = this.setChartData();
|
|
|
|
this.chartOptions = this.setChartOptions();
|
2023-10-13 20:06:19 +00:00
|
|
|
|
2024-03-29 07:44:47 +00:00
|
|
|
this.redrawListener = () => {
|
2023-10-13 20:06:19 +00:00
|
|
|
this.chartOptions = this.setChartOptions();
|
|
|
|
};
|
|
|
|
|
2024-03-29 07:44:47 +00:00
|
|
|
EventBus.on('theme-palette-change', this.redrawListener);
|
|
|
|
EventBus.on('dark-mode-toggle-complete', this.redrawListener);
|
2023-10-13 19:52:02 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
setChartData() {
|
|
|
|
return {
|
|
|
|
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: 'Sales',
|
|
|
|
data: [540, 325, 702, 620],
|
2024-01-29 13:22:29 +00:00
|
|
|
backgroundColor: ['rgba(249, 115, 22, 0.2)', 'rgba(6, 182, 212, 0.2)', 'rgb(107, 114, 128, 0.2)', 'rgba(139, 92, 246, 0.2)'],
|
|
|
|
borderColor: ['rgb(249, 115, 22)', 'rgb(6, 182, 212)', 'rgb(107, 114, 128)', 'rgb(139, 92, 246)'],
|
2023-10-13 19:52:02 +00:00
|
|
|
borderWidth: 1
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
},
|
|
|
|
setChartOptions() {
|
|
|
|
const documentStyle = getComputedStyle(document.documentElement);
|
|
|
|
const textColor = documentStyle.getPropertyValue('--text-color');
|
2024-03-26 21:24:49 +00:00
|
|
|
const textColorSecondary = documentStyle.getPropertyValue('--text-secondary-color');
|
|
|
|
const surfaceBorder = documentStyle.getPropertyValue('--border-color');
|
2023-10-13 19:52:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
color: textColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
ticks: {
|
|
|
|
color: textColorSecondary
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: surfaceBorder
|
|
|
|
}
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
beginAtZero: true,
|
|
|
|
ticks: {
|
|
|
|
color: textColorSecondary
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
color: surfaceBorder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-02-28 08:29:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|