echarts橫向柱狀圖簡單實現方法
更新時間:2023年12月15日 10:11:13 作者:sukie04120
這篇文章主要給大家介紹了關于echarts橫向柱狀圖簡單實現的相關資料,ECharts是百度前端開發(fā)部開發(fā)的一個開源可視化庫,它可以幫助開發(fā)者輕松的實現各種數據可視化,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
最近項目涉及好幾種echarts的圖表,特此記錄一下
涉及點:
橫向柱狀圖、不顯示x軸標簽、柱子漸變色、數量及單位顯示在柱子內部。
效果圖:

echarts配置:
option我是放置在data內部,然后再動態(tài)獲取y軸和數值(series)的數據再進行圖表的渲染
option: {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
grid: {
left: 0,
top: '8%',
right: '3%',
bottom: '8%',
containLabel: true
},
xAxis: {
type: 'value',
axisLabel: {
show: false // 不顯示x軸標簽
},
axisLine: {
// x軸線的顏色以及寬度
show: true,
lineStyle: {color: 'rgba(255,255,255,0.1)'}
},
axisTick: {
show: false // x軸刻度線
},
splitLine: { // x軸網格線
show: true,
lineStyle: {color: 'rgba(255,255,255,0.1)'}
}
},
yAxis: {
type: 'category',
axisTick: { show: false }, // y軸刻度線
axisLabel: { color: '#fff' }, // y軸文字的配置
axisLine: {
//x軸線的顏色
show: true,
lineStyle: {color: 'rgba(255,255,255,0.1)'}
},
data: ['內科','外科','婦科','兒科','牙科']
},
series: [
{
name: '人數',
type: 'bar',
stack: '總量',
label: {
normal: {
show: true,
position: 'inside', //顯示在柱子內部
textStyle: { color: '#fff' },
formatter: '{c}人' //單位
}
},
itemStyle: {
color: {
colorStops:[ //柱子的漸變色
{
offset: 0,
color: 'rgba(5, 80, 57, 1)' // 0% 處的顏色
},
{
offset: 1,
color: 'rgba(13, 253, 178, 1)' // 100% 處的顏色
}
],
global: false
}
},
barWidth: 20, //柱子的寬度
data: [88,75,53,39,36]
}
]
}渲染圖表方法及HTML:
//html:
<div class="echartDiv" ref="chart"></div>
//方法(渲染圖表):
initEcharts() {
this.myChart && this.myChart.dispose();
let chartDom = this.$refs.chart;
this.myChart = echarts.init(chartDom);
this.myChart.setOption(this.option);
let that = this;
window.addEventListener('resize', function() {
that.myChart.resize();
});
},附:echarts橫向柱狀圖的兩種簡便畫法
示例圖

第一種方法: 可以隨意控制y左軸和y右軸的顯示位置
let option = {
// 柱狀圖的位置
grid: {
left: '0%',
right: '0%',
bottom: '0%',
top: '0',
containLabel: false // 圖表兩側是否留白
},
dataZoom: [
{
type: 'inside'
}
],
xAxis: {
show: false,
type: 'value'
},
yAxis: [
// 左軸
{
type: 'category',
inverse: true,
axisLabel: {
inside: true,
zlevel: 2,
color: '#fff'
},
axisTick: {
show: false
},
axisLine: {
show: false
},
data: props.dataX
},
// 右軸
{
type: 'category',
inverse: true,
axisTick: {
show: false
},
axisLine: {
show: false
},
axisLabel: {
inside: true,
formatter: '{value}s',
color: 'black'
},
data: props.dataY,
},
],
series: [
{
zlevel: -1, // 層級
type: 'bar',
barWidth: 30, // 內柱條的寬度
// animationDuration: 1500, // 內柱條的動畫顯示時間
showBackground: true,
// 內柱條樣式
itemStyle: {
color: '#2596FF',
},
// ------------------數據及其樣式----------------------------
// 內柱條的數據
data: props.dataY,
align: 'center'
},
],
}
第二種:y右軸的顯示文字不能隨意控制位置,此種寫法是文字顯示只能跟在內柱條的后面
let option = {
// 柱狀圖的位置
grid: {
left: '0%',
right: '0%',
bottom: '0%',
top: '0',
containLabel: false // 圖表兩側是否留白
},
dataZoom: [
{
type: 'inside'
}
],
xAxis: {
show: false,
type: 'value'
},
yAxis: {
type: "category",
triggerEvent: true,
data: props.dataX,
axisLine: { show: false }, //不顯示坐標軸
axisTick: { show: false }, //不顯示坐標軸刻度線
axisLabel: {
inside: true, // 坐標顯示在軸內側
zlevel: 1,
color: '#ccc',
},
splitLine: {
show: false,
},
},
dataZoom: [
{
type: 'inside'
}
],
series: [
{
name: '',
zlevel: -1,
type: "bar",
color: '#2596FF',
barWidth: 30,
showBackground: true,
backgroundStyle: {
color: 'rgba(180, 180, 180, 0.2)'
},
label: {
show: true,
position: "right",
color: "red",
fontSize: "12px",
formatter: '{c}s',
},
data: props.dataY,
},
],
}總結
到此這篇關于echarts橫向柱狀圖簡單實現的文章就介紹到這了,更多相關echarts橫向柱狀圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript之querySelector和querySelectorAll使用介紹
其實關于querySelector和querySelectorAll的介紹說明很多。在此主要是做個記錄2011-12-12

