diff --git a/taxonium_component/src/components/Key.jsx b/taxonium_component/src/components/Key.jsx index 26f1f448..eb0b56b8 100644 --- a/taxonium_component/src/components/Key.jsx +++ b/taxonium_component/src/components/Key.jsx @@ -1,6 +1,7 @@ -import prettifyName from "../utils/prettifyName"; -import { useState } from "react"; +import React, { useState, useEffect } from "react"; import classNames from "classnames"; +import prettifyName from "../utils/prettifyName"; + const Key = ({ keyStuff, colorByField, @@ -14,31 +15,80 @@ const Key = ({ }) => { const numLegendEntries = 10; const [collapsed, setCollapsed] = useState(window.innerWidth < 800); - // sort by item.count in descending order - const sortedKeyStuff = keyStuff.sort((a, b) => b.count - a.count); - // truncate to 10 items - const isTruncated = sortedKeyStuff.length > numLegendEntries; - const topTenKeyStuff = sortedKeyStuff.slice(0, numLegendEntries); - // if there is an item with value of "", remove it - const filteredKeyStuff = topTenKeyStuff.filter((item) => item.value !== ""); - if (colorByField === "None") { - return null; - } - if (!filteredKeyStuff || filteredKeyStuff.length == 0) { + const [colorRamp, setColorRamp] = useState(null); + + useEffect(() => { + if (config.colorRamps && config.colorRamps[colorByField]) { + setColorRamp(config.colorRamps[colorByField]); + } else { + setColorRamp(null); + } + }, [colorByField, config.colorRamps]); + + if (colorByField === "None" || !keyStuff || keyStuff.length === 0) { return null; } + + const sortedKeyStuff = keyStuff + .sort((a, b) => b.count - a.count) + .filter((item) => item.value !== ""); + const isTruncated = sortedKeyStuff.length > numLegendEntries; + const topKeyStuff = sortedKeyStuff.slice(0, numLegendEntries); + + const ColorRampScale = ({ colorRamp }) => { + const { scale } = colorRamp; + const minValue = scale[0][0]; + const maxValue = scale[scale.length - 1][0]; + const range = maxValue - minValue; + + const getPositionPercentage = (value) => { + return ((value - minValue) / range) * 100; + }; + + const gradientStops = scale + .map(([value, color]) => { + const percentage = getPositionPercentage(value); + return `${color} ${percentage}%`; + }) + .join(", "); + + return ( +