一、Echarts簡介
Echarts是一個基於JavaScript的數據可視化庫,可以製作各種類型的圖表,比如折線圖、柱狀圖、餅圖等。它非常易於使用,支持大量的圖表類型和選項設置。Echarts提供了豐富的交互方式,輕鬆的支持控制項、圖例、標籤等。Echarts的圖表非常精美,支持分類軸、時間軸、值域軸等多種類型的軸。
在Echarts中,圖表是通過JavaScript代碼來創建的,可以自定義各種選項。Echarts採用模塊化設計,用戶可以非常靈活地組合各種組件、配置文件和數據來實現需要的圖表。同時,Echarts的官方文檔非常齊全,用戶可以輕鬆地找到需要的解決方案。
二、Echarts的基本用法
Echarts使用起來非常方便,首先需要在HTML文件中引入Echarts的JavaScript文件。
<script src="echarts.js"></script>
然後,在JavaScript代碼中創建Echarts實例,並設置需要展示的數據和配置項。
// 初始化Echarts實例
var myChart = echarts.init(document.getElementById('chart'));
// 設置數據和配置項
var option = {
title: {
text: '某地區的銷售情況'
},
tooltip: {},
legend: {
data:['銷量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用剛指定的配置項和數據顯示圖表
myChart.setOption(option);
在以上示例中,我們創建了一個柱狀圖,使用了Echarts提供的基本配置項和數據。其中,xAxis表示x軸,yAxis表示y軸,series表示系列。Echarts還提供了其他更高級的選項,比如grid、toolbox和dataZoom等。可以根據需要自定義配置選項來製作更多樣化的圖表。
三、實例展示
下面包含三個實例展示,分別是折線圖、餅圖和地圖。
折線圖
折線圖常用於展示隨時間變化的數據趨勢。以下代碼展示了如何製作一個折線圖。
<div id="line-chart" style="width: 700px;height:400px;"></div>
<script>
var lineChart = echarts.init(document.getElementById('line-chart'));
var option = {
title: {
text: '產品銷量'
},
tooltip: {
trigger: 'axis'
},
legend: {
data:['銷量']
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
},
yAxis: {
type: 'value',
axisLabel: {
formatter: '{value} 件'
}
},
series: [
{
name:'銷量',
type:'line',
data:[120, 132, 101, 134, 90, 230, 210, 200, 150, 110, 90, 200],
itemStyle: {color: '#5ab1ef'},
lineStyle: {color: '#5ab1ef'}
}
]
};
lineChart.setOption(option);
</script>
以上代碼展示了一個折線圖,使用了Echarts提供的基本配置項和數據。其中,xAxis表示x軸,yAxis表示y軸,series表示系列。折線圖中的每條線通過series配置中的type項來指定,這裡我們製作了一條線。Echarts還提供了其他更多樣化的選項,可以根據需要自定義配置選項來製作更多樣化的圖表。
餅圖
餅圖常用於展示數據的佔比。以下代碼展示了如何製作一個餅圖。
<div id="pie-chart" style="width: 700px;height:400px;"></div>
<script>
var pieChart = echarts.init(document.getElementById('pie-chart'));
var option = {
title: {
text: '產品銷售佔比',
subtext: '2021年第一季度',
x: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b} : {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 'left',
data: ['iphone12','小米11','華為P40','OPPO Reno5','vivo X60']
},
series: [
{
name: '銷售佔比',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data:[
{value:335, name:'iphone12'},
{value:310, name:'小米11'},
{value:234, name:'華為P40'},
{value:135, name:'OPPO Reno5'},
{value:1548, name:'vivo X60'}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
pieChart.setOption(option);
</script>
以上代碼展示了一個餅圖,使用了Echarts提供的基本配置項和數據。其中,series表示系列,這裡我們製作了一個餅圖系列。Echarts還提供了其他更多樣化的選項,可以根據需要自定義配置選項來製作更多樣化的圖表。
地圖
地圖常用於展示地理信息。以下代碼展示了如何製作一個地圖。
<div id="map-chart" style="width: 700px;height:400px;"></div>
<script>
var mapChart = echarts.init(document.getElementById('map-chart'));
var option = {
title: {
text: '中國各省份銷售額',
subtext: '2021年第一季度',
left: 'center'
},
tooltip: {
trigger: 'item'
},
visualMap: {
min: 0,
max: 1500,
left: 'left',
top: 'bottom',
text: ['高','低'],
calculable: true
},
series: [
{
name: '銷售額',
type: 'map',
mapType: 'china',
roam: false,
data:[
{name: '北京', value: Math.round(Math.random()*1000)},
{name: '天津', value: Math.round(Math.random()*1000)},
{name: '上海', value: Math.round(Math.random()*1000)},
{name: '重慶', value: Math.round(Math.random()*1000)},
{name: '河北', value: Math.round(Math.random()*1000)},
{name: '河南', value: Math.round(Math.random()*1000)},
{name: '雲南', value: Math.round(Math.random()*1000)},
{name: '遼寧', value: Math.round(Math.random()*1000)},
{name: '黑龍江', value: Math.round(Math.random()*1000)},
{name: '湖南', value: Math.round(Math.random()*1000)},
{name: '安徽', value: Math.round(Math.random()*1000)},
{name: '山東', value: Math.round(Math.random()*1000)},
{name: '新疆', value: Math.round(Math.random()*1000)},
{name: '江蘇', value: Math.round(Math.random()*1000)},
{name: '浙江', value: Math.round(Math.random()*1000)},
{name: '江西', value: Math.round(Math.random()*1000)},
{name: '湖北', value: Math.round(Math.random()*1000)},
{name: '廣西', value: Math.round(Math.random()*1000)},
{name: '甘肅', value: Math.round(Math.random()*1000)},
{name: '山西', value: Math.round(Math.random()*1000)},
{name: '內蒙古', value: Math.round(Math.random()*1000)},
{name: '陝西', value: Math.round(Math.random()*1000)},
{name: '吉林', value: Math.round(Math.random()*1000)},
{name: '福建', value: Math.round(Math.random()*1000)},
{name: '貴州', value: Math.round(Math.random()*1000)},
{name: '廣東', value: Math.round(Math.random()*1000)},
{name: '青海', value: Math.round(Math.random()*1000)},
{name: '西藏', value: Math.round(Math.random()*1000)},
{name: '四川', value: Math.round(Math.random()*1000)},
{name: '寧夏', value: Math.round(Math.random()*1000)},
{name: '海南', value: Math.round(Math.random()*1000)},
{name: '台灣', value: Math.round(Math.random()*1000)},
{name: '香港', value: Math.round(Math.random()*1000)},
{name: '澳門', value: Math.round(Math.random()*1000)}
],
emphasis: {
label: {
show: true
}
}
}
]
};
mapChart.setOption(option);
</script>
以上代碼展示了一個地圖,使用了Echarts提供的基本配置項和數據。其中,visualMap表示視覺映射,使用了最小值和最大值對數據進行映射,並且設置了文本和是否可計算選項。series表示系列,這裡我們製作了一個地圖系列。Echarts還提供了其他更多樣化的選項,可以根據需要自定義配置選項來製作更多樣化的圖表。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/283703.html