echar设置label位置_echarts柱状图的label设置,动态设置position

本文展示了如何在ECharts中根据数值正负动态设置柱状图label的位置,当数值大于零时,label显示在柱状图上方,反之显示在下方。通过示例代码详细解释了配置项的使用,包括设置颜色渐变、自定义标签内容和位置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看效果:图一:

a51d9bec997b78185a3c6fd8d30a3bb5.png

图二:

d7d4d2e8a7a72f6dc6043a14ba43e0b4.png

数值大于零的在x轴上方,label在柱状图上方,小于零的在x轴下方,label在轴下方;相关代码如下:

import { fontSize } from "@/utils/fn";

import ChartTitle from "@/components/welcome/chartTitle";

import bus from "@/components/js/bus";

let fs16 = fontSize(0.16);

let fs36 = fontSize(0.36);

let fs18 = fontSize(0.18);

let list = [];

export default {

components: { ChartTitle },

data() {

return {

myChart: null,

myChart2: null,

};

},

props: {

config: {

type: Object,

default: () => {},

},

},

mounted() {

// window.addEventListener("resize", this.resizeMyChart);

// bus.$on("collapse", (msg) => {

// });

this.initChart();

this.initChart2();

},

destroyed() {

// window.removeEventListener("resize", this.resizeMyChart);

},

methods: {

initChart() {

this.myChart = this.$echarts.init(document.getElementById("areaRate"));

let colorList = [

{

type: "linear",

x: 0,

y: 0,

x2: 0,

y2: 1,

colorStops: [

{

offset: 0,

color: this.config.oneTopColors[0], // 0% 处的颜色

},

{

offset: 1,

color: this.config.oneTopColors[1], // 100% 处的颜色

},

],

global: false, // 缺省为 false

},

{

type: "linear",

x: 0,

y: 0,

x2: 0,

y2: 1,

colorStops: [

{

offset: 0,

color: this.config.oneBottomColors[0], // 0% 处的颜色

},

{

offset: 1,

color: this.config.oneBottomColors[1], // 100% 处的颜色

},

],

global: false, // 缺省为 false

},

];

let option = {

grid: {

left: '5%',

right: '3%',

},

tooltip: {

formatter: "{b}
{a}:{c}",

},

toolbox: {

show: false,

},

xAxis: [

{

position: "bottom",

type: "category",

nameLocation: "middle",

offset:15,

nameGap: 0,

data: this.config.dataX,

splitLine:{

show:false,

color: ["#BFCCE3"]

},

axisTick:{

show:false

},

axisLine: {

lineStyle: {

color: 'rgb(191,204,227)',

type: 'solid'

}

}

},

],

yAxis: [

{

type: "value",

show: true,

axisLine: {

lineStyle: {

color:'#fff',

width:'2'

}

},

splitLine: {

show: true,

lineStyle: {

color:['#BFCCE3'],

type: 'dashed'

}

},

axisLabel:{

show: true,

color: "#A9B6CD",

formatter: '{value}%'

}

},

],

series: [

{

name: "学段课收增长率",

type: "bar",

fontSize: 20,

barWidth: 60,

// barGap: 1,

// barCategoryGap: 20,

data: this.config.dataTop.map(item => {

return {

value: item,

label: {

// 设置显示label

show: true,

// 设置label的位置

position: item > 0 ? 'top' : 'bottom',

// 设置label的文字颜色

color: '#6783EE',

// 格式化label文字

formatter: item > 0 ? '+'+'{c}%' : '{c}%'

},

itemStyle:{

color: item > 0 ? colorList[0] : colorList[1],

barBorderRadius: item > 0 ? [5, 5, 0, 0] : [0, 0, 5, 5]

}

}

}),

},

],

};

this.myChart.setOption(option, true);

},

initChart2() {

this.myChart2 = this.$echarts.init(document.getElementById("difference"));

let colorList = [

{

type: "linear",

x: 0,

y: 0,

x2: 0,

y2: 1,

colorStops: [

{

offset: 0,

color: this.config.twoTopColors[0], // 0% 处的颜色

},

{

offset: 1,

color: this.config.twoTopColors[1], // 100% 处的颜色

},

],

global: false, // 缺省为 false

},

{

type: "linear",

x: 0,

y: 0,

x2: 0,

y2: 1,

colorStops: [

{

offset: 0,

color: this.config.twoBottomColors[0], // 0% 处的颜色

},

{

offset: 1,

color: this.config.twoBottomColors[1], // 100% 处的颜色

},

],

global: false, // 缺省为 false

},

];

let option = {

// 直角坐标系内绘图网格

grid: {

left: '5%', // grid 组件离容器左侧的距离

right: '3%', // grid 组件离容器右侧的距离

},

// 提示框设置

tooltip: {

// 提示框浮层内容格式器

formatter: "{b}
{a}:{c}",

},

toolbox: {

show: false,

},

xAxis: [

{

position: "top", // x 轴的位置。

type: "category", // 坐标轴类型。

nameLocation: "middle", // 坐标轴名称显示位置。

offset:15, // X 轴相对于默认位置的偏移

nameGap: 0, // 坐标轴名称与轴线之间的距离。

data: this.config.dataX,

// 分割线设置

splitLine:{

show:false

},

// 坐标轴刻度相关设置。

axisTick:{

show:false

},

// 坐标轴轴线相关设置。

axisLine: {

lineStyle: {

color: 'rgb(191,204,227)',

type: 'solid'

}

}

},

],

yAxis: [

{

type: "value",

show: true,

// 坐标轴轴线相关设置

axisLine: {

lineStyle: {

color:'#fff', // 坐标轴线线的颜色

width:'2' // 坐标轴线线的宽度

}

},

// y轴分割线设置

splitLine: {

show: true, // 是否显示分割线

lineStyle: {

color:['#BFCCE3'], // 分割线颜色

type: 'dashed' // 分割线类型

}

},

// 坐标轴刻度标签的相关设置。

axisLabel:{

show: true, // 坐标轴标签是否显示

color: "#A9B6CD", // 坐标轴标签颜色

formatter: '{value}%' // 坐标轴标签文案

}

},

],

series: [

{

name: "学段课收差额",

type: "bar",

fontSize: 20,

// 标签设置

label: {

normal: {

show: true, // 标签

// 标签内容格式化

formatter: function(data){

if(data.value > 0){

return '{a|' + '+' + data.value + '%}'

} else {

return '{b|' + data.value + '%}'

}

},

// 自定义样式

rich: {

a: {

// 颜色设置

color: '#F68C9D',

},

b: {

// 颜色设置

color: '#74C7C6'

}

}

}

},

// 柱条的宽度

barWidth: 60,

data: this.config.dataBottom.map(item => {

return {

value: item,

label: {

// 设置显示label

show: true,

// 设置label的位置

position: item > 0 ? 'top' : 'bottom',

},

itemStyle: {

// 设置柱条的背景色

color: item > 0 ? '#F68C9D' : '#74C7C6',

barBorderRadius: item > 0 ? [5, 5, 0, 0] : [0, 0, 5, 5] // 设置倒角

}

}

}),

},

],

};

this.myChart2.setOption(option, true);

},

},

};

#areaRate {

height: 2.8rem;

}

#difference {

height: 2.8rem;

}

根据提供的引用内容,可以使用Echarts来绘制柱状图来表示男女比例。首先,需要引入Echarts库并创建一个容器来展示图表。根据引用中的代码,可以使用`echar.HistogramL`函数来绘制柱状图,其中`seriesData`参数可以设置男女比例的数据。同时,可以使用`yAxisData`参数来设置男女的分类信息。男性和女性的比例可以通过正负数来表示,例如男性的比例为正数,女性的比例为负数。通过设置不同的数据,可以绘制出横向的柱状图,其中柱形的长度表示男女比例的大小。例如,如果男性比例为320,女性比例为-332,那么男性的柱形将比女性的柱形长。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [echarts横向柱状图](https://download.youkuaiyun.com/download/qq_42396791/11856336)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [ECharts2.0横向显示柱状图](https://blog.youkuaiyun.com/www93111/article/details/78115592)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Echarts实现男女比例图表](https://blog.youkuaiyun.com/weixin_58099903/article/details/127928584)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值