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
|
package chart;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import objects.CSVCellInfo;
public class BarChartPanel extends JPanel {
private List<List<CSVCellInfo>> values;
private String[] filenames;
private String title;
/**
*
*/
private static final long serialVersionUID = 1L;
public BarChartPanel(final List<List<CSVCellInfo>> values, final String[] filenames, final String title) {
this.values = values;
this.filenames = filenames;
this.title = title;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("a");
if(values == null || values.size() == 0)
return;
//final double minValue = 1;
final double maxValue = 1;
final Dimension d = getSize();
final int clientWidth = d.width;
final int clientHeight = d.height;
final int barWidth = clientWidth / values.size();
final Font titleFont = new Font("SansSerif", Font.BOLD, 20);
final FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
final Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
final FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
final int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(this.title, x, y);
final int top = titleFontMetrics.getHeight();
final int bottom = labelFontMetrics.getHeight();
/*if (maxValue == minValue)
return;*/
//final double scale = (clientHeight - top - bottom) / (maxValue - minValue);
final List<CSVCellInfo> tcells = values.get(0);
int num = 3;
if(tcells.size()>1)
num = tcells.size()+1;
//System.out.println("=>" + tcells.size());
final double scale = (clientHeight - top - bottom)/num;
//System.out.println("scale: " + scale);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);
for (int i = 0; i < values.size(); i++) {
int valueX = i * barWidth + 1;
int valueY = top;
//System.out.println("valueY: " + valueY);
int height = (int) (maxValue * scale);
valueY += (int) (maxValue * scale) ;
valueY-=y / scale;
//System.out.println("Y: " + y / scale);
final List<CSVCellInfo> cells = values.get(i);
for(final CSVCellInfo cellInfo : cells) {
//System.out.println("X: " + valueX + "; Y: " + valueY + "; barWidth: " + barWidth + "; height: " + height);
final Color color = CSVCellInfo.getColor(cellInfo);
g.setColor(color);
g.fillRect(valueX, valueY, barWidth - 2, height);
g.setColor(Color.black);
int labelWidth = labelFontMetrics.stringWidth(cellInfo.getValue());
int xpos = i * barWidth + (barWidth - labelWidth) / 2;
g.drawString(cellInfo.getValue(), xpos, valueY+(height/2));
g.drawRect(valueX, valueY, barWidth - 2, height);
valueY += (int) (maxValue * scale);
}
int labelWidth = labelFontMetrics.stringWidth(this.filenames[i]);
x = i * barWidth + (barWidth - labelWidth) / 2;
//System.out.println("names[i]: " + this.filenames[i] + "; x: " + x + "; y: " + y);
g.drawString(this.filenames[i], x, y);
}
}
public static void main(String[] argv) {
JFrame f = new JFrame();
f.setSize(600, 400);
//double[] values = new double[3];
final List<List<CSVCellInfo>> values = new ArrayList<List<CSVCellInfo>>();
String[] filenames = new String[3];
final List<CSVCellInfo> valuesFil1 = new ArrayList<CSVCellInfo>();
final CSVCellInfo f1CSVCI = new CSVCellInfo("Name", 3);
final CSVCellInfo f2CSVCI = new CSVCellInfo("Id", 0);
final CSVCellInfo f3CSVCI = new CSVCellInfo("Born date", 1);
valuesFil1.add(f1CSVCI);
valuesFil1.add(f2CSVCI);
valuesFil1.add(f3CSVCI);
values.add(valuesFil1);
filenames[0] = "File 1";
final List<CSVCellInfo> valuesFil2 = new ArrayList<CSVCellInfo>();
final CSVCellInfo f1CSVCI2 = new CSVCellInfo("Id", 0);
final CSVCellInfo f2CSVCI2 = new CSVCellInfo("Name", 3);
final CSVCellInfo f3CSVCI2 = new CSVCellInfo("Born date", 1);
valuesFil2.add(f1CSVCI2);
valuesFil2.add(f2CSVCI2);
valuesFil2.add(f3CSVCI2);
values.add(valuesFil2);
filenames[1] = "File 2";
final List<CSVCellInfo> valuesFil3 = new ArrayList<CSVCellInfo>();
final CSVCellInfo f1CSVCI3 = new CSVCellInfo("Born date", 1);
final CSVCellInfo f2CSVCI3 = new CSVCellInfo("Name", 3);
final CSVCellInfo f3CSVCI3 = new CSVCellInfo("Id", 0);
valuesFil3.add(f1CSVCI3);
valuesFil3.add(f2CSVCI3);
valuesFil3.add(f3CSVCI3);
values.add(valuesFil3);
filenames[2] = "File 3";
f.getContentPane().add(new BarChartPanel(values, filenames, "Test"));
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
f.addWindowListener(wndCloser);
f.setVisible(true);
}
} |
Partager