Function getLinearSyntax

  • Converts a given set of points into an array of strings in this format ["value percent%", ...] e.g. ["0", "0.25 13.8%", "0.6 45.6%", "0.1", "0.4 60%", ...].

    Parameters

    • points: [x: number, y: number][]

      The array of points to be converted. Each point is represented as a pair of numbers: [pos, val].

    • round: number

      The number of decimal places to which point values should be rounded.

    Returns string[]

    The formatted points as an array of strings, or an empty array if the input was null.

    The function first checks if the input points are null. If they are, it returns an empty array. If they are not null, the function does the following:

    • It creates a NumberFormat object for formatting the x and y values of the points to the specified number of decimal places. The x values are formatted with 2 fewer decimal places than the y values.

    • It iterates over the points to find those for which the x value does not need to be stated explicitly. The x value of a point does not need to be stated if it is 0 for the first point, 1 for the last point (provided the x value of the point before it is not greater than 1), or the average of the x values of the previous and next points.

    • It groups the points into subarrays such that all points in a subarray have the same y value.

    • It iterates over the groups and, for each group, formats the y value and the x values of the points that need to be stated explicitly. The formatted values are concatenated into a string, with the x values followed by the y value and separated by commas. If a group contains more than one point, the function also creates a string in which the y value is followed by the x values of the first and last points of the group, separated by a space. This string is used if its length is shorter than the length of the string with all the x values.

    • The function returns the array of formatted strings.

    This function makes use of the Intl.NumberFormat object for formatting the numbers. This not only rounds the numbers to the specified number of decimal places but also formats them according to the en-US locale. This means that the numbers will be formatted with a period as the decimal separator and without thousands separators.

    This function also optimizes the representation of the points by omitting the x values where they are not needed and by grouping points with the same y value. This can significantly reduce the length of the output strings when many points have the same y value or when the x values are close to their expected values based on their position in the array.

Generated using TypeDoc