Code to create bar and line charts both in same frame but in seperate panels. In this we have values on the top of the bars and re-scaling is done using tick units in both charts related to the data present. Two classes 1. MyLineChart and 2. TestMyLineChart
MyLineChart.java
mport javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
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.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.*;
import java.text.DecimalFormat;
public class MyLineChart extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
//private static final long serialVersionUID = 1L;
public MyLineChart(final String title) {
super();
CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final JFreeChart chart2 = createChart2(dataset);
CategoryPlot plot = chart2.getCategoryPlot();
BarRenderer renderer = (BarRenderer) plot.getRenderer();
DecimalFormat decimalformat1 = new DecimalFormat("###.0");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalformat1));
renderer.setItemLabelsVisible(true);
//double r = renderer.getUpperClip();
//set the range axis to display integers...
final NumberAxis rangeaxis = (NumberAxis) plot.getRangeAxis();
NumberTickUnit tick = new NumberTickUnit(rangeaxis.getUpperBound()/2);
rangeaxis.setTickUnit(tick);
//rangeaxis.getTickLabelFont();
//CategoryAxis domainaxis = plot.getDomainAxis();
//domainaxis.setCategoryMargin(1.0);
//renderer.setItemMargin(0.30);
// this.setLayout(new BorderLayout);
ChartPanel panel = new ChartPanel(chart);
ChartPanel panel2 = new ChartPanel(chart2);
panel.setPreferredSize(new Dimension(400,300));
panel2.setPreferredSize(new Dimension(600,400));
this.add(panel);
this.add(panel2);
}
private CategoryDataset createDataset()
{
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
String category1 = "2001";
String category2 = "2002";
String category3 = "2003";
/*Number number1 = new Integer(20);
Number number2 = new Integer (35);
Number number3 = new Integer (50);*/
DefaultCategoryDataset dataset= new DefaultCategoryDataset();
dataset.addValue(1111.0, series1, category1);
dataset.addValue(4444.0, series1, category2);
dataset.addValue(3333.0, series1, category3);
dataset.addValue(5555.0, series2, category1);
dataset.addValue(7777.0, series2, category2);
dataset.addValue(6666.0, series2, category3);
dataset.addValue(44444.0, series3, category1);
dataset.addValue(333.0, series3, category2);
dataset.addValue(2222.0, series3, category3);
return dataset;
}
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
"Line Chart ", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// final StandardLegend legend = (StandardLegend) chart.getLegend();
// legend.setDisplaySeriesShapes(true);
// legend.setShapeScaleX(1.5);
// legend.setShapeScaleY(1.5);
//legend.setDisplaySeriesLines(true);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.white);
final NumberAxis rangeaxis = (NumberAxis) plot.getRangeAxis();
NumberTickUnit tick = new NumberTickUnit(rangeaxis.getUpperBound()/2);
rangeaxis.setTickUnit(tick);
/*renderer.setSeriesStroke(
0, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {10.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
1, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
2, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {2.0f, 6.0f}, 0.0f
)
);
// OPTIONAL CUSTOMISATION COMPLETED.*/
return chart;
}
private JFreeChart createChart2(CategoryDataset dataset) {
final JFreeChart chart2 = ChartFactory.createBarChart(
"Bar Chart", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
false, // tooltips
false // urls
);
return chart2;
}
}
TestMyLineChart.java
import javax.swing.JFrame;
public class TestMyLineChart {
public static void main (String[] args)
{
MyLineChart bar = new MyLineChart("Line and Bar Chart");
JFrame frame = new JFrame("Line Chart");
frame.setVisible(true);
//frame.setLayout(BorderLayout);
frame.add(bar);
frame.setSize(800, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Tuesday, September 23, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment