Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/__stories__/__data__/other/bands/plot-bands-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function prepareData(): ChartData {
color: '#0fd17a',
opacity: 0.5,
layerPlacement: 'after',
label: {
text: 'plot band label',
},
},
{
from: 150,
Expand Down
6 changes: 6 additions & 0 deletions src/__stories__/__data__/other/bands/plot-bands-y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function prepareData(): ChartData {
color: '#0fd17a',
opacity: 0.5,
layerPlacement: 'after',
label: {
text: 'plot band label',
style: {
fontColor: 'white',
},
},
},
{
from: 5,
Expand Down
3 changes: 3 additions & 0 deletions src/__stories__/__data__/other/lines/plot-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function prepareData(): ChartData {
dashStyle: 'Dot',
opacity: 0.6,
layerPlacement: 'before', // Line behind the graph
label: {
text: 'plot line label',
},
},
],
},
Expand Down
3 changes: 3 additions & 0 deletions src/__stories__/__data__/other/lines/y-axis-plot-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function prepareData(): ChartData {
color: 'red',
dashStyle: 'Dash',
layerPlacement: 'after',
label: {
text: 'plot line label',
},
},
{
value: 200,
Expand Down
151 changes: 98 additions & 53 deletions src/components/Axis/AxisX.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import React from 'react';
import {line, select} from 'd3';
import type {AxisDomain, AxisScale, Selection} from 'd3';

import type {ChartScale, PreparedAxis, PreparedAxisPlotLine, PreparedSplit} from '../../hooks';
import type {
ChartScale,
PreparedAxis,
PreparedAxisPlotBand,
PreparedAxisPlotLine,
PreparedSplit,
} from '../../hooks';
import type {AxisPlotBand} from '../../types';
import {
block,
Expand Down Expand Up @@ -101,21 +107,6 @@ export const AxisX = React.memo(function AxisX(props: Props) {
const svgElement = select(ref.current);
svgElement.selectAll('*').remove();

const plotDataAttr = 'data-plot-x';

let plotBeforeContainer = null;
let plotAfterContainer = null;

if (plotBeforeRef?.current) {
plotBeforeContainer = select(plotBeforeRef.current);
plotBeforeContainer.selectAll(`[${plotDataAttr}]`).remove();
}

if (plotAfterRef?.current) {
plotAfterContainer = select(plotAfterRef.current);
plotAfterContainer.selectAll(`[${plotDataAttr}]`).remove();
}

if (!axis.visible) {
return;
}
Expand Down Expand Up @@ -181,6 +172,21 @@ export const AxisX = React.memo(function AxisX(props: Props) {
});
}

const plotDataAttr = 'data-plot-x';

let plotBeforeContainer = null;
let plotAfterContainer = null;

if (plotBeforeRef?.current) {
plotBeforeContainer = select(plotBeforeRef.current);
plotBeforeContainer.selectAll(`[${plotDataAttr}]`).remove();
}

if (plotAfterRef?.current) {
plotAfterContainer = select(plotAfterRef.current);
plotAfterContainer.selectAll(`[${plotDataAttr}]`).remove();
}

// add plot bands
if (axis.plotBands.length > 0) {
const plotBandDataAttr = 'plot-x-band';
Expand All @@ -200,26 +206,46 @@ export const AxisX = React.memo(function AxisX(props: Props) {
.attr(plotDataAttr, 1)
.attr(plotBandDataAttr, 1);

plotBandsSelection
.append('rect')
.attr('x', (band) => {
const {from, to} = getBandsPosition({band, axisScale, axis: 'x'});
const halfBandwidth = (axisScale.bandwidth?.() ?? 0) / 2;
const startPos = halfBandwidth + Math.min(from, to);

return Math.max(0, startPos);
})
.attr('width', (band) => {
const {from, to} = getBandsPosition({band, axisScale, axis: 'x'});
const startPos = width - Math.min(from, to);
const endPos = Math.min(Math.abs(to - from), startPos);

return Math.min(endPos, width);
})
.attr('y', 0)
.attr('height', totalHeight)
.attr('fill', (band) => band.color)
.attr('opacity', (band) => band.opacity);
plotBandsSelection.each(function () {
const plotBandSelection = select(this);
const band = plotBandSelection.datum() as PreparedAxisPlotBand;
const {from, to} = getBandsPosition({band, axisScale, axis: 'x'});
const halfBandwidth = (axisScale.bandwidth?.() ?? 0) / 2;
const startPos = halfBandwidth + Math.min(from, to);
const x = Math.max(0, startPos);

plotBandSelection
.append('rect')
.attr('x', x)
.attr('width', () => {
const endPos = Math.min(
Math.abs(to - from),
width - Math.min(from, to),
);

return Math.min(endPos, width);
})
.attr('y', 0)
.attr('height', totalHeight)
.attr('fill', () => band.color)
.attr('opacity', () => band.opacity);

if (band.label.text) {
const labelPadding = band.label?.padding ?? 0;
plotBandSelection
.append('text')
.text(band.label.text)
.style('fill', () => band.label.style?.fontColor ?? null)
.style('font-size', () => band.label.style?.fontSize ?? null)
.style('font-weight', () => band.label.style?.fontWeight ?? null)
.style('dominant-baseline', 'text-before-edge')
.style('text-anchor', 'end')
.style(
'transform',
`translate(${x + labelPadding}px, ${labelPadding}px) rotate(-90deg)`,
);
}
});
};

setPlotBands(
Expand Down Expand Up @@ -253,23 +279,42 @@ export const AxisX = React.memo(function AxisX(props: Props) {
.attr(plotLineDataAttr, 1);

const lineGenerator = line();
plotLinesSelection
.append('path')
.attr('d', (plotLine) => {
const plotLineValue = Number(axisScale(plotLine.value));
const points: [number, number][] = [
[plotLineValue, 0],
[plotLineValue, totalHeight],
];

return lineGenerator(points);
})
.attr('stroke', (plotLine) => plotLine.color)
.attr('stroke-width', (plotLine) => plotLine.width)
.attr('stroke-dasharray', (plotLine) =>
getLineDashArray(plotLine.dashStyle, plotLine.width),
)
.attr('opacity', (plotLine) => plotLine.opacity);
plotLinesSelection.each(function () {
const itemSelection = select(this);
const plotLine = itemSelection.datum() as PreparedAxisPlotLine;

const plotLineValue = Number(axisScale(plotLine.value));
const points: [number, number][] = [
[plotLineValue, 0],
[plotLineValue, totalHeight],
];

itemSelection
.append('path')
.attr('d', lineGenerator(points))
.attr('stroke', plotLine.color)
.attr('stroke-width', plotLine.width)
.attr(
'stroke-dasharray',
getLineDashArray(plotLine.dashStyle, plotLine.width),
)
.attr('opacity', plotLine.opacity);

if (plotLine.label.text) {
itemSelection
.append('text')
.text(plotLine.label.text)
.style('fill', () => plotLine.label.style.fontColor ?? null)
.attr('font-size', plotLine.label.style.fontSize)
.style('font-weight', () => plotLine.label.style.fontWeight ?? null)
.style('dominant-baseline', 'text-after-edge')
.style('text-anchor', 'end')
.style(
'transform',
`translate(${plotLineValue - plotLine.label.padding}px, ${plotLine.label.padding}px) rotate(-90deg)`,
);
}
});
};

setPlotLines(
Expand Down
110 changes: 72 additions & 38 deletions src/components/Axis/AxisY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import React from 'react';
import {axisLeft, axisRight, line, select} from 'd3';
import type {Axis, AxisDomain, AxisScale, BaseType, Selection} from 'd3';

import type {ChartScale, PreparedAxis, PreparedAxisPlotLine, PreparedSplit} from '../../hooks';
import type {
ChartScale,
PreparedAxis,
PreparedAxisPlotBand,
PreparedAxisPlotLine,
PreparedSplit,
} from '../../hooks';
import type {AxisPlotBand} from '../../types';
import {
block,
Expand Down Expand Up @@ -297,26 +303,38 @@ export const AxisY = (props: Props) => {
.attr(plotBandDataAttr, 1)
.style('transform', getAxisPlotsPosition(d, split));

plotBandsSelection
.append('rect')
.attr('x', 0)
.attr('width', width)
.attr('y', (band) => {
const {from, to} = getBandsPosition({band, axisScale, axis: 'y'});
const halfBandwidth = (axisScale.bandwidth?.() ?? 0) / 2;
const startPos = halfBandwidth + Math.min(from, to);

return Math.max(0, startPos);
})
.attr('height', (band) => {
const {from, to} = getBandsPosition({band, axisScale, axis: 'y'});
const startPos = height - Math.min(from, to);
const endPos = Math.min(Math.abs(to - from), startPos);

return Math.min(endPos, height);
})
.attr('fill', (band) => band.color)
.attr('opacity', (band) => band.opacity);
plotBandsSelection.each(function () {
const plotBandSelection = select(this);
const band = plotBandSelection.datum() as PreparedAxisPlotBand;

const {from, to} = getBandsPosition({band, axisScale, axis: 'y'});
const halfBandwidth = (axisScale.bandwidth?.() ?? 0) / 2;
const startPos = halfBandwidth + Math.min(from, to);
const endPos = Math.min(Math.abs(to - from), height - Math.min(from, to));
const y = Math.max(0, startPos);

plotBandSelection
.append('rect')
.attr('x', 0)
.attr('width', width)
.attr('y', y)
.attr('height', Math.min(endPos, height))
.attr('fill', () => band.color)
.attr('opacity', () => band.opacity);

if (band.label.text) {
const labelPadding = band.label?.padding ?? 0;
plotBandSelection
.append('text')
.text(band.label.text)
.style('fill', () => band.label.style?.fontColor ?? null)
.style('font-size', () => band.label.style?.fontSize ?? null)
.style('font-weight', () => band.label.style?.fontWeight ?? null)
.style('dominant-baseline', 'text-before-edge')
.attr('x', labelPadding)
.attr('y', y + labelPadding);
}
});
};

setPlotBands(
Expand Down Expand Up @@ -349,23 +367,38 @@ export const AxisY = (props: Props) => {
.attr(plotLineDataAttr, 1)
.style('transform', getAxisPlotsPosition(d, split));

plotLinesSelection
.append('path')
.attr('d', (plotLine) => {
const plotLineValue = Number(axisScale(plotLine.value));
const points: [number, number][] = [
[0, plotLineValue],
[width, plotLineValue],
];

return lineGenerator(points);
})
.attr('stroke', (plotLine) => plotLine.color)
.attr('stroke-width', (plotLine) => plotLine.width)
.attr('stroke-dasharray', (plotLine) =>
getLineDashArray(plotLine.dashStyle, plotLine.width),
)
.attr('opacity', (plotLine) => plotLine.opacity);
plotLinesSelection.each(function () {
const itemSelection = select(this);
const plotLine = itemSelection.datum() as PreparedAxisPlotLine;

const plotLineValue = Number(axisScale(plotLine.value));
const points: [number, number][] = [
[0, plotLineValue],
[width, plotLineValue],
];

itemSelection
.append('path')
.attr('d', lineGenerator(points))
.attr('stroke', plotLine.color)
.attr('stroke-width', plotLine.width)
.attr(
'stroke-dasharray',
getLineDashArray(plotLine.dashStyle, plotLine.width),
)
.attr('opacity', plotLine.opacity);

if (plotLine.label.text) {
itemSelection
.append('text')
.text(plotLine.label.text)
.style('fill', () => plotLine.label.style.fontColor ?? null)
.attr('font-size', plotLine.label.style.fontSize)
.style('font-weight', () => plotLine.label.style.fontWeight ?? null)
.attr('x', plotLine.label.padding)
.attr('y', plotLineValue - plotLine.label.padding);
}
});
};

setPlotLines(
Expand Down Expand Up @@ -451,6 +484,7 @@ export const AxisY = (props: Props) => {
lineGenerator,
plotBeforeRef,
plotAfterRef,
topLimit,
]);

return <g ref={ref} className={b('container')} />;
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useChartOptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export type PreparedAxisPlotLine = {
dashStyle: DashStyle;
opacity: number;
layerPlacement: PlotLayerPlacement;
label: {
text: string;
style: BaseTextStyle;
padding: number;
};
};

export type PreparedAxis = Omit<ChartAxis, 'type' | 'labels' | 'plotLines' | 'plotBands'> & {
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useChartOptions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {DEFAULT_AXIS_LABEL_FONT_SIZE} from '../../constants';
import type {AxisPlot} from '../../types';

export function prepareAxisPlotLabel(d: AxisPlot) {
return {
text: d.label?.text ?? '',
style: {
fontSize: DEFAULT_AXIS_LABEL_FONT_SIZE,
fontColor: 'var(--g-color-text-secondary)',
...d.label?.style,
},
padding: d.label?.padding ?? 5,
};
}
Loading
Loading