(
id: string,
colors: Array<{
color: string;
proportion: number;
}>,
svgRoot?: SVGElement
)
| 730 | } |
| 731 | |
| 732 | function getGradient( |
| 733 | id: string, |
| 734 | colors: Array<{ |
| 735 | color: string; |
| 736 | proportion: number; |
| 737 | }>, |
| 738 | svgRoot?: SVGElement |
| 739 | ): string { |
| 740 | let escapedId = tf_graph_util.escapeQuerySelector(id); |
| 741 | if (!svgRoot) return `url(#${escapedId})`; |
| 742 | let $svgRoot = d3.select(svgRoot); |
| 743 | let gradientDefs = $svgRoot.select('defs#_graph-gradients'); |
| 744 | if (gradientDefs.empty()) { |
| 745 | gradientDefs = $svgRoot.append('defs').attr('id', '_graph-gradients'); |
| 746 | } |
| 747 | let linearGradient = gradientDefs.select('linearGradient#' + escapedId); |
| 748 | // If the linear gradient is not there yet, create it. |
| 749 | if (linearGradient.empty()) { |
| 750 | linearGradient = gradientDefs.append('linearGradient').attr('id', id); |
| 751 | // Re-create the stops of the linear gradient. |
| 752 | linearGradient.selectAll('*').remove(); |
| 753 | let cumulativeProportion = 0; |
| 754 | // For each color, create a stop using the proportion of that device. |
| 755 | _.each(colors, (d) => { |
| 756 | let color = d.color; |
| 757 | linearGradient |
| 758 | .append('stop') |
| 759 | .attr('offset', cumulativeProportion) |
| 760 | .attr('stop-color', color); |
| 761 | linearGradient |
| 762 | .append('stop') |
| 763 | .attr('offset', cumulativeProportion + d.proportion) |
| 764 | .attr('stop-color', color); |
| 765 | cumulativeProportion += d.proportion; |
| 766 | }); |
| 767 | } |
| 768 | return `url(#${escapedId})`; |
| 769 | } |
| 770 | export function removeGradientDefinitions(svgRoot: SVGElement) { |
| 771 | d3.select(svgRoot).select('defs#_graph-gradients').remove(); |
| 772 | } |
no test coverage detected
searching dependent graphs…