Skip to content

Commit

Permalink
feat(D3 plugin): axis maxPadding options (#231)
Browse files Browse the repository at this point in the history
* feat(D3 plugin) axis maxPadding options

* fix formatAxisTickLabel
  • Loading branch information
kuzmadom authored Aug 17, 2023
1 parent 0a4c3c0 commit 89602a3
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ const shapeScatterChartData = (
categories?: string[],
): ChartKitWidgetData => {
let xAxis: ChartKitWidgetAxis = {
labels: {
enabled: false,
},
title: {
text: text('X axis title', ''),
},
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/d3/__stories__/scatter/Timestamp.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ const shapeData = (data: Record<string, any>[]): ChartKitWidgetData<string> => {
grid: {
enabled: boolean('xAxis.grid.enabled', true),
},
labels: {
enabled: boolean('xAxis.labels.enabled', true),
},
},
yAxis: [
{
grid: {
enabled: boolean('yAxis.grid.enabled', true),
},
labels: {
enabled: boolean('yAxis.labels.enabled', true),
},
},
],
tooltip: {
Expand All @@ -90,9 +96,6 @@ const shapeData = (data: Record<string, any>[]): ChartKitWidgetData<string> => {
return <div style={{color: d.custom}}>{dateTime({input: d.x}).format('LL')}</div>;
},
},
title: {
text: 'Chart title',
},
};
};

Expand Down
14 changes: 11 additions & 3 deletions src/plugins/d3/renderer/components/AxisX.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,25 @@ export const AxisX = ({axis, width, height, scale}: Props) => {
.tickSize(tickSize)
.tickPadding(axis.labels.padding)
.tickFormat((value) => {
if (!axis.labels.enabled) {
return '';
}

return formatAxisTickLabel({
axisType: axis.type,
value,
dateFormat: axis.labels.dateFormat,
numberFormat: axis.labels.numberFormat,
dateFormat: axis.labels['dateFormat'],
numberFormat: axis.labels['numberFormat'],
});
});

svgElement.call(xAxisGenerator).attr('class', b());
svgElement.select('.domain').attr('d', `M0,0V0H${width}`);
svgElement.selectAll('.tick text').style('font-size', axis.labels.style.fontSize);

if (axis.labels.enabled) {
svgElement.selectAll('.tick text').style('font-size', axis.labels.style.fontSize);
}

const transformStyle = svgElement.select('.tick').attr('transform');
const {x} = parseTransformStyle(transformStyle);

Expand Down
20 changes: 14 additions & 6 deletions src/plugins/d3/renderer/components/AxisY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,28 @@ export const AxisY = ({axises, width, height, scale}: Props) => {
.tickSize(tickSize)
.tickPadding(axis.labels.padding)
.tickFormat((value) => {
if (!axis.labels.enabled) {
return '';
}

return formatAxisTickLabel({
axisType: axis.type,
value,
dateFormat: axis.labels.dateFormat,
numberFormat: axis.labels.numberFormat,
dateFormat: axis.labels['dateFormat'],
numberFormat: axis.labels['numberFormat'],
});
});

svgElement.call(yAxisGenerator).attr('class', b());
svgElement.select('.domain').attr('d', `M0,${height}H0V0`);
svgElement
.selectAll('.tick text')
.style('font-size', axis.labels.style.fontSize)
.style('transform', 'translateY(-1px)');

if (axis.labels.enabled) {
svgElement
.selectAll('.tick text')
.style('font-size', axis.labels.style.fontSize)
.style('transform', 'translateY(-1px)');
}

const transformStyle = svgElement.select('.tick').attr('transform');
const {y} = parseTransformStyle(transformStyle);

Expand Down
26 changes: 18 additions & 8 deletions src/plugins/d3/renderer/hooks/useChartOptions/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ const getAxisLabelMaxWidth = (args: {axis: PreparedAxis; series: ChartKitWidgetS
}
}

const formattedValue = formatAxisTickLabel({
axisType: axis.type,
value: maxDomainValue,
dateFormat: axis.labels.dateFormat,
numberFormat: axis.labels.numberFormat,
});
let formattedValue = '';

if (axis.labels.enabled) {
formattedValue = formatAxisTickLabel({
axisType: axis.type,
value: maxDomainValue,
dateFormat: axis.labels['dateFormat'],
numberFormat: axis.labels['numberFormat'],
});
}

select(document.body)
.append('text')
Expand Down Expand Up @@ -69,11 +73,17 @@ export const getPreparedChart = (args: {
preparedY1Axis.labels.padding +
getAxisLabelMaxWidth({axis: preparedY1Axis, series: series.data}) +
(preparedY1Axis.title.height || 0);
const marginTop =
get(chart, 'margin.top', 0) +
getHorisontalSvgTextDimensions({text: 'Tmp', style: preparedY1Axis.labels.style}) / 2;
const marginRight =
get(chart, 'margin.right', 0) +
getAxisLabelMaxWidth({axis: preparedXAxis, series: series.data}) / 2;

return {
margin: {
top: get(chart, 'margin.top', 0),
right: get(chart, 'margin.right', 0),
top: marginTop,
right: marginRight,
bottom: marginBottom,
left: marginLeft,
},
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type PreparedAxis = Omit<ChartKitWidgetAxis, 'type' | 'labels'> & {
grid: {
enabled: boolean;
};
maxPadding: number;
};

export type PreparedTitle = ChartKitWidgetData['title'] & {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/x-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const getPreparedXAxis = ({xAxis}: {xAxis: ChartKitWidgetData['xAxis']}):
: 0,
},
min: get(xAxis, 'min'),
maxPadding: get(xAxis, 'maxPadding', 0.01),
grid: {
enabled: get(xAxis, 'grid.enabled', true),
},
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/y-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const getPreparedYAxis = ({yAxis}: {yAxis: ChartKitWidgetData['yAxis']}):
: 0,
},
min: get(yAxis1, 'min'),
maxPadding: get(yAxis1, 'maxPadding', 0.05),
grid: {
enabled: get(yAxis1, 'grid.enabled', true),
},
Expand Down
21 changes: 12 additions & 9 deletions src/plugins/d3/renderer/hooks/useScales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ export const useScales = (args: Args): ReturnValue => {
switch (xType) {
case 'linear': {
const domain = getDomainDataXBySeries(visibleSeries);
const range = [0, boundsWidth - boundsWidth * xAxis.maxPadding];

if (isNumericalArrayData(domain)) {
const [xMin, xMax] = extent(domain) as [number, number];
xScale = scaleLinear().domain([xMin, xMax]).range([0, boundsWidth]).nice();
xScale = scaleLinear().domain([xMin, xMax]).range(range).nice();
}

break;
Expand All @@ -78,15 +79,17 @@ export const useScales = (args: Args): ReturnValue => {
break;
}
case 'datetime': {
const range = [0, boundsWidth - boundsWidth * xAxis.maxPadding];

if (xTimestamps) {
const [xMin, xMax] = extent(xTimestamps) as [number, number];
xScale = scaleUtc().domain([xMin, xMax]).range([0, boundsWidth]).nice();
xScale = scaleUtc().domain([xMin, xMax]).range(range).nice();
} else {
const domain = getDomainDataXBySeries(visibleSeries);

if (isNumericalArrayData(domain)) {
const [xMin, xMax] = extent(domain) as [number, number];
xScale = scaleUtc().domain([xMin, xMax]).range([0, boundsWidth]).nice();
xScale = scaleUtc().domain([xMin, xMax]).range(range).nice();
}
}

Expand All @@ -101,14 +104,12 @@ export const useScales = (args: Args): ReturnValue => {
switch (yType) {
case 'linear': {
const domain = getDomainDataYBySeries(visibleSeries);
const range = [boundsHeight, boundsHeight * yAxis[0].maxPadding];

if (isNumericalArrayData(domain)) {
const [domainYMin, yMax] = extent(domain) as [number, number];
const yMinValue = typeof yMin === 'number' ? yMin : domainYMin;
yScale = scaleLinear()
.domain([yMinValue, yMax])
.range([boundsHeight, 0])
.nice();
yScale = scaleLinear().domain([yMinValue, yMax]).range(range).nice();
}

break;
Expand All @@ -125,15 +126,17 @@ export const useScales = (args: Args): ReturnValue => {
break;
}
case 'datetime': {
const range = [boundsHeight, boundsHeight * yAxis[0].maxPadding];

if (yTimestamps) {
const [yMin, yMax] = extent(yTimestamps) as [number, number];
yScale = scaleUtc().domain([yMin, yMax]).range([boundsHeight, 0]).nice();
yScale = scaleUtc().domain([yMin, yMax]).range(range).nice();
} else {
const domain = getDomainDataYBySeries(visibleSeries);

if (isNumericalArrayData(domain)) {
const [yMin, yMax] = extent(domain) as [number, number];
yScale = scaleUtc().domain([yMin, yMax]).range([boundsHeight, 0]).nice();
yScale = scaleUtc().domain([yMin, yMax]).range(range).nice();
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/types/widget-data/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ export type ChartKitWidgetAxis = {
* */
enabled?: boolean;
};

/** Padding of the max value relative to the length of the axis.
* A padding of 0.05 will make a 100px axis 5px longer.
*
* Defaults to 0.05 for Y axis and to 0.01 for X axis
* */
maxPadding?: number;
};

0 comments on commit 89602a3

Please sign in to comment.