提交 3889eff0 authored 作者: junjun's avatar junjun

feat: 自定义颜色

上级 09324a21
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
getSlider, getSlider,
getAnalyse getAnalyse
} from '@/views/chart/chart/common/common_antv' } from '@/views/chart/chart/common/common_antv'
import { antVCustomColor } from '@/views/chart/chart/util'
export function baseBarOptionAntV(plot, container, chart, action, isGroup, isStack) { export function baseBarOptionAntV(plot, container, chart, action, isGroup, isStack) {
// theme // theme
...@@ -98,6 +99,8 @@ export function baseBarOptionAntV(plot, container, chart, action, isGroup, isSta ...@@ -98,6 +99,8 @@ export function baseBarOptionAntV(plot, container, chart, action, isGroup, isSta
} else { } else {
delete options.isStack delete options.isStack
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
...@@ -198,6 +201,8 @@ export function hBaseBarOptionAntV(plot, container, chart, action, isGroup, isSt ...@@ -198,6 +201,8 @@ export function hBaseBarOptionAntV(plot, container, chart, action, isGroup, isSt
} else { } else {
delete options.isStack delete options.isStack
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
......
import { getLabel, getLegend, getPadding, getTheme, getTooltip } from '@/views/chart/chart/common/common_antv' import { getLabel, getLegend, getPadding, getTheme, getTooltip } from '@/views/chart/chart/common/common_antv'
import { Funnel } from '@antv/g2plot' import { Funnel } from '@antv/g2plot'
import { antVCustomColor } from '@/views/chart/chart/util'
export function baseFunnelOptionAntV(plot, container, chart, action) { export function baseFunnelOptionAntV(plot, container, chart, action) {
// theme // theme
...@@ -62,6 +63,8 @@ export function baseFunnelOptionAntV(plot, container, chart, action) { ...@@ -62,6 +63,8 @@ export function baseFunnelOptionAntV(plot, container, chart, action) {
const s = JSON.parse(JSON.stringify(customAttr.size)) const s = JSON.parse(JSON.stringify(customAttr.size))
} }
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
......
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
getSlider, getSlider,
getAnalyse getAnalyse
} from '@/views/chart/chart/common/common_antv' } from '@/views/chart/chart/common/common_antv'
import { antVCustomColor } from '@/views/chart/chart/util'
export function baseLineOptionAntV(plot, container, chart, action) { export function baseLineOptionAntV(plot, container, chart, action) {
// theme // theme
...@@ -90,6 +91,8 @@ export function baseLineOptionAntV(plot, container, chart, action) { ...@@ -90,6 +91,8 @@ export function baseLineOptionAntV(plot, container, chart, action) {
} }
} }
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
...@@ -184,6 +187,8 @@ export function baseAreaOptionAntV(plot, container, chart, action) { ...@@ -184,6 +187,8 @@ export function baseAreaOptionAntV(plot, container, chart, action) {
} }
} }
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
} from '@/views/chart/chart/common/common_antv' } from '@/views/chart/chart/common/common_antv'
import { Pie, Rose } from '@antv/g2plot' import { Pie, Rose } from '@antv/g2plot'
import { antVCustomColor } from '@/views/chart/chart/util'
export function basePieOptionAntV(plot, container, chart, action) { export function basePieOptionAntV(plot, container, chart, action) {
// theme // theme
...@@ -84,6 +85,8 @@ export function basePieOptionAntV(plot, container, chart, action) { ...@@ -84,6 +85,8 @@ export function basePieOptionAntV(plot, container, chart, action) {
options.innerRadius = parseFloat(parseInt(s.pieInnerRadius) / 100) options.innerRadius = parseFloat(parseInt(s.pieInnerRadius) / 100)
} }
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
...@@ -165,6 +168,8 @@ export function basePieRoseOptionAntV(plot, container, chart, action) { ...@@ -165,6 +168,8 @@ export function basePieRoseOptionAntV(plot, container, chart, action) {
options.innerRadius = parseFloat(parseInt(s.pieInnerRadius) / 100) options.innerRadius = parseFloat(parseInt(s.pieInnerRadius) / 100)
} }
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
......
import { getLabel, getLegend, getPadding, getTheme, getTooltip } from '@/views/chart/chart/common/common_antv' import { getLabel, getLegend, getPadding, getTheme, getTooltip } from '@/views/chart/chart/common/common_antv'
import { Radar } from '@antv/g2plot' import { Radar } from '@antv/g2plot'
import { antVCustomColor } from '@/views/chart/chart/util'
export function baseRadarOptionAntV(plot, container, chart, action) { export function baseRadarOptionAntV(plot, container, chart, action) {
// theme // theme
...@@ -132,6 +133,9 @@ export function baseRadarOptionAntV(plot, container, chart, action) { ...@@ -132,6 +133,9 @@ export function baseRadarOptionAntV(plot, container, chart, action) {
options.xAxis = xAxis options.xAxis = xAxis
options.yAxis = yAxis options.yAxis = yAxis
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
plot.destroy() plot.destroy()
......
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
} from '@/views/chart/chart/common/common_antv' } from '@/views/chart/chart/common/common_antv'
import { Scatter } from '@antv/g2plot' import { Scatter } from '@antv/g2plot'
import { antVCustomColor } from '@/views/chart/chart/util'
export function baseScatterOptionAntV(plot, container, chart, action) { export function baseScatterOptionAntV(plot, container, chart, action) {
// theme // theme
...@@ -86,6 +87,8 @@ export function baseScatterOptionAntV(plot, container, chart, action) { ...@@ -86,6 +87,8 @@ export function baseScatterOptionAntV(plot, container, chart, action) {
options.shape = s.scatterSymbol options.shape = s.scatterSymbol
} }
} }
// custom color
options.color = antVCustomColor(chart)
// 开始渲染 // 开始渲染
if (plot) { if (plot) {
......
...@@ -397,7 +397,7 @@ export const TYPE_CONFIGS = [ ...@@ -397,7 +397,7 @@ export const TYPE_CONFIGS = [
'color-selector', 'color-selector',
'size-selector', 'size-selector',
'label-selector', 'label-selector',
'title-selector', 'title-selector'
] ]
}, },
...@@ -665,3 +665,124 @@ export function customSort(custom, data) { ...@@ -665,3 +665,124 @@ export function customSort(custom, data) {
return joinArr.concat(subArr) return joinArr.concat(subArr)
} }
export function customColor(custom, res, colors) {
const result = []
for (let i = 0; i < res.length; i++) {
const r = res[i]
let flag = false
for (let j = 0; j < custom.length; j++) {
const c = custom[j]
if (r.name === c.name) {
flag = true
result.push(c)
}
}
if (!flag) {
result.push(r)
}
}
return result
}
export function getColors(chart, colors, reset) {
// 自定义颜色,先按照没有设定的情况,并排好序,当做最终结果
let seriesColors = []
let series
if (chart.type.includes('stack')) {
if (chart.data) {
const data = chart.data.datas
const stackData = []
for (let i = 0; i < data.length; i++) {
const s = data[i]
stackData.push(s.category)
}
const sArr = stackData.filter(function(item, index, stackData) {
return stackData.indexOf(item, 0) === index
})
for (let i = 0; i < sArr.length; i++) {
const s = sArr[i]
seriesColors.push({
name: s,
color: colors[i % colors.length],
isCustom: false
})
}
}
} else if (chart.type.includes('bar') || chart.type.includes('line') || chart.type.includes('scatter') || chart.type.includes('radar')) {
if (Object.prototype.toString.call(chart.yaxis) === '[object Array]') {
series = JSON.parse(JSON.stringify(chart.yaxis))
} else {
series = JSON.parse(chart.yaxis)
}
for (let i = 0; i < series.length; i++) {
const s = series[i]
seriesColors.push({
name: s.name,
color: colors[i % colors.length],
isCustom: false
})
}
} else {
if (chart.data) {
const data = chart.data.datas
// data 的维度值,需要根据自定义顺序排序
// let customSortData
// if (Object.prototype.toString.call(chart.customSort) === '[object Array]') {
// customSortData = JSON.parse(JSON.stringify(chart.customSort))
// } else {
// customSortData = JSON.parse(chart.customSort)
// }
// if (customSortData && customSortData.length > 0) {
// data = customSort(customSortData, data)
// }
for (let i = 0; i < data.length; i++) {
const s = data[i]
seriesColors.push({
name: s.field,
color: colors[i % colors.length],
isCustom: false
})
}
}
}
// 如果有自定义,则与上述中的结果合并。
// res,custom,以custom为准,去掉res中不存在的,并将custom中name一样的color赋值给res,不存在的name,即新增值,使用i % colors.length,从配色方案中选
if (!reset) {
let sc = null
if (Object.prototype.toString.call(chart.customAttr) === '[object Object]') {
sc = JSON.parse(JSON.stringify(chart.customAttr)).color.seriesColors
} else {
sc = JSON.parse(chart.customAttr).color.seriesColors
}
if (sc && sc.length > 0) {
seriesColors = customColor(sc, seriesColors)
}
// 根据isCustom字段,修正color
for (let i = 0; i < seriesColors.length; i++) {
if (!seriesColors[i].isCustom) {
seriesColors[i].color = colors[i % colors.length]
}
}
}
return seriesColors
}
export function antVCustomColor(chart) {
const colors = []
if (chart.customAttr) {
const customAttr = JSON.parse(chart.customAttr)
// color
if (customAttr.color) {
const c = JSON.parse(JSON.stringify(customAttr.color))
const customColors = getColors(chart, c.colors, false)
for (let i = 0; i < customColors.length; i++) {
colors.push(hexColorToRGBA(customColors[i].color, c.alpha))
}
}
}
return colors
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论