fix(图表): 修复轮播提示,饼图只能轮播一轮的问题,以及修复环形图不能轮播的问题

This commit is contained in:
jianneng-fit2cloud
2025-04-08 09:24:18 +08:00
committed by jianneng-fit2cloud
parent 720fdc9ca3
commit 509b11a6d4
3 changed files with 71 additions and 38 deletions

View File

@@ -11,8 +11,33 @@ const CHART_CATEGORY = {
COLUMN: ['bar', 'bar-stack', 'bar-group', 'bar-group-stack'],
LINE: ['line', 'area', 'area-stack'],
MIX: ['chart-mix', 'chart-mix-group', 'chart-mix-stack', 'chart-mix-dual-line'],
PIE: ['pie']
PIE: ['pie', 'pie-donut']
}
/**
* 判断是否为柱状图
* @param chartType
*/
export function isColumn(chartType: string) {
return CHART_CATEGORY.COLUMN.includes(chartType)
}
/**
* 判断是否为折线图
* @param chartType
*/
export function isLine(chartType: string) {
return CHART_CATEGORY.LINE.includes(chartType)
}
/**
* 判断是否为饼图
* @param chartType
*/
export function isPie(chartType: string) {
return CHART_CATEGORY.PIE.includes(chartType)
}
const isSupport = (chartType: string) => {
return Object.values(CHART_CATEGORY).some(category => category.includes(chartType))
}
@@ -125,30 +150,6 @@ class ChartCarouselTooltip {
})
}
/**
* 判断是否为柱状图
* @param chartType
*/
static isColumn(chartType: string) {
return CHART_CATEGORY.COLUMN.includes(chartType)
}
/**
* 判断是否为折线图
* @param chartType
*/
static isLine(chartType: string) {
return CHART_CATEGORY.LINE.includes(chartType)
}
/**
* 判断是否为饼图
* @param chartType
*/
static isPie(chartType: string) {
return CHART_CATEGORY.PIE.includes(chartType)
}
/**
* 暂停轮播
* @param id
@@ -232,6 +233,9 @@ class ChartCarouselTooltip {
if (this.currentIndex > this.values.length) {
this.currentIndex = 0
this.hideTooltip()
this.plot.chart.showTooltip({ x: 0, y: 0 })
this.plot.chart.getController('tooltip').update()
this.unHighlightPoint(currentValue)
this.timers.interval = setTimeout(() => processArray(), this.config.interval)
} else {
// 如果未遍历完,继续处理下一个数据点
@@ -284,7 +288,7 @@ class ChartCarouselTooltip {
private calculatePosition(value: string) {
const view = this.plot.chart.views?.[0] || this.plot.chart
// 饼图特殊处理
if (['pie', 'pie-donut'].includes(this.chart.type)) {
if (CHART_CATEGORY.PIE.includes(this.chart.type)) {
return this.getPieTooltipPosition(view, value)
}
if (this.plot instanceof DualAxes) {
@@ -318,9 +322,12 @@ class ChartCarouselTooltip {
.scale()
.getGeometries()[0]
.elements.find(item => item.data.field === value)
.getModel()
?.getModel()
if (!piePoint) {
return { x: 0, y: 0 }
}
const coordinates = [
{ x: piePoint.x[0], y: piePoint.y[0] },
{ x: [].concat(piePoint.x)[0], y: piePoint.y[0] },
{ x: piePoint.x[0], y: piePoint.y[1] },
{ x: piePoint.x[1], y: piePoint.y[0] },
{ x: piePoint.x[1], y: piePoint.y[1] }
@@ -366,17 +373,28 @@ class ChartCarouselTooltip {
* 高亮指定元素
* */
private highlightElement(value: string) {
if (CHART_CATEGORY.LINE.includes(this.chart.type)) return
this.unHighlightPoint(value)
const activeType = this.chart.type === 'pie' ? 'selected' : 'inactive'
this.plot.setState(activeType, (data: any) => data[this.config.xField] === value, true)
this.plot.setState(
this.getHighlightType(),
(data: any) => data[this.config.xField] === value,
true
)
}
/**
* 取消高亮
* **/
private unHighlightPoint(value?: string) {
const activeType = this.chart.type === 'pie' ? 'selected' : 'inactive'
this.plot.setState(activeType, (data: any) => data[this.config.xField] !== value, false)
if (CHART_CATEGORY.LINE.includes(this.chart.type)) return
this.plot.setState(
this.getHighlightType(),
(data: any) => data[this.config.xField] !== value,
false
)
}
private getHighlightType() {
return 'active'
}
/**

View File

@@ -120,6 +120,14 @@ export class Pie extends G2PlotChartView<PieOptions, G2Pie> {
field: {
type: 'cat'
}
},
state: {
active: {
style: {
lineWidth: 2,
fillOpacity: 0.5
}
}
}
}
const options = this.setupOptions(chart, initOptions)

View File

@@ -42,7 +42,11 @@ import {
qqMapStyleOptions,
tdtMapStyleOptions
} from '@/views/chart/components/js/panel/charts/map/common'
import ChartCarouselTooltip from '@/views/chart/components/js/g2plot_tooltip_carousel'
import ChartCarouselTooltip, {
isPie,
isLine,
isColumn
} from '@/views/chart/components/js/g2plot_tooltip_carousel'
const { t: tI18n } = useI18n()
@@ -1724,11 +1728,14 @@ function calculateTooltipPosition(
) {
// 辅助函数: 根据不同图表类型计算 Tooltip 的y位置
const getTooltipY = () => {
return (
Number(chartElement.getBoundingClientRect().top) +
chartElement.getBoundingClientRect().height / 2
)
// return tooltipCtl.point.y + Number(chartElement.getBoundingClientRect().top)
if (isColumn(chart.type)) {
return (
60 +
Number(chartElement.getBoundingClientRect().top) +
chartElement.getBoundingClientRect().height / 2
)
}
return 60 + tooltipCtl.point.y + Number(chartElement.getBoundingClientRect().top)
}
if (isCarousel) {
return {