I've got a number of controls (charts in this case) that's determined at runtime. I'd like to put these in a grid with the proper number of rows and columns. For example,
Sorry, I don't really have any code to show my attempts. I started playing with determining if the square root is an integer, if the number is evenly divisible by 2 etc. and realized I'm not sure how to attack this problem. But this is what I'm thinking:
I'd appreciate it if someone could point me in the right direction - or suggest a different algorithm if I'm way off base.
Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.
If for example your matrix is A, you can use : size(A,1) for number of rows. size(A,2) for number of columns. Also there are some other ways like : length ( A(:,1) ) for number of rows. Sign in to answer this question.
Count the number of rows and columns of Dataframe using len() function. The len() function returns the length rows of the Dataframe, we can filter a number of columns using the df. columns to get the count of columns.
Idea: If square root is not integer, floor it, then divide whole number by this, ceil it.
int columns = (int)sqrt(number);
int lines = (int)ceil(number / (float)columns);
Example: 21 => columns = 4, lines = 6.
UPDATE: bonus, it also works when sqrt(number) is integer. No rounding occurs anywhere, and values are correct.
The "usual" way of handling this problem is by saying that there will always be N columns (less often, always N rows). The problem then becomes a matter of taking the number of items, dividing by N, and that's the number of rows you have (plus one if there's a remainder).
Changing the size of the grid makes for a confusing user interface. Users won't understand why the size of the grid keeps changing. They won't really wonder about it, but they'll be confused by the seemingly random changes.
If you still want to do what you're saying, I think you'll need to define your problem a little better. Is there a maximum number of items that can fit on the grid? Is there a maximum number of columns that you'll allow? For example, if you allow 50 items, should they be in 25 rows of 2 items? 5 rows of 10 items? 10 rows of 5 items?
At some point you'll have to either scroll horizontally or say, "maximum number of columns is X". And if you're going to impose that maximum number of columns, then you're better off just saying "There will always be X columns."
Unless there's a compelling reason to do the variable-dimension grid that you ask for, you're way better off just fixing the number of columns. It makes for trivially simple code rather than some complicated hack, and it presents a much more consistent interface to your users.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With