Skip to content

Commit

Permalink
Layout improvements in BarWidgetUI (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Clebal authored Jun 27, 2022
1 parent a153d4f commit 665172d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released

- Layout improvements in BarWidgetUI [#438](https://github.com/CartoDB/carto-react/pull/438)
- Fix FormulaWidget column check [#437](https://github.com/CartoDB/carto-react/pull/437)

## 1.3
Expand Down
38 changes: 29 additions & 9 deletions packages/react-ui/src/widgets/BarWidgetUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function BarWidgetUI(props) {
type: 'bar',
name: series[componentIdx] || '',
animation,
barCategoryGap: '10px',
barMaxWidth: 100,
data: row.map((value, dataIdx) => {
const isSelected = selectedBars.some(
([sDataIdx, sComponentIdx = 0]) =>
Expand Down Expand Up @@ -203,10 +203,21 @@ function BarWidgetUI(props) {
const options = useMemo(
() => ({
grid: {
left: xAxisData.length >= 4 ? calculateMargin(xAxisFormatter(xAxisData[0])) : 0,
left:
xAxisData.length >= 4
? calculateMargin(
processFormatterRes(xAxisFormatter(xAxisData[0])),
xAxisData.length
)
: 0,
top: theme.spacing(2),
right:
xAxisData.length >= 4 ? calculateMargin(xAxisFormatter(xAxisData[xAxisData.length - 1])) : 0,
xAxisData.length >= 4
? calculateMargin(
processFormatterRes(xAxisFormatter(xAxisData[xAxisData.length - 1])),
xAxisData.length
)
: 0,
bottom: theme.spacing(0),
containLabel: true
},
Expand All @@ -221,7 +232,16 @@ function BarWidgetUI(props) {
yAxis: yAxisOptions,
series: seriesOptions
}),
[xAxisData, theme, colors, tooltipOptions, xAxisOptions, yAxisOptions, seriesOptions, xAxisFormatter]
[
xAxisData,
theme,
colors,
tooltipOptions,
xAxisOptions,
yAxisOptions,
seriesOptions,
xAxisFormatter
]
);

const clearBars = () => {
Expand Down Expand Up @@ -353,11 +373,11 @@ BarWidgetUI.propTypes = {
export default BarWidgetUI;

// Aux
function calculateMargin(label = '') {
// For less than 8 characters, the margin isn't necessary
if (label.length <= 8) return 0;
// Calculated manually. For each 6 characters, the margin should be 6.5px
return (label.length * 6.5) / 6;
function calculateMargin(label = '', amountCategories = 0) {
// For less than 8 characters and less than 15 categories, the margin isn't necessary
if (amountCategories <= 15 && label.length <= 8) return 0;
// Calculated manually. For each 6 characters, the margin should be 8.5px
return (label.length * 8.5) / 6;
}

function defaultTooltipFormatter(params, xAxisFormatter, yAxisFormatter) {
Expand Down

0 comments on commit 665172d

Please sign in to comment.