Download the PHP package mistralys/column-widths-calculator without Composer
On this page you can find all versions of the php package mistralys/column-widths-calculator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mistralys/column-widths-calculator
More information about mistralys/column-widths-calculator
Files in mistralys/column-widths-calculator
Package column-widths-calculator
Short Description PHP column widths calculator utility.
License MIT
Informations about the package column-widths-calculator
Column widths calculator
Small utility that can be used to convert arbitrary numeric values to column widths. Given a list of column names and values, it will intelligently convert the values to percentages, and fill empty columns with meaningful values.
Features:
- Fills empty values with meaningful values
- Converts any values to percentages, using proportional calculations
- Ensures that the total always matches 100% exactly
Installation
Using composer, simply require the package.
Via command line:
Via composer.json:
Usage
Use the factory method to instantiate the calculator:
This will return an array with the same keys, and the missing column value filled out:
Switching between integers and floats
By default, the calculator will produce integer values, even if all internal calculations are float based for precision. This can be turned off to retrieve float values:
In this case, no rounding is done at all - you will have to manually handle any rounding you wish to apply.
Handling minimum widths
When leaving columns empty to be automatically filled, there may not be enough width left for the columns to fill. Consider this configuration of columns:
By default, the calculator will guarantee that every column has a minimum width of 1
. As a result, the list would be adjusted like this, to allow the third column to have a width:
The surplus is subtracted from all non-empty columns, proportionally to their size (without going below the minimum width).
Setting the minimum size
The minimum size can be set like this:
This will ensure that all columns have a minimum size of 20%.
Example:
Would give the following result:
NOTE: The maximum possible value for the minimum size depends on the amount of columns. Trying to set the minimum size to 40% for 3 columns for example, will throw an exception (40 x 3 is bigger than 100).
Arbitrary numbering
The calculator can work with any number system, and converts the values proportionally.
Consider the following values:
This will be converted to the following values:
Missing values
As long as there are more than one column with values, missing values will be filled with a value based on an average of the existing values, to get realistic results.
For example, the values:
Will be converted to this:
Changing the target value
By default, the Calculator assumes you want to work with percentages, and has the target value locked to 100. However, it is possible to adjust this value:
In the example above, the column widths will be calculated to reach a total of 1000, instead of the default 100.
Converting to absolute pixel values
For convenience, the getPixelValues()
method can convert the column percentages to absolute pixel values, given the maximum target width.
The following example converts the column widths to reach a width of 600px:
The resulting widths will be: