import React from 'react'; import { range } from 'd3'; import { SequentialGradientColorscale, DivergingGradientColorscale, } from '../../types/plots/addOns'; // set props for custom legend function export interface PlotLegendGradientProps { legendMax: number; legendMin: number; gradientColorscaleType?: 'sequential' | 'divergent'; nTicks?: number; // MUST be odd! showMissingness?: boolean; } // legend ellipsis function for legend title and legend items (from custom legend work) const legendEllipsis = (label: string, ellipsisLength: number) => { return (label || '').length > ellipsisLength ? (label || '').substring(0, ellipsisLength) + '...' : label; }; // make gradient colorscale legend into a component so it can be more easily incorporated into DK's custom legend if we need export default function PlotGradientLegend({ legendMax, legendMin, gradientColorscaleType, nTicks, showMissingness, }: PlotLegendGradientProps) { // Declare constants const gradientBoxHeight = 150; const gradientBoxWidth = 20; const tickLength = 4; const defaultNTicks = 5; const tickFontSize = '0.8em'; const legendTextSize = '1.0em'; nTicks = nTicks || defaultNTicks; let gradientColorscale = gradientColorscaleType == 'divergent' ? DivergingGradientColorscale : SequentialGradientColorscale; // Create gradient stop points from the colorscale const stopPoints = gradientColorscale.map((color: string, index: number) => { let stopPercentage = (100 * index) / (gradientColorscale.length - 1) + '%'; return ( ); }); // Create ticks const ticks = range(nTicks).map((a: number) => { const location: number = gradientBoxHeight - gradientBoxHeight * (a / (nTicks! - 1)); // draw bottom to top return ( {( (a / (nTicks! - 1)) * (legendMax - legendMin) + legendMin ).toPrecision(3)} ); }); return (
{stopPoints} {ticks} {showMissingness && (
×
)}
); }