diff --git a/src/page/index/components/BrandConsumptionLevel/BrandConsumptionLevel.vue b/src/page/index/components/BrandConsumptionLevel/BrandConsumptionLevel.vue index 1b894f8..d4fc7ec 100644 --- a/src/page/index/components/BrandConsumptionLevel/BrandConsumptionLevel.vue +++ b/src/page/index/components/BrandConsumptionLevel/BrandConsumptionLevel.vue @@ -34,60 +34,7 @@ onMounted(async () => { myChart = echarts.init(chartDom); - const option = { - legend: { - // selectedMode: false, - right: 0, - textStyle: { - color: '#fff', - fontSize: 12, - padding: [0, 0, 0, 5] - }, - }, - grid: { - left: '0', - right: '0', - bottom: '0', - top: '30', - containLabel: true - }, - yAxis: { - name: '%', - type: 'value', - splitLine: { show: false }, // 隐藏刻度线 - axisLine: { - show: true, - lineStyle: { - color: '#fff' - } - }, - axisLabel: { - color: '#fff', - } - }, - xAxis: { - type: 'category', - data: res.category, - axisLabel: { - interval: 0 // 强制显示所有标签 - }, - axisLine: { - show: true, - lineStyle: { - color: '#fff' - } - }, - }, - series: res.seriesData, - tooltip: { - trigger: 'item', - formatter: function (params: any) { - return ` -
${params.seriesName} ${params.value}%
- `; - } - }, - }; + const option = handleSetConfig(res) myChart.setOption(option); myChart.resize(); @@ -162,6 +109,64 @@ const handleGetData = async () => { return res } +const handleSetConfig = (res: any) => { + const option = { + legend: { + // selectedMode: false, + right: 0, + textStyle: { + color: '#fff', + fontSize: 12, + padding: [0, 0, 0, 5] + }, + }, + grid: { + left: '0', + right: '0', + bottom: '0', + top: '30', + containLabel: true + }, + yAxis: { + name: '%', + type: 'value', + splitLine: { show: false }, // 隐藏刻度线 + axisLine: { + show: true, + lineStyle: { + color: '#fff' + } + }, + axisLabel: { + color: '#fff', + } + }, + xAxis: { + type: 'category', + data: res.category, + axisLabel: { + interval: 0 // 强制显示所有标签 + }, + axisLine: { + show: true, + lineStyle: { + color: '#fff' + } + }, + }, + series: res.seriesData, + tooltip: { + trigger: 'item', + formatter: function (params: any) { + return ` +
${params.seriesName} ${params.value}%
+ `; + } + }, + }; + + return option +} const resizeChart = () => { myChart?.resize(); @@ -172,7 +177,18 @@ onBeforeUnmount(() => { myChart?.dispose(); }); +const handleUpdatePie = async () => { + myChart.setOption({ + series: [], // 清空 series + }, { replaceMerge: 'series' }); // 替换 series 部分 + const res: any = await handleGetData() + const option = handleSetConfig(res) + myChart.setOption(option) +} +defineExpose({ + handleUpdatePie +}); diff --git a/src/page/index/components/BrandDetail/BrandDetail.vue b/src/page/index/components/BrandDetail/BrandDetail.vue index d050015..11cb4fc 100644 --- a/src/page/index/components/BrandDetail/BrandDetail.vue +++ b/src/page/index/components/BrandDetail/BrandDetail.vue @@ -66,67 +66,7 @@ const handleGoMounted = async () => { myChart = echarts.init(chartDom); - const option = { - legend: { - orient: 'vertical', // 图例纵向排列 - left: 200, // 距离右侧5% - top: 'center', // 垂直居中 - itemWidth: 12, // 图例标记宽度 - itemHeight: 12, // 图例标记高度 - textStyle: { - color: '#fff', // 文字颜色 - rich: { - // 可以在这里添加富文本样式 - value: { - fontWeight: 'bold', - padding: [0, 0, 0, 5] // 文字间距 - } - } - }, - // 自定义图例内容 - formatter: function (name: string) { - // 找到对应的数据项 - const dataItem = res.pieData.find((item: any) => item.name === name); - // 返回自定义格式 - return `${name} ${dataItem?.value}%`; - - - } - }, - tooltip: { // 新增 tooltip 配置 - trigger: 'item', // 触发类型:坐标轴触发 - axisPointer: { // 坐标轴指示器配置 - type: 'shadow' // 阴影指示器(适合柱状图) - }, - formatter: function (params: any) { // 自定义提示框内容 - return ` -
${params.name} ${params?.percent}%
- `; - } - }, - series: [ - { - name: 'Access From', - type: 'pie', - radius: ['0%', '70%'], - center: ['25%', '50%'], - avoidLabelOverlap: false, - itemStyle: { - color: function (params: any) { - return colorList[params.dataIndex]; - } - }, - label: { - show: false - }, - emphasis: false, - labelLine: { - show: false - }, - data: res.pieData - } - ] - }; + const option = handleSetConfig(res) myChart.setOption(option); @@ -285,6 +225,86 @@ onBeforeUnmount(() => { myChart?.dispose(); }); + +const handleSetConfig = (res: any) => { + const option = { + legend: { + orient: 'vertical', // 图例纵向排列 + left: 200, // 距离右侧5% + top: 'center', // 垂直居中 + itemWidth: 12, // 图例标记宽度 + itemHeight: 12, // 图例标记高度 + textStyle: { + color: '#fff', // 文字颜色 + rich: { + // 可以在这里添加富文本样式 + value: { + fontWeight: 'bold', + padding: [0, 0, 0, 5] // 文字间距 + } + } + }, + // 自定义图例内容 + formatter: function (name: string) { + // 找到对应的数据项 + const dataItem = res.pieData.find((item: any) => item.name === name); + // 返回自定义格式 + return `${name} ${dataItem?.value}%`; + + + } + }, + tooltip: { // 新增 tooltip 配置 + trigger: 'item', // 触发类型:坐标轴触发 + axisPointer: { // 坐标轴指示器配置 + type: 'shadow' // 阴影指示器(适合柱状图) + }, + formatter: function (params: any) { // 自定义提示框内容 + return ` +
${params.name} ${params?.percent}%
+ `; + } + }, + series: [ + { + name: 'Access From', + type: 'pie', + radius: ['0%', '70%'], + center: ['25%', '50%'], + avoidLabelOverlap: false, + itemStyle: { + color: function (params: any) { + return colorList[params.dataIndex]; + } + }, + label: { + show: false + }, + emphasis: false, + labelLine: { + show: false + }, + data: res.pieData + } + ] + }; + return option +} + +const handleUpdatePie = async () => { + myChart.setOption({ + series: [], // 清空 series + }, { replaceMerge: 'series' }); // 替换 series 部分 + + const res: any = await handleGetData() + const option = handleSetConfig(res) + myChart.setOption(option) + + handleBrandTabList() +} +defineExpose({ + handleUpdatePie +}); diff --git a/src/page/index/components/BusinessCase/BusinessCase.vue b/src/page/index/components/BusinessCase/BusinessCase.vue index e5a3bcb..d39e297 100644 --- a/src/page/index/components/BusinessCase/BusinessCase.vue +++ b/src/page/index/components/BusinessCase/BusinessCase.vue @@ -447,6 +447,11 @@ onBeforeUnmount(() => { myChartBottom?.dispose(); }); +defineExpose({ + handleGetData +}); + + diff --git a/src/page/index/components/BusinessStructure/BusinessStructure.vue b/src/page/index/components/BusinessStructure/BusinessStructure.vue index 3e24bfa..d869d3d 100644 --- a/src/page/index/components/BusinessStructure/BusinessStructure.vue +++ b/src/page/index/components/BusinessStructure/BusinessStructure.vue @@ -55,86 +55,7 @@ const handleGoMounted = async () => { const centerX = width * 0.2; const centerY = height * 0.5; - const option = { - - tooltip: { // 新增 tooltip 配置 - trigger: 'item', // 触发类型:坐标轴触发 - axisPointer: { // 坐标轴指示器配置 - type: 'shadow' // 阴影指示器(适合柱状图) - }, - formatter: function (params: any) { // 自定义提示框内容 - return ` -
${params.data.name} ${params?.percent}% ${res.realData[params.dataIndex]}
- `; - } - }, - graphic: { // 关键配置:在图表中心添加图片 - elements: [ - { - type: 'image', - style: { - image: businessFormatCenterIcon, - width: 83, - height: 83 - }, - left: centerX - 41.5, - top: centerY - 41.5, - z: 10 - } - ] - }, - series: [ - { - name: 'Access From', - type: 'pie', - radius: ['55%', '70%'], - center: ['20%', '50%'], // 图形向左偏移 - avoidLabelOverlap: false, - itemStyle: { - color: function (params: any) { - return colorList[params.dataIndex]; - } - }, - label: { - show: false - }, - emphasis: false, - labelLine: { - show: false - }, - data: res.seriesData - } - ], - legend: { - orient: 'vertical', - left: 200, - top: 'center', - align: 'left', - itemGap: 10, - itemWidth: 12, - itemHeight: 12, - textStyle: { - color: '#fff', - rich: { - name: { - width: 90, // 固定名称宽度 - align: 'left', - padding: [0, 10, 0, 0] - }, - percent: { - width: 50, // 固定百分比宽度 - align: 'right', - padding: [0, 10, 0, 0] - } - } - }, - formatter: function (name: any) { - let percentData: any = res.lengedData.filter((item: any) => item.name === name) - - return `{name|${name}}{percent|${percentData[0].value}%}`; // 使用rich样式 - } - } - }; + const option = handleSetConfig(res) myChart.setOption(option); myChart.resize(); @@ -232,12 +153,121 @@ onBeforeUnmount(() => { myChart?.dispose(); }); +const handleSetConfig = (res: any) => { + const chartDom = document.getElementById('BusinessStructure'); + if (!chartDom) return; + + myChart = echarts.init(chartDom); + + const rect = chartDom.getBoundingClientRect(); + const width = rect.width; + const height = rect.height; + + // 计算center位置(20%,50%)对应的实际像素 + const centerX = width * 0.2; + const centerY = height * 0.5; + + const option = { + tooltip: { // 新增 tooltip 配置 + trigger: 'item', // 触发类型:坐标轴触发 + axisPointer: { // 坐标轴指示器配置 + type: 'shadow' // 阴影指示器(适合柱状图) + }, + formatter: function (params: any) { // 自定义提示框内容 + return ` +
${params.data.name} ${params?.percent}% ${res.realData[params.dataIndex]}
+ `; + } + }, + graphic: { // 关键配置:在图表中心添加图片 + elements: [ + { + type: 'image', + style: { + image: businessFormatCenterIcon, + width: 83, + height: 83 + }, + left: centerX - 41.5, + top: centerY - 41.5, + z: 10 + } + ] + }, + series: [ + { + name: 'Access From', + type: 'pie', + radius: ['55%', '70%'], + center: ['20%', '50%'], // 图形向左偏移 + avoidLabelOverlap: false, + itemStyle: { + color: function (params: any) { + return colorList[params.dataIndex]; + } + }, + label: { + show: false + }, + emphasis: false, + labelLine: { + show: false + }, + data: res.seriesData + } + ], + legend: { + orient: 'vertical', + left: 200, + top: 'center', + align: 'left', + itemGap: 10, + itemWidth: 12, + itemHeight: 12, + textStyle: { + color: '#fff', + rich: { + name: { + width: 90, // 固定名称宽度 + align: 'left', + padding: [0, 10, 0, 0] + }, + percent: { + width: 50, // 固定百分比宽度 + align: 'right', + padding: [0, 10, 0, 0] + } + } + }, + formatter: function (name: any) { + let percentData: any = res.lengedData.filter((item: any) => item.name === name) + + return `{name|${name}}{percent|${percentData[0].value}%}`; // 使用rich样式 + } + } + }; + return option +} + +const handleUpdatePie = async () => { + myChart.setOption({ + series: [], // 清空 series + }, { replaceMerge: 'series' }); // 替换 series 部分 + + const res: any = await handleGetData() + const option = handleSetConfig(res) + myChart.setOption(option) +} + +defineExpose({ + handleUpdatePie +}); -