123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <template>
- <div class="lineBarCharts" ref="lineBarCharts" />
- </template>
- <script>
- import * as echarts from 'echarts'
- export default {
- name: '',
- props: {
- title: {
- type: String,
- default: ''
- },
- xAxisData: {
- type: Array,
- default: []
- },
- seriesData: {
- type: Object,
- default: {}
- }
- },
- data () {
- return {
- uEcharts: null,
- uOption: null,
- }
- },
- watch: {
- seriesData: {
- handler(newValue, oldValue){
- console.log(newValue, oldValue)
- if(newValue){
- this.setSeriesData(newValue)
- }
- },
- deep: true,
- // immediate: true
- }
- },
- created () {
- },
- mounted () {
- this.$nextTick(() => {
- this.initChart()
- this.setSeriesData()
- })
- },
- methods: {
- initChart(){
- this.uEcharts = echarts.init(this.$refs.lineBarCharts)
- let color = ['#42CEAF', '#FF882A', '#16B4FE', '#747AFE','#f9c129', '#13c2c2', '#79ce42', '#f55837'];
- // fc8452
- this.uOption = {
- // title: {
- // text: this.title + '图',
- // left: 'center'
- // },
- color: color,
- tooltip: {
- trigger: 'axis',
- formatter: function (params) {
- console.log(params)
- let str = params[0].name + "<br />";
- params.forEach((item) => {
- str +=
- '<span style="display:inline-block;margin-right:5px;border-radius:50%;width:10px;height:10px;left:5px;background-color:'+item.color+'"></span>' + item.seriesName + " : " + (Number(item.value) / 10000).toFixed(2) +'万元' + "<br />";
- });
- return str;
- },
- },
- grid: {
- left: '7%',
- right: '5%',
- bottom: '10%',
- },
- legend: {
- type: 'scroll',
- width: '60%',
- // orient: 'vertical',
- // right: 90,
- // left: 20,
- top: 0,
- // bottom: 20,
- // selectedMode: 'single',
- padding: [
- 5, // 上
- 150, // 右
- 0, // 下
- 20, // 左
- ],
- },
- toolbox: {
- show: true,
- feature: {
- // dataView: { readOnly: false }, // 展示数据--不过没用
- magicType: { type: ['bar', 'line'] },
- // restore: {}, // 还原
- saveAsImage: {}
- }
- },
- // dataZoom: [
- // {
- // startValue: '2023-02-01'
- // },
- // {
- // type: 'inside'
- // }
- // ],
- xAxis: {
- type: 'category',
- // boundaryGap: true, // 解决数据溢出X轴的问题
- data: this.xAxisData
- },
- yAxis: {
- type: 'value',
- axisLabel: {
- // 可以动态设置是数量还是金额
- // formatter: '{value} 万元'
- // 设置动态单位转换
- formatter: function (value, index) {
- // if (value >= 100000000) {
- // return value / 100000000 + "亿";
- // } else if (value >= 10000000) {
- // return value / 10000000 + "千万";
- // } else if (value >= 1000000) {
- // return value / 1000000 + "百万";
- // } else if (value >= 100000) {
- // return value / 100000 + "十万";
- // } else if (value >= 10000) {
- // return value / 10000 + "万";
- // } else if (value >= 1000) {
- // return value / 1000 + "千";
- // } else {
- // return value;
- // }
- // if(value > 0){
- // return (Number(value) / 10000).toFixed(1) + "万"
- // } else {
- // return value;
- // }
- return (Number(value) / 10000).toFixed(1) + "万"
- }
- }
- },
- series: [
- // {
- // name: 'Highest',
- // type: 'line',
- // data: [10, 11, 13, 11, 12, 12, 9],
- // markPoint: {
- // data: [
- // { type: 'max', name: 'Max' },
- // { type: 'min', name: 'Min' }
- // ]
- // },
- // // markLine: {
- // // data: [{ type: 'average', name: 'Avg' }]
- // // }
- // },
- ]
- };
- this.uOption && this.uEcharts.setOption(this.uOption);
- window.addEventListener('resize', ()=>{
- this.uEcharts.resize()
- })
- },
- setSeriesData(data){
- // console.log(this.uEcharts)
- var seriesData = data ? data : this.seriesData
- console.log(seriesData)
- // var option = this.uOption
- this.uOption.xAxis.data = this.xAxisData
- this.uOption.series = seriesData.seriesData.map(res => {
- return {
- name: res.name,
- type: 'bar',
- smooth: true,
- label: {
- show: true,
- formatter: function (params) {
- if(params.value === 0){
- return ''
- } else {
- return (Number(params.value) / 10000).toFixed(2);
- }
- },
- },
- data: res.data,
- }
- })
- // this.uOption.series.unshift({
- // name: '总数',
- // type: 'line',
- // smooth: true,
-
- // // label: {
- // // normal: {
- // // offset:['50', '80'],
- // // show: true,
- // // position: 'insideBottom',
- // // formatter:'{c}',
- // // textStyle:{ color:'#000' }
- // // }
- // // },
- // // itemStyle:{
- // // normal:{
- // // color:'rgba(128, 128, 128, 0)'
- // // }
- // // },
- // data: seriesData.sumZcbData,
- // })
- // console.log(this.uOption.series)
- this.uEcharts.hideLoading()
- this.uEcharts.setOption(this.uOption, true)
- }
- }
- }
- </script>
- <style lang="less" scoped>
- .lineBarCharts{
- width: 100%;
- height: 100%;
- }
- </style>
|