use of org.jfree.chart.annotations.XYTextAnnotation in project polyGembler by c-zhou.
the class JfreeChart method plotLineChart.
private void plotLineChart(String file_in, Dimension dim, Color color, String position, String title) {
JPanel collinearChart = new JPanel();
collinearChart.setSize(dim);
collinearChart.setMinimumSize(dim);
collinearChart.setPreferredSize(dim);
collinearChart.setBackground(color);
final XYSeriesCollection lineSeries = new XYSeriesCollection();
int n1 = 0;
double x_max = Double.NEGATIVE_INFINITY, y_max = Double.NEGATIVE_INFINITY;
try {
BufferedReader br = Utils.getBufferedReader(file_in);
String line = br.readLine();
String[] s;
int series = 0;
while ((line = br.readLine()) != null && line.length() > 0) {
s = line.split("\\s+");
series = Integer.parseInt(s[2]);
if (lineSeries.getSeriesCount() < series) {
lineSeries.addSeries(new XYSeries(series));
n1 = 0;
}
double x = Double.parseDouble(s[1]) / 1000, y = Double.parseDouble(s[0]);
lineSeries.getSeries(series - 1).add(x, y);
if (x > x_max)
x_max = x;
if (y > y_max)
y_max = y;
n1++;
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final int n = n1;
final int m = lineSeries.getSeriesCount();
// create error bars
final XYSeriesCollection errorBarSeries2 = new XYSeriesCollection();
for (int i = 0; i < n; i++) {
double[] f = new double[m];
for (int j = 0; j < m; j++) f[j] = lineSeries.getSeries(j).getY(i).doubleValue();
double mean = StatUtils.mean(f), sd = Math.sqrt(StatUtils.variance(f));
double x = lineSeries.getSeries(0).getX(i).doubleValue();
errorBarSeries2.addSeries(new XYSeries(i));
errorBarSeries2.getSeries(i).add(x, mean - sd);
errorBarSeries2.getSeries(i).add(x, mean);
errorBarSeries2.getSeries(i).add(x, mean + sd);
}
final XYSeriesCollection upperBoundSeries = new XYSeriesCollection();
final XYSeriesCollection upperQ3Series = new XYSeriesCollection();
final XYSeriesCollection boxSeriesE = new XYSeriesCollection();
final XYSeriesCollection boxSeriesN = new XYSeriesCollection();
final XYSeriesCollection boxSeriesW = new XYSeriesCollection();
final XYSeriesCollection boxSeriesS = new XYSeriesCollection();
final XYSeriesCollection q2Series = new XYSeriesCollection();
final XYSeriesCollection lowerQ2Series = new XYSeriesCollection();
final XYSeriesCollection lowerBoundSeries = new XYSeriesCollection();
final XYSeriesCollection meanDotSeries = new XYSeriesCollection();
final XYSeriesCollection fillingSeries = new XYSeriesCollection();
for (int i = 0; i < n; i++) {
double[] f = new double[m];
for (int j = 0; j < m; j++) f[j] = lineSeries.getSeries(j).getY(i).doubleValue();
double q1 = StatUtils.percentile(f, 25), q2 = StatUtils.percentile(f, 50), q3 = StatUtils.percentile(f, 75);
double IQR = q3 - q1;
double[] boundary = new double[] { q1 - 1.5 * IQR, q3 + 1.5 * IQR };
final Set<Integer> outliers = new HashSet<Integer>();
for (int k = 0; k < m; k++) if (f[k] < boundary[0] || f[k] > boundary[1])
outliers.add(k);
double lower_bound = Double.POSITIVE_INFINITY, upper_bound = Double.NEGATIVE_INFINITY;
for (int k = 0; k < m; k++) {
if (outliers.contains(k))
continue;
if (f[k] < lower_bound)
lower_bound = f[k];
if (f[k] > upper_bound)
upper_bound = f[k];
}
double x = lineSeries.getSeries(0).getX(i).doubleValue();
double mean = StatUtils.mean(f);
if (mean >= thres) {
int w = upperBoundSeries.getSeriesCount();
upperBoundSeries.addSeries(new XYSeries(i));
upperBoundSeries.getSeries(w).add(x - half_width, upper_bound);
upperBoundSeries.getSeries(w).add(x + half_width, upper_bound);
upperQ3Series.addSeries(new XYSeries(i));
upperQ3Series.getSeries(w).add(x, upper_bound);
upperQ3Series.getSeries(w).add(x, q3);
boxSeriesE.addSeries(new XYSeries(i));
boxSeriesE.getSeries(w).add(x + half_width, q1);
boxSeriesE.getSeries(w).add(x + half_width, q3);
boxSeriesN.addSeries(new XYSeries(i));
boxSeriesN.getSeries(w).add(x + half_width, q3);
boxSeriesN.getSeries(w).add(x - half_width, q3);
boxSeriesW.addSeries(new XYSeries(i));
boxSeriesW.getSeries(w).add(x - half_width, q3);
boxSeriesW.getSeries(w).add(x - half_width, q1);
boxSeriesS.addSeries(new XYSeries(i));
boxSeriesS.getSeries(w).add(x - half_width, q1);
boxSeriesS.getSeries(w).add(x + half_width, q1);
q2Series.addSeries(new XYSeries(i));
q2Series.getSeries(w).add(x - half_width, q2);
q2Series.getSeries(w).add(x + half_width, q2);
lowerQ2Series.addSeries(new XYSeries(i));
lowerQ2Series.getSeries(w).add(x, q1);
lowerQ2Series.getSeries(w).add(x, lower_bound);
lowerBoundSeries.addSeries(new XYSeries(i));
lowerBoundSeries.getSeries(w).add(x - half_width, lower_bound);
lowerBoundSeries.getSeries(w).add(x + half_width, lower_bound);
double x_inter = x - half_width, x_end = x + half_width;
while (x_inter < x_end) {
XYSeries tmp = new XYSeries("shading");
tmp.add(x_inter, q1);
tmp.add(x_inter, q3);
fillingSeries.addSeries(tmp);
x_inter += 0.01;
}
}
meanDotSeries.addSeries(new XYSeries(i));
meanDotSeries.getSeries(i).add(x, mean);
}
final JFreeChart chart = ChartFactory.createXYLineChart(null, // domain axis label
null, // range axis label
null, // data
null, PlotOrientation.VERTICAL, // include legend
false, // tooltips?
true, // URL generator? Not required...
false);
final NumberAxis domainAxis = new NumberAxis("SNP Physical Position (Kb) [" + n + " SNPs]");
final NumberAxis rangeAxis = new NumberAxis("Estimated RF");
domainAxis.setTickUnit(new NumberTickUnit(50));
domainAxis.setRange(-x_max / 20 / dim.getWidth() * dim.getHeight(), x_max * (1 + 0.1 / dim.getWidth() * dim.getHeight()));
rangeAxis.setRange(-y_max / 20, y_max * 1.1);
domainAxis.setLabelFont(font3);
rangeAxis.setLabelFont(font3);
domainAxis.setTickLabelFont(font4);
rangeAxis.setTickLabelFont(font4);
XYPlot plot = chart.getXYPlot();
plot.setDomainAxis(domainAxis);
plot.setRangeAxis(rangeAxis);
for (int i = 0; i < meanDotSeries.getSeriesCount(); i++) {
double mean = meanDotSeries.getSeries(i).getY(0).doubleValue();
if (mean < thres)
continue;
double x = meanDotSeries.getSeries(i).getX(0).doubleValue();
XYTextAnnotation anno = new XYTextAnnotation(String.format(formatStr2, mean) + "\u00B1" + String.format(formatStr2, mean - errorBarSeries2.getSeries(i).getY(0).doubleValue()), x > x_max / 2 ? x - 30 : x + 30, mean);
anno.setFont(font3);
plot.addAnnotation((XYAnnotation) anno);
}
final ChartPanel cp = new ChartPanel(chart, collinearChart.getWidth(), collinearChart.getHeight(), collinearChart.getWidth(), collinearChart.getHeight(), collinearChart.getWidth(), collinearChart.getHeight(), ChartPanel.DEFAULT_BUFFER_USED, true, true, true, true, true);
plot.setDataset(0, meanDotSeries);
plot.setRenderer(0, getRenderer1());
plot.setDataset(1, upperQ3Series);
plot.setRenderer(1, getRenderer3());
plot.setDataset(2, lowerQ2Series);
plot.setRenderer(2, getRenderer3());
plot.setDataset(3, boxSeriesE);
plot.setRenderer(3, getRenderer2());
plot.setDataset(4, boxSeriesN);
plot.setRenderer(4, getRenderer2());
plot.setDataset(5, boxSeriesW);
plot.setRenderer(5, getRenderer2());
plot.setDataset(6, boxSeriesS);
plot.setRenderer(6, getRenderer2());
plot.setDataset(7, q2Series);
plot.setRenderer(7, getRenderer2());
plot.setDataset(8, upperBoundSeries);
plot.setRenderer(8, getRenderer2());
plot.setDataset(9, lowerBoundSeries);
plot.setRenderer(9, getRenderer2());
plot.setDataset(10, fillingSeries);
plot.setRenderer(10, getRenderer4());
plot.setDataset(99, lineSeries);
plot.setRenderer(99, getRenderer0());
/**
* XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(){
* Stroke soild = new BasicStroke(2.0f);
* Stroke dashed = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
* @Override
* public Stroke getItemStroke(int row, int column) {
* if (row>=N[0]&&row<N[1]){
* return new BasicStroke(stroke*2);
* } else if(row>=N[1]&&row<N[2]) {
* return new BasicStroke(stroke*2);
* } else {
* return new BasicStroke(stroke/2);
* }
* }
* };
*
* //XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
* for(int i=0; i<seriesCollection.getSeriesCount(); i++) {
* if(i>=N[3]) {
* renderer.setSeriesPaint(i, Color.gray);
* } else if(i>=N[2]) {
* renderer.setSeriesPaint(i, Color.decode(hex_str[0]));
* } else if(i>=N[1]) {
* renderer.setSeriesPaint(i, Color.decode(hex_str[0]));
* } else if(i>=N[0]) {
* renderer.setSeriesPaint(i, Color.black);
* } else {
* renderer.setSeriesPaint(i, Color.decode(hex_str[4]));
* renderer.setSeriesShapesVisible(i,true);
* renderer.setSeriesShapesFilled(i,true);
* renderer.setSeriesShape(i, shape);
* }
*
* }
*/
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderPaint(Color.WHITE);
chart.setTitle(title);
cp.setPreferredSize(dim);
cp.setBorder(null);
collinearChart.add(cp);
jframe.add(collinearChart, position);
}
use of org.jfree.chart.annotations.XYTextAnnotation in project zaproxy by zaproxy.
the class ScanProgressDialog method updateProgress.
/**
* Updates the scan progress shown by the dialogue (scanners' progress/state and chart).
*/
private void updateProgress() {
// Start panel data settings
HostProcess hp = getSelectedHostProcess();
if (scan.getHostProcesses() != null && hp != null) {
// Update the main table entries
model.updateValues(scan, hp);
if (scan.isStopped()) {
this.stopThread = true;
}
if (chart != null) {
ResponseCountSnapshot snapshot = scan.getRequestHistory();
while (snapshot != null) {
try {
Second second = new Second(snapshot.getDate());
this.seriesTotal.add(second, snapshot.getTotal());
this.series100.add(second, snapshot.getResp100());
this.series200.add(second, snapshot.getResp200());
this.series300.add(second, snapshot.getResp300());
this.series400.add(second, snapshot.getResp400());
this.series500.add(second, snapshot.getResp500());
snapshot = scan.getRequestHistory();
for (Plugin plugin : scan.getHostProcesses().get(0).getRunning()) {
if (!labelsAdded.contains(plugin.getName())) {
// Add a vertical line with the plugin name
ValueMarker vm = new ValueMarker(plugin.getTimeStarted().getTime());
double center = chart.getXYPlot().getRangeAxis().getRange().getCentralValue();
if (lastCentre != center) {
if (lastCentre != -1) {
// Move the existing labels so they stay in the centre
@SuppressWarnings("rawtypes") List annotations = chart.getXYPlot().getAnnotations();
for (Object o : annotations) {
if (o instanceof XYTextAnnotation) {
XYTextAnnotation annotation = (XYTextAnnotation) o;
annotation.setY(center);
}
}
}
lastCentre = center;
}
XYTextAnnotation updateLabel = new XYTextAnnotation(plugin.getName(), plugin.getTimeStarted().getTime(), center);
updateLabel.setFont(FontUtils.getFont("Sans Serif"));
updateLabel.setRotationAnchor(TextAnchor.BASELINE_CENTER);
updateLabel.setTextAnchor(TextAnchor.BASELINE_CENTER);
updateLabel.setRotationAngle(-3.14 / 2);
updateLabel.setPaint(Color.black);
chart.getXYPlot().addDomainMarker(vm, Layer.BACKGROUND);
chart.getXYPlot().addAnnotation(updateLabel);
labelsAdded.add(plugin.getName());
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
snapshot = null;
}
}
}
}
}
use of org.jfree.chart.annotations.XYTextAnnotation in project SIMVA-SoS by SESoS.
the class CombinedDomainXYPlotTest method createPlot.
/**
* Creates a sample plot.
*
* @return A sample plot.
*/
private CombinedDomainXYPlot createPlot() {
// create subplot 1...
XYDataset data1 = createDataset1();
XYItemRenderer renderer1 = new StandardXYItemRenderer();
NumberAxis rangeAxis1 = new NumberAxis("Range 1");
XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
annotation.setRotationAngle(Math.PI / 4.0);
subplot1.addAnnotation(annotation);
// create subplot 2...
XYDataset data2 = createDataset2();
XYItemRenderer renderer2 = new StandardXYItemRenderer();
NumberAxis rangeAxis2 = new NumberAxis("Range 2");
rangeAxis2.setAutoRangeIncludesZero(false);
XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
// parent plot...
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
plot.setGap(10.0);
// add the subplots...
plot.add(subplot1, 1);
plot.add(subplot2, 1);
plot.setOrientation(PlotOrientation.VERTICAL);
return plot;
}
use of org.jfree.chart.annotations.XYTextAnnotation in project SIMVA-SoS by SESoS.
the class YIntervalRendererTest method testCloning.
/**
* Confirm that cloning works.
*/
@Test
public void testCloning() throws CloneNotSupportedException {
YIntervalRenderer r1 = new YIntervalRenderer();
YIntervalRenderer r2 = (YIntervalRenderer) r1.clone();
assertTrue(r1 != r2);
assertTrue(r1.getClass() == r2.getClass());
assertTrue(r1.equals(r2));
// check independence
r1.setSeriesItemLabelGenerator(0, new StandardXYItemLabelGenerator());
assertFalse(r1.equals(r2));
r2.setSeriesItemLabelGenerator(0, new StandardXYItemLabelGenerator());
assertTrue(r1.equals(r2));
r1.setSeriesToolTipGenerator(0, new StandardXYToolTipGenerator());
assertFalse(r1.equals(r2));
r2.setSeriesToolTipGenerator(0, new StandardXYToolTipGenerator());
assertTrue(r1.equals(r2));
r1.addAnnotation(new XYTextAnnotation("ABC", 1.0, 2.0), Layer.FOREGROUND);
assertFalse(r1.equals(r2));
r2.addAnnotation(new XYTextAnnotation("ABC", 1.0, 2.0), Layer.FOREGROUND);
assertTrue(r1.equals(r2));
r1.addAnnotation(new XYTextAnnotation("ABC", 1.0, 2.0), Layer.BACKGROUND);
assertFalse(r1.equals(r2));
r2.addAnnotation(new XYTextAnnotation("ABC", 1.0, 2.0), Layer.BACKGROUND);
assertTrue(r1.equals(r2));
}
use of org.jfree.chart.annotations.XYTextAnnotation in project polyGembler by c-zhou.
the class HMMPanel method getChart.
public ChartPanel getChart() {
Range range = this.updateRates();
final JFreeChart chart = ChartFactory.createXYLineChart(null, // domain axis label
null, // range axis label
null, // data
rates, PlotOrientation.VERTICAL, // include legend
false, // tooltips?
true, // URL generator? Not required...
false);
final NumberAxis domainAxis = new NumberAxis("SNP relative physical position");
// final LogarithmicAxis rangeAxis = new LogarithmicAxis("Trans-out probability");
final NumberAxis rangeAxis = new NumberAxis("Trans-out probability");
AxisSpace space = new AxisSpace();
// reserved space on the left side of the plot
space.setRight(22);
space.setLeft(67);
chart.getXYPlot().setFixedRangeAxisSpace(space);
// double fixedDemension = HMMPanel.this.getX(0, false)+shift;
// rangeAxis.setFixedDimension(fixedDemension);
domainAxis.setTickUnit(new NumberTickUnit(1));
// rangeAxis.setLog10TickLabelsFlag(true);
rangeAxis.setRange(range);
domainAxis.setRange(new Range(1 - offset_yR, noSnps + offset_yR));
chart.getXYPlot().setDomainAxis(domainAxis);
chart.getXYPlot().setRangeAxis(rangeAxis);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
for (int i = 0; i < rates.getSeriesCount(); i++) {
Color c;
if (i == 0)
c = ca.getColor(i);
else
c = Color.lightGray;
renderer.setSeriesPaint(i, c);
if (i == 0)
renderer.setSeriesShapesVisible(i, true);
else
renderer.setSeriesShapesVisible(i, false);
}
/**
* XYPolygonAnnotation a = new XYPolygonAnnotation(new double[] {5.0,
* 0.0, 5.0, 0.5, 6.0, 0.5, 6.0, 0.0}, null, null,Color.black) {
* @Override
* public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) {
* Graphics2D g22 = (Graphics2D) g2.create();
* g22.setXORMode(new Color(0xff0000));
* super.draw(g22, plot, dataArea, domainAxis, rangeAxis, rendererIndex, info);
* }
* };
* renderer.addAnnotation(a, Layer.BACKGROUND);
*/
chart.getXYPlot().setRenderer(renderer);
double anno_offset_x = 0.5;
XYSeries d = rates.getSeries(0);
for (int i = 0; i < d.getItemCount(); i++) {
double x = d.getX(i).doubleValue();
double y = d.getY(i).doubleValue();
if (y > 0.1)
chart.getXYPlot().addAnnotation((XYAnnotation) new XYTextAnnotation(String.format(formatStr2, y), x > noSnps / 2 ? (x - anno_offset_x) : (x + anno_offset_x), y));
else
chart.getXYPlot().addAnnotation((XYAnnotation) new XYTextAnnotation(String.format(formatStr, y), x, y + .05));
}
// chart.getXYPlot().getRangeAxis().get
// chart.getXYPlot().setRangeAxis(new LogarithmicAxis("rates"));
final ChartPanel cp = new ChartPanel(chart, // width
this.getWidth(), // height
150, // mindrawWidth
this.getWidth(), // mindrawHeight
150, // maxDrawWith
this.getWidth(), // maxDrawHeight
150, ChartPanel.DEFAULT_BUFFER_USED, // properties
true, // save
true, // print
true, // zoom
true, // tooltips
true);
{
Range r = chart.getXYPlot().getDomainAxis().getRange();
chart.getXYPlot().getDomainAxis().setAutoRange(false);
chart.getXYPlot().getDomainAxis().setRange(1, r.getUpperBound());
}
{
Range r = chart.getXYPlot().getRangeAxis().getRange();
if (r.getLowerBound() < range.getUpperBound()) {
chart.getXYPlot().getRangeAxis().setAutoRange(false);
chart.getXYPlot().getRangeAxis().setRange(r.getLowerBound(), range.getUpperBound());
}
}
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderPaint(Color.WHITE);
cp.setBorder(null);
/**
* RectangleEdge edge = chart.getXYPlot().getDomainAxisEdge();
* Rectangle2D edge2 = cp.getScreenDataArea();
* double x = RectangleEdge.coordinate(edge2, edge);
* ValueAxis edge3 = chart.getXYPlot().getDomainAxisForDataset(0);
* double xx = domainAxis.valueToJava2D(3, edge2, edge);
*/
return cp;
}
Aggregations