Skip to content

Commit

Permalink
feat: add horizontal bar chart double datalabel
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuseduardomedeiros committed Jan 8, 2025
1 parent bc46c88 commit 549ac40
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
81 changes: 64 additions & 17 deletions src/components/insights/charts/HorizontalBarChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default {
scales: {
x: {
display: false,
grace: 15,
},
y: {
display: true,
Expand All @@ -140,6 +141,12 @@ export default {
backgroundColor: '#00A49F',
hoverBackgroundColor: '#00DED2',
plugins: {
datalabels: {
display: false,
},
doubleDataLabel: {
datalabelsSuffix: this.datalabelsSuffix,
},
horizontalBackgroundColorPlugin: {
backgroundColor: '#C6FFF7',
},
Expand All @@ -150,20 +157,6 @@ export default {
label: () => false,
},
},
datalabels: {
formatter: (value) => {
return value + this.datalabelsSuffix;
},
color: '#fff',
anchor: 'end',
align: 'start',
textStrokeColor: '#fff',
font: {
size: '12',
weight: '700',
lineHeight: 1.66,
},
},
},
};
},
Expand All @@ -183,11 +176,11 @@ export default {
ctx.beginPath();
ctx.fillStyle = plugins.backgroundColor;
data.datasets[0].data.forEach((_point, index) => {
data.datasets[0].data.forEach((_dataPoint, index) => {
ctx.roundRect(
left, // start position
y.getPixelForValue(index) - barThickness / 2, // align background to center bar
width - 30, // background padding right
width - 160, // background padding right
barThickness,
4, // border radius
);
Expand All @@ -197,8 +190,62 @@ export default {
},
};
},
doubleDataLabel() {
return {
id: 'doubleDataLabel',
afterDatasetsDraw(chart, _args, plugins) {
if (plugins.display === false) return;
const {
ctx,
data: { datasets },
chartArea: { width },
} = chart;
ctx.save();
chart.getDatasetMeta(0).data.forEach((dataPoint, index) => {
const { data, fullValues } = datasets[0];
ctx.textBaseline = 'middle';
ctx.font = 'bold 16px Lato';
ctx.fillStyle = '#4E5666';
const startTextPosition = width - 42;
ctx.fillText(
`${data[index]} ${plugins.datalabelsSuffix}`,
startTextPosition,
dataPoint.y,
);
const valueCharCount = String(data[index]).length;
const widthCompensationMap = {
1: 30,
3: 40,
4: 50,
5: 60,
};
ctx.font = 'normal 14px Lato';
ctx.fillStyle = '#67738B';
ctx.fillText(
`| ${fullValues[index]}`,
startTextPosition + widthCompensationMap[valueCharCount] || 0,
dataPoint.y,
);
});
},
};
},
chartPlugins() {
return [ChartDataLabels, Tooltip, this.horizontalBackgroundColorPlugin];
return [
ChartDataLabels,
Tooltip,
this.horizontalBackgroundColorPlugin,
this.doubleDataLabel,
];
},
graphContainerHeight() {
const barSpacingY = 4;
Expand Down
4 changes: 4 additions & 0 deletions src/components/insights/charts/LineChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default {
},
isLoading: Boolean,
},
emits: ['seeMore'],
setup() {
Expand Down Expand Up @@ -159,6 +160,9 @@ export default {
},
},
},
doubleDataLabel: {
display: false,
},
datalabels: {
display: false,
},
Expand Down
2 changes: 2 additions & 0 deletions src/components/insights/widgets/DynamicWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ export default {
const labels = data.map((item) => item.label);
const values = data.map((item) => item.value);
const fullValues = data.map((item) => item.full_value);
const newData = {
labels,
datasets: [
{
data: values,
fullValues,
},
],
};
Expand Down

0 comments on commit 549ac40

Please sign in to comment.