Why is there no text in JFreeChart? - java

JFreeChart seems to be working, except for all the text. It just doesn't show up at all, and I've no idea why. I attached a picture of a window with a pie graph that I got from a tutorial site. As you can see, the text isn't visible. (sorry my twitter feed was really long)
Thanks
Edit:
Here is the code that generates the above graph:
package analyzer_main;
import java.awt.Font;
public class FloatChart extends Composite implements Screen {
JFreeChart floatChart;
public FloatChart(Composite parent, int style){
super(parent,style);
createContents();
}
private void createContents(){
this.setLayout(new FormLayout());
floatChart = createChart(createDataset());
ChartComposite chartComposite = new ChartComposite(this,SWT.NONE,floatChart, true);
FormData fd_chartComposite = new FormData();
fd_chartComposite.left = new FormAttachment(0);
fd_chartComposite.right = new FormAttachment(100,0);
fd_chartComposite.top = new FormAttachment(0);
fd_chartComposite.bottom= new FormAttachment(100,0);
chartComposite.setLayoutData(fd_chartComposite);
}
/** * Creates the Dataset for the Pie chart */
private PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("One", new Double(43.2));
dataset.setValue("Two", new Double(10.0));
dataset.setValue("Three", new Double(27.5));
dataset.setValue("Four", new Double(17.5));
dataset.setValue("Five", new Double(11.0));
dataset.setValue("Six", new Double(19.4));
return dataset;
}
private JFreeChart createChart(PieDataset dataset) {
JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart
// title
dataset, // data
true, // include legend
true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setNoDataMessage("No data available");
plot.setCircular(false);
plot.setLabelGap(0.02);
return chart;
}
#Override
public void Load() {
}
}
As you can see, it's pretty much the same as from the tutorial.

I was having the same problem using Linux Mint 11, Eclipse, and JFreeChart 1.0.14. I found that backing up to 1.0.13 resolved the problem.

