25package umontreal.ssj.charts;
27import org.jfree.chart.*;
28import org.jfree.chart.axis.*;
29import org.jfree.chart.plot.*;
30import org.jfree.chart.renderer.xy.*;
31import org.jfree.data.xy.*;
34import java.util.Locale;
35import java.util.Formatter;
38import java.awt.geom.*;
39import cern.colt.list.DoubleArrayList;
40import javax.swing.JFrame;
50 protected void init(String title, String XLabel, String YLabel) {
52 chart = ChartFactory.createXYLineChart(title,
55 dataset.getSeriesCollection(),
56 PlotOrientation.VERTICAL,
false,
62 if (title.startsWith(
"cdf") || title.startsWith(
"prob") || title.startsWith(
"density"))
66 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
71 protected void initAxis() {
72 XAxis =
new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(),
Axis.ORIENTATION_HORIZONTAL);
73 YAxis =
new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(),
Axis.ORIENTATION_VERTICAL);
83 init(
null,
null,
null);
116 public XYLineChart(String title, String XLabel, String YLabel,
double[][]... data) {
119 init(title, XLabel, YLabel);
141 public XYLineChart(String title, String XLabel, String YLabel,
double[][] data,
int numPoints) {
144 init(title, XLabel, YLabel);
164 public XYLineChart(String title, String XLabel, String YLabel,
double[][] data,
int x,
int y) {
166 int len = data[0].length;
167 double[][] proj =
new double[2][len];
168 for (
int i = 0; i < len; i++) {
169 proj[0][i] = data[x][i];
170 proj[1][i] = data[y][i];
173 init(title, XLabel, YLabel);
187 public XYLineChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
190 init(title, XLabel, YLabel);
204 public XYLineChart(String title, String XLabel, String YLabel, XYSeriesCollection data) {
207 init(title, XLabel, YLabel);
223 public int add(
double[] x,
double[] y, String name, String plotStyle) {
224 int seriesIndex =
add(x, y);
240 public int add(
double[] x,
double[] y) {
258 public int add(
double[] x,
double[] y,
int numPoints) {
275 public int add(
double[][] data) {
295 public int add(
double[][] data,
int numPoints) {
316 this.dataset = dataset;
325 XYSeriesCollection seriesCollection = (XYSeriesCollection) this.dataset.getSeriesCollection();
326 double[] values =
new double[seriesCollection.getItemCount(s)];
328 for (
int i = 0; i < seriesCollection.getItemCount(s); i++)
329 values[i] = seriesCollection.getXValue(s, i);
331 XAxis.setLabels(values);
346 public JFrame
view(
int width,
int height) {
348 if (chart.getTitle() !=
null)
349 myFrame =
new JFrame(
"XYLineChart from SSJ: " + chart.getTitle().getText());
351 myFrame =
new JFrame(
"XYLineChart from SSJ");
352 ChartPanel chartPanel =
new ChartPanel(chart);
353 chartPanel.setPreferredSize(
new java.awt.Dimension(width, height));
354 myFrame.setContentPane(chartPanel);
356 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
357 myFrame.setLocationRelativeTo(
null);
358 myFrame.setVisible(
true);
374 public JFrame
viewBar(
int width,
int height) {
376 if (chart.getTitle() !=
null)
377 myFrame =
new JFrame(
"XYLineChart from SSJ: " + chart.getTitle().getText());
379 myFrame =
new JFrame(
"XYLineChart from SSJ");
381 XYPlot plot = (XYPlot) chart.getPlot();
384 plot.setDataset(0, dataset.getSeriesCollection());
385 final XYLineAndShapeRenderer renderer =
new XYLineAndShapeRenderer(
false,
true);
386 renderer.setSeriesPaint(0, Color.ORANGE);
387 renderer.setSeriesShape(0,
new Line2D.Double(0, 0, 0, 1000));
388 plot.setRenderer(0, renderer);
391 plot.setDataset(1, dataset.getSeriesCollection());
392 final XYLineAndShapeRenderer renderer2 =
new XYLineAndShapeRenderer(
false,
true);
393 renderer2.setSeriesPaint(0, Color.ORANGE);
394 renderer2.setSeriesShape(0,
new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0));
395 plot.setRenderer(1, renderer2);
397 ChartPanel chartPanel =
new ChartPanel(chart);
398 chartPanel.setPreferredSize(
new java.awt.Dimension(width, height));
399 myFrame.setContentPane(chartPanel);
401 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
402 myFrame.setLocationRelativeTo(
null);
403 myFrame.setVisible(
true);
410 public String
toLatex(
double width,
double height) {
411 double xunit = 0, yunit = 0;
412 double[] save =
new double[4];
414 if (dataset.getSeriesCollection().getSeriesCount() == 0)
415 throw new IllegalArgumentException(
"Empty chart");
418 double XScale = computeXScale(XAxis.getTwinAxisPosition());
419 double YScale = computeYScale(YAxis.getTwinAxisPosition());
422 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
423 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
425 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
426 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
428 Formatter formatter =
new Formatter(Locale.US);
432 formatter.format(
"\\documentclass[12pt]{article}%n%n");
433 formatter.format(
"\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
435 if (chart.getTitle() !=
null)
436 formatter.format(
"%% PGF/TikZ picture from SSJ: %s%n", chart.getTitle().getText());
438 formatter.format(
"%% PGF/TikZ picture from SSJ %n");
439 formatter.format(
"%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
440 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
441 formatter.format(
"%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
442 formatter.format(
"%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
443 if (chart.getTitle() !=
null)
444 formatter.format(
"\\begin{figure}%n");
445 formatter.format(
"\\begin{center}%n");
446 formatter.format(
"\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
447 formatter.format(
"\\footnotesize%n");
449 formatter.format(
"\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
450 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
451 - XAxis.getTwinAxisPosition()) * XScale,
452 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
453 - YAxis.getTwinAxisPosition()) * YScale,
454 xstepGrid * XScale, ystepGrid * YScale,
455 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
456 - XAxis.getTwinAxisPosition()) * XScale,
457 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
458 - YAxis.getTwinAxisPosition()) * YScale);
460 formatter.format(
"%s", XAxis.toLatex(XScale));
461 formatter.format(
"%s", YAxis.toLatex(YScale));
463 formatter.format(
"%s",
464 dataset.toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
465 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
466 YAxis.getAxis().getUpperBound()));
468 formatter.format(
"\\end{tikzpicture}%n");
469 formatter.format(
"\\end{center}%n");
470 if (chart.getTitle() !=
null) {
471 formatter.format(
"\\caption{");
472 formatter.format(chart.getTitle().getText());
473 formatter.format(
"}%n\\end{figure}%n");
476 formatter.format(
"\\end{document}%n");
477 return formatter.toString();
Represents an axis of a chart encapsulated by an instance of XYChart.
This class provides tools to create charts from data in a simple way.
void setprobFlag(boolean flag)
Must be set true when plotting probabilities, false otherwise.
void setAutoRange()
The and the ranges of the chart are set automatically.
int add(double[][] data)
Adds the new collection of data series data into the series collection.
int add(double[] x, double[] y)
Adds a data series into the series collection.
JFrame viewBar(int width, int height)
Displays bar chart on the screen using Swing.
XYLineChart()
Initializes a new XYLineChart instance with an empty data set.
XYLineChart(String title, String XLabel, String YLabel, double[][] data, int numPoints)
Initializes a new XYLineChart instance with sets of points data.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
int add(double[] x, double[] y, String name, String plotStyle)
Adds a data series into the series collection.
XYLineChart(String title, String XLabel, String YLabel, double[][]... data)
Initializes a new XYLineChart instance with sets of points data.
void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th series.
void setSeriesCollection(XYListSeriesCollection dataset)
Links a new dataset to the current chart.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
XYLineChart(String title, String XLabel, String YLabel, DoubleArrayList... data)
Initializes a new XYLineChart instance with data data.
XYLineChart(String title, String XLabel, String YLabel, double[][] data, int x, int y)
Initializes a new XYLineChart instance using subsets of data.
XYLineChart(String title, String XLabel, String YLabel, XYSeriesCollection data)
Initializes a new XYLineChart instance with data data.
int add(double[][] data, int numPoints)
Adds the new collection of data series data into the series collection.
int add(double[] x, double[] y, int numPoints)
Adds a data series into the series collection.
XYListSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
XYListSeriesCollection()
Stores data used in a XYLineChart or in other related charts, and provides complementary tools to dra...