In this series of Java Graphic examples, we are going to show you how to draw simple bar chart in Java. A bar chart (bar graph) is a way of comparing two or more values using rectangular bars of lengths. The bars can be horizontally or vertically oriented. We are providing you an example which shows the Bar chart in Java.
To draw a bar chart, the variables minvalue, maxvalue
of double type are define. The width of the bar is determined by clientWidth/value.length.
The FontMetrics class defines a font
metrics object which encapsulates information about the rendering of a
particular font on a particular screen. The Font class represents font.
The font 'Book Antiqua' is defined in the constructor of Font class
The method getAscent() determines the font ascent of the font described by the FontMetrics object. The font ascent is the distance from the font's baseline to the top of most alphanumeric characters. The method getDescent() determines the font descent. The method getHeight() gets the standard height of a line of text in the font.
Following code describes the scale of bar chart:
|double scale = (clientHeight - top - bottom) / (maxValue - minValue);|
Here is the code of SimpleBarChart.java
Output will be displayed as:
Liked it! Share this Tutorial