First of all, like Kevin Stembridge said, check your JDK. You are using Ubuntu, then try this command in a terminal:
sudo update-alternatives --config java
You can see the JDKs installed on your system and the JDK (is selected at left with a *) you are using for this project.
If you have the Oracle JDK (from package java-6-sun) and OpenJDK (the openjdk-6-jdk package), try to select the Oracle JDK, because the OpenJDK has some graphical differences from the Oracle JDK and maybe these are the reason of this strange behavior on your JFreeChart. Select the Oracle JDK, recompile the Java project, and see if something in your JFreeChart is changed.
Check this Ubuntu Help page for details about Java, or read this old question of SO, or this page if you want to install the latest Oracle JDK.
About the code, it's very similar as from the tutorial, like you said. I have tested your code on Ubuntu, using Eclipse and Oracle JDK 6. The result is this:
adding the chart to a panel, editing only the createContents method (and commenting the ChartComposite parts, because I don't know what is ChartComposite), with this code:
private void createContents(){
//this.setLayout(new FormLayout());
floatChart = createChart(createDataset());
ChartPanel chartPanel = new ChartPanel(floatChart);
// default size
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
// add it to our application
setContentPane(chartPanel);
/*ChartComposite chartComposite = new ChartComposite(this,SWT.NONE,floatChart, true);
FormData fd_chartComposite = new FormData();
fd_chartComposite.left = new FormAttachment(0);
fd_chartComposite.right = new FormAttachment(100,0);
fd_chartComposite.top = new FormAttachment(0);
fd_chartComposite.bottom= new FormAttachment(100,0);
chartComposite.setLayoutData(fd_chartComposite);*/
}
Then, my advice is to review all your code used for the GUI I saw in your screenshot, and you should solve the problem.
Comment this answer if you have questions or problems.

Related

Apache POI Java - Docx Charts and Graphs

Iv been working with XWPF documents for several weeks now and I have not been able to add charts. Pie charts, Bar charts. I plan to manually inject a chart with XML into the file but i think its excessive. I just want to add a chart to a Docx template. Aspose and javadocx are not options.
XWPFDocument document = new XWPFDocument(getClass().getResourceAsStream("/templates/standard.docx"));
//INSERT PIE CHART
FileOutputStream out = new FileOutputStream(new File("output/output.docx");
document.write(out);
[UPDATE - The Easy Route]
Due to time it would take to successfully write an injection method, iv found a handy (Quick n dirty) way of adding charts. This is not the normal word charts but one generated from a library, stored as a picture and inserted.
First, i downloaded the library from http://knowm.org/open-source/xchart/xchart-example-code.
Second, one you have implemented your XWPFdocument, you create a chart and append it as an image.
private XWPFDocument add_chart(XWPFDocument document)
{
// New Chart Element
CategoryChart chart = new CategoryChartBuilder().width(500).height(400).theme(Styler.ChartTheme.GGPlot2).title(getClass().getSimpleName()).build();
chart.setTitle("Issue Count");
// Customize Chart
Color[] sliceColors = new Color[]{new Color(27, 50, 119), new Color(58, 146, 56), new Color(0, 161, 222), new Color(154, 205, 102), new Color(246, 199, 182)};
chart.getStyler().setSeriesColors(sliceColors);
// Series
chart.addSeries("Critical", new ArrayList<>(Arrays.asList(new String[]{"Count"})), new ArrayList<>(Arrays.asList(new Number[]{10})));
chart.addSeries("High", new ArrayList<>(Arrays.asList(new String[]{"High"})), new ArrayList<>(Arrays.asList(new Number[]{5})));
chart.addSeries("Medium", new ArrayList<>(Arrays.asList(new String[]{"Medium"})), new ArrayList<>(Arrays.asList(new Number[]{2})));
chart.addSeries("Low", new ArrayList<>(Arrays.asList(new String[]{"Low"})), new ArrayList<>(Arrays.asList(new Number[]{1})));
// Create and store a jpg image of the chart, then append it to the document
BitmapEncoder.saveBitmapWithDPI(chart, "tmp.jpg", BitmapFormat.JPG, 300);
document.createParagraph().createRun().addPicture(new FileInputStream("tmp.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, "tmp.jpg", Units.toEMU(500), Units.toEMU(400));
return document;
}
An example of one chart i made using the library:
You can use below location's customise poi jar to read and modify chart of document file and then write into your actual document file.
using XWPFChart class you can use all method as we have available for POI EXCEL/PPT.
https://github.com/sandeeptiwari32/POI_ENHN/POI3.14.jar
below is simple example to read chart from MS-WORD File
public class PoiDocTest {
public static void main(String arg[]) throws FileNotFoundException, IOException
{
#SuppressWarnings("resource")
XWPFDocument document = new XWPFDocument(new FileInputStream("chart.docx"));
#SuppressWarnings("unused")
XWPFChart chart;
for (POIXMLDocumentPart part : document.getRelations()) {
if (part instanceof XWPFChart) {
chart = (XWPFChart) part;
break;
}
}
}
}

How to rotate a video placed on a JFrame (VLCJ)?

I am wondering if it is possible to rotate 90 degrees a video played with VLCJ. Part of the code used for displaying the video is the following:
mediaPlayerFactory = new MediaPlayerFactory();
mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
frame.setResizable(false);
frame.setUndecorated(true);
Canvas c = new Canvas();
c.setBackground(Color.black);
final JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
frame.add(p, BorderLayout.CENTER);
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
frame.setLocation(650, 200);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayer.playMedia(file);
mediaPlayer.mute(false);
The code works nicely, the video can be watched without problems, but I would like to perform a rotation on it. I have looked up on the Internet but most of the posts are about rotation of images... so anyone can help me with that? Thank you!
When you create the MediaPlayerFactory, make sure to specify the video filter and options you want as factory arguments, for example:
String[] args = {
"--video-filter",
"rotate",
"rotate-angle",
"10"
};
mediaPlayerFactory = new MediaPlayerFactory(args);
I don't think there's any way to set this dynamically while the video is playing.
The available filters come from:
$vlc --list
The available options come from:
$vlc -H
Alternatively, you could use a DirectMediaPlayer where you render the video yourself into a Graphics2D or OpenGL or whatever context and apply whatever rotation/transformation you want.
Here is mistake:
String[] args = {
"--video-filter",
"rotate",
"rotate-angle",
"10"
};
Should be
String[] args = {
"--video-filter",
"rotate",
"--rotate-angle",
"10"
};
A half a day spent

Disable window resizing in SWT - using composite

Am developing an eclipse plugin which has few wizard pages. I need the wizard window size to be constant, with "BOTH MAXIMISE and MINIMISE disabled", "window RESIZE disabled".
The point is I am not using SHELL. I am using COMPOSITE instead, which doesn't have any style bits.
How can I do that? I am just providing a part of my entire code:
public void createControl(Composite parent)
{
// TODO Auto-generated method stub
composite = new Composite(parent, SWT.NONE );
composite.setLayout(new GridLayout());
Composite selectAdapterComposite = new Composite(composite, SWT.NONE);
FormLayout reportOptionsCompositeLayout = new FormLayout();
reportOptionsCompositeLayout.marginHeight = 1;
reportOptionsCompositeLayout.marginWidth = 1;
selectAdapterComposite.setLayout(reportOptionsCompositeLayout);
buttonInterfaceSelection = new Button(selectAdapterComposite,SWT.RADIO);
//SWT.CHECK);
buttonInterfaceSelection.setText("Generate adapter using interface !");
buttonInterfaceSelection.setSelection(true);
buttonInterfaceSelection.addListener(SWT.Selection, this);
FormData exportInToExcelButtonData = new FormData();
exportInToExcelButtonData.left = new FormAttachment(null, 5);
buttonInterfaceSelection.setLayoutData(exportInToExcelButtonData);
// One Text Box
Label searchBoxLabel = new Label(selectAdapterComposite, SWT.None);
searchBoxLabel.setText("Search to select [Type to get the results below]");
FormData destinationLabelData = new FormData();
destinationLabelData.top = new FormAttachment(buttonInterfaceSelection, 10);
destinationLabelData.left = new FormAttachment(null, 5);
searchBoxLabel.setLayoutData(destinationLabelData);
searchTextBox = new Text(selectAdapterComposite, SWT.BORDER);
searchTextBox.setSize(20, 2);
FormData searchTextBoxData = new FormData();
searchTextBoxData.top = new FormAttachment(searchBoxLabel, 8);
searchTextBoxData.left = new FormAttachment(null, 5);
// destinationFolderPathData.left = new
// FormAttachment(destinationLabel,15);
searchTextBoxData.width = 400;
searchTextBox.addListener(SWT.Modify, this);
searchTextBox.setEnabled(true);
searchTextBox.setLayoutData(searchTextBoxData);
.
.
.
.
.
setControl(composite);
}
Please help me out.
Your code snippet is irrelevant to your question. The key word is wizard. When you create that wizard, it requires a Shell, so you can set its style bits there.
A WizardDialog's constructor:
public WizardDialog(Shell parentShell, IWizard newWizard)
Example of shell style bits:
parentShell.setShellStyle(parentShell.getShellStyle() | (~SWT.RESIZE));
Thanks for your reply... am a newbie to swt and your answer gave me an important info which I dint know before. Now then, I just took some time to go through widgets documentation and found something.
Composite : Instances of this class are controls which are capable of containing other controls.
Shell : Instances of this class represent the "windows" which the desktop or "window manager" is managing.
I realised that my understanding of SHELL and COMPOSITE was wrong.
Conclusion: So I have to depend upon SHELL to give window resizing controls and using a COMPOSITE does not give me any resizing option...
Correct me if am wrong please.. hope this will be useful to other noobs too...
Thanks.
P.S.: now i understoood, my code segment is irrelevant to my question cos, I am working on someone else's code and trying to make some changes to it. instead of making changes in SHELL (which is created in some other class) i am doing it in COMPOSITE.

How to print a JFace TreeViewer on to the printer

I am developing a plugin in Eclipse, that shows the results in a scrolledComposite. The composite contains a JFace TreeViewer. I want to print this TreeViewer to the printer. I found import org.eclipse.swt.printing.Printer; to print to the printer.
But when i am printing using following snippet
GC gc= new GC(printer);
Control abc[] = Composite.getChildren();
abc[0].print(gc);
The tree that i want to print contains the workspace, project explorer.
The print output is showing only the icons. it is not displaying the names of classes, methods.
i cant post screenshot till my reputation is above 10.check it here
Please let me know if i am not clear..
Thanks in advance
Ramesh Emandi
Tree myWidget = treeViewer.getTree();
Point size = myWidget.getSize();
Image image = new Image(display, size.x, size.y);
GC gc = new GC(myWidget);
gc.copyArea(image, 0, 0);
gc.dispose();
// Get the ImageData and create a new printer Image from it
ImageData imageData = image.getImageData();
Image printImage = new Image(printer, imageData);
http://www.eclipse.org/swt/faq.php#noprintimage

How do I export charts from Excel as graphics

I've got a series of Excel spreadsheets, each with at least one page of data and one page of a chart created from the data. I need to capture ( not regenerate from the data ) the existing chart as a web friendly image. Is this possible via Java or .Net? I know the POI stuff (Java) won't do it (or so I'm told, haven't tried it myself).
Have you tried using the Chart.Export Method?
The example in help is:
Worksheets("Sheet1").ChartObjects(1).Chart. Export _
FileName:="current_sales.gif", FilterName:="GIF"
From memory, I think you can export to PNG as well.
Chart images are not stored in workbooks, so you need a component which can render Excel compatible charts.
SpreadsheetGear for .NET will let you load Excel workbooks, optionally plug in new values / formulas / formats / etc..., calculate, and then get an image from a range of cells or from a chart.
You can see some samples here and download the free trial here if you want to try it yourself.
Disclaimer: I own SpreadsheetGear LLC
You can convert or copy charts(graphs) using J XL or Aspose Cells(Aspose is not free).
This is the code snippet to extract excel chart to image
public class ExportChartToImage
{
public static void main(String[] args) throws Exception
{
//Start Excel
Application excelApp = new Application();
excelApp.setVisible(true);
//Create test workbook
Workbook workbook = excelApp.createWorkbook("/home/tejus/Desktop/Chart Test");
//Get the first (and the only) worksheet
final Worksheet worksheet1 = workbook.getWorksheet(1);
//Fill-in the first worksheet with sample data
worksheet1.getCell("A1").setValue("Date");
worksheet1.getCell("A2").setValue("March 1");
worksheet1.getCell("A3").setValue("March 8");
worksheet1.getCell("A4").setValue("March 15");
worksheet1.getCell("B1").setValue("Customer");
worksheet1.getCell("B2").setValue("Smith");
worksheet1.getCell("B3").setValue("Jones");
worksheet1.getCell("B4").setValue("James");
worksheet1.getCell("C1").setValue("Sales");
worksheet1.getCell("C2").setValue("23");
worksheet1.getCell("C3").setValue("17");
worksheet1.getCell("C4").setValue("39");
excelApp.getOleMessageLoop().doInvokeAndWait(new Runnable()
{
public void run()
{
final Variant unspecified = Variant.createUnspecifiedParameter();
final Int32 localeID = new Int32(LocaleID.LOCALE_SYSTEM_DEFAULT);
Range sourceDataNativePeer = worksheet1.getRange("A1:C4").getPeer();
_Worksheet worksheetNativePeer = worksheet1.getPeer();
IDispatch chartObjectDispatch = worksheetNativePeer.chartObjects(unspecified, localeID);
ChartObjectsImpl chartObjects = new ChartObjectsImpl(chartObjectDispatch);
ChartObject chartObject = chartObjects.add(new DoubleFloat(100), new DoubleFloat(150), new DoubleFloat(300), new DoubleFloat(225));
_Chart chart = chartObject.getChart();
chart.setSourceData(sourceDataNativePeer, new Variant(XlRowCol.xlRows));
BStr fileName = new BStr("/home/tejus/Desktop/chart.gif");
Variant filterName = new Variant("gif");
Variant interactive = new Variant(false);
chart.export(fileName, filterName, interactive);
chart.setAutoDelete(false);
chart.release();
chartObject.setAutoDelete(false);
chartObject.release();
chartObjects.setAutoDelete(false);
chartObjects.release();
chartObjectDispatch.setAutoDelete(false);
chartObjectDispatch.release();
}
});
System.out.println("Press 'Enter' to terminate the application");
System.in.read();
//Close the MS Excel application.
boolean saveChanges = false;
workbook.close(saveChanges);
boolean forceQuit = true;
excelApp.close(forceQuit);
}
}
i used J excel

Categories

Resources