recharts icon indicating copy to clipboard operation
recharts copied to clipboard

Fix: bar width when there is only one data point

Open souzolliveira opened this issue 2 years ago • 12 comments

Description

  • Adding condition to return a bar size when there is only one data point;
  • Creating test for it.

Related Issue

  • Bug #3640

Motivation and Context

  • I choose this bug to solve for a graduation issue and because it seems easy.

How Has This Been Tested?

  • I created a test to count one rectangle for one data point.

Screenshots:

image

Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x] My code follows the code style of this project.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [x] I have added tests to cover my changes.
  • [ ] I have added a storybook story or extended an existing story to show my changes
  • [x] All new and existing tests passed.

souzolliveira avatar Oct 13 '23 19:10 souzolliveira

@souzolliveira thanks, going to wait for some other reviews

ckifer avatar Oct 14 '23 00:10 ckifer

also build is failing

ckifer avatar Oct 14 '23 00:10 ckifer

@ckifer I could increase the x-axis width to centralize the unique bar in the axis if you tell me the path to do this. Did my changes break the tests that failed in the build?

souzolliveira avatar Oct 14 '23 02:10 souzolliveira

@ckifer I could increase the x-axis width to centralize the unique bar in the axis if you tell me the path to do this. Did my changes break the tests that failed in the build?

Yes, this specifically

 <BarChart /> › Render a bar › renders a bar if size is limited

nikolasrieble avatar Oct 14 '23 06:10 nikolasrieble

Agreed in principle, but the flexibility does add a lot of pressure on the side of the maintainers.

We support this but is it expected to look a certain way or be functional? I guess it might be useful depending on the case and customizations applied. I wish the original creators had limited flexibility a bit (even though it's one of the things people like about the lib now) 😅

Doesn't matter for now though, just trying to think aloud a bit. Thanks for the input!

ckifer avatar Oct 15 '23 02:10 ckifer

@souzolliveira looks like test is still failing

ckifer avatar Oct 15 '23 02:10 ckifer

To write in favor of having bar charts with numerical X and Y axes, here are some ones I am using for a project:

https://bferentz.vercel.app/

In a lot of charts on that page, I am comparing a numeric value (potential points scored in a game) against another numeric value (the % chance of those points being score).

TheCleric avatar Oct 16 '23 22:10 TheCleric

@TheCleric nice, thanks for the data point (and cool site!)

At small scale things look relatively non-functional but these types of examples help challenge (at least my) perception of what people are able to do with the lib. Kudos for creativity and we'll make sure number support doesn't go away

ckifer avatar Oct 16 '23 22:10 ckifer

@souzolliveira any chance you could merge the latest and fix this test?

ckifer avatar Oct 22 '23 05:10 ckifer

@souzolliveira any chance you could merge the latest and fix this test?

Yes, sorry for the delay, I will update my branch and correct the tests.

souzolliveira avatar Oct 23 '23 12:10 souzolliveira

No worries at all

ckifer avatar Oct 23 '23 15:10 ckifer

Trying to land this, I merged master via UI. Testing this locally, tests are failing 🤔 Strangely the CI was not triggered by actions in the UI.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 4 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/chart/BarChart.spec.tsx > <BarChart /> > Renders 1 bar in simple BarChart
AssertionError: expected  to have a length of 1 but got +0

- Expected
+ Received

- 1
+ 0

 ❯ test/chart/BarChart.spec.tsx:40:63
     38|     );
     39| 
     40|     expect(container.querySelectorAll('.recharts-rectangle')).toHaveLength(1);
       |                                                               ^
     41|   });
     42| 

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/4]⎯

 FAIL  test/chart/BarChart.spec.tsx > <BarChart /> > Render a bar > renders a bar if size is limited
Error: expect(element).toHaveAttribute("x", "79") // element.getAttribute("x") === "79"

Expected the element to have attribute:
  x="79"
Received:
  x="83"
 ❯ test/chart/BarChart.spec.tsx:332:30
    330| 
    331|       const rectangleProps = rectangles[0];
    332|       expect(rectangleProps).toHaveAttribute('x', '79');
       |                              ^
    333|       expect(rectangleProps).toHaveAttribute('y', '5');
    334|       expect(rectangleProps).toHaveAttribute('width', '40');

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/4]⎯

 FAIL  test/chart/BarChart.spec.tsx > <BarChart /> > Renders a (Bar) chart with less width than left, right margin and less height than top, bottom margin
TypeError: Cannot read properties of null (reading 'children')
 ❯ test/chart/BarChart.spec.tsx:364:21
    362| 
    363|     const clipPath = container.querySelector('#recharts53-clip');
    364|     expect(clipPath.children[0]).not.toBeNull();
       |                     ^
    365| 
    366|     // expect clipPath rect to have a width and height of 0

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/4]⎯

 FAIL  test/util/ChartUtils.spec.tsx > getBandSizeOfAxis > DataUtils.getBandSizeOfAxis({ type: "number", scale }, ticks) should return 0 
AssertionError: expected +0 to be 2 // Object.is equality

- Expected
+ Received

- 2
+ 0

 ❯ test/util/ChartUtils.spec.tsx:189:44
    187|     const axis: BaseAxisProps = { type: 'number' };
    188|     const ticks = [{ coordinate: 13 }, { coordinate: 15 }, { coordinate: 20 }];
    189|     expect(getBandSizeOfAxis(axis, ticks)).toBe(2);
       |                                            ^
    190|   });
    191| });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯

 Test Files  2 failed | 83 passed (85)
      Tests  4 failed | 1431 passed (1435)
   Start at  14:42:57
   Duration  14.68s (transform 1.62s, setup 6.52s, collect 42.94s, tests 10.26s, environment 24.40s, prepare 6.44s)

nikolasrieble avatar Nov 19 '23 13:11 nikolasrieble

Going to close this as stale for now, potentially @graup will take it over

ckifer avatar Mar 20 '24 18:03 ckifer