import Vue from 'vue' Vue.component("rank-chart", { template: `
`, props: { data:{ type:Array }, height:{ type:Number }, showval:{ type:Boolean, default:false } }, data() { return { option:{} } }, mounted(){ this.init() }, methods: { init(){ this.getChartData() }, //分类及工时情况 getChartData(){ var chChartBar = echarts.init(this.$refs.chart); this.echartBar(chChartBar,this.data) }, echartBar(chChart,chartData){ let newPromise = new Promise((resolve) => { resolve() }) //然后异步执行echarts的初始化函数 newPromise.then(() => { var value = []; var prop = []; var text = []; var zero = [] var bgd = [] var total = 0 for (let i = chartData.length-1; i >=0; i--) { total += +chartData[i].value; value.push(chartData[i].value) prop.push(chartData[i].prop) text.push(chartData[i].text) bgd.push(100) zero.push(0) } var data = [] var data_all = new Array(prop.length) for (let i = 0; i { //富文本固定格式{colorName|这里填你想要写的内容} if(this.showval){ return '{arrow|}{index|No.'+(chartData.length-data.dataIndex)+'}{name|' + text[data.dataIndex] + '}{prop|' +value[data.dataIndex]+ ' ' +prop[data.dataIndex] + '} {color| %}'; } return '{arrow|}{index|No.'+(chartData.length-data.dataIndex)+'}{name|' + text[data.dataIndex] + '}{prop|' +prop[data.dataIndex] + '} {color| %}'; }, } }, data: value }, { type: "bar", stack: "总", barWidth: 6, itemStyle: { normal: { barBorderColor: 'rgba(0,0,0,0)', color: 'rgba(0,0,0,0)' } }, data: data_all }, ] for (let i = 0; i < data.length; i++) { series.push({ type: "bar", barWidth: 6, stack: "总", // itemStyle: { // normal: { // color: '#0780d9' // } // }, itemStyle: { normal: { color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [ { offset: 0, color: "#41adf5", }, { offset: 1, color: "#175eac", }, ]), barBorderRadius: [5,5,5,5], }, }, data: data[i] }) } this.option = { grid: { left: '2%', right: '2%', bottom: '-8%', top: '3%', containLabel: true }, yAxis: { type: 'category', axisLabel: { show: false, //让Y轴数据不显示 }, itemStyle: { }, axisTick: { show: false, //隐藏Y轴刻度 }, axisLine: { show: false, //隐藏Y轴线段 }, data: text, }, xAxis: [{ show: false, }, //由于下边X轴已经是百分比刻度了,所以需要在顶部加一个X轴,刻度是金额,也隐藏掉 { show: false, } ], series: series }; chChart.setOption(this.option); window.onresize = chChart.resize; }) }, }, watch:{ data:function () { this.init() } } })