Calculating the scale factor for “bar rectangles” of Chart app

风流意气都作罢 提交于 2019-12-13 06:14:48

问题


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:

Thanks


回答1:


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)


来源:https://stackoverflow.com/questions/5602644/calculating-the-scale-factor-for-bar-rectangles-of-chart-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!