react d3 比色卡的使用
d3有自带的比色卡,可以使用最大值最小值来设置地图等的比色卡,例如这样:
import React, { useEffect, useRef } from 'react';
import * as d3 from 'd3';
const ColorCard = ({ minValue, maxValue }) => {
const svgRef = useRef(null);
useEffect(() => {
const svg = d3.select(svgRef.current);
const colorScale = d3
.scaleLinear()
.domain([minValue, maxValue])
.range(['blue', 'red']);
svg.append('rect')
.attr('x', 10)
.attr('y', 10)
.attr('width', 200)
.attr('height', 20)
.style('fill', (d) => colorScale(d));
}, [minValue, maxValue]);
return (
<svg ref={svgRef} width={220} height={40} />
);
};
export default ColorCard;