import * as React from 'react';
import { LegendDataWithId } from './PlotUtils';
import { Point } from 'victory';
import { toFixedWithoutTrailingZeros } from '../../lib/FormatUtils';
export interface ILegendDataComponentProps {
orientation: 'horizontal' | 'vertical';
// these props are typed as optional because of the way victory api works:
// dataComponent={},
// but they are always passed in
datum?: LegendDataWithId;
x?: number;
y?: number;
}
export default class LegendDataComponent extends React.Component<
ILegendDataComponentProps,
{}
> {
render() {
const horizontal = this.props.orientation === 'horizontal';
if (this.props.datum!.symbol.type === 'gradient') {
const colorFn = this.props.datum!.symbol.colorFn;
const range = this.props.datum!.symbol.range;
const gradientId = this.props.datum!.symbol.gradientUid;
const GRADIENTMESH = 30;
const gradientStopPoints = [];
for (let i = 0; i < GRADIENTMESH; i++) {
const fraction = i / GRADIENTMESH;
gradientStopPoints.push(
);
}
const gradientElt = (
{gradientStopPoints}
);
const rectThickness = 25;
const largeRange = range[1] - range[0] > 5;
const rectLength = largeRange ? 90 : 50;
let rectWidth, rectHeight, rectX, rectY, labelDx, labelDy;
if (horizontal) {
rectWidth = rectLength;
rectHeight = rectThickness;
rectX = this.props.x!;
rectY = this.props.y! - rectHeight / 2;
labelDx = '-0.5em';
labelDy = '1em';
} else {
rectWidth = rectThickness;
rectHeight = rectLength;
rectX = this.props.x!;
rectY = this.props.y!;
labelDx = '0em';
labelDy = '0.5em';
}
const rect = (
);
const labels = [
{toFixedWithoutTrailingZeros(range[1], 2)}
,
{toFixedWithoutTrailingZeros(range[0], 2)}
,
];
if (largeRange) {
// only add a middle label if theres room
labels.push(
{toFixedWithoutTrailingZeros(
(range[0] + range[1]) / 2,
2
)}
);
}
return (
{gradientElt}
{rect}
{labels}
);
} else {
// default victory component
const point = ;
if (this.props.datum!.margin !== undefined) {
const dx = horizontal ? this.props.datum!.margin : 0;
const dy = horizontal ? 0 : this.props.datum!.margin;
return {point};
} else {
return point;
}
}
}
}