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
| public class GraphRenderer implements TableCellRenderer {
class CustomRenderer extends BarRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
/** The colors. */
private Paint[] colors;
// Creates a new renderer.
public CustomRenderer(final Paint[] colors) {
this.colors = colors;
}
// Returns the paint for an item. Overrides the default behaviour inherited from
public Paint getItemPaint(final int row, final int column) {
return this.colors[column % this.colors.length];
}
}
public Component getTableCellRendererComponent(final JTable table, Object value,
boolean isSelected, boolean hasFocus,int row, int column) {
CategoryDataset dataset = createDataset(row, table);
JFreeChart chart = createChart(dataset,table);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setMinimumDrawWidth(100);
chartPanel.setMinimumDrawHeight(13);
chartPanel.setRequestFocusEnabled(false);
chartPanel.setRangeZoomable(false);
chartPanel.setMouseZoomable(false);
chartPanel.setDisplayToolTips(false);
chartPanel.setBackground(Color.WHITE);
chartPanel.setAutoscrolls(false);
return chartPanel;
}
private CategoryDataset createDataset(int row, JTable table) {
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(Integer.parseInt(table.getValueAt(row, 13).toString()), "", "");
return dataset;
}
private JFreeChart createChart(final CategoryDataset dataset, JTable table) {
ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
BarRenderer.setDefaultShadowsVisible(false);
BarRenderer.setDefaultBarPainter(new StandardBarPainter());
JFreeChart chart = ChartFactory.createBarChart(
null,
"",
"",
dataset,
PlotOrientation.HORIZONTAL,
false,
false,
false);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setInsets(new RectangleInsets(0.0D, 0.0D, 0.0D, 0.0D));
plot.setRangeGridlinesVisible(false);
Paint apaint[] = { Color.cyan };
CategoryItemRenderer customrenderer = new CustomRenderer(apaint);
customrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
customrenderer.setBaseItemLabelsVisible(true);
ItemLabelPosition itemlabelposition = new ItemLabelPosition(
ItemLabelAnchor.CENTER,
TextAnchor.CENTER);
customrenderer.setBasePositiveItemLabelPosition(itemlabelposition);
customrenderer.setBaseItemLabelFont(new Font("Arial", Font.BOLD, 13));
plot.setRenderer(customrenderer);
CategoryAxis categoryaxis = plot.getDomainAxis();
categoryaxis.setLowerMargin(0);
categoryaxis.setUpperMargin(0);
categoryaxis.setVisible(false);
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setRange(0, (Integer.parseInt(table.getValueAt(table.getRowCount() - 1, 13).toString()) / 2));
numberaxis.setVisible(false);
return chart;
}
} |
Partager