1. 程式人生 > >【echarts】柱狀圖上方顯示數值

【echarts】柱狀圖上方顯示數值

使用官網的例子,只不過加了itemStyle屬性

<!DOCTYPE html>

<head>
	<meta charset="utf-8">
	<title>ECharts</title>
</head>

<body>
	<!-- 為ECharts準備一個具備大小(寬高)的Dom -->
	<div id="main" style="width:900px;height:300px"></div>
	<!-- ECharts單檔案引入 -->
	<script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
	<script type="text/javascript">
		// 路徑配置
		require.config({
			paths: {
				echarts: 'http://echarts.baidu.com/build/dist'
			}
		});

		// 使用
		require(
			[
				'echarts',
				'echarts/chart/bar' // 使用柱狀圖就載入bar模組,按需載入
			],
			function(ec) {
				// 基於準備好的dom,初始化echarts圖表
				var myChart = ec.init(document.getElementById('main'));

				var option = {
					tooltip: {
						show: true
					},
					legend: {
						data: ['銷量']
					},
					xAxis: [{
						type: 'category',
						data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
					}],
					yAxis: [{
						type: 'value'
					}],
					series: [{
						"name": "銷量",
						"type": "bar",
						"data": [5, 20, 40, 10, 10, 20],
						itemStyle: {
							normal: {
								label: {
									show: true, //開啟顯示
									position: 'top', //在上方顯示
									textStyle: { //數值樣式
										color: 'black',
										fontSize: 16
									}
								}
							}
						},
					}]
				};

				// 為echarts物件載入資料 
				myChart.setOption(option);
			}
		);
	</script>

</body>

顯示的效果如下