Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zirius.olfi.modules.ordre.detailedline.ui;
- import com.jidesoft.swing.JideSwingUtilities;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.math.BigDecimal;
- import javax.swing.JPanel;
- /**
- *
- * @author Petter Holone <petter@zirius.no>
- */
- public class SimpleStackedBarChart extends JPanel {
- private final String[] columnTitles;
- private final BigDecimal[] values;
- private final Color[] colors;
- private StringBuilder tooltipBuilder = new StringBuilder();
- private final String pieChartTitle;
- private final double[] distribution;
- private BigDecimal totalValues = BigDecimal.ZERO;
- private int column = 0;
- private int currentColumnStart = 0;
- private int currentWidth;
- private int currentHeight;
- private int lastColumnPainted;
- public static Color getTransparentColorFromColor(Color color) {
- return new Color(color.getRed(), color.getGreen(), color.getBlue(), 40);
- }
- public SimpleStackedBarChart(String[] columnTitles, BigDecimal[] values, Color[] colors, String pieChartTitle) {
- this.columnTitles = columnTitles;
- this.values = values;
- this.colors = colors;
- this.pieChartTitle = pieChartTitle;
- distribution = new double[values.length];
- init();
- }
- @Override
- protected void paintComponent(Graphics g) {
- if (totalValues.compareTo(BigDecimal.ZERO) == 0) {
- JideSwingUtilities.fillGradient((Graphics2D) g, getVisibleRect(), getTransparentColorFromColor(colors[0]), colors[0], true);
- } else {
- currentWidth = getWidth();
- currentHeight = getHeight();
- currentColumnStart = 0;
- lastColumnPainted = 0;
- for (column = 0; column < distribution.length; column++) {
- int columnWidth = (int) (currentWidth * distribution[column]);
- if (columnWidth > 0) {
- JideSwingUtilities.fillGradient((Graphics2D) g,
- new Rectangle(currentColumnStart, 0, columnWidth, currentHeight),
- getTransparentColorFromColor(colors[column]), colors[column], true);
- lastColumnPainted = column;
- currentColumnStart += columnWidth;
- }
- }
- //paint the rounding error if any
- if (currentColumnStart <= currentWidth) {
- JideSwingUtilities.fillGradient((Graphics2D) g,
- new Rectangle(currentColumnStart, 0, (currentColumnStart - currentWidth)+5, currentHeight),
- getTransparentColorFromColor(colors[lastColumnPainted]), colors[lastColumnPainted], true);
- }
- }
- }
- public Color[] getColors() {
- return colors;
- }
- public String[] getColumnTitles() {
- return columnTitles;
- }
- public String getPieChartTitle() {
- return pieChartTitle;
- }
- public BigDecimal[] getValues() {
- return values;
- }
- protected String getNoDataTooltip() {
- return "Ingen data";
- }
- private double scaleDouble(double d) {
- return ((double) ((int) (d * 100000.0))) / 1000.0;
- }
- private void init() {
- for (int i = 0; i < values.length; i++) {
- totalValues = totalValues.add(values[i]);
- }
- for (int i = 0; i < values.length; i++) {
- distribution[i] = values[i].divide(totalValues, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
- }
- tooltipBuilder.append("<html>");
- for (int i = 0; i < values.length; i++) {
- tooltipBuilder.append(columnTitles != null && columnTitles.length > i ? columnTitles[i] + ": " : "").append(values[i]).append(" (").append(scaleDouble(distribution[i])).append("%)").append("<br>");
- }
- tooltipBuilder.append("Totalt: ").append(totalValues);
- setToolTipText(tooltipBuilder.toString());
- }
- public SimplePieChart getPieChartOfBarChart() {
- return new SimplePieChart(columnTitles, values, colors, pieChartTitle);
- }
- }
Add Comment
Please, Sign In to add comment