How can I print charts from Chart.J?

Article ID: 1046
Last updated: 01 Feb, 2008
Article ID: 1046
Last updated: 01 Feb, 2008
Revision: 1
Views: 3824
Posted: 27 Jan, 1999
by Dean J.
Updated: 01 Feb, 2008
by Dean J.
Problem


How do I print charts from Chart.J?


Cause





Action


You can draw your chart directly to a Graphics object and then send that to the printer. The following is a sample that shows how to accomplish this. In this example, clicking on a chart part will start the print job.

PrintExample.zip

With some overlay charts and ChartLite, you might need to use the line

      chart.paintFrame(pgraphics);
instead of
chart.paint(pgraphics);

//File PrintExample.java
package PrintExample;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*; // Add for print properties

import com.roguewave.chart.awt.core.v2_2.*;
import com.roguewave.chart.awt.standard.v2_2.events.*;
import com.roguewave.chart.awt.standard.v2_2.*;
import com.roguewave.chart.awt.datamodels.v2_2.TextData;
import com.roguewave.chart.awt.datamodels.v2_2.DataAdaptor;


public class PrintExample extends Applet implements ChartClickListener
{
Chart chart;
Frame frame;

public void init()

{
// Read data from a file:
TextData data = new TextData(getDocumentBase(), SalesData);
if(data.getLoadError())
{
System.err.println(Error loading data from 'SalesData');
System.exit(1);
}

// Create a chart component:
chart = new Chart(new MultiColumnBarChart(), Chart.VIEW2D, data);
chart.setSize(400, 400);

// Register with the chart as a ChartClickListener:
chart.addChartClickListener(this);

// Create a Frame:
frame = new Frame(Chart Click Example);
frame.add(Center, chart);

// Add a label component to the bottom to display instructions:
Label label = new Label(Click on a bar to print!, Label.CENTER);
frame.add(South, label);
frame.pack();
frame.show();
frame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{ frame.dispose(); }
} );
}



/**
* Handle a chart click event by printing the chart.
*
* @param event a ChartClickEvent.
*/
public void chartClick(ChartClickEvent event)
{
Properties props = new Properties();
String name = new String(ChartJ Print Job);
PrintJob pjob = getToolkit().getPrintJob(frame, name, props);

if(pjob != null)
{
Graphics pgraphics = pjob.getGraphics();
chart.paint(pgraphics);
pgraphics.dispose();
pjob.end();
}
}
}



This article was:   Helpful | Not helpful
Report an issue
Article ID: 1046
Last updated: 01 Feb, 2008
Revision: 1
Views: 3824
Posted: 27 Jan, 1999 by Dean J.
Updated: 01 Feb, 2008 by Dean J.
Attached files


Others in this category