Code JavaScript tạo biểu đồ vùng theo phạm vi đa chuỗi với trục ngày tháng

Giống như bất kỳ kiểu biểu đồ khác trong CanvasJS, biểu đồ vùng xếp chồng 100% có số, danh mục hoặc trục ngày tháng. Các biểu đồ với trục ngày tháng còn được gọi là biểu đồ/đồ thị timeline (theo dòng thời gian). Bạn có thể dùng các đối tượng ngày tháng trong JavaScript hoặc dấu thời gian dưới dạng giá trị X trong điểm dữ liệu cho trục ngày tháng.

Ví dụ bên dưới minh họa biểu đồ xếp chồng theo vùng 100% kèm trục ngày tháng. Bạn sẽ thấy có code JavaScript tạo biểu đồ chi tiết ở bên dưới để chỉnh sửa hoặc lưu về máy để chạy nội bộ.

Mẫu biểu đồ vùng với trục thời gian

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

var chart = new CanvasJS.Chart("chartContainer", {
	title:{
		text: "Tỷ lệ số người liên quan tới các vụ tai nạn theo hình thức vận tải"
	},
	theme: "light2", // bạn có thể thay bằng "light1", "light2", "dark1", "dark2"
	animationEnabled: true,
	axisX: {
		interval: 1,
		intervalType: "month"
	},
	toolTip: {
		shared: true
	},
	data: [
	{        
		type: "stackedArea100",
		name: "Ô tô",
		xValueFormatString: "MMM, YYYY",
		showInLegend: "true",
		dataPoints: [
			{ x: new Date(2016, 00, 1), y: 1100 },
			{ x: new Date(2016, 01, 1), y: 1200 },
			{ x: new Date(2016, 02, 1), y: 1000 },
			{ x: new Date(2016, 03, 1), y: 1200 },
			{ x: new Date(2016, 04, 1), y: 1600 },
			{ x: new Date(2016, 05, 1), y: 1800 },
			{ x: new Date(2016, 06, 1), y: 1400 },
			{ x: new Date(2016, 07, 1), y: 1500 },
			{ x: new Date(2016, 08, 1), y: 1600 },
			{ x: new Date(2016, 09, 1), y: 1800 }
		]
	},
	{        
		type: "stackedArea100",
		name: "Xe tải",
		showInLegend: "true",
		dataPoints: [
			{ x: new Date(2016, 00, 1), y: 150 },
			{ x: new Date(2016, 01, 1), y: 171 },
			{ x: new Date(2016, 02, 1), y: 155 },
			{ x: new Date(2016, 03, 1), y: 150 },
			{ x: new Date(2016, 04, 1), y: 165 },
			{ x: new Date(2016, 05, 1), y: 195 },
			{ x: new Date(2016, 06, 1), y: 155 },
			{ x: new Date(2016, 07, 1), y: 145 },
			{ x: new Date(2016, 08, 1), y: 140 },
			{ x: new Date(2016, 09, 1), y: 151 }
		]
	},
	{        
		type: "stackedArea100",
		name: "Xe đạp",
		showInLegend: "true",
		dataPoints: [
			{ x: new Date(2016, 00, 1), y: 71 },
			{ x: new Date(2016, 01, 1), y: 41 },
			{ x: new Date(2016, 02, 1), y: 55 },
			{ x: new Date(2016, 03, 1), y: 50 },
			{ x: new Date(2016, 04, 1), y: 65 },
			{ x: new Date(2016, 05, 1), y: 95 },
			{ x: new Date(2016, 06, 1), y: 45 },
			{ x: new Date(2016, 07, 1), y: 95 },
			{ x: new Date(2016, 08, 1), y: 60 },
			{ x: new Date(2016, 09, 1), y: 40 }
		]
	},
	{        
		type: "stackedArea100",
		name: "Xe máy",
		showInLegend: "true",
		dataPoints: [
			{ x: new Date(2016, 00, 1), y: 861 },
			{ x: new Date(2016, 01, 1), y: 761 },
			{ x: new Date(2016, 02, 1), y: 775 },
			{ x: new Date(2016, 03, 1), y: 680 },
			{ x: new Date(2016, 04, 1), y: 785 },
			{ x: new Date(2016, 05, 1), y: 374 },
			{ x: new Date(2016, 06, 1), y: 365 },
			{ x: new Date(2016, 07, 1), y: 455 },
			{ x: new Date(2016, 08, 1), y: 657 },
			{ x: new Date(2016, 09, 1), y: 659 }
		]
	},
	{        
		type: "stackedArea100",
		name: "Người đi bộ",
		showInLegend: "true",
		dataPoints: [
			{ x: new Date(2016, 00, 1), y: 220 },
			{ x: new Date(2016, 01, 1), y: 240 },
			{ x: new Date(2016, 02, 1), y: 295 },
			{ x: new Date(2016, 03, 1), y: 300 },
			{ x: new Date(2016, 04, 1), y: 265 },
			{ x: new Date(2016, 05, 1), y: 235 },
			{ x: new Date(2016, 06, 1), y: 245 },
			{ x: new Date(2016, 07, 1), y: 255 },
			{ x: new Date(2016, 08, 1), y: 275 },
			{ x: new Date(2016, 09, 1), y: 290 }
		]
	}
	]
});
chart.render();

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

Nguồn: CanvasJS

Tùy biến biểu đồ/đồ thị

Bạn có thể chọn interval và intervalType cho trục để thay đổi khoảng thời gian của các nhãn trục. Một số lựa chọn chỉnh sửa biểu đồ khác bao gồm: minimum, maximum, viewportMinimum, viewportMaximum, zoomEnabled, etc.

Thứ Hai, 20/02/2023 16:55
51 👨 154
0 Bình luận
Sắp xếp theo
    ❖ JavaScript