Code JavaScript tạo biểu đồ & đồ thị xếp chồng thanh

JavaScript Stacked Bar Charts được tạo bằng cách xếp chồng nhiều chuỗi dữ liệu với nhau. Điều này cho phép bạn so sánh tỷ lệ các chuỗi dữ liệu khác nhau với tổng số ở thời điểm bất kỳ. Biểu đồ Stacked Bar Chart được tạo bằng JavaScript có thể tương tác, hỗ trợ hoạt ảnh, thu phóng, xoay & xuất dưới dạng ảnh.

Ví dụ bên dưới minh hoạ một mẫu biểu đồ xếp chồng thanh được tạo bằng JavaScript. Bài viết bao gồm mã nguồn HTML mà bạn có thể chỉnh sửa trong trình duyệt hoặc lưu về máy để chạy nội bộ.

Biểu đồ thanh xếp chồng được tạo bằng JavaScript

<!DOCTYPE HTML>
<html>
<head>  
<script>
window.onload = function () {

var chart = new CanvasJS.Chart("chartContainer", {
	animationEnabled: true,
	title:{
		text: "Doanh thu buổi tối ở một nhà hàng"
	},
	axisX: {
		valueFormatString: "DDD"
	},
	axisY: {
		prefix: "$"
	},
	toolTip: {
		shared: true
	},
	legend:{
		cursor: "pointer",
		itemclick: toggleDataSeries
	},
	data: [{
		type: "stackedBar",
		name: "Bữa ăn",
		showInLegend: "true",
		xValueFormatString: "DD, MMM",
		yValueFormatString: "$#,##0",
		dataPoints: [
			{ x: new Date(2017, 0, 30), y: 56 },
			{ x: new Date(2017, 0, 31), y: 45 },
			{ x: new Date(2017, 1, 1), y: 71 },
			{ x: new Date(2017, 1, 2), y: 41 },
			{ x: new Date(2017, 1, 3), y: 60 },
			{ x: new Date(2017, 1, 4), y: 75 },
			{ x: new Date(2017, 1, 5), y: 98 }
		]
	},
	{
		type: "stackedBar",
		name: "Ăn nhẹ",
		showInLegend: "true",
		xValueFormatString: "DD, MMM",
		yValueFormatString: "$#,##0",
		dataPoints: [
			{ x: new Date(2017, 0, 30), y: 86 },
			{ x: new Date(2017, 0, 31), y: 95 },
			{ x: new Date(2017, 1, 1), y: 71 },
			{ x: new Date(2017, 1, 2), y: 58 },
			{ x: new Date(2017, 1, 3), y: 60 },
			{ x: new Date(2017, 1, 4), y: 65 },
			{ x: new Date(2017, 1, 5), y: 89 }
		]
	},
	{
		type: "stackedBar",
		name: "Đồ uống",
		showInLegend: "true",
		xValueFormatString: "DD, MMM",
		yValueFormatString: "$#,##0",
		dataPoints: [
			{ x: new Date(2017, 0, 30), y: 48 },
			{ x: new Date(2017, 0, 31), y: 45 },
			{ x: new Date(2017, 1, 1), y: 41 },
			{ x: new Date(2017, 1, 2), y: 55 },
			{ x: new Date(2017, 1, 3), y: 80 },
			{ x: new Date(2017, 1, 4), y: 85 },
			{ x: new Date(2017, 1, 5), y: 83 }
		]
	},
	{
		type: "stackedBar",
		name: "Tráng miệng",
		showInLegend: "true",
		xValueFormatString: "DD, MMM",
		yValueFormatString: "$#,##0",
		dataPoints: [
			{ x: new Date(2017, 0, 30), y: 61 },
			{ x: new Date(2017, 0, 31), y: 55 },
			{ x: new Date(2017, 1, 1), y: 61 },
			{ x: new Date(2017, 1, 2), y: 75 },
			{ x: new Date(2017, 1, 3), y: 80 },
			{ x: new Date(2017, 1, 4), y: 85 },
			{ x: new Date(2017, 1, 5), y: 105 }
		]
	},
	{
		type: "stackedBar",
		name: "Đem về",
		showInLegend: "true",
		xValueFormatString: "DD, MMM",
		yValueFormatString: "$#,##0",
		dataPoints: [
			{ x: new Date(2017, 0, 30), y: 52 },
			{ x: new Date(2017, 0, 31), y: 55 },
			{ x: new Date(2017, 1, 1), y: 20 },
			{ x: new Date(2017, 1, 2), y: 35 },
			{ x: new Date(2017, 1, 3), y: 30 },
			{ x: new Date(2017, 1, 4), y: 45 },
			{ x: new Date(2017, 1, 5), y: 25 }
		]
	}]
});
chart.render();

function toggleDataSeries(e) {
	if(typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
		e.dataSeries.visible = false;
	}
	else {
		e.dataSeries.visible = true;
	}
	chart.render();
}

}
</script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>

Nguồn: CanvasJS

Tùy biến biểu đồ

Bạn có thể thay đổi color (màu sắc) của từng chuỗi dữ liệu để dễ phân biệt hơn. Một số lựa chọn phổ biến khác bao gồm showInLegend - Hiện chú giải, shared(in toolTip) - chia sẻ tooltip, dataPointWidth - Độ rộng điểm dữ liệu, etc.

Thứ Ba, 07/03/2023 16:56
51 👨 203
0 Bình luận
Sắp xếp theo
    ❖ JavaScript