de.tsl2.nano.incubation.graph.SVGChart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.incubation Show documentation
Show all versions of tsl2.nano.incubation Show documentation
TSL2 Framework Incubations (currently: Attach Agents on Runtime, SVGChart)
package de.tsl2.nano.incubation.graph;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.knowm.xchart.ChartBuilder_Category;
import org.knowm.xchart.ChartBuilder_Pie;
import org.knowm.xchart.ChartBuilder_XY;
import org.knowm.xchart.Chart_Category;
import org.knowm.xchart.Chart_Pie;
import org.knowm.xchart.Chart_XY;
import org.knowm.xchart.Series_XY.ChartXYSeriesRenderStyle;
import org.knowm.xchart.VectorGraphicsEncoder;
import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat;
import org.knowm.xchart.internal.ChartBuilder;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.internal.style.Styler.ChartTheme;
import org.knowm.xchart.internal.style.markers.SeriesMarkers;
import de.tsl2.nano.core.ENV;
import de.tsl2.nano.core.ManagedException;
import de.tsl2.nano.core.util.FileUtil;
import de.tsl2.nano.core.util.Util;
import de.tsl2.nano.scanner.FieldReader;
/**
* creates simple charts and saves them to at least SVG files. this class has indirect dependencies to java.awt! so it
* is not usable on android dalvik. while there exist some libraries like jfreechart, jfreesvg, gral, ... we decided to
* use the small xchart library with dependency to VectorGraphics2D.
*
* the author of gral shows an overview of java plotters here: http://trac.erichseifert.de/gral/wiki/Comparison.
* in wikipedia you get an overview of graph libraries here:
* https://en.wikipedia.org/wiki/Wikipedia:How_to_create_charts_for_Wikipedia_articles
*
* @author Tom, Thomas Schneider
* @version $Revision$
*/
public class SVGChart {
public enum Type {
XY, BAR, PIE, SCATTER;
}
/**
* uses xgraph to create a simple XY graph persisting it to an svg file.
*
* @throws IOException
* @throws FileNotFoundException
*/
public static void createGraph(String title,
String xTitle,
String yTitle,
int width,
int height,
N... yData) {
Chart chart =
createChart(Type.XY, title, xTitle, yTitle, width, height, false, null, serie("y(x)", Arrays.asList(yData)));
String file = ENV.getTempPath() + FileUtil.getValidFileName(title);
exportGraph(file, chart);
}
/**
* exportGraph
*
* @param title
* @param chart
*/
public static void exportGraph(String file, Chart chart) {
try {
// BitmapEncoder.saveBitmap(chart, file, BitmapFormat.PNG);
// BitmapEncoder.saveBitmap(chart, file, BitmapFormat.JPG);
// BitmapEncoder.saveJPGWithQuality(chart, file + "_With_Quality.jpg", 0.95f);
// BitmapEncoder.saveBitmap(chart, file, BitmapFormat.BMP);
// BitmapEncoder.saveBitmap(chart, file, BitmapFormat.GIF);
//
// BitmapEncoder.saveBitmapWithDPI(chart, file + "_300_DPI", BitmapFormat.PNG, 300);
// BitmapEncoder.saveBitmapWithDPI(chart, file + "_300_DPI", BitmapFormat.JPG, 300);
// BitmapEncoder.saveBitmapWithDPI(chart, file + "_300_DPI", BitmapFormat.GIF, 300);
//
// VectorGraphicsEncoder.saveVectorGraphic(chart, file, VectorGraphicsFormat.EPS);
// VectorGraphicsEncoder.saveVectorGraphic(chart, file, VectorGraphicsFormat.PDF);
VectorGraphicsEncoder.saveVectorGraphic(chart, file, VectorGraphicsFormat.SVG);
} catch (Exception e) {
ManagedException.forward(e);
}
}
public static String createGraph(String title,
String xTitle,
String yTitle,
int width,
int height,
List