Guest User

Untitled

a guest
Jan 22nd, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. package com.zirius.olfi.modules.ordre.detailedline.ui;
  2.  
  3. import com.jidesoft.swing.JideSwingUtilities;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Rectangle;
  8. import java.math.BigDecimal;
  9. import javax.swing.JPanel;
  10.  
  11. /**
  12. *
  13. * @author Petter Holone <petter@zirius.no>
  14. */
  15. public class SimpleStackedBarChart extends JPanel {
  16.  
  17. private final String[] columnTitles;
  18. private final BigDecimal[] values;
  19. private final Color[] colors;
  20. private StringBuilder tooltipBuilder = new StringBuilder();
  21. private final String pieChartTitle;
  22. private final double[] distribution;
  23. private BigDecimal totalValues = BigDecimal.ZERO;
  24. private int column = 0;
  25. private int currentColumnStart = 0;
  26. private int currentWidth;
  27. private int currentHeight;
  28. private int lastColumnPainted;
  29. public static Color getTransparentColorFromColor(Color color) {
  30. return new Color(color.getRed(), color.getGreen(), color.getBlue(), 40);
  31.  
  32. }
  33.  
  34. public SimpleStackedBarChart(String[] columnTitles, BigDecimal[] values, Color[] colors, String pieChartTitle) {
  35. this.columnTitles = columnTitles;
  36. this.values = values;
  37. this.colors = colors;
  38. this.pieChartTitle = pieChartTitle;
  39. distribution = new double[values.length];
  40. init();
  41. }
  42.  
  43. @Override
  44. protected void paintComponent(Graphics g) {
  45. if (totalValues.compareTo(BigDecimal.ZERO) == 0) {
  46. JideSwingUtilities.fillGradient((Graphics2D) g, getVisibleRect(), getTransparentColorFromColor(colors[0]), colors[0], true);
  47. } else {
  48. currentWidth = getWidth();
  49. currentHeight = getHeight();
  50. currentColumnStart = 0;
  51. lastColumnPainted = 0;
  52. for (column = 0; column < distribution.length; column++) {
  53. int columnWidth = (int) (currentWidth * distribution[column]);
  54. if (columnWidth > 0) {
  55. JideSwingUtilities.fillGradient((Graphics2D) g,
  56. new Rectangle(currentColumnStart, 0, columnWidth, currentHeight),
  57. getTransparentColorFromColor(colors[column]), colors[column], true);
  58. lastColumnPainted = column;
  59. currentColumnStart += columnWidth;
  60. }
  61. }
  62. //paint the rounding error if any
  63. if (currentColumnStart <= currentWidth) {
  64. JideSwingUtilities.fillGradient((Graphics2D) g,
  65. new Rectangle(currentColumnStart, 0, (currentColumnStart - currentWidth)+5, currentHeight),
  66. getTransparentColorFromColor(colors[lastColumnPainted]), colors[lastColumnPainted], true);
  67. }
  68. }
  69. }
  70.  
  71. public Color[] getColors() {
  72. return colors;
  73. }
  74.  
  75. public String[] getColumnTitles() {
  76. return columnTitles;
  77. }
  78.  
  79. public String getPieChartTitle() {
  80. return pieChartTitle;
  81. }
  82.  
  83. public BigDecimal[] getValues() {
  84. return values;
  85. }
  86.  
  87. protected String getNoDataTooltip() {
  88. return "Ingen data";
  89. }
  90.  
  91. private double scaleDouble(double d) {
  92. return ((double) ((int) (d * 100000.0))) / 1000.0;
  93. }
  94.  
  95. private void init() {
  96. for (int i = 0; i < values.length; i++) {
  97. totalValues = totalValues.add(values[i]);
  98. }
  99. for (int i = 0; i < values.length; i++) {
  100. distribution[i] = values[i].divide(totalValues, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
  101. }
  102. tooltipBuilder.append("<html>");
  103. for (int i = 0; i < values.length; i++) {
  104. tooltipBuilder.append(columnTitles != null && columnTitles.length > i ? columnTitles[i] + ": " : "").append(values[i]).append(" (").append(scaleDouble(distribution[i])).append("%)").append("<br>");
  105. }
  106. tooltipBuilder.append("Totalt: ").append(totalValues);
  107. setToolTipText(tooltipBuilder.toString());
  108. }
  109.  
  110. public SimplePieChart getPieChartOfBarChart() {
  111. return new SimplePieChart(columnTitles, values, colors, pieChartTitle);
  112. }
  113. }
Add Comment
Please, Sign In to add comment