25package umontreal.ssj.charts;
28import org.jfree.chart.JFreeChart;
29import org.jfree.chart.annotations.*;
30import org.jfree.chart.plot.XYPlot;
31import org.jfree.chart.ui.TextAnchor;
32import javax.swing.JFrame;
61 protected JFreeChart chart;
62 protected boolean latexDocFlag =
true;
64 protected boolean autoRange;
65 protected double[] manualRange;
67 protected boolean grid =
false;
68 protected double xstepGrid;
69 protected double ystepGrid;
73 protected boolean probFlag =
false;
75 protected double chartMargin = 0.02;
107 public abstract JFrame
view(
int width,
int height);
115 return chart.getTitle().getText();
125 chart.setTitle(title);
157 private double[] adjustRangeBounds(
double bmin,
double bmax) {
161 double del = (bmax - bmin) / 20.0;
162 int a = (int) Math.floor(0.5 + Math.log10(del));
163 double d = Math.pow(10.0, (
double) a);
164 double lower = d * Math.ceil((bmin - del) / d);
167 if (0 == Math.abs(bmin))
169 double upper = d * Math.floor((bmax + del) / d);
172 double[] range =
new double[2];
178 protected void setAutoRange(
boolean xZero,
boolean yZero,
boolean right,
boolean top) {
184 if (BorneMax - BorneMin < 1)
185 del = (BorneMax - BorneMin) * chartMargin;
189 BorneMin *= 1.0 + del;
191 BorneMin *= 1.0 - del;
193 BorneMax *= 1.0 - del;
195 BorneMax *= 1.0 + del;
196 double[] newRange =
new double[2];
197 newRange = adjustRangeBounds(BorneMin, BorneMax);
198 if (probFlag && (BorneMin == 0.0))
200 XAxis.
getAxis().setLowerBound(newRange[0]);
201 XAxis.
getAxis().setUpperBound(newRange[1]);
205 if (BorneMax - BorneMin < 1)
206 del = (BorneMax - BorneMin) * chartMargin;
210 BorneMin *= 1.0 + del;
212 BorneMin *= 1.0 - del;
214 BorneMax *= 1.0 - del;
216 BorneMax *= 1.0 + del;
217 newRange = adjustRangeBounds(BorneMin, BorneMax);
218 if (probFlag && (newRange[0] <= 0.0))
220 YAxis.
getAxis().setLowerBound(newRange[0]);
221 YAxis.
getAxis().setUpperBound(newRange[1]);
279 private void setManualRange(
double[] range,
boolean xZero,
boolean yZero,
boolean right,
boolean top) {
280 if (range.length != 4)
281 throw new IllegalArgumentException(
"range must have the format: [xmin, xmax, ymin, ymax]");
283 XAxis.
getAxis().setLowerBound(Math.min(range[0], range[1]));
284 XAxis.
getAxis().setUpperBound(Math.max(range[0], range[1]));
285 YAxis.
getAxis().setLowerBound(Math.min(range[2], range[3]));
286 YAxis.
getAxis().setUpperBound(Math.max(range[2], range[3]));
337 throw new IllegalArgumentException(
"margin < 0");
338 chartMargin = margin;
360 double ybottom = YAxis.getAxis().getLowerBound();
361 final Object o =
this;
364 double ytop = YAxis.getAxis().getUpperBound();
365 XYLineAnnotation line =
new XYLineAnnotation(x, ybottom, x, ytop);
366 XYTextAnnotation text =
new XYTextAnnotation(name, x, ytop * yfrac);
368 text.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
370 text.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
372 plot.addAnnotation(line);
373 plot.addAnnotation(text);
392 this.xstepGrid = xstep;
393 this.ystepGrid = ystep;
415 public abstract String
toLatex(
double width,
double height);
421 public void toLatexFile(String fileName,
double width,
double height) {
422 String output =
toLatex(width, height);
425 file =
new FileWriter(fileName);
428 }
catch (IOException e) {
429 System.err.println(
" toLatexFile: cannot write to " + fileName);
434 }
catch (IOException ioe) {
454 protected void setTick0Flags() {
465 YAxis.setTick0Flag(
false);
467 YAxis.setTick0Flag(
true);
472 XAxis.setTick0Flag(
false);
474 XAxis.setTick0Flag(
true);
477 protected double computeXScale(
double position) {
478 double[] bounds =
new double[2];
479 bounds[0] = XAxis.
getAxis().getLowerBound();
480 bounds[1] = XAxis.
getAxis().getUpperBound();
482 if (position < bounds[0])
483 bounds[0] = position;
484 if (position > bounds[1])
485 bounds[1] = position;
486 bounds[0] -= position;
487 bounds[1] -= position;
488 return computeScale(bounds);
491 protected double computeYScale(
double position) {
492 double[] bounds =
new double[2];
493 bounds[0] = YAxis.
getAxis().getLowerBound();
494 bounds[1] = YAxis.
getAxis().getUpperBound();
496 if (position < bounds[0])
497 bounds[0] = position;
498 if (position > bounds[1])
499 bounds[1] = position;
500 bounds[0] -= position;
501 bounds[1] -= position;
502 return computeScale(bounds);
505 protected double computeScale(
double[] bounds) {
506 int tenPowerRatio = 0;
508 while (bounds[1] > 1000 || bounds[0] < -1000) {
514 while (bounds[1] < 100 && bounds[0] > -100) {
519 return 1 / Math.pow(10, tenPowerRatio);
Represents an axis of a chart encapsulated by an instance of XYChart.
NumberAxis getAxis()
Returns the NumberAxis instance (from JFreeChart) linked with the current variable.
void setTwinAxisPosition(double position)
Defines where the opposite axis must be drawn on the current axis, where it should appear,...
double getTwinAxisPosition()
Returns the drawing position parameter (default equals 0).
Provides tools to create and manage histograms.
Stores data used in a XYChart.
double[] getRangeBounds()
Returns range ( -coordinates) min and max values.
double[] getDomainBounds()
Returns domain ( -coordinates) min and max values.
This class provides tools to create charts from data in a simple way.
void enableGrid(double xstep, double ystep)
Puts a grid on the background.
String getTitle()
Gets the current chart title.
void drawVerticalLine(double x, String name, double yfrac, boolean right)
Draws a vertical line on the chart at -coordinate x.
Axis getXAxis()
Returns the chart’s domain axis ( -axis) object.
void toLatexFile(String fileName, double width, double height)
Transforms the chart to LaTeX form and writes it in file fileName.
void setManualRange00(double[] range, boolean xZero, boolean yZero)
Sets the and ranges of the chart using the format: range = [xmin, xmax, ymin, ymax].
void setLatexDocFlag(boolean flag)
Flag to remove the \documentclass (and other) commands in the created LaTeX files.
double getChartMargin()
Returns the chart margin, which is the fraction by which the chart is enlarged on its borders.
void setprobFlag(boolean flag)
Must be set true when plotting probabilities, false otherwise.
void setManualRange(double[] range, boolean right, boolean top)
Sets the and ranges of the chart using the format: range = [xmin, xmax, ymin, ymax].
abstract void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th series.
void setAutoRange()
The and the ranges of the chart are set automatically.
void setManualRange(double[] range)
Sets the and ranges of the chart using the format: range = [xmin, xmax, ymin, ymax].
void setTitle(String title)
Sets a title to this chart.
abstract JFrame view(int width, int height)
Displays chart on the screen using Swing.
void disableGrid()
Disables the background grid.
JFreeChart getJFreeChart()
Returns the JFreeChart object associated with this chart.
void setAutoRange00(boolean xZero, boolean yZero)
The and the ranges of the chart are set automatically.
Axis getYAxis()
Returns the chart’s range axis ( -axis) object.
abstract String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
void setChartMargin(double margin)
Sets the chart margin to margin.
void setAutoRange(boolean right, boolean top)
The and the ranges of the chart are set automatically.