How to get the bars in a bar chart start from the x-axis in java? [closed]

淺唱寂寞╮ 提交于 2019-12-02 14:54:40
trashgod

How do you normally position a bar over the x-axis?

A bar's fillRect() parameters should be left, top, width and height. Focusing on just the vertical size and position, as shown here, you can scale the bar height to the plot area height using a proportion:

barHeightInPixels : plotHeightInPixels :: value : maxDataValue

Solving for barHeightInPixels,

panelHeightInPixels = panel.getHeight();
plotHeightInPixels  = panelHeightInPixels - axisOffset;
barHeightInPixels   = value * plotHeightInPixels / maxDataValue;

Now use the scaled height:

g2.fillRect(
    barCenter - (barWidth / 2),                           //left
    panelHeightInPixels - barHeightInPixels - axisOffset, //top
    barWidth,                                             //width
    barHeightInPixels);                                   //height

To see how additional features are implemented, study the code provided by a chart library, as suggested in this answer to your previous question on this topic.

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