Merge pull request #13476 from dataease/pr@dev-v2@chart-line-feat-label

feat(图表): 折线图标签颜色可以根据范围设定 #12536
This commit is contained in:
jianneng-fit2cloud 2024-11-21 19:25:27 +08:00 committed by GitHub
commit 6cf298f4bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 4 deletions

View File

@ -13,6 +13,8 @@ import {
import { cloneDeep } from 'lodash-es'
import {
flow,
getLineConditions,
getLineLabelColorByCondition,
hexColorToRGBA,
parseJson,
setUpStackSeriesColor
@ -135,6 +137,7 @@ export class Area extends G2PlotChartView<AreaOptions, G2Area> {
}
}
const { label: labelAttr, basicStyle } = parseJson(chart.customAttr)
const conditions = getLineConditions(chart)
const formatterMap = labelAttr.seriesLabelFormatter?.reduce((pre, next) => {
pre[next.id] = next
return pre
@ -163,6 +166,9 @@ export class Area extends G2PlotChartView<AreaOptions, G2Area> {
? -2 - basicStyle.lineSymbolSize
: 10 + basicStyle.lineSymbolSize
const value = valueFormatter(data.value, labelCfg.formatterCfg)
const color =
getLineLabelColorByCondition(conditions, data.value, data.quotaList[0].id) ||
labelCfg.color
const group = new Group({})
group.addShape({
type: 'text',
@ -173,7 +179,7 @@ export class Area extends G2PlotChartView<AreaOptions, G2Area> {
textAlign: 'start',
textBaseline: 'top',
fontSize: labelCfg.fontSize,
fill: labelCfg.color
fill: color
}
})
return group
@ -311,6 +317,7 @@ export class StackArea extends Area {
}
protected configLabel(chart: Chart, options: AreaOptions): AreaOptions {
const { label: labelAttr, basicStyle } = parseJson(chart.customAttr)
const conditions = getLineConditions(chart)
if (!labelAttr?.show) {
return {
...options,
@ -334,7 +341,10 @@ export class StackArea extends Area {
fill: labelAttr.color,
fontSize: labelAttr.fontSize
},
formatter: function (param: Datum) {
formatter: function (param: Datum, point) {
point.color =
getLineLabelColorByCondition(conditions, param.value, point._origin.quotaList[0]) ||
labelAttr.color
return valueFormatter(param.value, labelAttr.labelFormatter)
}
}

View File

@ -11,6 +11,8 @@ import {
} from '../../common/common_antv'
import {
flow,
getLineConditions,
getLineLabelColorByCondition,
hexColorToRGBA,
parseJson,
setUpGroupSeriesColor
@ -134,6 +136,7 @@ export class Line extends G2PlotChartView<LineOptions, G2Line> {
}
}
const { label: labelAttr, basicStyle } = parseJson(chart.customAttr)
const conditions = getLineConditions(chart)
const formatterMap = labelAttr.seriesLabelFormatter?.reduce((pre, next) => {
pre[next.id] = next
return pre
@ -162,6 +165,9 @@ export class Line extends G2PlotChartView<LineOptions, G2Line> {
? -2 - basicStyle.lineSymbolSize
: 10 + basicStyle.lineSymbolSize
const value = valueFormatter(data.value, labelCfg.formatterCfg)
const color =
getLineLabelColorByCondition(conditions, data.value, data.quotaList[0].id) ||
labelCfg.color
const group = new Group({})
group.addShape({
type: 'text',
@ -172,7 +178,7 @@ export class Line extends G2PlotChartView<LineOptions, G2Line> {
textAlign: 'start',
textBaseline: 'top',
fontSize: labelCfg.fontSize,
fill: labelCfg.color
fill: color
}
})
return group

View File

@ -1334,8 +1334,11 @@ export function getConditions(chart: Chart) {
const annotations = []
if (!threshold.enable) return annotations
const conditions = threshold.lineThreshold ?? []
const yAxisIds = chart.yAxis.map(i => i.id)
for (const field of conditions) {
if (!yAxisIds.includes(field.fieldId)) {
continue
}
for (const t of field.conditions) {
if ([2, 3, 4].includes(field.field.deType)) {
const annotation = {

View File

@ -1071,3 +1071,51 @@ export function filterEmptyMinValue(sourceData, field) {
)
return notEmptyMinValue
}
/**
* 获取折线条件样式
* @param chart
*/
export function getLineConditions(chart) {
const { threshold } = parseJson(chart.senior)
const conditions = []
if (threshold.enable) {
threshold.lineThreshold.forEach(item =>
item.conditions.forEach(c =>
conditions.push({
fieldId: item.fieldId,
term: c.term,
value: c.value,
color: c.color,
min: c.min,
max: c.max
})
)
)
}
return conditions
}
/**
* 根据折线阈值条件获取新的标签颜色
* @param conditions
* @param value
* @param fieldId
*/
export function getLineLabelColorByCondition(conditions, value, fieldId) {
const fieldConditions = conditions.filter(item => item.fieldId === fieldId)
let color = undefined
if (fieldConditions.length) {
fieldConditions.some(item => {
if (
(item.term === 'lt' && value <= item.value) ||
(item.term === 'gt' && value >= item.value) ||
(item.term === 'between' && value >= item.min && value <= item.max)
) {
color = item.color
return true
}
})
}
return color
}