export function calculateSegmentColorsFromData()

in packages/attribute-slicer-powerbi/src/dataConversion.ts [106:131]


export function calculateSegmentColorsFromData(
	dataView: DataView,
): { [key: string]: string } {
	const values: DataViewValueColumns = dataView.categorical.values;

	// Sometimes the segments have RGB names, use them as colors
	const groups: DataViewValueColumnGroup[] = values && values.grouped && values.grouped();
	const segmentColors: { [key: string]: string } = {};

	// If the segment by is a color segment
	if (
		dataView.metadata.columns.filter(
			(n: DataViewMetadataColumn) => n.roles && n.roles.Color,
		).length >= 0 &&
		groups
	) {
		groups.forEach((n: DataViewValueColumnGroup, i: number) => {
			const name: string = `${n.name || ""}`;
			if (name && (HEX_COLOR_REGEX.test(name) || RGB_COLOR_REGEX.test(name))) {
				segmentColors[i] = name;
			}
		});
	}

	return segmentColors;
}