|
| 1 | +import {scale} from "../scales.js"; |
| 2 | +import {create, scaleLinear, quantize, interpolate, interpolateRound, quantile, range, format, scaleBand, axisBottom} from "d3"; |
| 3 | + |
| 4 | +export function legendRamp(color, { |
| 5 | + label, |
| 6 | + tickSize = 6, |
| 7 | + width = 240, |
| 8 | + height = 44 + tickSize, |
| 9 | + marginTop = 18, |
| 10 | + marginRight = 0, |
| 11 | + marginBottom = 16 + tickSize, |
| 12 | + marginLeft = 0, |
| 13 | + ticks = width / 64, |
| 14 | + tickFormat, |
| 15 | + tickValues |
| 16 | +} = {}) { |
| 17 | + color = scale(color); |
| 18 | + const svg = create("svg") |
| 19 | + .attr("width", width) |
| 20 | + .attr("height", height) |
| 21 | + .attr("viewBox", [0, 0, width, height]) |
| 22 | + .style("overflow", "visible") |
| 23 | + .style("display", "block"); |
| 24 | + |
| 25 | + let tickAdjust = g => g.selectAll(".tick line").attr("y1", marginTop + marginBottom - height); |
| 26 | + let x; |
| 27 | + |
| 28 | + // Continuous |
| 29 | + if (color.interpolate) { |
| 30 | + const n = Math.min(color.domain().length, color.range().length); |
| 31 | + x = color.copy().rangeRound(quantize(interpolate(marginLeft, width - marginRight), n)); |
| 32 | + let color2 = color.copy().domain(quantize(interpolate(0, 1), n)); |
| 33 | + // special case for log scales |
| 34 | + if (color.base) { |
| 35 | + const p = scaleLinear( |
| 36 | + quantize(interpolate(0, 1), color.domain().length), |
| 37 | + color.domain().map(d => Math.log(d)) |
| 38 | + ); |
| 39 | + color2 = t => color(Math.exp(p(t))); |
| 40 | + } |
| 41 | + svg.append("image") |
| 42 | + .attr("x", marginLeft) |
| 43 | + .attr("y", marginTop) |
| 44 | + .attr("width", width - marginLeft - marginRight) |
| 45 | + .attr("height", height - marginTop - marginBottom) |
| 46 | + .attr("preserveAspectRatio", "none") |
| 47 | + .attr("xlink:href", ramp(color2).toDataURL()); |
| 48 | + } |
| 49 | + |
| 50 | + // Sequential |
| 51 | + else if (color.interpolator) { |
| 52 | + x = Object.assign(color.copy() |
| 53 | + .interpolator(interpolateRound(marginLeft, width - marginRight)), |
| 54 | + {range() { return [marginLeft, width - marginRight]; }}); |
| 55 | + |
| 56 | + svg.append("image") |
| 57 | + .attr("x", marginLeft) |
| 58 | + .attr("y", marginTop) |
| 59 | + .attr("width", width - marginLeft - marginRight) |
| 60 | + .attr("height", height - marginTop - marginBottom) |
| 61 | + .attr("preserveAspectRatio", "none") |
| 62 | + .attr("xlink:href", ramp(color.interpolator()).toDataURL()); |
| 63 | + |
| 64 | + // scaleSequentialQuantile doesn’t implement ticks or tickFormat. |
| 65 | + if (!x.ticks) { |
| 66 | + if (tickValues === undefined) { |
| 67 | + const n = Math.round(ticks + 1); |
| 68 | + tickValues = range(n).map(i => quantile(color.domain(), i / (n - 1))); |
| 69 | + } |
| 70 | + if (typeof tickFormat !== "function") { |
| 71 | + tickFormat = format(tickFormat === undefined ? ",f" : tickFormat); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Threshold |
| 77 | + else if (color.invertExtent) { |
| 78 | + const thresholds |
| 79 | + = color.thresholds ? color.thresholds() // scaleQuantize |
| 80 | + : color.quantiles ? color.quantiles() // scaleQuantile |
| 81 | + : color.domain(); // scaleThreshold |
| 82 | + |
| 83 | + const thresholdFormat |
| 84 | + = tickFormat === undefined ? d => d |
| 85 | + : typeof tickFormat === "string" ? format(tickFormat) |
| 86 | + : tickFormat; |
| 87 | + |
| 88 | + x = scaleLinear() |
| 89 | + .domain([-1, color.range().length - 1]) |
| 90 | + .rangeRound([marginLeft, width - marginRight]); |
| 91 | + |
| 92 | + svg.append("g") |
| 93 | + .selectAll("rect") |
| 94 | + .data(color.range()) |
| 95 | + .join("rect") |
| 96 | + .attr("x", (d, i) => x(i - 1)) |
| 97 | + .attr("y", marginTop) |
| 98 | + .attr("width", (d, i) => x(i) - x(i - 1)) |
| 99 | + .attr("height", height - marginTop - marginBottom) |
| 100 | + .attr("fill", d => d); |
| 101 | + |
| 102 | + tickValues = range(thresholds.length); |
| 103 | + tickFormat = i => thresholdFormat(thresholds[i], i); |
| 104 | + } |
| 105 | + |
| 106 | + // Ordinal |
| 107 | + else { |
| 108 | + x = scaleBand() |
| 109 | + .domain(color.domain()) |
| 110 | + .rangeRound([marginLeft, width - marginRight]); |
| 111 | + |
| 112 | + svg.append("g") |
| 113 | + .selectAll("rect") |
| 114 | + .data(color.domain()) |
| 115 | + .join("rect") |
| 116 | + .attr("x", x) |
| 117 | + .attr("y", marginTop) |
| 118 | + .attr("width", Math.max(0, x.bandwidth() - 1)) |
| 119 | + .attr("height", height - marginTop - marginBottom) |
| 120 | + .attr("fill", color); |
| 121 | + |
| 122 | + tickAdjust = () => {}; |
| 123 | + } |
| 124 | + |
| 125 | + svg.append("g") |
| 126 | + .attr("transform", `translate(0,${height - marginBottom})`) |
| 127 | + .call(axisBottom(x) |
| 128 | + .ticks(ticks, typeof tickFormat === "string" ? tickFormat : undefined) |
| 129 | + .tickFormat(typeof tickFormat === "function" ? tickFormat : undefined) |
| 130 | + .tickSize(tickSize) |
| 131 | + .tickValues(tickValues)) |
| 132 | + .call(tickAdjust) |
| 133 | + .call(g => g.select(".domain").remove()) |
| 134 | + .call(label === undefined ? () => {} |
| 135 | + : g => g.append("text") |
| 136 | + .attr("x", marginLeft) |
| 137 | + .attr("y", marginTop + marginBottom - height - 6) |
| 138 | + .attr("fill", "currentColor") |
| 139 | + .attr("text-anchor", "start") |
| 140 | + .attr("font-weight", "bold") |
| 141 | + .attr("class", "label") |
| 142 | + .text(label)); |
| 143 | + |
| 144 | + return svg.node(); |
| 145 | +} |
| 146 | + |
| 147 | +function ramp(color, n = 256) { |
| 148 | + const canvas = create("canvas").attr("width", n).attr("height", 1).node(); |
| 149 | + const context = canvas.getContext("2d"); |
| 150 | + for (let i = 0; i < n; ++i) { |
| 151 | + context.fillStyle = color(i / (n - 1)); |
| 152 | + context.fillRect(i, 0, 1, 1); |
| 153 | + } |
| 154 | + return canvas; |
| 155 | +} |
0 commit comments