/** * Paint bar chart with vertical bars. * * @param g2d The graphics. */ @SuppressWarnings("unchecked") private void paintVertical(Graphics2D g2d) { int width = getWidth() - 1 - LineChart.padWidth; int height = getHeight() - 1 - LineChart.padHeight; AffineTransform shift = AffineTransform.getTranslateInstance(LineChart.padWidth / 2f, LineChart.padHeight / 2f); AffineTransform transform = g2d.getTransform(); if (transform != null) transform.concatenate(shift); else transform = shift; g2d.setTransform(transform); Toolkit toolkit = (Toolkit) Creator.getToolkit(); IColorFeature<Color, Graphics2D> colorFeature = toolkit.getFeature(IColorFeature.class); // set grid color g2d.setColor(getForeground()); // get label paragraphs float maxLabelHeight = 0; List<ParagraphLayout> labelLayouts = new ArrayList<ParagraphLayout>(); for (Plot plot : plots) { for (Point point : plot.getPoints()) { ParagraphLayout layout = new ParagraphLayout(point.label, getFont(), Justify.center, g2d.getFontRenderContext()); labelLayouts.add(layout); if (layout.getHeight() > maxLabelHeight) maxLabelHeight = layout.getHeight(); } } // adjust height of graph to accomodate labels height -= maxLabelHeight + 3; // draw vertical grid-lines IScale yscale = (yAxis != null) ? yAxis.getScale() : null; if (yscale != null) { for (Tick tick : yscale.getTicks()) { int y = (int) Math.round(yscale.plot(tick.value) * height); g2d.drawLine(0, y, width, y); } } else { for (int i = 0; i < max; i++) { int y = (int) Math.round(i * width / max); g2d.drawLine(0, y, width, y); } } // ick... get number of points... i hate this schema int n = 0; for (Plot plot : plots) n += plot.getPoints().size(); if (n == 0) return; // horizontal orientation double wbt = width / n; double wb = wbt * 0.5; double x = wbt / 2; for (Plot plot : plots) { for (Point point : plot.getPoints()) { plotBar.reset(); double x0 = x - (wb / 2); double x1 = x + (wb / 2); double y = (yscale != null) ? yscale.plot(point.coords[0]) * height : point.coords[0] * height / max; if (y > 0) { plotBar.moveTo(x0, height); plotBar.lineTo(x0, height - y); plotBar.lineTo(x1, height - y); plotBar.lineTo(x1, height); plotBar.lineTo(x0, height); // draw bar g2d.setColor(getBackground()); colorFeature.applyColor(plot.getBackground(), g2d, width, height); g2d.fill(plotBar); Stroke stroke = strokes.get(plot); if (stroke != null) g2d.setStroke(stroke); colorFeature.applyColor(plot.getForeground(), g2d, width, height); g2d.draw(plotBar); } // draw text colorFeature.applyColor(plot.getForeground(), g2d, width, height); ParagraphLayout layout = new ParagraphLayout(point.label, getFont(), Justify.center, g2d.getFontRenderContext()); layout.draw(g2d, (int) (x - (layout.getWidth() / 2)), height + 3); x += wbt; } } }
/** * Paint bar chart with horizontal bars. * * @param g2d The graphics. */ @SuppressWarnings("unchecked") private void paintHorizontal(Graphics2D g2d) { int width = getWidth() - 1 - LineChart.padWidth; int height = getHeight() - 1 - LineChart.padHeight; AffineTransform shift = AffineTransform.getTranslateInstance(LineChart.padWidth / 2f, LineChart.padHeight / 2f); AffineTransform transform = g2d.getTransform(); if (transform != null) transform.concatenate(shift); else transform = shift; g2d.setTransform(transform); Toolkit toolkit = (Toolkit) Creator.getToolkit(); IColorFeature<Color, Graphics2D> colorFeature = toolkit.getFeature(IColorFeature.class); // draw gradient background // GradientPaint paint = new GradientPaint( 0, 0, Color.white, 0, height, Color.lightGray); // g2d.setPaint( paint); // g2d.fillRect( 0, 0, width, height); // set grid color g2d.setColor(getForeground()); FontMetrics metrics = g2d.getFontMetrics(); // draw vertical grid-lines IScale xscale = (xAxis != null) ? xAxis.getScale() : null; if (xscale != null) { for (Tick tick : xscale.getTicks()) { int x = (int) Math.round(xscale.plot(tick.value) * width); g2d.drawLine(x, 0, x, height); } } else { for (int i = 0; i < max; i++) { int x = (int) Math.round(i * width / max); g2d.drawLine(x, 0, x, height); } } // ick... get number of points... i hate this schema int n = 0; for (Plot plot : plots) n += plot.getPoints().size(); // horizontal orientation double hbt = height / (n + 1); double hb = hbt * 0.75; double y = hbt; for (Plot plot : plots) { for (Point point : plot.getPoints()) { plotBar.reset(); double y0 = y - (hb / 2); double y1 = y + (hb / 2); double x = (xscale != null) ? xscale.plot(point.coords[0]) * width : point.coords[0] * width / max; if (x > 0) { plotBar.moveTo(0, y0); plotBar.lineTo(x, y0); plotBar.lineTo(x, y1); plotBar.lineTo(0, y1); plotBar.lineTo(0, y0); // draw bar g2d.setColor(getBackground()); colorFeature.applyColor(plot.getBackground(), g2d, width, height); g2d.fill(plotBar); Stroke stroke = strokes.get(plot); if (stroke != null) g2d.setStroke(stroke); colorFeature.applyColor(plot.getForeground(), g2d, width, height); g2d.draw(plotBar); } // draw text double textWidth = metrics.stringWidth(point.label); double textX = (x / 2) - (textWidth / 2); if (textWidth > (x + textPad * 2)) textX = x + textPad; g2d.drawString(point.label, (float) textX, (float) (y + metrics.getAscent() / 2)); y += hbt; } } }