async createChartStep2(type, title, barData, barData2,lineData,months,yearToMonth) {
const chart = echarts.init(document.getElementById("chartStep2-" + type), null, { renderer: 'svg' });// 替换默认的 Canvas
let stepTwoData = {}
stepTwoData = await this.getProjectInitiationTrendChartStepTwo(title,yearToMonth)
let barWidth = stepTwoData.deptNameList&&stepTwoData.deptNameList.length > 1 ? 'auto' : '15%'; // 动态调整柱形图宽度
const option = {
animation: true,
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'none'
},
formatter: function (params) {
// 默认圆点样式
const defaultDot = (color) => `<span style="display:inline-block;margin-right:5px;border-radius:50%;width:10px;height:10px;background-color:${color};"></span>`;
// 构建 tooltip 内容
let tooltipContent = `<div style="font-size:14px;margin-bottom:10px;">${params[0].name}</div>`;
params.forEach(param => {
const value = param.seriesIndex === 2 ? `${param.value}%` : param.value; // 显示为百分比
tooltipContent += `
<div style="margin-bottom:5px;">
${defaultDot(param.color)}
<span style="font-size:14px;">${param.seriesName} : ${value}</span>
</div>
`;
});
return tooltipContent;
}
},
legend: {
data: this.legends[title],
top: 0
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
data: stepTwoData.deptNameList?stepTwoData.deptNameList:[],
axisLabel: {
fontSize: 12,
formatter: function (value) {
// 将字符串拆分为单个字符,并用 \n 连接
return value.toString().split('').join('\n');
}
}
},
yAxis: [
{
type: 'value',
name: title.includes('资金')?'金额(万元)':'数量',
position: 'left',
splitLine: {
show: true, // 显示背景线
lineStyle: {
type: "dotted", // 设置为点线
color: "#d6d6d6", // 背景线的颜色
width: 1, // 背景线的宽度
},
},
},
{
type: 'value',
name: '百分比(%)',
min: 0,
max: 100,
position: 'right',
axisLabel: {
formatter: '{value}%'
},
splitLine: {
show: false, // 显示背景线
lineStyle: {
type: "dotted", // 设置为点线
color: "#d6d6d6", // 背景线的颜色
width: 1, // 背景线的宽度
},
},
}
],
series: [
{
name: this.legends[title][0],
type: 'bar',
barMaxWidth: barWidth,
data: (type=='zijin1' || type=='zijin2' )?(stepTwoData.planPaymentAmountList || []):(stepTwoData.sumList || []),
itemStyle: {
color: '#2563eb'
},
label: {
normal: {
show: true,
position: 'top',
distance: 15, // 上浮15像素
verticalAlign: 'bottom', // 文字底部对齐标签点
fontSize: 14,
fontWeight: "bold",
formatter: function (params, ticket, callback) {
return params.value ? params.value : '';
}
}
}},
{
name: this.legends[title][1],
type: 'bar',
barMaxWidth: barWidth,
data: (type=='zijin1' || type=='zijin2' )?(stepTwoData.expectExpenditureAmountList || []):(stepTwoData.numList || []),
itemStyle: {
color: '#f97316'
},
label: {
normal: {
show: true,
position: 'top', // 改为底部显示
distance: 2, // 上浮15像素
verticalAlign: 'bottom', // 文字底部对齐标签点
fontSize: 14,
fontWeight: "bold",
formatter: function (params, ticket, callback) {
return params.value ? params.value : '';
}
}
}},
{
name: this.legends[title][2],
type: 'line',
yAxisIndex: 1,
data: stepTwoData.rateList,
itemStyle: {
color: '#22c55e'
},
label: {
show: true,
formatter: '{c}%',
position: 'top',
fontSize: 15,
fontWeight: "bold",
}
}
]
};
chart.setOption(option);
let than = this;
// 绑定事件前先移除旧的事件
chart.off('click'); // 以 click 事件为例
chart.on("click", function (params) {
let searchFieldsName = ''
if(title == '立项完成率趋势图(累加值)' || title == '立项完成率趋势图(当月值)'){
than.showbar1Step2 = false
than.showbar1Step3 = true
if(title == '立项完成率趋势图(累加值)') {
than.showOneSetpEd = 1
searchFieldsName = 'search14Fields'
}
if(title == '立项完成率趋势图(当月值)') {
than.showOneSetpEd = 2
searchFieldsName = 'search15Fields'
}
than.stepbar1Breadcrumb = [
{
title: title,
step: 1,
value: 1 + '1',
selected: false
},
{
title: title+'部门情况',
step: 2,
value: 1 + '2',
selected: false
},
{
title: '详情',
step: 3,
value: 1 + '3',
selected: true
}
]
}
if(title == '结项完成率趋势图(累加值)' || title == '结项完成率趋势图(当月值)'){
than.showbar2Step2 = false
than.showbar2Step3 = true
if(title == '结项完成率趋势图(累加值)') {
than.showTwoSetpEd = 1
searchFieldsName = 'search16Fields'
}
if(title == '结项完成率趋势图(当月值)') {
than.showTwoSetpEd = 2
searchFieldsName = 'search17Fields'
}
than.stepbar2Breadcrumb = [
{
title: title,
step: 1,
value: 1 + '1',
selected: false
},
{
title: title+'部门情况',
step: 2,
value: 1 + '2',
selected: false
},
{
title: '详情',
step: 3,
value: 1 + '3',
selected: true
}
]
}
if(title == '资金完成率趋势图(累加值)'){
than.showbar3Step2 = false
than.showbar3Step3 = true
if(title == '资金完成率趋势图(累加值)') {
than.showTreeSetpEd = 1
searchFieldsName = 'search18Fields'
}
if(title == '资金完成率趋势图(当月值)') {
than.showTreeSetpEd = 2
searchFieldsName = 'search23Fields'
}
than.stepbar3Breadcrumb = [
{
title: title,
step: 1,
value: 1 + '1',
selected: false
},
{
title: title+'部门情况',
step: 2,
value: 1 + '2',
selected: false
},
{
title: '详情',
step: 3,
value: 1 + '3',
selected: true
}
]
}
if(title == '计划外项目趋势图(累加值)' || title == '计划外项目趋势图(当月值)'){
than.showbar4Step2 = false
than.showbar4Step3 = true
if(title == '计划外项目趋势图(累加值)') {
than.showFourSetpEd = 1
searchFieldsName = 'search19Fields'
}
if(title == '计划外项目趋势图(当月值)') {
than.showFourSetpEd = 2
searchFieldsName = 'search20Fields'
}
than.stepbar4Breadcrumb = [
{
title: title,
step: 1,
value: 1 + '1',
selected: false
},
{
title: title+'部门情况',
step: 2,
value: 1 + '2',
selected: false
},
{
title: '详情',
step: 3,
value: 1 + '3',
selected: true
}
]
}
if(title == '项目变更趋势图(累加值)' || title == '项目变更趋势图(当月值)'){
than.showbar5Step3 = false
than.showbar5Step3 = true
if(title == '项目变更趋势图(累加值)') {
than.showFiveSetpEd = 1
searchFieldsName = 'search21Fields'
}
if(title == '项目变更趋势图(当月值)') {
than.showFiveSetpEd = 2
searchFieldsName = 'search22Fields'
}
than.stepbar5Breadcrumb = [
{
title: title,
step: 1,
value: 1 + '1',
selected: false
},
{
title: title+'部门情况',
step: 2,
value: 1 + '2',
selected: false
},
{
title: '详情',
step: 3,
value: 1 + '3',
selected: true
}
]
}
setTimeout(() => {
than.$nextTick(()=>{
if(than.$refs[searchFieldsName]){
than.$refs[searchFieldsName].handleQuery('init',stepTwoData.deptIdList[params.dataIndex]*1,than.legends[title][params.seriesIndex],'',than.formatMonth(yearToMonth))
}
})
}, 1000);
});
// 监听窗口尺寸变化并自动调整图表
window.addEventListener("resize", function () {
chart.resize();
});
// 使用 ResizeObserver 监听 div 大小变化
const resizeObserver = new ResizeObserver(() => {
chart.resize(); // 当 div 尺寸发生变化时,重新调整 ECharts 图表大小
});
// 开始监听指定的 div 元素
resizeObserver.observe(document.getElementById("chartStep2-" + type));
},
stepTwoData.planPaymentAmountList 如果存在负值会导致 百分比的基线在负数上 不对
最新发布