use of org.jfree.data.xy.DefaultXYDataset in project dkpro-lab by dkpro.
the class ChartUtilTest method testPDF.
@Test
public void testPDF() throws Exception {
double[][] data = new double[2][10];
for (int n = 1; n < 10; n++) {
data[0][n] = 1.0 / n;
data[1][n] = 1.0 - (1.0 / n);
}
DefaultXYDataset dataset = new DefaultXYDataset();
dataset.addSeries("data", data);
JFreeChart chart = ChartFactory.createXYLineChart(null, "Recall", "Precision", dataset, PlotOrientation.VERTICAL, false, false, false);
chart.getXYPlot().setRenderer(new XYSplineRenderer());
chart.getXYPlot().getRangeAxis().setRange(0.0, 1.0);
chart.getXYPlot().getDomainAxis().setRange(0.0, 1.0);
File tmp = File.createTempFile("testfile", ".pdf");
try (OutputStream os = new FileOutputStream(tmp)) {
ChartUtil.writeChartAsPDF(os, chart, 400, 400);
}
// Do not have an assert here because the creation date encoded in the PDF changes
// String ref = FileUtils.readFileToString(new File("src/test/resources/chart/test.pdf"),
// "UTF-8");
// String actual = FileUtils.readFileToString(tmp, "UTF-8");
// assertEquals(ref, actual);
}
use of org.jfree.data.xy.DefaultXYDataset in project dkpro-lab by dkpro.
the class ChartUtilTest method testSvg.
@Test
public void testSvg() throws Exception {
double[][] data = new double[2][10];
for (int n = 1; n < 10; n++) {
data[0][n] = 1.0 / n;
data[1][n] = 1.0 - (1.0 / n);
}
DefaultXYDataset dataset = new DefaultXYDataset();
dataset.addSeries("data", data);
JFreeChart chart = ChartFactory.createXYLineChart(null, "Recall", "Precision", dataset, PlotOrientation.VERTICAL, false, false, false);
chart.getXYPlot().setRenderer(new XYSplineRenderer());
chart.getXYPlot().getRangeAxis().setRange(0.0, 1.0);
chart.getXYPlot().getDomainAxis().setRange(0.0, 1.0);
File tmp = File.createTempFile("testfile", ".svg");
try (OutputStream os = new FileOutputStream(tmp)) {
ChartUtil.writeChartAsSVG(os, chart, 400, 400);
}
// String ref = FileUtils.readFileToString(new File("src/test/resources/chart/test.svg"),
// "UTF-8");
// String actual = FileUtils.readFileToString(tmp, "UTF-8");
// assertEquals(ref, actual);
}
use of org.jfree.data.xy.DefaultXYDataset in project Gemma by PavlidisLab.
the class ExpressionExperimentQCController method writeDetailedFactorAnalysis.
private boolean writeDetailedFactorAnalysis(ExpressionExperiment ee, OutputStream os) throws Exception {
SVDValueObject svdo = svdService.getSvdFactorAnalysis(ee.getId());
if (svdo == null)
return false;
if (svdo.getFactors().isEmpty() && svdo.getDates().isEmpty()) {
return false;
}
Map<Integer, Map<Long, Double>> factorCorrelations = svdo.getFactorCorrelations();
// Map<Integer, Map<Long, Double>> factorPvalues = svdo.getFactorPvalues();
Map<Integer, Double> dateCorrelations = svdo.getDateCorrelations();
assert ee.getId().equals(svdo.getId());
// need the experimental design
ee = expressionExperimentService.thawLite(ee);
int maxWidth = 30;
Map<Long, String> efs = this.getFactorNames(ee, maxWidth);
Map<Long, ExperimentalFactor> efIdMap = EntityUtils.getIdMap(ee.getExperimentalDesign().getExperimentalFactors());
Collection<Long> continuousFactors = new HashSet<>();
for (ExperimentalFactor ef : ee.getExperimentalDesign().getExperimentalFactors()) {
boolean isContinous = ExperimentalDesignUtils.isContinuous(ef);
if (isContinous) {
continuousFactors.add(ef.getId());
}
}
/*
* Make plots of the dates vs. PCs, factors vs. PCs.
*/
int MAX_COMP = 3;
Map<Long, List<JFreeChart>> charts = new LinkedHashMap<>();
ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
/*
* FACTORS
*/
String componentShorthand = "PC";
for (Integer component : factorCorrelations.keySet()) {
if (component >= MAX_COMP)
break;
String xaxisLabel = componentShorthand + (component + 1);
for (Long efId : factorCorrelations.get(component).keySet()) {
/*
* Should not happen.
*/
if (!efs.containsKey(efId)) {
log.warn("No experimental factor with id " + efId);
continue;
}
if (!svdo.getFactors().containsKey(efId)) {
// this should not happen.
continue;
}
boolean isCategorical = !continuousFactors.contains(efId);
Map<Long, String> categories = new HashMap<>();
if (isCategorical) {
this.getCategories(efIdMap, efId, categories);
}
if (!charts.containsKey(efId)) {
charts.put(efId, new ArrayList<JFreeChart>());
}
Double a = factorCorrelations.get(component).get(efId);
// unique?
String plotname = (efs.get(efId) == null ? "?" : efs.get(efId)) + " " + xaxisLabel;
if (a != null && !Double.isNaN(a)) {
String title = plotname + " " + String.format("%.2f", a);
List<Double> values = svdo.getFactors().get(efId);
Double[] eigenGene = this.getEigenGene(svdo, component);
assert values.size() == eigenGene.length;
/*
* Plot eigengene vs values, add correlation to the plot
*/
JFreeChart chart;
if (isCategorical) {
/*
* Categorical factor
*/
// use the absolute value of the correlation, since direction is arbitrary.
title = plotname + " " + String.format("r=%.2f", Math.abs(a));
DefaultMultiValueCategoryDataset dataset = new DefaultMultiValueCategoryDataset();
/*
* What this code does is organize the factor values by the groups.
*/
Map<String, List<Double>> groupedValues = new TreeMap<>();
for (int i = 0; i < values.size(); i++) {
Long fvId = values.get(i).longValue();
String fvValue = categories.get(fvId);
if (fvValue == null) {
// is this all we need to do?
continue;
}
if (!groupedValues.containsKey(fvValue)) {
groupedValues.put(fvValue, new ArrayList<Double>());
}
groupedValues.get(fvValue).add(eigenGene[i]);
if (log.isDebugEnabled())
log.debug(fvValue + " " + values.get(i));
}
for (String key : groupedValues.keySet()) {
dataset.add(groupedValues.get(key), plotname, key);
}
// don't show the name of the X axis: it's redundant with the title.
NumberAxis rangeAxis = new NumberAxis(xaxisLabel);
rangeAxis.setAutoRangeIncludesZero(false);
// rangeAxis.setAutoRange( false );
rangeAxis.setAutoRangeMinimumSize(4.0);
// rangeAxis.setRange( new Range( -2, 2 ) );
CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis(null), rangeAxis, new ScatterRenderer());
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
chart = new JFreeChart(title, new Font("SansSerif", Font.BOLD, 12), plot, false);
ScatterRenderer renderer = (ScatterRenderer) plot.getRenderer();
float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
renderer.setSeriesFillPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
renderer.setUseOutlinePaint(false);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
} else {
/*
* Continuous value factor
*/
DefaultXYDataset series = new DefaultXYDataset();
series.addSeries(plotname, new double[][] { ArrayUtils.toPrimitive(values.toArray(new Double[] {})), ArrayUtils.toPrimitive(eigenGene) });
// don't show x-axis label, which would otherwise be efs.get( efId )
chart = ChartFactory.createScatterPlot(title, null, xaxisLabel, series, PlotOrientation.VERTICAL, false, false, false);
XYPlot plot = chart.getXYPlot();
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
XYItemRenderer renderer = plot.getRenderer();
renderer.setBasePaint(Color.white);
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
renderer.setSeriesPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
plot.setRenderer(renderer);
}
chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
charts.get(efId).add(chart);
}
}
}
/*
* DATES
*/
charts.put(-1L, new ArrayList<JFreeChart>());
for (Integer component : dateCorrelations.keySet()) {
String xaxisLabel = componentShorthand + (component + 1);
List<Date> dates = svdo.getDates();
if (dates.isEmpty())
break;
long secspan = ubic.basecode.util.DateUtil.numberOfSecondsBetweenDates(dates);
if (component >= MAX_COMP)
break;
Double a = dateCorrelations.get(component);
if (a != null && !Double.isNaN(a)) {
Double[] eigenGene = svdo.getvMatrix().getColObj(component);
/*
* Plot eigengene vs values, add correlation to the plot
*/
TimeSeries series = new TimeSeries("Dates vs. eigen" + (component + 1));
int i = 0;
for (Date d : dates) {
// if span is less than an hour, retain the minute.
if (secspan < 60 * 60) {
series.addOrUpdate(new Minute(d), eigenGene[i++]);
} else {
series.addOrUpdate(new Hour(d), eigenGene[i++]);
}
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Dates: " + xaxisLabel + " " + String.format("r=%.2f", a), null, xaxisLabel, dataset, false, false, false);
XYPlot xyPlot = chart.getXYPlot();
chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
// standard renderer makes lines.
XYDotRenderer renderer = new XYDotRenderer();
renderer.setBaseFillPaint(Color.white);
renderer.setDotHeight(3);
renderer.setDotWidth(3);
// has no effect, need dotheight.
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
renderer.setSeriesPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
ValueAxis domainAxis = xyPlot.getDomainAxis();
domainAxis.setVerticalTickLabels(true);
xyPlot.setRenderer(renderer);
xyPlot.setRangeGridlinesVisible(false);
xyPlot.setDomainGridlinesVisible(false);
charts.get(-1L).add(chart);
}
}
/*
* Plot in a grid, with each factor as a column. FIXME What if we have too many factors to fit on the screen?
*/
int columns = (int) Math.ceil(charts.size());
int perChartSize = ExpressionExperimentQCController.DEFAULT_QC_IMAGE_SIZE_PX;
BufferedImage image = new BufferedImage(columns * perChartSize, MAX_COMP * perChartSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
int currentX = 0;
int currentY = 0;
for (Long id : charts.keySet()) {
for (JFreeChart chart : charts.get(id)) {
this.addChartToGraphics(chart, g2, currentX, currentY, perChartSize, perChartSize);
if (currentY + perChartSize < MAX_COMP * perChartSize) {
currentY += perChartSize;
} else {
currentY = 0;
currentX += perChartSize;
}
}
}
os.write(ChartUtilities.encodeAsPNG(image));
return true;
}
use of org.jfree.data.xy.DefaultXYDataset in project SIMVA-SoS by SESoS.
the class DatasetUtilitiesTest method testIterateDomainBounds_NaN.
/**
* Check that NaN values in the dataset are ignored.
*/
@Test
public void testIterateDomainBounds_NaN() {
DefaultXYDataset dataset = new DefaultXYDataset();
double[] x = new double[] { 1.0, 2.0, Double.NaN, 3.0 };
double[] y = new double[] { 9.0, 8.0, 7.0, 6.0 };
dataset.addSeries("S1", new double[][] { x, y });
Range r = DatasetUtilities.iterateDomainBounds(dataset);
assertEquals(1.0, r.getLowerBound(), EPSILON);
assertEquals(3.0, r.getUpperBound(), EPSILON);
}
use of org.jfree.data.xy.DefaultXYDataset in project SIMVA-SoS by SESoS.
the class ChartPanelTest method test2502355_zoomInRange.
/**
* Checks that a call to the zoomInRange() method, for a plot with more
* than one range axis, generates just one ChartChangeEvent.
*/
@Test
public void test2502355_zoomInRange() {
DefaultXYDataset dataset = new DefaultXYDataset();
JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X", "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setRangeAxis(1, new NumberAxis("X2"));
ChartPanel panel = new ChartPanel(chart);
chart.addChangeListener(this);
this.chartChangeEvents.clear();
panel.zoomInRange(1.0, 2.0);
assertEquals(1, this.chartChangeEvents.size());
}
Aggregations