function drawVerticalTriangle(ctx, x, y1, y2) {
ctx.beginPath();
ctx.moveTo(x, y1);
ctx.lineTo(x + 6, y2);
ctx.lineTo(x - 6, y2);
ctx.fill();
};
function drawRightArrow(ctx, xLeft, xRight, y) {
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(xLeft, y);
ctx.lineTo(xRight - 5, y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(xRight, y);
ctx.lineTo(xRight - 10, y + 6);
ctx.lineTo(xRight - 10, y - 6);
ctx.fill();
};
function drawTopArrow(ctx, x, yBottom, yTop) {
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(x, yBottom);
ctx.lineTo(x, yTop - 5);
ctx.stroke();
drawVerticalTriangle(ctx, x, yTop -10, yTop);
};
new Chart('myChart', {
type: 'bar',
plugins: [{
beforeDraw: chart => {
let ctx = chart.ctx;
let xAxis = chart.scales.x;
let yAxis = chart.scales.y;
xAxis.ticks.forEach((v, i) => {
let x = xAxis.getPixelForTick(i);
let y = yAxis.getPixelForValue(chart.data.datasets[0].data[i]) - 3;
ctx.fillStyle = chart.data.datasets[0].borderColor;
drawVerticalTriangle(ctx, x, y, y + 10);
y = yAxis.getPixelForValue(chart.data.datasets[1].data[i]) + 3;
ctx.fillStyle = chart.data.datasets[1].borderColor;
drawVerticalTriangle(ctx, x, y, y - 10);
});
ctx.strokeStyle = '#9999FF';
ctx.fillStyle = '#9999FF';
let y = yAxis.getPixelForValue(0);
drawRightArrow(ctx, xAxis.left - 5, xAxis.right + 40, y);
ctx.font = '12px Arial';
ctx.textAlign = 'right';
ctx.fillText('PERIOD', xAxis.right + 30, y + 20);
drawTopArrow(ctx, xAxis.left, yAxis.bottom + 10, yAxis.top - 10);
}
}],
data: {
labels: ['1st year', '2nd year', '3rd year', '4th year'],
datasets: [
{
label: 'Cash inflow',
data: [560, 590, 520, 620],
backgroundColor: '#0F9D58',
borderColor: '#0F9D58',
barThickness: 4
},
{
label: 'Cash outflow',
data: [-600, -550, -410, -570],
backgroundColor: '#DB4437',
borderColor: '#DB4437',
barThickness: 4
}
]
},
options: {
animation: {
duration: 100
},
layout: {
padding: {
right: 40
}
},
scales: {
y: {
grid: {
display: false,
}
},
x: {
stacked: true,
grid: {
display: false,
drawBorder: false
}
}
}
}
});
canvas {
max-width: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.0/chart.min.js"></script>
<canvas id="myChart" height="110"></canvas>