Skip to content

Commit

Permalink
feat(D3 plugin): add axis grid settings (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom authored Aug 17, 2023
1 parent 6c76eeb commit 0a4c3c0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/plugins/d3/__stories__/scatter/Timestamp.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import random from 'lodash/random';
import {Meta, Story} from '@storybook/react';
import {boolean} from '@storybook/addon-knobs';
import {dateTime} from '@gravity-ui/date-utils';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
Expand Down Expand Up @@ -72,7 +73,17 @@ const shapeData = (data: Record<string, any>[]): ChartKitWidgetData<string> => {
xAxis: {
type: 'datetime',
timestamps: data.map((d) => d.x),
grid: {
enabled: boolean('xAxis.grid.enabled', true),
},
},
yAxis: [
{
grid: {
enabled: boolean('yAxis.grid.enabled', true),
},
},
],
tooltip: {
renderer: ({hovered}) => {
const d = hovered.data as ScatterSeriesData<string>;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/components/AxisX.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export const AxisX = ({axis, width, height, scale}: Props) => {

const svgElement = select(ref.current);
svgElement.selectAll('*').remove();
const tickSize = axis.grid.enabled ? height * -1 : 0;
const xAxisGenerator = axisBottom(scale as AxisScale<AxisDomain>)
.tickSize(height * -1)
.tickSize(tickSize)
.tickPadding(axis.labels.padding)
.tickFormat((value) => {
return formatAxisTickLabel({
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/components/AxisY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export const AxisY = ({axises, width, height, scale}: Props) => {
const axis = axises[0];
const svgElement = select(ref.current);
svgElement.selectAll('*').remove();
const tickSize = axis.grid.enabled ? width * -1 : 0;
const yAxisGenerator = axisLeft(scale as AxisScale<AxisDomain>)
.tickSize(width * -1)
.tickSize(tickSize)
.tickPadding(axis.labels.padding)
.tickFormat((value) => {
return formatAxisTickLabel({
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export type PreparedAxis = Omit<ChartKitWidgetAxis, 'type' | 'labels'> & {
style: BaseTextStyle;
};
min?: number;
grid: {
enabled: boolean;
};
};

export type PreparedTitle = ChartKitWidgetData['title'] & {
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/d3/renderer/hooks/useChartOptions/x-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const getPreparedXAxis = ({xAxis}: {xAxis: ChartKitWidgetData['xAxis']}):
? getHorisontalSvgTextDimensions({text: titleText, style: titleStyle})
: 0,
},
min: get(xAxis, 'min'),
grid: {
enabled: get(xAxis, 'grid.enabled', true),
},
};

return preparedXAxis;
Expand Down
3 changes: 3 additions & 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,9 @@ export const getPreparedYAxis = ({yAxis}: {yAxis: ChartKitWidgetData['yAxis']}):
: 0,
},
min: get(yAxis1, 'min'),
grid: {
enabled: get(yAxis1, 'grid.enabled', true),
},
};

return [preparedY1Axis];
Expand Down
8 changes: 8 additions & 0 deletions src/types/widget-data/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ export type ChartKitWidgetAxis = {

/** The minimum value of the axis. If undefined the min value is automatically calculate */
min?: number;

grid?: {
/** Enable or disable the grid lines.
*
* Defaults to true.
* */
enabled?: boolean;
};
};

0 comments on commit 0a4c3c0

Please sign in to comment.