25package umontreal.ssj.charts;
27import org.jfree.chart.axis.NumberAxis;
28import org.jfree.chart.ChartFactory;
29import org.jfree.chart.ChartPanel;
30import org.jfree.chart.plot.XYPlot;
31import org.jfree.chart.plot.PlotOrientation;
32import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
33import org.jfree.chart.renderer.xy.XYDotRenderer;
34import org.jfree.data.xy.XYSeriesCollection;
35import java.util.Locale;
36import java.util.Formatter;
37import cern.colt.list.DoubleArrayList;
38import javax.swing.JFrame;
48 protected void init(String title, String XLabel, String YLabel) {
50 chart = ChartFactory.createScatterPlot(title,
53 dataset.getSeriesCollection(),
54 PlotOrientation.VERTICAL,
true,
59 ((XYPlot) chart.getPlot()).setRenderer(dataset.getRenderer());
64 for (
int i = 0; i < nb; i++) {
70 protected void initAxis() {
71 XAxis =
new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getDomainAxis(),
Axis.ORIENTATION_HORIZONTAL);
72 YAxis =
new Axis((NumberAxis) ((XYPlot) chart.getPlot()).getRangeAxis(),
Axis.ORIENTATION_VERTICAL);
82 init(
null,
null,
null);
107 public ScatterChart(String title, String XLabel, String YLabel,
double[][]... data) {
110 init(title, XLabel, YLabel);
134 public ScatterChart(String title, String XLabel, String YLabel,
double[][] data,
int numPoints) {
137 init(title, XLabel, YLabel);
157 public ScatterChart(String title, String XLabel, String YLabel,
double[][] data,
int x,
int y) {
159 int len = data[0].length;
160 double[][] proj =
new double[2][len];
161 for (
int i = 0; i < len; i++) {
162 proj[0][i] = data[x][i];
163 proj[1][i] = data[y][i];
166 init(title, XLabel, YLabel);
180 public ScatterChart(String title, String XLabel, String YLabel, DoubleArrayList... data) {
183 init(title, XLabel, YLabel);
197 public ScatterChart(String title, String XLabel, String YLabel, XYSeriesCollection data) {
200 init(title, XLabel, YLabel);
216 public int add(
double[] x,
double[] y, String name, String plotStyle) {
217 int seriesIndex =
add(x, y);
233 public int add(
double[] x,
double[] y) {
234 return add(x, y, x.length);
249 public int add(
double[] x,
double[] y,
int numPoints) {
272 this.dataset = dataset;
281 XYSeriesCollection seriesCollection = (XYSeriesCollection) this.dataset.getSeriesCollection();
282 double[] values =
new double[seriesCollection.getItemCount(s)];
284 for (
int i = 0; i < seriesCollection.getItemCount(s); i++)
285 values[i] = seriesCollection.getXValue(s, i);
287 XAxis.setLabels(values);
302 public JFrame
view(
int width,
int height) {
304 if (chart.getTitle() !=
null)
305 myFrame =
new JFrame(
"ScatterChart from SSJ: " + chart.getTitle().getText());
307 myFrame =
new JFrame(
"ScatterChart from SSJ");
308 XYPlot plot = chart.getXYPlot();
318 XYDotRenderer shape =
new XYDotRenderer();
319 final int dotSize = 3;
320 shape.setDotWidth(dotSize);
321 shape.setDotHeight(dotSize);
323 for (
int i = 0; i < nb; i++)
324 plot.setRenderer(i, shape);
326 ChartPanel chartPanel =
new ChartPanel(chart);
327 chartPanel.setPreferredSize(
new java.awt.Dimension(width, height));
328 myFrame.setContentPane(chartPanel);
330 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
331 myFrame.setLocationRelativeTo(
null);
332 myFrame.setVisible(
true);
339 public String
toLatex(
double width,
double height) {
340 double xunit = 0, yunit = 0;
341 double[] save =
new double[4];
343 if (dataset.getSeriesCollection().getSeriesCount() == 0)
344 throw new IllegalArgumentException(
"Empty chart");
347 double XScale = computeXScale(XAxis.getTwinAxisPosition());
348 double YScale = computeYScale(YAxis.getTwinAxisPosition());
351 xunit = width / ((Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition()) * XScale)
352 - (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition()) * XScale));
354 yunit = height / ((Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition()) * YScale)
355 - (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition()) * YScale));
357 Formatter formatter =
new Formatter(Locale.US);
361 formatter.format(
"\\documentclass[12pt]{article}%n%n");
362 formatter.format(
"\\usepackage{tikz}%n\\usetikzlibrary{plotmarks}%n\\begin{document}%n%n");
364 if (chart.getTitle() !=
null)
365 formatter.format(
"%% PGF/TikZ picture from SSJ: %s%n", chart.getTitle().getText());
367 formatter.format(
"%% PGF/TikZ picture from SSJ %n");
368 formatter.format(
"%% XScale = %s, YScale = %s, XShift = %s, YShift = %s%n", XScale, YScale,
369 XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition());
370 formatter.format(
"%% Therefore, thisFileXValue = (originalSeriesXValue+XShift)*XScale%n");
371 formatter.format(
"%% and thisFileYValue = (originalSeriesYValue+YShift)*YScale%n%n");
372 if (chart.getTitle() !=
null)
373 formatter.format(
"\\begin{figure}%n");
374 formatter.format(
"\\begin{center}%n");
375 formatter.format(
"\\begin{tikzpicture}[x=%scm, y=%scm]%n", xunit, yunit);
376 formatter.format(
"\\footnotesize%n");
378 formatter.format(
"\\draw[color=lightgray] (%s, %s) grid[xstep = %s, ystep=%s] (%s, %s);%n",
379 (Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition())
380 - XAxis.getTwinAxisPosition()) * XScale,
381 (Math.min(YAxis.getAxis().getRange().getLowerBound(), YAxis.getTwinAxisPosition())
382 - YAxis.getTwinAxisPosition()) * YScale,
383 xstepGrid * XScale, ystepGrid * YScale,
384 (Math.max(XAxis.getAxis().getRange().getUpperBound(), XAxis.getTwinAxisPosition())
385 - XAxis.getTwinAxisPosition()) * XScale,
386 (Math.max(YAxis.getAxis().getRange().getUpperBound(), YAxis.getTwinAxisPosition())
387 - YAxis.getTwinAxisPosition()) * YScale);
389 formatter.format(
"%s", XAxis.toLatex(XScale));
390 formatter.format(
"%s", YAxis.toLatex(YScale));
392 formatter.format(
"%s",
393 dataset.toLatex(XScale, YScale, XAxis.getTwinAxisPosition(), YAxis.getTwinAxisPosition(),
394 XAxis.getAxis().getLowerBound(), XAxis.getAxis().getUpperBound(), YAxis.getAxis().getLowerBound(),
395 YAxis.getAxis().getUpperBound()));
397 formatter.format(
"\\end{tikzpicture}%n");
398 formatter.format(
"\\end{center}%n");
399 if (chart.getTitle() !=
null) {
400 formatter.format(
"\\caption{");
401 formatter.format(chart.getTitle().getText());
402 formatter.format(
"}%n\\end{figure}%n");
405 formatter.format(
"\\end{document}%n");
406 return formatter.toString();
Represents an axis of a chart encapsulated by an instance of XYChart.
ScatterChart()
Initializes a new ScatterChart instance with an empty data set.
ScatterChart(String title, String XLabel, String YLabel, DoubleArrayList... data)
Initializes a new ScatterChart instance with data data.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
void setTicksSynchro(int s)
Synchronizes -axis ticks to the -th series.
ScatterChart(String title, String XLabel, String YLabel, XYSeriesCollection data)
Initializes a new ScatterChart instance with data data.
int add(double[] x, double[] y)
Adds a data series into the series collection.
ScatterChart(String title, String XLabel, String YLabel, double[][] data, int x, int y)
Initializes a new ScatterChart instance using subsets of data.
void setSeriesCollection(XYListSeriesCollection dataset)
Links a new dataset to the current chart.
ScatterChart(String title, String XLabel, String YLabel, double[][] data, int numPoints)
Initializes a new ScatterChart instance with sets of points data.
ScatterChart(String title, String XLabel, String YLabel, double[][]... data)
Initializes a new ScatterChart instance with data data.
String toLatex(double width, double height)
Exports the chart to a LaTeX source code using PGF/TikZ.
int add(double[] x, double[] y, int numPoints)
Adds a data series into the series collection.
XYListSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
int add(double[] x, double[] y, String name, String plotStyle)
Adds a data series into the series collection.
This class provides tools to create charts from data in a simple way.
void setAutoRange()
The and the ranges of the chart are set automatically.
XYListSeriesCollection()
Stores data used in a XYLineChart or in other related charts, and provides complementary tools to dra...