当柱形图的柱子颜色渐变时,重新定柱形图的tooltip-formmatter

 

当柱形图的柱子颜色渐变时,marker取到的是[object object],因此,需要手动设置新的值

option = {
    tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
        },
        formatter: function(params) {
            var str = params[0].name + ":<br>";
            let marker = "";
            let unit = "";
            let marker1 = "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:rgba(2,242,180,1);'></span>";
            let marker2 = "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:rgba(2,209,223,1);'></span>";
            for (var i = 0; i < params.length; i++) {
                if (i % 2 === 0) { //偶数
                    marker = marker1;
                    unit = "人";
                } else { //基数
                    marker = marker2;
                    unit = "座";
                }
                console.log(i);
                str += marker + params[i].seriesName + ":" + params[i].value + unit + "<br>";
            }
            return str;
        }
    },
    legend: {
        data: ['人口', '城市'],
        top:"15%"
    },
    grid: {
        left: '20%',
        right: '20%',
        bottom: '20%',
        top: '20%',
        containLabel: true
    },
    xAxis: [{
        type: 'category',
        data: ['中国', '美国', '韩国', '日本', '泰国']
    }],
    yAxis: [{
        type: 'value'
    }],
    series: [{
            name: '人口',
            type: 'bar',
            data: [320, 332, 301, 334, 390, 330, 320],
            itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 0, 1,
                        [{
                                offset: 0,
                                color: 'rgba(2,242,180,.2)'
                            },
                            {
                                offset: 1,
                                color: 'rgba(2,242,180,1)'
                            }
                        ]
                    )
                }

            }
        },
        {
            name: '城市',
            type: 'bar',
            data: [120, 132, 101, 134, 90, 230, 210],
            itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 0, 1,
                        [{
                                offset: 0,
                                color: 'rgba(2,209,223,.2)'
                            },
                            {
                                offset: 1,
                                color: 'rgba(2,209,223,1)'
                            }
                        ]
                    )
                },
            },

        }
    ]
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在Matplotlib中,柱形图柱子颜色可以通过设置`color`参数来实现。 例如,以下代码将柱形图柱子颜色设置为红色: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] plt.bar(x, y, color='r') plt.show() ``` 如果想要每个柱子颜色不同,可以将颜色值以列表的形式传递给`color`参数,例如: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] colors = ['r', 'g', 'b', 'y', 'k'] plt.bar(x, y, color=colors) plt.show() ``` 这将生成一个五个柱子颜色分别为红色、绿色、蓝色、黄色和黑色的柱形图。 ### 回答2: 在Matplotlib中,柱形图柱子颜色可以通过`color`参数来指。`color`参数可以接受一个字符串(表示颜色的名称),也可以接受一个RGB元组或一个十六进制颜色码。 首先,我们可以使用颜色的名称来指柱子颜色。例如,`color='red'`将柱子颜色设置为红色。 其次,我们可以使用RGB元组来指柱子颜色。RGB是红绿蓝三个颜色通道的数值,取值范围为0到1(0表示最小强度,1表示最大强度)。例如,`color=(0.2, 0.4, 0.6)`将柱子颜色设置为淡蓝色。 最后,我们可以使用十六进制颜色码来指柱子颜色。十六进制颜色码由一个井号(#)和六位十六进制数组成,每两位表示一个颜色通道(红绿蓝)。例如,`color='#FFA500'`将柱子颜色设置为橙色。 在Matplotlib的柱形图中,可以通过在`plt.bar()`函数中传入`color`参数来指柱子颜色。例如: ``` import matplotlib.pyplot as plt # 创建数据 x = [1, 2, 3, 4] y = [10, 20, 30, 40] # 绘制柱形图 plt.bar(x, y, color='red') # 显示图形 plt.show() ``` 以上代码将绘制一个红色的柱形图。您可以根据需要选择不同的颜色名称、RGB元组或十六进制颜色码来指柱子颜色。 ### 回答3: 在matplotlib中绘制柱形图,可以通过设置参数来调整柱子颜色。具体的实现方式有以下几种: 1. 使用预义的颜色:可以通过设置参数`color`来指柱形图颜色。例如,`color='red'`可以将柱子设置为红色,`color='blue'`可以将柱子设置为蓝色。 2. 使用RGB颜色:可以通过设置参数`color`为一个RGB元组来指柱子颜色。RGB元组由三个值组成,分别表示红、绿、蓝三个通道的颜色强度。例如,`color=(0.5, 0.2, 0.9)`可以将柱子设置为紫色。 3. 使用颜色映射:可以通过设置参数`cmap`来指柱子颜色映射,将柱子的高度值映射为具体的颜色。常用的颜色映射有`viridis`、`hot`、`cool`等。例如,`cmap='viridis'`可以将柱子的高度值映射为绿色到黄色的渐变效果。 除了以上方法,还可以通过自义Colormap对象来实现更加独特的柱子颜色。在自义过程中,可以指每个高度值对应的具体颜色,以及渐变的方式等。 综上所述,通过设置参数,我们可以轻松地对柱形图柱子颜色进行个性化的调整,以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佛佛ง

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值