I have a java application using org.eclipse.swtchart (https://projects.eclipse.org/projects/science.swtchart/downloads). I would like to save the outputted chart to a .jpg or similar image format instead of having the chart displayed, thus far I am at a loss as to how to do so. The code is not fully complete yet, I intend to have it monitor a specific file for CSV files and automatically create and save a graphic from the available data.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;[enter image description here][1]
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtchart.Chart;
import org.eclipse.swtchart.IAxis;
import org.eclipse.swtchart.IAxisSet;
import org.eclipse.swtchart.IBarSeries;
import org.eclipse.swtchart.IGrid;
import org.eclipse.swtchart.ISeries.SeriesType;
import org.eclipse.swtchart.LineStyle;
import org.eclipse.swtchart.Range;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class CSVBarChart[enter image description here][1] {
private static double[] ySeries;
private static String [] xSeries;
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
String csvFile = "C:\\Users\\Kevin\\Desktop\\FAD-Export\\42510149_10101764726495991_5660171154099273728_o.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
for (int i = 0; i < 1; i++)
br.readLine();
line = br.readLine();
// use comma as separator
String[] stringSeries = line.split(cvsSplitBy);
String[] xSeries1 = {"Face " + stringSeries[0],
"Condifence " + stringSeries[1],
"AU01 Inner Brow Raiser " + stringSeries[2],
"AU02 Outer Brow Raiser " + stringSeries[3],
"AU04 Brow Lowerer " + stringSeries[4],
"AU05 Upper Lid Raiser " + stringSeries[5],
"AU06 Cheek Raiser " + stringSeries[6],
"AU07 Lid Tightener " + stringSeries[7],
"AU09 Nose Wrinkler " + stringSeries[8],
"AU10 Upper Lip Raiser " + stringSeries[9],
"AU12 Lip Corner Puller " + stringSeries[10],
"AU14 Dimpler " + stringSeries[11],
"AU15 Lip Corner Depressor " + stringSeries[12],
"AU17 Chin Raiser " + stringSeries[13],
"AU20 Lip Stretcher " + stringSeries[14],
"AU23 Lip Tightener " + stringSeries[15],
"AU25 Lips Part " + stringSeries[16],
"AU26 Jaw Drop " + stringSeries[17],
"AU45 Blink " + stringSeries[18]};
xSeries = xSeries1;
ySeries = Arrays.stream(stringSeries)
.mapToDouble(Double::parseDouble)
.toArray();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("");
shell.setSize(900, 900);
shell.setLayout(new FillLayout());
createChart(shell);
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* create the chart.
*
* @param parent
* The parent composite
* @return The created chart
*/
static public Chart createChart(Composite parent) {
// create a chart
Chart chart = new Chart(parent, SWT.NONE);
chart.setOrientation(SWT.VERTICAL);
// set titles
chart.getTitle().setText("Bar Chart");
IAxis yAxis = chart.getAxisSet().getYAxis(0);
yAxis.setRange(new Range(0, 5));
IAxis xAxis = chart.getAxisSet().getXAxis(0);
xAxis.setCategorySeries(xSeries);
xAxis.enableCategory(true);
xAxis.setRange(new Range(2, 20));
IAxisSet axisSet = chart.getAxisSet();
IGrid xGrid = axisSet.getXAxis(0).getGrid();
xGrid.setStyle(LineStyle.NONE);
chart.getAxisSet().getXAxis(0).getTitle().setText("Action Units");
chart.getAxisSet().getYAxis(0).getTitle().setText("Intensity");
// create bar series
IBarSeries barSeries = (IBarSeries)chart.getSeriesSet().createSeries(SeriesType.BAR, "AU Intensity");
barSeries.setYSeries(ySeries);
return chart;
}
}