|
| 1 | +import * as axes from "./axes" |
| 2 | +import {AxesLabelFont, AxisLocation, defaultAxesLabelFont} from "./axes" |
| 3 | +import * as d3 from "d3"; |
| 4 | +import {ScaleBand} from "d3"; |
| 5 | +import {useChart} from "./hooks/useChart"; |
| 6 | +import {useEffect, useRef} from "react"; |
| 7 | +import {Dimensions, Margin} from "./margins"; |
| 8 | +import {SvgSelection} from "./d3types"; |
| 9 | + |
| 10 | +interface Props { |
| 11 | + // the unique ID of the axis |
| 12 | + axisId: string |
| 13 | + // the location of the axis. for y-axes, this mut be either left or right |
| 14 | + location: AxisLocation.Left | AxisLocation.Right |
| 15 | + // category axes |
| 16 | + scale?: ScaleBand<string> |
| 17 | + // the min and max values for the axis |
| 18 | + categories: Array<string> |
| 19 | + // the font for drawing the axis ticks and labels |
| 20 | + font?: Partial<AxesLabelFont> |
| 21 | + // the axis label |
| 22 | + label: string |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Category axis, which for the moment is only available as a y-axis. The category axis requires |
| 27 | + * a set of categories that will form the y-axis. Generally, these categories should be the name |
| 28 | + * of the series used to represent each category. |
| 29 | + * @param props The properties for the component |
| 30 | + * @return null |
| 31 | + * @constructor |
| 32 | + */ |
| 33 | +export function CategoryAxis(props: Props): null { |
| 34 | + const { |
| 35 | + chartId, |
| 36 | + container, |
| 37 | + plotDimensions, |
| 38 | + margin, |
| 39 | + addYAxis, |
| 40 | + color, |
| 41 | + } = useChart() |
| 42 | + |
| 43 | + const { |
| 44 | + axisId, |
| 45 | + location, |
| 46 | + categories, |
| 47 | + label, |
| 48 | + } = props |
| 49 | + |
| 50 | + const axisRef = useRef<axes.CategoryAxis>() |
| 51 | + |
| 52 | + const axisIdRef = useRef<string>(axisId) |
| 53 | + const marginRef = useRef<Margin>(margin) |
| 54 | + useEffect( |
| 55 | + () => { |
| 56 | + axisIdRef.current = axisId |
| 57 | + marginRef.current = margin |
| 58 | + }, |
| 59 | + [axisId, margin] |
| 60 | + ) |
| 61 | + |
| 62 | + useEffect( |
| 63 | + () => { |
| 64 | + if (container) { |
| 65 | + const svg = d3.select<SVGSVGElement, any>(container) |
| 66 | + const font: AxesLabelFont = {...defaultAxesLabelFont, color, ...props.font} |
| 67 | + |
| 68 | + if (axisRef.current === undefined) { |
| 69 | + axisRef.current = addCategoryYAxis( |
| 70 | + chartId, |
| 71 | + axisId, |
| 72 | + svg, |
| 73 | + plotDimensions, |
| 74 | + categories, |
| 75 | + font, |
| 76 | + margin, |
| 77 | + label, |
| 78 | + location |
| 79 | + ) |
| 80 | + // add the y-axis to the chart context |
| 81 | + addYAxis(axisRef.current, axisId) |
| 82 | + } else { |
| 83 | + // update the category size in case the plot dimensions changed |
| 84 | + axisRef.current.categorySize = axisRef.current.update(categories, categories.length, plotDimensions, margin) |
| 85 | + svg.select(`#${labelIdFor(chartId, location)}`).attr('fill', color) |
| 86 | + } |
| 87 | + } |
| 88 | + }, |
| 89 | + [addYAxis, axisId, categories, chartId, color, container, label, location, margin, plotDimensions, props.font] |
| 90 | + ) |
| 91 | + |
| 92 | + return null |
| 93 | +} |
| 94 | + |
| 95 | +function labelIdFor(chartId: number, location: AxisLocation.Left | AxisLocation.Right): string { |
| 96 | + return `stream-chart-x-axis-${location}-label-${chartId}` |
| 97 | +} |
| 98 | + |
| 99 | +function categorySizeFor(dimensions: Dimensions, margin: Margin, numCategories: number): number { |
| 100 | + return Math.max(margin.bottom, dimensions.height - margin.bottom) / numCategories |
| 101 | +} |
| 102 | + |
| 103 | +function addCategoryYAxis( |
| 104 | + chartId: number, |
| 105 | + axisId: string, |
| 106 | + svg: SvgSelection, |
| 107 | + plotDimensions: Dimensions, |
| 108 | + categories: Array<string>, |
| 109 | + axesLabelFont: AxesLabelFont, |
| 110 | + margin: Margin, |
| 111 | + axisLabel: string, |
| 112 | + location: AxisLocation.Left | AxisLocation.Right, |
| 113 | +): axes.CategoryAxis { |
| 114 | + const categorySize = categorySizeFor(plotDimensions, margin, categories.length) |
| 115 | + const scale = d3.scaleBand() |
| 116 | + .domain(categories) |
| 117 | + .range([0, categorySize * categories.length]); |
| 118 | + |
| 119 | + // create and add the axes |
| 120 | + const generator = location === AxisLocation.Left ? d3.axisLeft(scale) : d3.axisRight(scale) |
| 121 | + |
| 122 | + const selection = svg |
| 123 | + .append<SVGGElement>('g') |
| 124 | + .attr('id', `y-axis-selection-${chartId}`) |
| 125 | + .attr('class', 'y-axis') |
| 126 | + .attr('transform', `translate(${xTranslation(location, plotDimensions, margin)}, ${margin.top})`) |
| 127 | + .call(generator); |
| 128 | + |
| 129 | + svg |
| 130 | + .append<SVGTextElement>('text') |
| 131 | + .attr('id', labelIdFor(chartId, location)) |
| 132 | + .attr('text-anchor', 'middle') |
| 133 | + .attr('font-size', axesLabelFont.size) |
| 134 | + .attr('fill', axesLabelFont.color) |
| 135 | + .attr('font-family', axesLabelFont.family) |
| 136 | + .attr('font-weight', axesLabelFont.weight) |
| 137 | + .attr('transform', `translate(${labelXTranslation(location, plotDimensions, margin, axesLabelFont)}, ${labelYTranslation(plotDimensions, margin)}) rotate(-90)`) |
| 138 | + .text(axisLabel) |
| 139 | + |
| 140 | + const axis = {axisId, selection, location, scale, generator, categorySize, update: () => categorySize} |
| 141 | + |
| 142 | + return { |
| 143 | + ...axis, |
| 144 | + update: (categoryNames, unfilteredSize, dimensions) => |
| 145 | + updateCategoryYAxis(chartId, svg, axis, dimensions, unfilteredSize, categoryNames, axesLabelFont, margin, location) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +function updateCategoryYAxis( |
| 150 | + chartId: number, |
| 151 | + svg: SvgSelection, |
| 152 | + axis: axes.CategoryAxis, |
| 153 | + plotDimensions: Dimensions, |
| 154 | + unfilteredSize: number, |
| 155 | + names: Array<string>, |
| 156 | + axesLabelFont: AxesLabelFont, |
| 157 | + margin: Margin, |
| 158 | + location: AxisLocation.Left | AxisLocation.Right, |
| 159 | +): number { |
| 160 | + const categorySize = categorySizeFor(plotDimensions, margin, unfilteredSize) |
| 161 | + axis.scale |
| 162 | + .domain(names) |
| 163 | + .range([0, categorySize * names.length]) |
| 164 | + axis.selection |
| 165 | + .attr('transform', `translate(${xTranslation(location, plotDimensions, margin)}, ${margin.top})`) |
| 166 | + .call(axis.generator) |
| 167 | + |
| 168 | + svg |
| 169 | + .select(`#${labelIdFor(chartId, location)}`) |
| 170 | + .attr('transform', `translate(${labelXTranslation(location, plotDimensions, margin, axesLabelFont)}, ${labelYTranslation(plotDimensions, margin)}) rotate(-90)`) |
| 171 | + |
| 172 | + return categorySize |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +function xTranslation(location: AxisLocation.Left | AxisLocation.Right, plotDimensions: Dimensions, margin: Margin): number { |
| 177 | + return location === AxisLocation.Left ? |
| 178 | + margin.left : |
| 179 | + margin.left + plotDimensions.width |
| 180 | +} |
| 181 | + |
| 182 | +function labelXTranslation( |
| 183 | + location: AxisLocation.Left | AxisLocation.Right, |
| 184 | + plotDimensions: Dimensions, |
| 185 | + margin: Margin, |
| 186 | + axesLabelFont: AxesLabelFont, |
| 187 | +): number { |
| 188 | + return location === AxisLocation.Left ? |
| 189 | + axesLabelFont.size : |
| 190 | + margin.left + plotDimensions.width + margin.right - axesLabelFont.size |
| 191 | +} |
| 192 | + |
| 193 | +function labelYTranslation(plotDimensions: Dimensions, margin: Margin): number { |
| 194 | + return (margin.top + margin.bottom + plotDimensions.height) / 2 |
| 195 | +} |
| 196 | + |
0 commit comments