| 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 |
package dr.app.gui.chart; |
| 27 |
|
| 28 |
import dr.app.gui.components.JVerticalLabel; |
| 29 |
|
| 30 |
import javax.swing.*; |
| 31 |
import java.awt.*; |
| 32 |
import java.awt.print.PageFormat; |
| 33 |
import java.awt.print.Printable; |
| 34 |
|
| 35 |
public class JChartPanel extends JPanel implements Printable { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private static final long serialVersionUID = -737128752110140264L; |
| 41 |
public JChartPanel(JChart chart, String title, String xAxisTitle, String yAxisTitle) { |
| 42 |
|
| 43 |
this.chart = chart; |
| 44 |
|
| 45 |
setOpaque(false); |
| 46 |
|
| 47 |
ChartLayout layout = new ChartLayout(4, 4); |
| 48 |
setLayout(layout); |
| 49 |
if (chart != null) { |
| 50 |
add(chart, "Chart"); |
| 51 |
} |
| 52 |
|
| 53 |
setTitle(title); |
| 54 |
setXAxisTitle(xAxisTitle); |
| 55 |
setYAxisTitle(yAxisTitle); |
| 56 |
} |
| 57 |
|
| 58 |
public JChart getChart() { |
| 59 |
return chart; |
| 60 |
} |
| 61 |
|
| 62 |
public void setTitle(String title) { |
| 63 |
|
| 64 |
if (titleLabel != null) { |
| 65 |
remove(titleLabel); |
| 66 |
} |
| 67 |
|
| 68 |
if (title != null) { |
| 69 |
titleLabel = new JLabel(title, JLabel.CENTER); |
| 70 |
add(titleLabel, "Title"); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public void setXAxisTitle(String xAxisTitle) { |
| 75 |
|
| 76 |
if (xAxisLabel != null) { |
| 77 |
remove(xAxisLabel); |
| 78 |
} |
| 79 |
|
| 80 |
if (xAxisTitle != null) { |
| 81 |
xAxisLabel = new JLabel(xAxisTitle, JLabel.CENTER); |
| 82 |
add(xAxisLabel, "XLabel"); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public void setYAxisTitle(String yAxisTitle) { |
| 87 |
|
| 88 |
if (yAxisLabel != null) { |
| 89 |
remove(yAxisLabel); |
| 90 |
} |
| 91 |
|
| 92 |
if (yAxisTitle != null) { |
| 93 |
yAxisLabel = new JVerticalLabel(yAxisTitle, JLabel.CENTER, false); |
| 94 |
add(yAxisLabel, "YLabel"); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public String getTitle() { |
| 99 |
return titleLabel.getText(); |
| 100 |
} |
| 101 |
|
| 102 |
public String getXAxisTitle() { |
| 103 |
return xAxisLabel.getText(); |
| 104 |
} |
| 105 |
|
| 106 |
public String getYAxisTitle() { |
| 107 |
return yAxisLabel.getText(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public int print(Graphics g, PageFormat pageFormat, int pageIndex) { |
| 115 |
if (pageIndex > 0) { |
| 116 |
return(NO_SUCH_PAGE); |
| 117 |
} else { |
| 118 |
Graphics2D g2d = (Graphics2D)g; |
| 119 |
|
| 120 |
double x0 = pageFormat.getImageableX(); |
| 121 |
double y0 = pageFormat.getImageableY(); |
| 122 |
|
| 123 |
double w0 = pageFormat.getImageableWidth(); |
| 124 |
double h0 = pageFormat.getImageableHeight(); |
| 125 |
|
| 126 |
double w1 = getWidth(); |
| 127 |
double h1 = getHeight(); |
| 128 |
|
| 129 |
double scale; |
| 130 |
|
| 131 |
if (w0 / w1 < h0 / h1) { |
| 132 |
scale = w0 / w1; |
| 133 |
} else { |
| 134 |
scale = h0 /h1; |
| 135 |
} |
| 136 |
|
| 137 |
g2d.translate(x0, y0); |
| 138 |
g2d.scale(scale, scale); |
| 139 |
|
| 140 |
Color bg = getBackground(); |
| 141 |
setBackground(Color.white); |
| 142 |
|
| 143 |
paint(g2d); |
| 144 |
|
| 145 |
setBackground(bg); |
| 146 |
|
| 147 |
return(PAGE_EXISTS); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
private final JChart chart; |
| 152 |
|
| 153 |
private JLabel titleLabel = null; |
| 154 |
private JLabel xAxisLabel = null; |
| 155 |
private JLabel yAxisLabel = null; |
| 156 |
} |