sample code - Printing

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sai_123

sample code - Printing

Post by sai_123 » Fri Mar 14, 2003 9:47 pm

Using VerticalBarChartDemo, I have created bar chart. Now I want to print the bar chart. Can some one please give me the sample code to print my garph??

Thanks in advance..

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Mar 17, 2003 10:24 am

Click on the chart with the right mouse button and select 'Print...', the code that handles this is all in the ChartPanel class.

Regards,

Dave Gilbert

sai_123

Print problem

Post by sai_123 » Mon Mar 17, 2003 8:53 pm

i clicked right button.. nothing happened.. Here is my code...

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;

import com.jrefinery.data.DatasetUtilities;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.data.DefaultPieDataset;
import com.jrefinery.data.XYDataset;
import com.jrefinery.data.XYSeries;
import com.jrefinery.data.XYSeriesCollection;
import com.jrefinery.chart.axis.HorizontalCategoryAxis;
import com.jrefinery.chart.axis.NumberAxis;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.ChartUtilities;
import com.jrefinery.chart.ChartPanel;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.plot.CategoryPlot;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.ui.RefineryUtilities;

//public class JChart1 extends JFrame {
public class JChart1 extends ApplicationFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JButton btChart = new JButton();
TitledBorder titledBorder1;
JLabel lbChart = new JLabel();

Vector vectData;


public JChart1(String title) {
super(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
..... init code .....
}


// btChart button
void btChart_actionPerformed(ActionEvent e) {
generateBChart(vectData);
}


// bar chart
public void generateBChart(Vector v1) {
vectData = v1;
CCompObj cc;
JFreeChart chart;
DefaultCategoryDataset dataset;

dataset = new DefaultCategoryDataset();
for (int i=0; i<v1.size(); i++) {
cc = (CCompObj) v1.elementAt(i);
dataset.addValue(cc.count, "Material", cc.mmyy);
}
chart = ChartFactory.createVerticalBarChart("Problems by monthly", "Month", "No of problems",
dataset, true, true, false);
chart.setBackgroundPaint(new Color(0xBBBBDD));
CategoryPlot plot = chart.getCategoryPlot();
HorizontalCategoryAxis domainAxis = (HorizontalCategoryAxis) plot.getDomainAxis();
domainAxis.setSkipCategoryLabelsToFit(true);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
try {
BufferedImage image = chart.createBufferedImage(700,300);
lbChart.setHorizontalAlignment(JLabel.CENTER);
lbChart.setVerticalAlignment(JLabel.CENTER);
lbChart.setIcon(new ImageIcon(image));
}
catch (Exception e1) {
System.out.println("Exception........");
}
} // end of generateBChart()

} // end of class

sai_123

ignore previous msg

Post by sai_123 » Mon Mar 17, 2003 9:11 pm

I found how to print. Pls ignore my previous post..

Locked