diff --git a/src/Components/Charts/BentoBarChart.tsx b/src/Components/Charts/BentoBarChart.tsx index 841d8e1..f3b9086 100644 --- a/src/Components/Charts/BentoBarChart.tsx +++ b/src/Components/Charts/BentoBarChart.tsx @@ -1,13 +1,11 @@ import React, { useCallback } from 'react'; -import { BarChart, Bar, Cell, XAxis, YAxis, Tooltip, Label, BarProps } from 'recharts'; +import { BarChart, Bar, Cell, XAxis, YAxis, Tooltip, Label, BarProps, ResponsiveContainer } from 'recharts'; import { TOOL_TIP_STYLE, COUNT_STYLE, LABEL_STYLE, - CHART_WRAPPER_STYLE, MAX_TICK_LABEL_CHARS, TITLE_STYLE, - ASPECT_RATIO, TICKS_SHOW_ALL_LABELS_BELOW, UNITS_LABEL_OFFSET, TICK_MARGIN, @@ -17,6 +15,7 @@ import type { BarChartProps, CategoricalChartDataItem, TooltipPayload } from '.. import { useChartTheme, useChartTranslation } from '../../ChartConfigProvider'; import NoData from '../NoData'; import { useTransformedChartData } from '../../util/chartUtils'; +import ChartWrapper from './ChartWrapper'; const tickFormatter = (tickLabel: string) => { if (tickLabel.length <= MAX_TICK_LABEL_CHARS) { @@ -25,7 +24,9 @@ const tickFormatter = (tickLabel: string) => { return `${tickLabel.substring(0, MAX_TICK_LABEL_CHARS)}...`; }; -const BentoBarChart = ({ height, units, title, onClick, colorTheme = 'default', ...params }: BarChartProps) => { +const BAR_CHART_MARGINS = { top: 10, bottom: 100, right: 20 }; + +const BentoBarChart = ({ height, width, units, title, onClick, colorTheme = 'default', ...params }: BarChartProps) => { const t = useChartTranslation(); const { fill: chartFill, missing } = useChartTheme().bar[colorTheme]; @@ -53,32 +54,34 @@ const BentoBarChart = ({ height, units, title, onClick, colorTheme = 'default', // on formatting a non-string. This hack manually overrides the ticks for the axis and blanks it out. // - David L, 2023-01-03 return ( -
+
{title}
- - - - - - } /> - - {data.map((entry) => ( - - ))} - - -
+ + + + + + + } /> + + {data.map((entry) => ( + + ))} + + + + ); }; diff --git a/src/Components/Charts/BentoPie.tsx b/src/Components/Charts/BentoPie.tsx index a7b01d8..2441d97 100644 --- a/src/Components/Charts/BentoPie.tsx +++ b/src/Components/Charts/BentoPie.tsx @@ -1,5 +1,15 @@ import React, { useCallback, useMemo, useState } from 'react'; -import { PieChart, Pie, Cell, Curve, Tooltip, Sector, PieProps, PieLabelRenderProps } from 'recharts'; +import { + PieChart, + Pie, + Cell, + Curve, + Tooltip, + Sector, + PieProps, + PieLabelRenderProps, + ResponsiveContainer, +} from 'recharts'; import type CSS from 'csstype'; import { @@ -7,9 +17,7 @@ import { LABEL_STYLE, COUNT_STYLE, CHART_MISSING_FILL, - CHART_WRAPPER_STYLE, RADIAN, - CHART_ASPECT_RATIO, LABEL_THRESHOLD, COUNT_TEXT_STYLE, TEXT_STYLE, @@ -23,6 +31,7 @@ import { } from '../../ChartConfigProvider'; import { polarToCartesian, useTransformedChartData } from '../../util/chartUtils'; import NoData from '../NoData'; +import ChartWrapper from './ChartWrapper'; const labelShortName = (name: string, maxChars: number) => { if (name.length <= maxChars) { @@ -32,11 +41,9 @@ const labelShortName = (name: string, maxChars: number) => { return `${name.substring(0, maxChars - 3)}\u2026`; }; -const OUTER_RADIUS_REDUCTION_FACTOR = 3.75; // originally from 300 / 80 -const INNER_RADIUS_REDUCTION_FACTOR = 8.5; // roughly originally from 300 / 35 - const BentoPie = ({ height, + width, onClick, sort = true, colorTheme = 'default', @@ -97,37 +104,39 @@ const BentoPie = ({ }, []); return ( -
- - - {data.map((entry, index) => { - const fill = entry.name.toLowerCase() === 'missing' ? CHART_MISSING_FILL : theme[index % theme.length]; - return ; - })} - - } - isAnimationActive={false} - allowEscapeViewBox={{ x: true, y: true }} - /> - -
+ + + + + {data.map((entry, index) => { + const fill = entry.name.toLowerCase() === 'missing' ? CHART_MISSING_FILL : theme[index % theme.length]; + return ; + })} + + } + isAnimationActive={false} + allowEscapeViewBox={{ x: true, y: true }} + /> + + + ); }; diff --git a/src/Components/Charts/ChartWrapper.tsx b/src/Components/Charts/ChartWrapper.tsx new file mode 100644 index 0000000..abe3f26 --- /dev/null +++ b/src/Components/Charts/ChartWrapper.tsx @@ -0,0 +1,15 @@ +import React, { ForwardedRef, forwardRef, ReactNode } from 'react'; +import { CHART_WRAPPER_STYLE } from '../../constants/chartConstants'; + +interface ChartWrapperProps { + children: ReactNode; +} + +const ChartWrapper = forwardRef(({children}: ChartWrapperProps, ref: ForwardedRef) => ( +
+ {children} +
+)); +ChartWrapper.displayName = "ChartWrapper"; + +export default ChartWrapper; diff --git a/src/constants/chartConstants.ts b/src/constants/chartConstants.ts index 7261e70..ac66380 100644 --- a/src/constants/chartConstants.ts +++ b/src/constants/chartConstants.ts @@ -83,6 +83,8 @@ export const CHART_WRAPPER_STYLE: CSS.Properties = { display: 'flex', flexDirection: 'column', alignItems: 'center', + overflowX: 'auto', + overflowY: 'hidden', }; // bar chart @@ -104,14 +106,12 @@ export const COUNT_TEXT_STYLE: CSS.Properties = { // ################### CHART CONSTANTS ################### // bar chart -export const ASPECT_RATIO = 1.2; export const MAX_TICK_LABEL_CHARS = 15; export const UNITS_LABEL_OFFSET = -75; export const TICKS_SHOW_ALL_LABELS_BELOW = 11; // Below this # of X-axis ticks, force-show all labels export const TICK_MARGIN = 5; // vertical spacing between tick line and tick label // pie chart -export const CHART_ASPECT_RATIO = 1.4; export const LABEL_THRESHOLD = 0.05; // ################### UTIL CONSTANTS ################### diff --git a/src/types/chartTypes.ts b/src/types/chartTypes.ts index b0324d5..5e1ccef 100644 --- a/src/types/chartTypes.ts +++ b/src/types/chartTypes.ts @@ -62,6 +62,9 @@ export interface CategoricalChartDataWithTransforms { // ################### COMPONENT PROPS ##################### export interface BaseChartComponentProps { height: number; + // Width is useful to have, to force re-render / force a specific width, but it is optional. + // Otherwise, it will be set to 100%. + width?: number | string; } export interface BaseCategoricalChartProps extends BaseChartComponentProps, CategoricalChartDataWithTransforms {} diff --git a/test/js/TestBarChart.tsx b/test/js/TestBarChart.tsx index e07ba3d..9689b98 100644 --- a/test/js/TestBarChart.tsx +++ b/test/js/TestBarChart.tsx @@ -1,16 +1,27 @@ import React from 'react'; +import { Typography } from 'antd'; import { BarChart } from '../../src/index'; const TestBarChart = () => ( - { - console.log(f); - alert(JSON.stringify(f, null, 2)); - }} - height={600} - /> + <> + Responsive bar chart: + + Fixed-width bar chart: + { + console.log(f); + alert(JSON.stringify(f, null, 2)); + }} + height={600} + width={960} + /> + ); export default TestBarChart;