Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(D3 plugin): add axis title option #222

Merged
merged 2 commits into from
Aug 15, 2023
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
27 changes: 17 additions & 10 deletions src/plugins/d3/__stories__/LinearCategories.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import random from 'lodash/random';
import {Meta, Story} from '@storybook/react';
import {withKnobs, select, radios} from '@storybook/addon-knobs';
import {withKnobs, select, radios, text} from '@storybook/addon-knobs';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../libs';
import {ChartKit} from '../../../components/ChartKit';
Expand Down Expand Up @@ -61,8 +61,16 @@ const shapeScatterChartData = (
labels: {
enabled: false,
},
title: {
text: text('X axis title', ''),
},
};

let yAxis: ChartKitWidgetAxis = {
title: {
text: text('Y axis title', ''),
},
};
let yAxis: ChartKitWidgetAxis[] | undefined;

if (categories && categoriesType === 'x') {
xAxis = {
Expand All @@ -73,20 +81,19 @@ const shapeScatterChartData = (
}

if (categories && categoriesType === 'y') {
yAxis = [
{
type: 'category',
categories,
},
];
yAxis = {
...yAxis,
type: 'category',
categories,
};
}

return {
series,
xAxis,
yAxis,
yAxis: [yAxis],
title: {
text: 'Chart title',
text: text('title', 'Chart title'),
},
};
};
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/d3/renderer/components/AxisX.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ export const AxisX = ({axis, width, height, scale}: Props) => {
svgElement.select('.tick').remove();
}

if (axis.title.text) {
const textY =
axis.title.height + parseInt(axis.labels.style.fontSize) + axis.labels.padding;

svgElement
.append('text')
.attr('class', b('title'))
.attr('text-anchor', 'middle')
.attr('x', width / 2)
.attr('y', textY)
.attr('font-size', axis.title.style.fontSize)
.text(axis.title.text);
}

removeOverlappingXTicks(svgElement);
}, [axis, width, height, scale]);

Expand Down
14 changes: 14 additions & 0 deletions src/plugins/d3/renderer/components/AxisY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ export const AxisY = ({axises, width, height, scale}: Props) => {
svgElement.select('.tick line').style('stroke', 'none');
}

if (axis.title.text) {
const textY = axis.title.height + axis.labels.padding;

svgElement
.append('text')
.attr('class', b('title'))
.attr('text-anchor', 'middle')
.attr('dy', -textY)
.attr('dx', -height / 2)
.attr('font-size', axis.title.style.fontSize)
.attr('transform', 'rotate(-90)')
.text(axis.title.text);
}

removeOverlappingYTicks(svgElement);
}, [axises, width, height, scale]);

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/d3/renderer/components/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const Chart = ({width, height, data}: Props) => {
margin: chart.margin,
legend,
title,
xAxis,
yAxis,
});
const {activeLegendItems, handleLegendItemClick} = useLegend({series});
const {chartSeries} = useSeries({activeLegendItems, series});
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/d3/renderer/components/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
& .tick line {
stroke: var(--g-color-line-generic);
}

&__title {
fill: var(--g-color-text-secondary);
}
}

.chartkit-d3-legend {
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/d3/renderer/hooks/useChartDimensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ChartMargin} from '../../../../../types/widget-data';

import type {ChartOptions, PreparedTitle} from '../useChartOptions/types';
import type {ChartOptions, PreparedAxis, PreparedTitle} from '../useChartOptions/types';

const LEGEND_LINE_HEIGHT = 15;

Expand All @@ -10,14 +10,23 @@ type Args = {
margin: ChartMargin;
legend: ChartOptions['legend'];
title?: PreparedTitle;
xAxis?: PreparedAxis;
yAxis?: PreparedAxis[];
};

