-
Notifications
You must be signed in to change notification settings - Fork 327
feat: painting bar/column color based on categories #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
73496d9
943d8ed
38de582
b0830c1
1a8518d
24567a8
a013a10
8a2ddc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,6 +7,8 @@ import { | |||||||||
Legend, | ||||||||||
CircleLegend, | ||||||||||
LegendDataList, | ||||||||||
Categories, | ||||||||||
DefaultCategories, | ||||||||||
} from '@t/store/store'; | ||||||||||
import { | ||||||||||
BubbleChartOptions, | ||||||||||
|
@@ -50,6 +52,8 @@ type LegendLabelsInfo = { | |||||||||
checked: boolean; | ||||||||||
viewLabel: string; | ||||||||||
width: number; | ||||||||||
colorByCategories?: boolean; | ||||||||||
colorIndex?: number; | ||||||||||
}[]; | ||||||||||
|
||||||||||
type LegendInfo = { | ||||||||||
|
@@ -298,23 +302,37 @@ function getViewLabelInfo(legendInfo: LegendInfo, label: string, maxTextLength?: | |||||||||
return { viewLabel, width: itemWidth ?? itemWidthWithFullText }; | ||||||||||
} | ||||||||||
|
||||||||||
function getLegendLabelsInfo(series: RawSeries, legendInfo: LegendInfo): LegendLabelsInfo { | ||||||||||
function getLegendLabelsInfo( | ||||||||||
series: RawSeries, | ||||||||||
legendInfo: LegendInfo, | ||||||||||
categories: DefaultCategories | ||||||||||
): LegendLabelsInfo { | ||||||||||
const maxTextLengthWithEllipsis = getMaxTextLengthWithEllipsis(legendInfo); | ||||||||||
let colorIndex = 0; | ||||||||||
|
||||||||||
return Object.keys(series).flatMap((type) => | ||||||||||
series[type].map(({ name, colorValue, visible }) => { | ||||||||||
return Object.keys(series).flatMap((type) => { | ||||||||||
const labelInfo = series[type].map(({ name, colorValue, visible, colorByCategories }) => { | ||||||||||
const label = colorValue ? colorValue : name; | ||||||||||
const currentColorIndex = colorIndex; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이정도면 위에 선언된 변수가 그냥 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중간에 값이 변경되니까 따로 할당한 것 같습니다ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 이게 중간에 값이 변경되어야 해서 애매했어요. 레전드 입장에선 컬러 리스트만 알고 있는 상황이고, 시리즈 단위로 컬러를 N개 가져가는 구조가 되다보니 다음 컬러 인덱스를 잡아줄 변수가 필요했습니다. |
||||||||||
const { width, viewLabel } = getViewLabelInfo(legendInfo, label, maxTextLengthWithEllipsis); | ||||||||||
|
||||||||||
colorIndex += colorByCategories ? categories.length : 1; | ||||||||||
|
||||||||||
return { | ||||||||||
label, | ||||||||||
type, | ||||||||||
colorByCategories: !!colorByCategories, | ||||||||||
colorIndex: currentColorIndex, | ||||||||||
checked: visible ?? true, | ||||||||||
viewLabel, | ||||||||||
width, | ||||||||||
}; | ||||||||||
}) | ||||||||||
); | ||||||||||
}); | ||||||||||
|
||||||||||
colorIndex += series[type].length - 1; | ||||||||||
|
||||||||||
return labelInfo; | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
function getItemWidth( | ||||||||||
|
@@ -337,14 +355,20 @@ function getLegendDataAppliedTheme(data: LegendDataList, series: Series) { | |||||||||
(acc, cur) => (cur && cur.colors ? [...acc, ...cur.colors] : acc), | ||||||||||
[] | ||||||||||
); | ||||||||||
const hasColorByCategories = data.some((legend) => legend.colorByCategories); | ||||||||||
|
||||||||||
return data.map((datum, idx) => ({ | ||||||||||
...datum, | ||||||||||
color: colors[idx], | ||||||||||
})); | ||||||||||
return data.map((datum, idx) => { | ||||||||||
const { colorByCategories, colorIndex } = datum; | ||||||||||
const index = hasColorByCategories ? colorIndex || idx : idx; | ||||||||||
Comment on lines
+361
to
+362
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
는 안될까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lja1018 |
||||||||||
|
||||||||||
return { | ||||||||||
...datum, | ||||||||||
color: colorByCategories ? '#aaa' : colors[index % colors.length], | ||||||||||
}; | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
function getLegendState(options: Options, series: RawSeries): Legend { | ||||||||||
function getLegendState(options: Options, series: RawSeries, categories?: Categories): Legend { | ||||||||||
const useSpectrumLegend = | ||||||||||
(options?.series as TreemapChartSeriesOptions)?.useColorValue ?? !!series.heatmap; | ||||||||||
|
||||||||||
|
@@ -365,19 +389,23 @@ function getLegendState(options: Options, series: RawSeries): Legend { | |||||||||
|
||||||||||
const legendLabelsInfo = hasNestedPieSeries(series) | ||||||||||
? getNestedPieLegendLabelsInfo(series, legendInfo) | ||||||||||
: getLegendLabelsInfo(series, legendInfo); | ||||||||||
|
||||||||||
const data = legendLabelsInfo.map(({ label, type, checked, width, viewLabel }) => ({ | ||||||||||
label, | ||||||||||
active: true, | ||||||||||
checked, | ||||||||||
width, | ||||||||||
iconType: getIconType(type), | ||||||||||
chartType: type, | ||||||||||
rowIndex: 0, | ||||||||||
columnIndex: 0, | ||||||||||
viewLabel, | ||||||||||
})); | ||||||||||
: getLegendLabelsInfo(series, legendInfo, categories as DefaultCategories); | ||||||||||
|
||||||||||
const data = legendLabelsInfo.map( | ||||||||||
({ label, type, checked, width, viewLabel, colorByCategories, colorIndex }) => ({ | ||||||||||
label, | ||||||||||
active: true, | ||||||||||
checked, | ||||||||||
width, | ||||||||||
iconType: getIconType(type), | ||||||||||
chartType: type, | ||||||||||
rowIndex: 0, | ||||||||||
columnIndex: 0, | ||||||||||
viewLabel, | ||||||||||
colorByCategories, | ||||||||||
colorIndex, | ||||||||||
}) | ||||||||||
); | ||||||||||
|
||||||||||
return { | ||||||||||
useSpectrumLegend, | ||||||||||
|
@@ -445,15 +473,18 @@ function setIndexToLegendData( | |||||||||
|
||||||||||
const legend: StoreModule = { | ||||||||||
name: 'legend', | ||||||||||
state: ({ options, series }) => { | ||||||||||
state: ({ options, series, categories }) => { | ||||||||||
return { | ||||||||||
legend: getLegendState(options, series) as Legend, | ||||||||||
legend: getLegendState(options, series, categories) as Legend, | ||||||||||
circleLegend: {} as CircleLegend, | ||||||||||
}; | ||||||||||
}, | ||||||||||
action: { | ||||||||||
initLegendState({ state, initStoreState }) { | ||||||||||
extend(state.legend, getLegendState(initStoreState.options, initStoreState.series)); | ||||||||||
extend( | ||||||||||
state.legend, | ||||||||||
getLegendState(initStoreState.options, initStoreState.series, initStoreState.categories) | ||||||||||
); | ||||||||||
}, | ||||||||||
setLegendLayout({ state }) { | ||||||||||
if (state.legend.useSpectrumLegend) { | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,15 @@ function getCoordinateDataRange(data, rawCategories: string[], zoomRange: RangeD | |
return [start, end]; | ||
} | ||
|
||
function getSeriesColors( | ||
colors: string[], | ||
colorIndex: number, | ||
size: number, | ||
isColorByCategories: boolean< F438 /span> | ||
) { | ||
return isColorByCategories ? colors.slice(0, size + 1) : colors[colorIndex % colors.length]; | ||
} | ||
|
||
function getSeriesDataInRange( | ||
data, | ||
rawCategories: Categories, | ||
|
@@ -173,19 +182,32 @@ const seriesData: StoreModule = { | |
const rawSeries = deepCopy(initStoreState.series); | ||
const { disabledSeries, theme, zoomRange, rawCategories } = state; | ||
const newSeriesData = {}; | ||
let colorIndex = 0; | ||
|
||
Object.keys(rawSeries).forEach((seriesName) => { | ||
const { colors, iconTypes } = theme.series![seriesName]; | ||
let originSeriesData = rawSeries[seriesName].map((m, idx) => ({ | ||
...m, | ||
rawData: m.data, | ||
data: getSeriesDataInRange(m.data, rawCategories, seriesName, zoomRange), | ||
color: colors ? colors[idx % colors.length] : '', | ||
})); | ||
|
||
let originSeriesData = rawSeries[seriesName].map((series) => { | ||
const isColorByCategories = !!series.colorByCategories; | ||
const size = isColorByCategories ? (rawCategories as string[]).length : 1; | ||
|
||
const color = colors | ||
? getSeriesColors(colors, colorIndex, size, isColorByCategories) | ||
: ''; | ||
|
||
colorIndex += size; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 연산 이후로 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. loop로 돌면서 195라인에서 계속 데이터를 사용하고 있어요. 이 함수 밖에서 사용 중입니다. reduce로 할까 했는데, 제 생각엔 map 성격이 더 강해보여서 함수 외부에 변수를 두고 누적 계산하는 식으로 처리했습니다. |
||
|
||
return { | ||
...series, | ||
rawData: series.data, | ||
data: getSeriesDataInRange(series.data, rawCategories, seriesName, zoomRange), | ||
color, | ||
}; | ||
}); | ||
|
||
if (seriesName === 'scatter') { | ||
originSeriesData = originSeriesData.map((m, idx) => ({ | ||
...m, | ||
originSeriesData = originSeriesData.map((series, idx) => ({ | ||
...series, | ||
iconType: iconTypes ? iconTypes[idx] : 'circle', | ||
})); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기존에도
color
가string
타입이란 가정하에 동작했던 건가요? 혹시 몰라서 여쭤봅니다.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네. 각 시리즈에 color값이 묶이는 구조인데 이번엔 컬러값이 배열로 들어와야 해서, 변수를 추가할까 타입을 추가할까 하다가 타입을 추가했어요.