Skip to content

Commit 0a4c3c0

Browse files
authored
feat(D3 plugin): add axis grid settings (#228)
1 parent 6c76eeb commit 0a4c3c0

7 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/plugins/d3/__stories__/scatter/Timestamp.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import random from 'lodash/random';
33
import {Meta, Story} from '@storybook/react';
4+
import {boolean} from '@storybook/addon-knobs';
45
import {dateTime} from '@gravity-ui/date-utils';
56
import {Button} from '@gravity-ui/uikit';
67
import {settings} from '../../../../libs';
@@ -72,7 +73,17 @@ const shapeData = (data: Record<string, any>[]): ChartKitWidgetData<string> => {
7273
xAxis: {
7374
type: 'datetime',
7475
timestamps: data.map((d) => d.x),
76+
grid: {
77+
enabled: boolean('xAxis.grid.enabled', true),
78+
},
7579
},
80+
yAxis: [
81+
{
82+
grid: {
83+
enabled: boolean('yAxis.grid.enabled', true),
84+
},
85+
},
86+
],
7687
tooltip: {
7788
renderer: ({hovered}) => {
7889
const d = hovered.data as ScatterSeriesData<string>;

src/plugins/d3/renderer/components/AxisX.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ export const AxisX = ({axis, width, height, scale}: Props) => {
4747

4848
const svgElement = select(ref.current);
4949
svgElement.selectAll('*').remove();
50+
const tickSize = axis.grid.enabled ? height * -1 : 0;
5051
const xAxisGenerator = axisBottom(scale as AxisScale<AxisDomain>)
51-
.tickSize(height * -1)
52+
.tickSize(tickSize)
5253
.tickPadding(axis.labels.padding)
5354
.tickFormat((value) => {
5455
return formatAxisTickLabel({

src/plugins/d3/renderer/components/AxisY.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export const AxisY = ({axises, width, height, scale}: Props) => {
4848
const axis = axises[0];
4949
const svgElement = select(ref.current);
5050
svgElement.selectAll('*').remove();
51+
const tickSize = axis.grid.enabled ? width * -1 : 0;
5152
const yAxisGenerator = axisLeft(scale as AxisScale<AxisDomain>)
52-
.tickSize(width * -1)
53+
.tickSize(tickSize)
5354
.tickPadding(axis.labels.padding)
5455
.tickFormat((value) => {
5556
return formatAxisTickLabel({

src/plugins/d3/renderer/hooks/useChartOptions/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export type PreparedAxis = Omit<ChartKitWidgetAxis, 'type' | 'labels'> & {
2828
style: BaseTextStyle;
2929
};
3030
min?: number;
31+
grid: {
32+
enabled: boolean;
33+
};
3134
};
3235

3336
export type PreparedTitle = ChartKitWidgetData['title'] & {

src/plugins/d3/renderer/hooks/useChartOptions/x-axis.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export const getPreparedXAxis = ({xAxis}: {xAxis: ChartKitWidgetData['xAxis']}):
3535
? getHorisontalSvgTextDimensions({text: titleText, style: titleStyle})
3636
: 0,
3737
},
38+
min: get(xAxis, 'min'),
39+
grid: {
40+
enabled: get(xAxis, 'grid.enabled', true),
41+
},
3842
};
3943

4044
return preparedXAxis;

src/plugins/d3/renderer/hooks/useChartOptions/y-axis.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export const getPreparedYAxis = ({yAxis}: {yAxis: ChartKitWidgetData['yAxis']}):
4040
: 0,
4141
},
4242
min: get(yAxis1, 'min'),
43+
grid: {
44+
enabled: get(yAxis1, 'grid.enabled', true),
45+
},
4346
};
4447

4548
return [preparedY1Axis];

src/types/widget-data/axis.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ export type ChartKitWidgetAxis = {
2525

2626
/** The minimum value of the axis. If undefined the min value is automatically calculate */
2727
min?: number;
28+
29+
grid?: {
30+
/** Enable or disable the grid lines.
31+
*
32+
* Defaults to true.
33+
* */
34+
enabled?: boolean;
35+
};
2836
};

0 commit comments

Comments
 (0)