ECharts histogram achieves rendering from the specified number of y-axis instead of always rendering from y=0

Let's take a look at the comparison chart first. The following picture is the result of putting the data into the echarts statistical chart without processing it (just look at the blue column). You can see that the histogram is based on y=0 regardless of whether the data is positive or negative. It is the starting point of the graph.
insert image description here
The following figure is the effect I want to achieve, which is equivalent to rendering the column with the minimum value of the y-axis as the starting point:
insert image description here
without further ado, let’s go directly to the solution:
Solution 1: If the echarts version is below 4.0.0 (not recorded If it is wrong), then the implementation is very simple.
If you don’t know what version of eachcharts you are using, you can print console.log(echarts.version); to see what version it is, it is very likely that the version higher than 4.0 cannot be reached For this effect,
just add the attribute axisLine.onZero = false in xAxis, the code is as follows:

xAxis: {
    
    
                data: ['衬衫11111111111111111', '羊毛衫', '雪纺衫', '裤子', '高跟鞋'],
                axisLine: {
    
    
                    onZero: false, //轴线是否在0刻度轴上
                },

            },

If axisLine.onZero = false is used but the effect cannot be achieved, you can directly refer to the following version to see if it can be achieved. If the effect can be achieved, it is likely to be a problem with the echarts version

<script crossorigin="anonymous" integrity="sha384-mYHbpb8SNpRR9uL7PfZoWk1rI3/VXsAkIC5Sy7+Aa7a79JKqZ19qg4OcgvgsCx36"
    src="https://lib.baomitu.com/echarts/4.0.0/echarts.min.js"></script>

(ps: I read the problem records on echarts GitHub before. After version 5.5.0, you can use barSeries.startValue to set the starting value, but it seems that this version has not been released yet. For specific questions, please refer to: https://github.com /apache/echarts/pull/17078)

Solution 2: Use stacked histograms to achieve
ideas:
the code is as follows:

 <body>
    <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div id="main" style="width: 600px;height:500px;"></div>
 <script type="text/javascript">
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
 let xData = ['衬衫11111111111111111', '羊毛衫', '雪纺衫', '裤子', '高跟鞋']
        let data1 = [20, 8, -18, -26, -37] //bar1数据
        let data2 = [] //bar2数据
        // 计算y轴最小值(根据自己实际情况而定)-------------------------
        let yMin = Math.ceil(Math.min(...data1))
        if (yMin < 0) {
    
    
            let numConvert = yMin;
            numConvert = yMin * -1
            let str = numConvert.toString().length;
            let arr = [1, 10, 100, 1000, 10000]
            let digit; //位数
            let num = 1
            if (str === 1) {
    
    
                // 个位
                digit = parseInt(yMin % 10);
                if (digit < 0) {
    
    
                    num = -1
                }
                yMin = num * 10
            } else {
    
    
                digit = parseInt((yMin % arr[str]) / arr[str - 1]);
                if (digit < 0) {
    
    
                    num = -1
                }
                yMin = (digit + num) * arr[str - 1]
            }
            //--------------------------------------------------
            //bar2值设置
            data1.forEach(item => {
    
    
                if (item > 0) {
    
    
                    data2.push(yMin)
                } else {
    
    
                    data2.push(item + yMin)
                }
            })
        } else {
    
    
            data2 = [0, 0, 0, 0, 0] //我这是柱状图固定只显示5个柱子,实际情况自行改逻辑
        }
         var option = {
    
    
          tooltip: {
    
    
                show: true,
                trigger: 'axis',
                formatter: function (par) {
    
    
                    return `<div>${
      
      par[0].name}</div><div>光功率:${
      
      par[0].value}</div>`
                }
            },
          xAxis: {
    
    
                data: xData,
                 axisLabel: {
    
    
                    // 文字省略
                    formatter: function (value) {
    
    
                        // console.log('999--', value);
                        if (value.length > 3) {
    
    
                            return `${
      
      value.slice(0, 3)}...`
                        }
                        return value
                    }
                },
                axisLine: {
    
    
                    onZero: false, //轴线是否在0刻度轴上
                },

            },
             yAxis: {
    
    
                splitLine: {
    
     show: false },
                min: yMin < 0 ? yMin : 0, //y最小值
            },
             series: [
             {
    
    
                    name: '销量',
                    type: 'bar',
                    stack: 'one', //设置堆叠图
                    data: data1.map(item => {
    
    
                        return {
    
    
                            value: item,
                            label: {
    
    
                                formatter: '{c}' + 'db',
                                show: item > 0 ? true : false, //bar1数据大于0顶部显示提示文字
                                position: 'top',
                                textStyle: {
    
    
                                    color: 'blue'
                                },
                            },
                            itemStyle: {
    
    
                                // 边框
                                borderColor: item > 0 ?"blue":"rgba(240, 240, 240,0)",
                            }
                        }
                    }),
                    barWidth: '50px', // 柱子宽度
                    itemStyle: {
    
    
                        normal: {
    
    
                        //bar1颜色控制,小于0,颜色与背景色相同,否则为蓝色
                            color: function (param) {
    
    
                                let color = "blue"
                                if (param.value < 0) {
    
    
                                    color = "rgba(240, 240, 240,0)"
                                }
                                return color
                            },
                        }
                    },
                    //设置柱状图背景
                    showBackground: true,
                    backgroundStyle: {
    
    
                        color: 'rgba(180, 180, 180, 0.2)'
                    },
                },
                {
    
    
                    name: 'bar2',
                    type: 'bar',
                    stack: 'one',
                    itemStyle: {
    
    
                        normal: {
    
    
                            color: "blue",
                            borderColor: "blue", //边框
                        }
                    },
                    barWidth: '50px', // 柱子宽度
                    data: data2.map((item, index) => {
    
    
                        return {
    
    
                            value: item,
                            label: {
    
    
                                formatter: data1[index] + 'db',
                                //bar1当前数据小于0,bar2顶部显示提示文字,否则不显示
                                show: data1[index] < 0 ? true : false,
                                position: 'top',
                                textStyle: {
    
    
                                    color: 'blue'
                                },
                            },
                        }
                    }),
                },
             ]


         }
          // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
        </script>
        </body>

End~~~ (The method is not necessarily very good, it is for reference only)

Guess you like

Origin blog.csdn.net/Hyanl/article/details/131974843