Update ! |
||
This bar chart compoment has now been moved into the Iceberg Charts project . Please use Iceberg Charts for a more complete charting experience. |
I’ve created a little Java component which creates bar graphs. It only displays bars along the Y axis, however the component is very versatile and configurable. You can change its size, fonts, ticks on the Y-axis, and bar width.
Here are some examples of using it :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ArrayList values = new ArrayList(); values.add(new Bar(90, Color.RED, "Apple")); values.add(new Bar(14, Color.BLUE, "Banana")); values.add(new Bar(67, Color.GREEN, "Plum")); values.add(new Bar(30, Color.ORANGE, "Radish")); values.add(new Bar(10, Color.YELLOW, "Corn")); int primaryIncrements = 20; int secondaryIncrements = 10; int tertiaryIncrements = 5; Axis yAxis = new Axis(100, 0, primaryIncrements, secondaryIncrements, tertiaryIncrements, "Number of Fruits"); BarChart barChart = new BarChart(values, yAxis); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
ArrayList values = new ArrayList(); values.add(new Bar(10, Color.BLACK, "1")); values.add(new Bar(14, Color.BLUE, "2")); values.add(new Bar(300, Color.BLACK, "3")); values.add(new Bar(12, Color.BLUE, "4")); values.add(new Bar(18, Color.BLACK, "5")); values.add(new Bar(19, Color.BLUE, "6")); values.add(new Bar(23, Color.BLACK, "7")); values.add(new Bar(33, Color.BLUE, "8")); values.add(new Bar(11, Color.BLACK, "9")); values.add(new Bar(33, Color.BLUE, "10")); values.add(new Bar(11, Color.BLACK, "11")); values.add(new Bar(33, Color.BLUE, "12")); values.add(new Bar(11, Color.BLACK, "13")); values.add(new Bar(88, Color.BLUE, "14")); values.add(new Bar(5, Color.BLACK, "15")); Axis yAxis2 = new Axis(500, 0, 50, 0, 0, "Numbers"); BarChart barChart2 = new BarChart(values, yAxis2); barChart2.topOffset = 160; barChart2.xFont = new Font("Blackadder ITC", Font.PLAIN, 20); barChart2.yFont = new Font("Blackadder ITC", Font.PLAIN, 20); barChart2.xAxis = "Playing with fonts"; barChart2.xCatFont = new Font("Blackadder ITC", Font.PLAIN, 14); barChart2.yCatFont = new Font("Blackadder ITC", Font.PLAIN, 16); barChart2.titleFont = new Font("Ravie", Font.PLAIN, 70); barChart2.title = "Really Big Text"; barChart2.width = 900; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
ArrayList values = new ArrayList(); values.add(new Bar(40, Color.PINK, "Mini")); values.add(new Bar(30, Color.CYAN, "Smart Car")); values.add(new Bar(80, Color.ORANGE, "Beetle")); Axis yAxis3 = new Axis(100, 0, 50, 20, 5, "Cuteness Factor"); BarChart barChart3 = new BarChart(values, yAxis3); barChart3.width = 240; barChart3.height = 200; barChart3.leftOffset = 60; barChart3.rightOffset = 60; barChart3.topOffset = 60; barChart3.bottomOffset = 60; barChart3.barWidth = 30; barChart3.xLabelOffset = 20; barChart3.title = "Small Cars"; barChart3.xAxis = "Small Thing"; barChart3.xCatFont = new Font("Ariel", Font.PLAIN, 6); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
ArrayList values = new ArrayList(); for (int i = 0; i < 365; i++) { double d = Math.random(); values.add(new Bar((int)(100 * d), Color.GRAY, "")); } Axis yAxis4 = new Axis(100, 0, 50, 10, 1, "Percent Sunlight"); BarChart barChart4 = new BarChart(values, yAxis4); barChart4.width = 1000; barChart4.xAxis = "Day of Year"; barChart4.titleFont = new Font("Ariel", Font.PLAIN, 24); barChart4.title = "Annual Sunlight Variability"; barChart4.barWidth = 1; |
The code is below. It’s basically three classes: BarChart, Bar and Axis.
A few things to note :
- It is highly customizable. All the package level fields in BarChart can modified.
- The Y-Axis can have up to 3 categories of ticks. In the Axis constructor, you just need to set the second and third to zero if you don’t want to used them (See my examples).
- It is geometrically relative, however you can have problems with it. The key things to know are width and height, and leftOffset, rightOffset, topOffset and bottomOffset. The offsets need to be big enough to accomodate a title with a very large font (again see my examples).
Please let me know if this component has been useful to you. I spent a lot of time creating it, so a little feedback would be very important to me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
/** * @copyright 2014 * @author Oliver Watkins (www.blue-walrus.com) * * All Rights Reserved */ package com.bluewalrus; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.util.ArrayList; import javax.swing.JPanel; /** * @copyright * @author Oliver Watkins * * (www.blue-walrus.com) All Rights Reserved */ public class BarChart extends JPanel { //offsets (padding of actual chart to its border) int leftOffset = 140; int topOffset = 120; int bottomOffset = 100; int rightOffset = 15; //height of X labels (must be significantly smaller than bottomOffset) int xLabelOffset = 40; //width of Y labels (must be significantly smaller than leftOffset) int yLabelOffset = 40; //tick widths int majorTickWidth = 10; int secTickWidth = 5; int minorTickWidth = 2; String xAxis = "X Axis"; String yAxisStr = "Y Axis"; String title = "My Fruits"; int width = 500; //total width of the component int height = 430; //total height of the component Color textColor = Color.BLACK; Color backgroundColor = Color.WHITE; Font textFont = new Font("Arial", Font.BOLD, 20); Font yFont = new Font("Arial", Font.PLAIN, 12); Font xFont = new Font("Arial", Font.BOLD, 12); Font titleFont = new Font("Arial", Font.BOLD, 18); Font yCatFont = new Font("Arial", Font.BOLD, 12); Font xCatFont = new Font("Arial", Font.BOLD, 12); ArrayList bars; Axis yAxis; int barWidth = 10; /** * Construct BarChart * * @param bars a number of bars to display * @param yAxis Axis object describes how to display y Axis */ BarChart(ArrayList bars, Axis yAxis) { this.bars = bars; this.yAxis = yAxis; this.yAxisStr = yAxis.yLabel; } @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias! RenderingHints.VALUE_ANTIALIAS_ON); g.drawRect(0, 0, width, height); g2d.setColor(backgroundColor); g.fillRect(0, 0, width, height); g2d.setColor(Color.BLACK); int heightChart = height - (topOffset + bottomOffset); int widthChart = width - (leftOffset + rightOffset); //left g.drawLine(leftOffset, topOffset, leftOffset, heightChart + topOffset); //bottom g.drawLine(leftOffset, heightChart + topOffset, leftOffset + widthChart, heightChart + topOffset); if (this.yAxis.primaryIncrements != 0) drawTick(heightChart, this.yAxis.primaryIncrements, g, Color.BLACK, majorTickWidth); if (this.yAxis.secondaryIncrements != 0) drawTick(heightChart, this.yAxis.secondaryIncrements, g, Color.BLACK, secTickWidth); if (this.yAxis.tertiaryIncrements != 0) drawTick(heightChart, this.yAxis.tertiaryIncrements, g, Color.BLACK, minorTickWidth); drawYLabels(heightChart, this.yAxis.primaryIncrements, g, Color.BLACK); drawBars(heightChart, widthChart, g); drawLabels(heightChart, widthChart, g); } private void drawTick(int heightChart, int increment, Graphics g, Color c, int tickWidth) { int incrementNo = yAxis.maxValue / increment; double factor = ((double) heightChart / (double) yAxis.maxValue); double incrementInPixel = (double) (increment * factor); g.setColor(c); for (int i = 0; i < incrementNo; i++) { int fromTop = heightChart + topOffset - (int) (i * incrementInPixel); g.drawLine(leftOffset, fromTop, leftOffset + tickWidth, fromTop); } } private void drawYLabels(int heightChart, int increment, Graphics g, Color c) { int incrementNo = yAxis.maxValue / increment; double factor = ((double) heightChart / (double) yAxis.maxValue); int incrementInPixel = (int) (increment * factor); g.setColor(c); FontMetrics fm = getFontMetrics(yCatFont); for (int i = 0; i < incrementNo; i++) { int fromTop = heightChart + topOffset - (i * incrementInPixel); String yLabel = "" + (i * increment); int widthStr = fm.stringWidth(yLabel); int heightStr = fm.getHeight(); g.setFont(yCatFont); g.drawString(yLabel, (leftOffset - yLabelOffset) + (yLabelOffset/2 - widthStr/2), fromTop + (heightStr / 2)); } } private void drawBars(int heightChart, int widthChart, Graphics g) { int i = 0; int barNumber = bars.size(); int pointDistance = (int) (widthChart / (barNumber + 1)); for (Bar bar : bars) { i++; double factor = ((double) heightChart / (double) yAxis.maxValue); int scaledBarHeight = (int) (bar.value * factor); int j = topOffset + heightChart - scaledBarHeight; g.setColor(bar.color); g.fillRect(leftOffset + (i * pointDistance) - (barWidth / 2), j, barWidth, scaledBarHeight); //draw tick g.drawLine(leftOffset + (i * pointDistance), topOffset + heightChart, leftOffset + (i * pointDistance), topOffset + heightChart + 2); FontMetrics fm = getFontMetrics(xCatFont); int widthStr = fm.stringWidth(bar.name); int heightStr = fm.getHeight(); g.setFont(xCatFont); g.setColor(Color.BLACK); int xPosition = leftOffset + (i * pointDistance) - (widthStr / 2); int yPosition = topOffset + heightChart + xLabelOffset - heightStr/2; //draw tick g.drawString(bar.name, xPosition, yPosition); } } private void drawLabels(int heightChart, int widthChart, Graphics g) { Graphics2D g2d = (Graphics2D)g; AffineTransform oldTransform = g2d.getTransform(); FontMetrics fmY = getFontMetrics(yFont); int yAxisStringWidth = fmY.stringWidth(yAxisStr); int yAxisStringHeight = fmY.getHeight(); FontMetrics fmX = getFontMetrics(xFont); int xAxisStringWidth = fmX.stringWidth(yAxisStr); int xAxisStringHeight = fmX.getHeight(); FontMetrics fmT = getFontMetrics(titleFont); int titleStringWidth = fmT.stringWidth(title); int titleStringHeight = fmT.getHeight(); g2d.setColor(Color.BLACK); //draw tick g2d.rotate(Math.toRadians(270)); //rotates to above out of screen. int translateDown = -leftOffset -(topOffset + heightChart/2 + yAxisStringWidth/2); //starts off being "topOffset" off, so subtract that first int translateLeft = -topOffset + (leftOffset-yLabelOffset)/2 + yAxisStringHeight/2; //pull down, which is basically the left offset, topOffset, then middle it by //usin chart height and using text height. g2d.translate(translateDown, translateLeft); g2d.setFont(yFont); g2d.drawString(yAxisStr, leftOffset, topOffset); //reset g2d.setTransform(oldTransform); int xAxesLabelHeight = bottomOffset - xLabelOffset; //x label g2d.setFont(xFont); g2d.drawString(xAxis, widthChart/2 + leftOffset - xAxisStringWidth/2, topOffset + heightChart + xLabelOffset + xAxesLabelHeight/2); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias! RenderingHints.VALUE_ANTIALIAS_ON); //title g2d.setFont(titleFont); int titleX = (leftOffset + rightOffset + widthChart)/2 - titleStringWidth/2; int titleY = topOffset/2 + titleStringHeight/2; System.out.println("titleStringHeight " + titleStringHeight); System.out.println("titleX " + titleX); System.out.println("titleY " + titleY); System.out.println("topOffset " + topOffset); g2d.drawString(title, titleX, titleY); } } package com.bluewalrus; import java.awt.Color; /** * @copyright 2014 * @author Oliver Watkins (www.blue-walrus.com) * * All Rights Reserved */ public class Bar { double value; Color color; String name; Bar(int value, Color color, String name) { this.value = value; this.color = color; this.name = name; } } package com.bluewalrus; /** * @copyright 2014 * @author Oliver Watkins (www.blue-walrus.com) * * All Rights Reserved */ public class Axis { int primaryIncrements = 0; int secondaryIncrements = 0; int tertiaryIncrements = 0; int maxValue = 100; int minValue = 0; String yLabel; Axis(String name) { this(100, 0, 50, 10, 5, name); } Axis(int primaryIncrements, int secondaryIncrements, int tertiaryIncrements, String name) { this(100, 0, primaryIncrements, secondaryIncrements, tertiaryIncrements, name); } Axis(Integer maxValue, Integer minValue, int primaryIncrements, int secondaryIncrements, int tertiaryIncrements, String name) { this.maxValue = maxValue; this.minValue = minValue; this.yLabel = name; if (primaryIncrements != 0) this.primaryIncrements = primaryIncrements; if (secondaryIncrements != 0) this.secondaryIncrements = secondaryIncrements; if (tertiaryIncrements != 0) this.tertiaryIncrements = tertiaryIncrements; } } |
This is a wrong code check it again…………..
for (Bar bar : bars) gives type mismatch
just change
“ArrayList bars;”
instead of
“ArrayList bars;”
How can i solve that part of code?
Change :
ArrayList bars;
To:
ArrayList bars;
Do the same in the constructor of BarChart aswell.
Change :
ArrayList bars;
To:
ArrayList < bars > ;
Do the same in the constructor of BarChart aswell.
Awesome, it worked like perfect. 2-3 minor misses, but I guess that because of configuration. You needed to add public for Bar and Axis constructor and define the type of arraylist in BarChart.java but that is something anybody can easily do.
Really appreciate it.
I changed the 3 things stated here earlier and my code compiles and runs without errors. There is no graph showing tho, what am I doing wrong? I’m not using an IDE. Any suggestions?