260825_04_ENH: 차트 색상을 t_survey 종속적으로 변경 및 후속처리
This commit is contained in:
@@ -18,7 +18,7 @@ import {
|
||||
SUBITEM_NEGATIVE_COLOR,
|
||||
} from "@/const/epolicy-dash.const";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { getGapPoints, getHighlightedIndexes, toChartData } from "@/lib/chart-utils";
|
||||
import { getGapPoints, getHighlightedIndexes, isNegativeCategory, toChartData } from "@/lib/chart-utils";
|
||||
import { formatDecimal1, formatThousands, round1 } from "@/lib/format";
|
||||
import { isSessionInvalid, resolveApiErrorMessage } from "@/lib/api-error-message";
|
||||
import { usePartDetailQuery } from "@/query/waves";
|
||||
@@ -110,8 +110,11 @@ function wrapLabelByWord(value: string, maxWidth: number, charWidth: number) {
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function ResultGroupChart({ group }: { group: ResultGroup }) {
|
||||
function ResultGroupChart({ group, item }: { group: ResultGroup; item: Item }) {
|
||||
const chartData = toChartData(group.categories, group.series);
|
||||
const visibleCategories = group.categories.filter((category) => category.chartVisible);
|
||||
const negativeMode = group.subItem === null ? item.negativeMode : group.subItem.negativeMode;
|
||||
const negativeOrder = group.subItem === null ? item.negativeOrder : group.subItem.negativeOrder;
|
||||
const hasChartData = chartData.categories.length > 0 && chartData.series.length > 0;
|
||||
const chartWrapperRef = useRef<HTMLDivElement>(null);
|
||||
const [axisLabelWidth, setAxisLabelWidth] = useState<number>();
|
||||
@@ -137,18 +140,29 @@ function ResultGroupChart({ group }: { group: ResultGroup }) {
|
||||
<div ref={chartWrapperRef}>
|
||||
<KriBarChart
|
||||
categories={chartData.categories}
|
||||
series={chartData.series.map((item) => {
|
||||
const highlightedIndexes = item.chartHighlightMode ? getHighlightedIndexes(group.categories, item) : [];
|
||||
const gapPoints = getGapPoints(group.categories, item);
|
||||
series={chartData.series.map((series) => {
|
||||
const highlightedIndexes = series.chartHighlightMode
|
||||
? getHighlightedIndexes(group.categories, series)
|
||||
: [];
|
||||
const gapPoints = getGapPoints(group.categories, series);
|
||||
|
||||
return {
|
||||
name: seriesLegendName(item.label, item.baseCount),
|
||||
data: item.values.map((value, index) =>
|
||||
highlightedIndexes.includes(index) && item.chartHighlightColor
|
||||
? { value: round1(value), itemStyle: { color: item.chartHighlightColor } }
|
||||
: round1(value),
|
||||
),
|
||||
color: item.color,
|
||||
name: seriesLegendName(series.label, series.baseCount),
|
||||
data: series.values.map((value, index) => {
|
||||
const category = visibleCategories[index];
|
||||
const isNegative = category ? isNegativeCategory(negativeMode, negativeOrder, category) : false;
|
||||
const isHighlighted = highlightedIndexes.includes(index);
|
||||
const color = isNegative
|
||||
? isHighlighted
|
||||
? series.negativeHighlightColor
|
||||
: series.negativeColor
|
||||
: isHighlighted
|
||||
? series.highlightColor
|
||||
: series.color;
|
||||
|
||||
return { value: round1(value), itemStyle: { color } };
|
||||
}),
|
||||
color: negativeMode === "NEGATIVE" ? series.negativeColor : series.color,
|
||||
option: {
|
||||
label: {
|
||||
show: true,
|
||||
@@ -166,7 +180,7 @@ function ResultGroupChart({ group }: { group: ResultGroup }) {
|
||||
show: true,
|
||||
position: "top",
|
||||
// 막대 자체의 값 라벨(position:"top")과 겹치지 않도록 그 위로 띄운다.
|
||||
distance: 15,
|
||||
distance: 30,
|
||||
color: CHART_GAP_MARKER_TEXT_COLOR,
|
||||
backgroundColor: CHART_GAP_MARKER_BG_COLOR,
|
||||
borderRadius: 20,
|
||||
@@ -176,12 +190,9 @@ function ResultGroupChart({ group }: { group: ResultGroup }) {
|
||||
formatter: ({ value }: { value: number }) =>
|
||||
`${value >= 0 ? "▲" : "▼"} ${formatDecimal1(Math.abs(value))}%p`,
|
||||
},
|
||||
// ALL: 현재 막대 바로 위. LAST_VISIBLE: 직전 막대와의 중간 지점(두 막대 사이)에 표시한다.
|
||||
data: gapPoints.map(({ index, previousIndex, value, previousValue, gap }) => ({
|
||||
coord:
|
||||
item.chartGapMode === "LAST_VISIBLE"
|
||||
? [(index + previousIndex) / 2, Math.max(value, previousValue)]
|
||||
: [index, value],
|
||||
// ALL: 각 현재 막대 위치. LAST_VISIBLE: 마지막 표시 막대 위치에 표시한다.
|
||||
data: gapPoints.map(({ index, value, gap }) => ({
|
||||
coord: [index, value],
|
||||
value: gap,
|
||||
})),
|
||||
},
|
||||
@@ -286,7 +297,7 @@ function ItemCard({ item }: { item: Item }) {
|
||||
<div className="inline-flex shrink-0 rounded-md border p-1">
|
||||
{item.resultGroups.map((group, index) => {
|
||||
const isActive = index === activeGroupIndex;
|
||||
const isNegativeSubItem = group.subItem?.partItemSubCd === 2;
|
||||
const isNegativeSubItem = group.subItem?.negativeMode === "NEGATIVE";
|
||||
|
||||
return (
|
||||
<Button
|
||||
@@ -322,7 +333,7 @@ function ItemCard({ item }: { item: Item }) {
|
||||
|
||||
{activeGroup ? (
|
||||
<div className="mt-4">
|
||||
<ResultGroupChart group={activeGroup} />
|
||||
<ResultGroupChart group={activeGroup} item={item} />
|
||||
{hiddenCategoryNote ? <p className="mt-1 text-xs text-muted-foreground">{hiddenCategoryNote}</p> : null}
|
||||
{showTable ? (
|
||||
<div className="mt-6">
|
||||
|
||||
@@ -2,9 +2,7 @@ import { round1 } from "@/lib/format";
|
||||
import type { Category, Series } from "@/types/wave";
|
||||
|
||||
export function toChartData<T extends { values: number[] }>(categories: Category[], series: T[]) {
|
||||
const visibleIndexes = categories
|
||||
.map((category, index) => (category.chartVisible ? index : -1))
|
||||
.filter((index) => index >= 0);
|
||||
const visibleIndexes = getVisibleCategoryIndexes(categories);
|
||||
|
||||
return {
|
||||
categories: visibleIndexes.map((index) => categories[index].label),
|
||||
@@ -15,6 +13,31 @@ export function toChartData<T extends { values: number[] }>(categories: Category
|
||||
};
|
||||
}
|
||||
|
||||
export function getVisibleCategoryIndexes(categories: Category[]): number[] {
|
||||
return categories
|
||||
.map((category, index) => (category.chartVisible ? index : -1))
|
||||
.filter((index) => index >= 0);
|
||||
}
|
||||
|
||||
export function isNegativeCategory(
|
||||
mode: "DEFAULT" | "NEGATIVE" | "SPECIFIED",
|
||||
negativeOrder: string | null,
|
||||
category: Category,
|
||||
): boolean {
|
||||
if (mode === "NEGATIVE") return true;
|
||||
if (mode !== "SPECIFIED") return false;
|
||||
return parseOrderSet(negativeOrder).has(category.order);
|
||||
}
|
||||
|
||||
function parseOrderSet(value: string | null): Set<number> {
|
||||
return new Set(
|
||||
(value ?? "")
|
||||
.split(",")
|
||||
.map((order) => Number(order.trim()))
|
||||
.filter((order) => Number.isFinite(order)),
|
||||
);
|
||||
}
|
||||
|
||||
// chartVisible=true인 카테고리만 대상으로, 강조할 데이터 포인트의 인덱스를 계산한다.
|
||||
// 반환되는 인덱스는 toChartData가 만든 필터링된(visible) 카테고리/값 배열 기준이다.
|
||||
export function getHighlightedIndexes(
|
||||
@@ -25,11 +48,10 @@ export function getHighlightedIndexes(
|
||||
|
||||
switch (series.chartHighlightMode) {
|
||||
case "MAX_VALUE": {
|
||||
const visibleValues = toChartData(categories, [series]).series[0].values;
|
||||
const visibleValues = getVisibleCategoryIndexes(categories).map((index) => series.values[index]);
|
||||
if (!visibleValues.length) return [];
|
||||
const maxValue = Math.max(...visibleValues);
|
||||
const index = visibleValues.indexOf(maxValue);
|
||||
return index >= 0 ? [index] : [];
|
||||
return visibleValues.flatMap((value, index) => (value === maxValue ? [index] : []));
|
||||
}
|
||||
case "LAST_VISIBLE": {
|
||||
if (!visibleCategories.length) return [];
|
||||
@@ -38,13 +60,9 @@ export function getHighlightedIndexes(
|
||||
return index >= 0 ? [index] : [];
|
||||
}
|
||||
case "SPECIFIED": {
|
||||
const orders = (series.chartHighlightValue ?? "")
|
||||
.split(",")
|
||||
.map((value) => Number(value.trim()))
|
||||
.filter((value) => !Number.isNaN(value));
|
||||
if (!orders.length) return [];
|
||||
const orders = parseOrderSet(series.chartHighlightValue);
|
||||
return visibleCategories
|
||||
.map((category, index) => (orders.includes(category.order) ? index : -1))
|
||||
.map((category, index) => (orders.has(category.order) ? index : -1))
|
||||
.filter((index) => index >= 0);
|
||||
}
|
||||
default:
|
||||
@@ -72,17 +90,26 @@ export function getGapPoints(
|
||||
if (series.chartGapMode !== "ALL" && series.chartGapMode !== "LAST_VISIBLE") return [];
|
||||
|
||||
const visiblePoints = categories
|
||||
.map((category, index) => ({ category, value: round1(series.values[index]) }))
|
||||
.map((category, originalIndex) => ({ category, originalIndex, value: round1(series.values[originalIndex]) }))
|
||||
.filter((point) => point.category.chartVisible)
|
||||
.sort((a, b) => a.category.order - b.category.order);
|
||||
|
||||
if (visiblePoints.length < 2) return [];
|
||||
|
||||
const displayIndexByOriginalIndex = new Map<number, number>();
|
||||
let displayIndex = 0;
|
||||
categories.forEach((category, originalIndex) => {
|
||||
if (category.chartVisible) {
|
||||
displayIndexByOriginalIndex.set(originalIndex, displayIndex);
|
||||
displayIndex += 1;
|
||||
}
|
||||
});
|
||||
|
||||
const gapPoints = visiblePoints.slice(1).map((current, index) => {
|
||||
const previous = visiblePoints[index];
|
||||
return {
|
||||
index: index + 1,
|
||||
previousIndex: index,
|
||||
index: displayIndexByOriginalIndex.get(current.originalIndex) ?? index + 1,
|
||||
previousIndex: displayIndexByOriginalIndex.get(previous.originalIndex) ?? index,
|
||||
value: current.value,
|
||||
previousValue: previous.value,
|
||||
gap: round1(current.value - previous.value),
|
||||
|
||||
@@ -12,12 +12,15 @@ export interface Category {
|
||||
}
|
||||
|
||||
export interface SubItem {
|
||||
negativeMode: NegativeMode;
|
||||
negativeOrder: string | null;
|
||||
partItemSubCd: number;
|
||||
partItemSubComment: string | null;
|
||||
title: string;
|
||||
}
|
||||
|
||||
export type ChartType = "BAR";
|
||||
export type NegativeMode = "DEFAULT" | "NEGATIVE" | "SPECIFIED";
|
||||
|
||||
export type ChartHighlightMode = "MAX_VALUE" | "LAST_VISIBLE" | "SPECIFIED";
|
||||
|
||||
@@ -29,13 +32,15 @@ export interface Series {
|
||||
/** DB에 저장된 원문 그대로. baseCount와 조합해 생성되는 값이 아니다 (예: "전국 만 18세 이상 성인 남녀"). */
|
||||
baseText: string | null;
|
||||
chartGapMode: ChartGapMode;
|
||||
chartHighlightColor: string | null;
|
||||
chartHighlightMode: ChartHighlightMode | null;
|
||||
/** chartHighlightMode가 "SPECIFIED"일 때 강조할 category.order 목록 (쉼표 구분 문자열, 예: "1,3,5"). */
|
||||
chartHighlightValue: string | null;
|
||||
chartType: ChartType;
|
||||
color: string;
|
||||
highlightColor: string;
|
||||
label: string;
|
||||
negativeColor: string;
|
||||
negativeHighlightColor: string;
|
||||
surveyCd: number;
|
||||
values: number[];
|
||||
}
|
||||
@@ -49,6 +54,8 @@ export interface ResultGroup {
|
||||
export interface Item {
|
||||
audience: Audience;
|
||||
comment: string;
|
||||
negativeMode: NegativeMode;
|
||||
negativeOrder: string | null;
|
||||
order: number;
|
||||
partItemCd: number;
|
||||
resultGroups: ResultGroup[];
|
||||
@@ -87,7 +94,10 @@ export interface SurveyItem {
|
||||
export interface Survey {
|
||||
alias: string;
|
||||
color: string;
|
||||
highlightColor: string;
|
||||
items: SurveyItem[];
|
||||
negativeColor: string;
|
||||
negativeHighlightColor: string;
|
||||
surveyCd: number;
|
||||
title: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user