echarts画的柱状图

在这里插入图片描述

<template>
    <div id="airportflight" style="width:100%;" class="bar-wrapper" ref="airportflight"></div>
</template>
<script>
  import Echarts from 'echarts'
    import * as echarts from 'echarts';
  export default {
    name: "airportflight",
    data() {
      return {

      }
    },
    props: {
		//数组
	    DrugTherapyChart: {
	      type: Array,
	      default: () => []
	    },
        ChartHeight:'',
        ChartWidth:'',
    },
    // props: ['yData','xData','seriesData','barLegend', 'color'],
    created() {
        let Create2 =
            setTimeout(() => {
            clearTimeout(Create2)
            this.fetchData()  
            }, 300)
    },
    mounted: function () {
        let clientWidth2 = document.documentElement.clientWidth -200;
        let myChart = document.getElementById('airportflight');
        myChart.style.width = clientWidth2 +'px';
        myChart.style.height = this.ChartHeight;
        Echarts.init(myChart);
        let _this = this;
        window.addEventListener("resize", function () {
        let myChart = document.getElementById('airportflight');
        myChart.style.width = clientWidth2 +'px';
        myChart.style.height = this.ChartHeight;
        });
    },
    methods: {
        barColor(){
            var colorList = [
                                '#C1232B','#B5C334','#FCCE10','#E87C25','#27727B',
                                '#FE8463','#9BCA63','#FAD860','#F3A43B','#60C0DD',
                                '#D7504B','#C6E579','#F4E001','#F0805A','#26C0C0',
                                '#FFB7DD','#660077','#FFCCCC','#FFC8B4','#550088',
                                '#FFFFBB','#FFAA33','#99FFFF','#CC00CC','#FF77FF',
                                '#CC00CC','#C63300','#F4E001','#9955FF','#66FF66',
                                '#C1232B','#B5C334','#FCCE10','#E87C25','#27727B',
                                '#FE8463','#9BCA63','#FAD860','#F3A43B','#60C0DD',
                                '#D7504B','#C6E579','#F4E001','#F0805A','#26C0C0',
                                '#FFB7DD','#660077','#FFCCCC','#FFC8B4','#550088',
                                '#FFFFBB','#FFAA33','#99FFFF','#CC00CC','#FF77FF',
                                '#CC00CC','#C63300','#F4E001','#9955FF','#66FF66'
                                
                                
                        ];
            return colorList;              
        },
        fetchData() {
            console.log(4445555,this.DrugTherapyChart)
            var chartDom = document.getElementById('airportflight');
            var myChart = echarts.init(chartDom);
            var option;
            let that = this;
            // console.log(6666,document.getElementById("airportflight").style.height)
            this.$nextTick(()=>{
        option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器,坐标轴触发有效
                    type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
                },
                //提示框浮层内容格式器,支持字符串模板和回调函数两种形式。
                formatter: function (params) {
                    // console.log(197,params)
                    var tar;
                    if (params[1].value !== '-') {
                        tar = params[1];
                    }
                    else {
                        tar = params[2];
                    }
                    let StartDate2 = ''
                    StartDate2 = that.DrugTherapyChart.yAxis[tar.dataIndex].Usage.StartDate
                    let GiveStrength = ''
                    GiveStrength = that.DrugTherapyChart.yAxis[tar.dataIndex].Usage.GiveStrength + ' ' + 
                        that.DrugTherapyChart.yAxis[tar.dataIndex].Usage.Frequency + ' ' +  
                        that.DrugTherapyChart.yAxis[tar.dataIndex].Usage.AdminRoute
                    return '药品名:' + tar.name + '<br/>' + '天数:' + tar.value + '<br/>' + '开嘱日期:' + StartDate2;
                    // return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value + '<br/>' + '日期' + StartDate2;
                }
            },
            
            grid: {
                // height: 300,
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'value',
                max: function (value) {
                    return value.max + 4;
                }
            },
            yAxis: {
                type: 'category',
                splitLine: {show: false},
                lineStyle: {
                    width:1
                },
                data: function () {
                    var list = [];
                    that.DrugTherapyChart.yAxis&&that.DrugTherapyChart.yAxis.forEach((item) => {
                        list.push(item.DrugName);
                    });
                    return list;
                }()
            },
            series: [
                {
                    name: '辅助',
                    type: 'bar',
                    stack: '总量',
                    itemStyle: {
                        barBorderColor: 'rgba(0,0,0,0)',
                        color: 'rgba(0,0,0,0)'
                    },
                    emphasis: {
                        itemStyle: {
                            barBorderColor: 'rgba(0,0,0,0)',
                            color: 'rgba(0,0,0,0)'
                        }
                    },
                    // data: [0, 2, 2, 3, 1, 1]
                    data: function () {
                        var list = [];
                        // let aa = 0
                        // list.push(aa);
                        that.DrugTherapyChart.Series&&that.DrugTherapyChart.Series.forEach((item,index) => {
                            if (item.Name=='辅助') {
                                list = item.Data
                            }
                            // aa += item.Usage.Duration
                            // list.push(aa);
                        });
                        return list;
                    }()
                },
                {
                    name: '长嘱',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        show: true,
                        position: 'insideLeft',
                        color:'#333',
                        fontSize:10,
                        formatter:function(params) {
                            let GiveStrength = ''
                            GiveStrength = that.DrugTherapyChart.yAxis[params.dataIndex].Usage.GiveStrength + ' ' +
                            that.DrugTherapyChart.yAxis[params.dataIndex].Usage.Frequency + ' ' +  
                            that.DrugTherapyChart.yAxis[params.dataIndex].Usage.AdminRoute
                            return GiveStrength 
                            //return that.DrugTherapyChart.yAxis[params.dataIndex].Usage.AFG
                        }
                    },
                    // data: ['2', 3, 3, 4, '-', '-']
                    // color: ["#d83c4d", "#e95831", "#e95831", "#e95831", "#ee8131", "#f9b12f", "#f9b12f", "#fcd81d", "#c8ce2f", "#8cb539"],
                    data: function () {
                        var list = [];
                        that.DrugTherapyChart.Series&&that.DrugTherapyChart.Series.forEach((item,index) => {
                            if (item.Name=='长嘱') {
                                list = item.Data
                            }
                        });
                        return list;
                    }(),
                    itemStyle: {   
                    //通常情况下:
                        normal:{
                    // 每个柱子的颜色即为colorList数组里的每一项,如果柱子数目多于colorList的长度,则柱子颜色循环使用该数组
                            color: function (params){
                                let ColorList = []
                                that.DrugTherapyChart.yAxis&&that.DrugTherapyChart.yAxis.forEach((item) =>{
                                    ColorList.push(item.Usage.Color)
                                })
                                return ColorList[params.dataIndex];
                            }
                        }
                        
                    },
                    barWidth : '50%',//柱图宽度
                },

                // {
                //     name: '临嘱',
                //     type: 'bar',
                //     stack: '总量',
                //     label: {
                //         show: true,
                //         position: 'insideLeft',
                //         color:'#333',
                //         fontSize:10,
                //         formatter:function(params) {
                //             let GiveStrength = ''
                //             GiveStrength = that.DrugTherapyChart.yAxis[params.dataIndex].Usage.GiveStrength + ' ' +
                //                 that.DrugTherapyChart.yAxis[params.dataIndex].Usage.Frequency + ' ' +  
                //                 that.DrugTherapyChart.yAxis[params.dataIndex].Usage.AdminRoute
                //             return GiveStrength 
                //         }
                //     },
                //     // data: ['-', '-', '-', '-', 1, 1]
                //     data: function () {
                //         var list = [];
                //         that.DrugTherapyChart.Series.forEach((item,index) => {
                //             if (item.Name=='临嘱') {
                //                 list = item.Data
                //             }
                //         });
                //         return list;
                //     }(),
                //     color: '#91cc75',
                //     barWidth : '50%',//柱图宽度
                // },

            ]
        };

        option && myChart.setOption(option);
        //如果给浏览器窗口放大放小,图表要跟着变动大小
        window.onresize = () => {
	       console.log(2333)
	        myChart.resize();
	      }
        })



        // let bar = Echarts.init(this.$refs.airportflight);
        // console.log('this.barLegend',this.barLegend)
        // console.log('this.xData',this.xData)
        // console.log('this.yData',this.yData)
        // console.log('this.color',this.color)
        // console.log('this.seriesData',this.seriesData)
        // const options = {
        //   tooltip: {
        //     trigger: 'axis',
        //     axisPointer: {
        //       // type: 'cross',  // y轴辅助显示线
        //       crossStyle: {
        //         color: '#999'
        //       }
        //     }
        //   },
        //   color: this.color,
        //   legend: this.barLegend,
        //   xAxis: this.xData,
        //   yAxis: this.yData, // y轴信息
        //   series: this.seriesData,
        // };
        // bar.setOption(options);
      }
    },
    watch: {
        DrugTherapyChartChange(value){
            console.log(330,value)
            if (value == true) {
                this.fetchData()
            }
        }
    //   yData(value) {
    //     this.fetchData();
    //   },
    //   seriesData(val) {
    //     console.log(111)
    //     this.fetchData();
    //   }
    }
  }
</script>

<style scoped>
  .bar-wrapper {
    width: calc(100%);
    height: calc(100%);
  }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值