export const useChartDimensions = (args: Args) => {
const {margin, legend, title, width, height} = args;
const {margin, legend, title, width, height, xAxis, yAxis} = args;
const legendHeight = legend.enabled ? LEGEND_LINE_HEIGHT : 0;
const titleHeight = title?.height || 0;
const boundsWidth = width - margin.right - margin.left;
const boundsHeight = height - margin.top - margin.bottom - legendHeight - titleHeight;
const xAxisTitleHeight = xAxis?.title.height || 0;
const yAxisTitleHeight =
yAxis?.reduce((acc, axis) => {
return acc + (axis.title.height || 0);
}, 0) || 0;

const boundsWidth = width - margin.right - margin.left - yAxisTitleHeight;
const boundsHeight =
height - margin.top - margin.bottom - legendHeight - titleHeight - xAxisTitleHeight;

return {boundsWidth, boundsHeight, legendHeight};
};
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/hooks/useChartOptions/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export const getPreparedChart = (args: {
const marginLeft =
get(chart, 'margin.left', AXIS_WIDTH) +
preparedY1Axis.labels.padding +
getAxisLabelMaxWidth({axis: preparedY1Axis, series});
getAxisLabelMaxWidth({axis: preparedY1Axis, series}) +
(preparedY1Axis.title.height || 0);

return {
margin: {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const DEFAULT_AXIS_LABEL_FONT_SIZE = '11px';
export const DEFAULT_AXIS_LABEL_PADDING = 10;
export const DEFAULT_AXIS_TITLE_FONT_SIZE = '14px';
5 changes: 5 additions & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export type PreparedLegend = Required<ChartKitWidgetLegend>;
export type PreparedAxis = Omit<ChartKitWidgetAxis, 'type' | 'labels'> & {
type: ChartKitWidgetAxisType;
labels: PreparedAxisLabels;
title: {
height: number;
text: string;
style: BaseTextStyle;
};
};

export type PreparedTitle = ChartKitWidgetData['title'] & {
Expand Down
20 changes: 19 additions & 1 deletion src/plugins/d3/renderer/hooks/useChartOptions/x-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ import get from 'lodash/get';
import type {ChartKitWidgetData} from '../../../../../types/widget-data';

import type {PreparedAxis} from './types';
import {DEFAULT_AXIS_LABEL_FONT_SIZE, DEFAULT_AXIS_LABEL_PADDING} from './constants';
import {
DEFAULT_AXIS_LABEL_FONT_SIZE,
DEFAULT_AXIS_LABEL_PADDING,
DEFAULT_AXIS_TITLE_FONT_SIZE,
} from './constants';
import {BaseTextStyle} from '../../../../../types/widget-data';
import {getHorisontalSvgTextDimensions} from './utils';

export const getPreparedXAxis = ({xAxis}: {xAxis: ChartKitWidgetData['xAxis']}): PreparedAxis => {
const titleText = get(xAxis, 'title.text', '');
const titleStyle: BaseTextStyle = {
fontSize: get(xAxis, 'title.style.fontSize', DEFAULT_AXIS_TITLE_FONT_SIZE),
};

const preparedXAxis: PreparedAxis = {
type: get(xAxis, 'type', 'linear'),
labels: {
Expand All @@ -17,6 +28,13 @@ export const getPreparedXAxis = ({xAxis}: {xAxis: ChartKitWidgetData['xAxis']}):
},
categories: get(xAxis, 'categories'),
timestamps: get(xAxis, 'timestamps'),
title: {
text: titleText,
style: titleStyle,
height: titleText
? getHorisontalSvgTextDimensions({text: titleText, style: titleStyle})
: 0,
},
};

return preparedXAxis;
Expand Down
19 changes: 18 additions & 1 deletion src/plugins/d3/renderer/hooks/useChartOptions/y-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import get from 'lodash/get';
import type {BaseTextStyle, ChartKitWidgetData} from '../../../../../types/widget-data';

import type {PreparedAxis} from './types';
import {DEFAULT_AXIS_LABEL_FONT_SIZE, DEFAULT_AXIS_LABEL_PADDING} from './constants';
import {
DEFAULT_AXIS_LABEL_FONT_SIZE,
DEFAULT_AXIS_LABEL_PADDING,
DEFAULT_AXIS_TITLE_FONT_SIZE,
} from './constants';
import {getHorisontalSvgTextDimensions} from './utils';

export const getPreparedYAxis = ({yAxis}: {yAxis: ChartKitWidgetData['yAxis']}): PreparedAxis[] => {
// FIXME: add support for n axises
const yAxis1 = yAxis?.[0];

const y1LabelsStyle: BaseTextStyle = {
fontSize: get(yAxis1, 'labels.style.fontSize', DEFAULT_AXIS_LABEL_FONT_SIZE),
};
const y1TitleText = get(yAxis1, 'title.text', '');
const y1TitleStyle: BaseTextStyle = {
fontSize: get(yAxis1, 'title.style.fontSize', DEFAULT_AXIS_TITLE_FONT_SIZE),
};
const preparedY1Axis: PreparedAxis = {
type: get(yAxis1, 'type', 'linear'),
labels: {
Expand All @@ -22,6 +32,13 @@ export const getPreparedYAxis = ({yAxis}: {yAxis: ChartKitWidgetData['yAxis']}):
},
categories: get(yAxis1, 'categories'),
timestamps: get(yAxis1, 'timestamps'),
title: {
text: y1TitleText,
style: y1TitleStyle,
height: y1TitleText
? getHorisontalSvgTextDimensions({text: y1TitleText, style: y1TitleStyle})
: 0,
},
};

return [preparedY1Axis];
Expand Down
3 changes: 3 additions & 0 deletions src/types/widget-data/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ export type ChartKitWidgetAxis = {
type?: ChartKitWidgetAxisType;
/** The axis labels show the number or category for each tick */
labels?: ChartKitWidgetAxisLabels;
title?: {
text?: string;
};
};