Search code examples
javagraphicsjava-2d

Calculating the scale factor for "bar rectangles" of Chart app


I have an array of integer values

String[] values={3100,7500,8000,4200,88000,71000,32000};

that need to be scaled into a known height of my JComponent , the question is how to scale these values into e.g. h=600px?

Here is a pic just for more clarification of what i want to achieve: enter image description here

Thanks


Solution

  • bar_height = chart_height*(value/max_value)

    To determine bar_height, you scale (multiply) chart_height by (value/max_value), where:

    • bar_height is the height of the a bar in pixels.
    • value is the value to be charted.
    • max_value is the maximum value on the y-axis.
    • chart_height is the height of the chart in pixels (600 in your example).

    For example:

    88000/88000 = 1.0, or 100% of the chart height        (600px)
        0/88000 = 0, or 0% of the chart's height          (  0px)
     3100/88000 = ~0.035, or ~3.53% of the chart's height (~21px)