Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64
Your IP : 18.117.254.177
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Getting Started with Chart JS with www.chartjs3.com</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
</head>
<body>
<div class="chartMenu">
<p>WWW.CHARTJS3.COM (Chart JS 3.8.0)</p>
</div>
<div class="chartCard">
<div class="chartBox">
<canvas id="myChart"></canvas>
</div>
</div>
</body>
</html>
<script>
// setup
const data = {
labels: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun","Sun"],
datasets: [
{
data: [1, 2, 3, 4, 5, 6,7,8],
backgroundColor: [
"#316065",
"#1A7F89",
"#2D9CA7",
"#2D86A7",
"#1167A7",
"#142440",
"#142460",
"#142470"
],
borderColor: [
"#316065",
"#1A7F89",
"#2D9CA7",
"#2D86A7",
"#1167A7",
"#142440",
"#142460",
"#142470",
]
}
]
};
// pieLabelsLine plugin
const pieLabelsLine = {
id: "pieLabelsLine",
afterDraw(chart) {
const {
ctx,
chartArea: { width, height }
} = chart;
const cx = chart._metasets[0].data[0].x;
const cy = chart._metasets[0].data[0].y;
const sum = chart.data.datasets[0].data.reduce((a, b) => a + b, 0);
chart.data.datasets.forEach((dataset, i) => {
chart.getDatasetMeta(i).data.forEach((datapoint, index) => {
const { x: a, y: b } = datapoint.tooltipPosition();
const x = 2 * a - cx;
const y = 2 * b - cy;
// draw line
const halfwidth = width / 2;
const halfheight = height / 2;
const xLine = x >= halfwidth ? x + 20 : x - 20;
const yLine = y >= halfheight ? y + 20 : y - 20;
const extraLine = x >= halfwidth ? 10 : -10;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.arc(x, y, 2, 0, 2 * Math.PI, true);
ctx.fill();
ctx.moveTo(x, y);
ctx.lineTo(xLine, yLine);
ctx.lineTo(xLine + extraLine, yLine);
// ctx.strokeStyle = dataset.backgroundColor[index];
ctx.strokeStyle = "black";
ctx.stroke();
// text
const textWidth = ctx.measureText(chart.data.labels[index]).width;
ctx.font = "12px Arial";
// control the position
const textXPosition = x >= halfwidth ? "left" : "right";
const plusFivePx = x >= halfwidth ? 5 : -5;
ctx.textAlign = textXPosition;
ctx.textBaseline = "middle";
// ctx.fillStyle = dataset.backgroundColor[index];
ctx.fillStyle = "black";
ctx.fillText(
chart.data.labels[index],
xLine + extraLine + plusFivePx,
yLine
);
});
});
}
};
// config
const config = {
type: "pie",
data,
options: {
maintainAspectRatio: false,
layout: {
padding: 30
},
scales: {
y: {
display: false,
beginAtZero: true,
ticks: {
display: false
},
grid: {
display: false
}
},
x: {
display: false,
ticks: {
display: false
},
grid: {
display: false
}
}
},
plugins: {
legend: {
display: false
}
}
},
plugins: [pieLabelsLine]
};
// render init block
const myChart = new Chart(document.getElementById("myChart"),config);
</script>
|