import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.text.NumberFormat;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import demo.ExtendedStackedBarRenderer;
public class Sample09 extends ApplicationFrame {
public Sample09(String s) {
super(s);
CategoryDataset cateDataset = createDataset();
JFreeChart jFreeChart = createChart(cateDataset);
File outFile = new File("C:\\\\app\\\\eclipse\\\\workspace\\\\ProtType\\\\hoge.jpg");
try{
ChartUtilities.saveChartAsJPEG(outFile, jFreeChart, 30,200);
}catch (Exception e) {
e.printStackTrace();
}
ChartPanel chartPanel = new ChartPanel(jFreeChart);
chartPanel.setPreferredSize(new Dimension(100, 100));
setContentPane(chartPanel);
}
private static CategoryDataset createDataset() {
DefaultCategoryDataset defCateDataset = new DefaultCategoryDataset();
String[] strs = new String[]{"AA", "BB", "CC"};
for(int j=0; j<strs.length; j++){
for(int i=1; i<11; i++){
defCateDataset.addValue(1D, "Series " + i, strs[j]);
}
}
return defCateDataset;
}
private static JFreeChart createChart(CategoryDataset categorydataset) {
JFreeChart jFreeChart =
ChartFactory.createStackedBarChart(
"",
"",
"",
categorydataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
CategoryPlot catPlot = jFreeChart.getCategoryPlot();
ExtendedStackedBarRenderer exStkBarRen = new ExtendedStackedBarRenderer();
catPlot.setBackgroundPaint(Color.white);
catPlot.setRangeGridlinesVisible(false);
catPlot.setDomainGridlinePaint(Color.white);
catPlot.setOutlinePaint(Color.white);
catPlot.setRangeCrosshairPaint(Color.red);
CategoryItemRenderer renderer = (BarRenderer) catPlot.getRenderer();
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesPaint(1, Color.RED);
renderer.setSeriesPaint(2, Color.RED);
renderer.setSeriesPaint(3, Color.RED);
renderer.setSeriesPaint(4, Color.RED);
renderer.setSeriesPaint(5, Color.RED);
renderer.setSeriesPaint(6, Color.RED);
renderer.setSeriesPaint(7, Color.RED);
renderer.setSeriesPaint(8, Color.WHITE);
renderer.setSeriesPaint(9, Color.WHITE);
exStkBarRen.setItemLabelsVisible(false);
catPlot.getDomainAxis().setVisible(true);
NumberAxis numAxis = (NumberAxis) catPlot.getRangeAxis();
numAxis.setVisible(false);
return jFreeChart;
}
public static JPanel createDemoPanel() {
JFreeChart jFreeChart = createChart(createDataset());
return new ChartPanel(jFreeChart);
}
public static void main(String args[]) {
Sample09 sample3 = new Sample09("Stacked Bar Chart Demo 3");
sample3.pack();
RefineryUtilities.centerFrameOnScreen(sample3);
sample3.setVisible(true);
}
}
|