Java/Chart/Compass Chart
Содержание
JFreeChart: Compass Chart Sample
JFreeChart: Compass Demo
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ----------------
* CompassDemo.java
* ----------------
* (C) Copyright 2002-2004, by the Australian Antarctic Division and Contributors.
*
* Original Author: Bryan Scott (for the Australian Antarctic Division);
* Contributor(s): David Gilbert (for Object Refinery Limited);
*
* $Id: CompassDemo.java,v 1.15 2004/04/26 19:11:53 taqua Exp $
*
* Changes
* -------
* 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
* 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 27-Mar-2003 : Changed dataset to ValueDataset (DG);
* 16-Mar-2004 : Fixed null data display issue (BRS);
*
*/
package org.jfree.chart.demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.rupassPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.Spinner;
/**
* A demo application showing how to use the {@link CompassPlot} class.
*
* @author Bryan Scott
*/
public class CompassDemo extends JPanel {
/** Whether to output messages to the console **/
public static final boolean DEBUG = true;
/** The available needle types. */
public static final String[] NEEDLE_TYPES
= {"Arrow", "Line", "Long", "Pin", "Plum", "Pointer", "Ship", "Wind", "Arrow"};
/** Dataset 1. */
private DefaultValueDataset compassData = new DefaultValueDataset(new Double(0.0));
/** Dataset 2. */
private DefaultValueDataset shipData = new DefaultValueDataset(new Double(0.0));
/** The compass plot. */
private CompassPlot compassPlot = new CompassPlot(this.rupassData);
/** The chart. */
private JFreeChart compassChart = new JFreeChart("Compass Test",
JFreeChart.DEFAULT_TITLE_FONT,
this.rupassPlot, false);
/** The chart panel. */
private ChartPanel panelCompass = new ChartPanel(this.rupassChart);
/** A grid layout. */
private GridLayout gridLayout1 = new GridLayout();
/** A panel. */
private JPanel panelCompassHolder = new JPanel();
/** A border layout. */
private BorderLayout borderLayout = new BorderLayout();
/** A panel. */
private JPanel jPanel12 = new JPanel();
/** A checkbox. */
private JCheckBox windNullCheckBox = new JCheckBox();
/** A checkbox. */
private JCheckBox shipNullCheckBox = new JCheckBox();
// SpinnerNumberModel modelWind = new SpinnerNumberModel(0, -1, 361, 1);
// SpinnerNumberModel modelShip = new SpinnerNumberModel(0, -1, 361, 1);
// JSpinner spinWind = new JSpinner(modelWind);
// JSpinner spinShip = new JSpinner(modelShip);
/** The wind spinner control. */
private Spinner spinWind = new Spinner(270);
/** The ship spinner control. */
private Spinner spinShip = new Spinner(45);
/** A panel. */
private JPanel jPanel1 = new JPanel();
/** A combo box. */
private JComboBox pick1Pointer = new JComboBox(NEEDLE_TYPES);
/** A panel. */
private JPanel jPanel2 = new JPanel();
/** A combo box. */
private JComboBox pick2Pointer = new JComboBox(NEEDLE_TYPES);
/** A titled border. */
private TitledBorder titledBorder1;
/** A titled border. */
private TitledBorder titledBorder2;
/** A grid bag layout. */
private GridBagLayout gridBagLayout1 = new GridBagLayout();
/** A grid bag layout. */
private GridBagLayout gridBagLayout2 = new GridBagLayout();
/** A titled border. */
private TitledBorder titledBorder3;
/** A grid layout. */
private GridLayout gridLayout2 = new GridLayout();
/**
* Default constructor.
*/
public CompassDemo() {
try {
//this.rupassPlot.addData(this.rupassData);
this.rupassPlot.addData(this.shipData);
this.rupassPlot.setSeriesNeedle(0, 7);
this.rupassPlot.setSeriesNeedle(1, 5);
this.rupassPlot.setSeriesPaint(0, Color.blue);
this.rupassPlot.setSeriesOutlinePaint(0, Color.blue);
this.rupassPlot.setSeriesPaint(1, Color.red);
this.rupassPlot.setSeriesOutlinePaint(1, Color.red);
this.pick1Pointer.setSelectedIndex(7);
this.pick2Pointer.setSelectedIndex(5);
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Initialises the user interface.
*
* @throws Exception if there are any exceptions.
*/
void jbInit() throws Exception {
this.titledBorder1 = new TitledBorder("");
this.titledBorder2 = new TitledBorder("");
this.titledBorder3 = new TitledBorder("");
setLayout(this.gridLayout1);
this.panelCompassHolder.setLayout(this.borderLayout);
this.windNullCheckBox.setHorizontalTextPosition(SwingConstants.LEADING);
this.windNullCheckBox.setText("Null");
this.windNullCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
checkWindNullActionPerformed(e);
}
});
this.shipNullCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
this.shipNullCheckBox.setText("Null");
this.shipNullCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
checkShipNullActionPerformed(e);
}
});
this.spinShip.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
if (DEBUG) {
System.out.println("compassDemo:spinShipPropertyChange");
}
final Spinner spinner = (Spinner) evt.getSource();
if (spinner.isEnabled()) {
shipData.setValue(new Double(spinner.getValue()));
}
}
});
this.spinWind.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
if (DEBUG) {
System.out.println("compassDemo:spinWindPropertyChange");
}
final Spinner spinner = (Spinner) evt.getSource();
if (spinner.isEnabled()) {
compassData.setValue(new Double(spinner.getValue()));
}
}
});
this.jPanel12.setLayout(this.gridLayout2);
this.jPanel2.setBorder(this.titledBorder1);
this.jPanel2.setLayout(this.gridBagLayout2);
this.jPanel1.setBorder(this.titledBorder2);
this.jPanel1.setLayout(this.gridBagLayout1);
this.titledBorder1.setTitle("Second Pointer");
this.titledBorder2.setTitle("First Pointer");
this.titledBorder3.setTitle("Plot Options");
this.pick2Pointer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
pick2PointerActionPerformed(e);
}
});
this.pick1Pointer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
pick1PointerActionPerformed(e);
}
});
add(this.panelCompassHolder, null);
this.panelCompassHolder.add(this.jPanel12, BorderLayout.SOUTH);
this.jPanel12.add(this.jPanel1, null);
this.jPanel1.add(this.pick1Pointer,
new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0),
0, 0));
this.jPanel1.add(this.windNullCheckBox,
new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0));
this.jPanel1.add(this.spinWind,
new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0),
0, 0));
this.jPanel12.add(this.jPanel2, null);
this.jPanel2.add(this.pick2Pointer,
new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0),
0, 0));
this.jPanel2.add(this.shipNullCheckBox,
new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0));
this.jPanel2.add(this.spinShip,
new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0),
0, 0));
this.panelCompassHolder.add(this.panelCompass, BorderLayout.CENTER);
}
/**
* Entry point for the demo application.
*
* @param args ignored.
*/
public static void main(final String[] args) {
final CompassDemo panel = new CompassDemo();
final JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout(5, 5));
frame.setDefaultCloseOperation(3);
frame.setTitle("Compass Demo");
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setSize(700, 400);
final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE *
// * The JFreeChart Developer Guide, written by David Gilbert, is available *
// * to purchase from Object Refinery Limited: *
// * *
// * http://www.object-refinery.ru/jfreechart/guide.html *
// * *
// * Sales are used to provide funding for the JFreeChart project - please *
// * support us so that we can continue developing free software. *
// ****************************************************************************
/**
* Updates the data.
*
* @param value the value.
*/
public void adjustData(final double value) {
final Number val = this.rupassData.getValue();
double newVal = value;
if (val != null) {
newVal += val.doubleValue();
}
if (newVal > 360) {
newVal = 0;
}
if (newVal < 0) {
newVal = 360;
}
this.rupassData.setValue(new Double(newVal));
}
/**
* Handles an action event.
*
* @param e the event.
*/
void checkWindNullActionPerformed(final ActionEvent e) {
if (CompassDemo.DEBUG) {
System.out.println("CompassDemo:checkWindNull: " + this.windNullCheckBox.isSelected());
}
if (this.windNullCheckBox.isSelected()) {
this.rupassData.setValue(null);
this.spinWind.setEnabled(false);
}
else {
// compassData.setValue((new Double(((Integer)spinWind.getValue()).intValue())));
this.rupassData.setValue(new Double(this.spinWind.getValue()));
this.spinWind.setEnabled(true);
}
if (CompassDemo.DEBUG) {
System.out.println("CompassDemo:checkWindNull: " + this.rupassData.getValue());
}
}
/**
* Handles an action event.
*
* @param e the event.
*/
void checkShipNullActionPerformed(final ActionEvent e) {
if (CompassDemo.DEBUG) {
System.out.println("CompassDemo:checkShipNull: " + this.shipNullCheckBox.isSelected());
}
if (this.shipNullCheckBox.isSelected()) {
this.shipData.setValue(null);
this.spinShip.setEnabled(false);
}
else {
// shipData.setValue((new Double(((Integer)spinShip.getValue()).intValue())));
this.shipData.setValue(new Double(this.spinShip.getValue()));
this.spinShip.setEnabled(true);
}
}
/**
* Handles an action event.
*
* @param e the event.
*/
void pick2PointerActionPerformed(final ActionEvent e) {
if (CompassDemo.DEBUG) {
System.out.println("compassDemo:pick2PointActionPerformed " + e.getActionCommand() + ",");
}
this.rupassPlot.setSeriesNeedle(1, this.pick2Pointer.getSelectedIndex());
this.rupassPlot.setSeriesPaint(1, Color.red);
this.rupassPlot.setSeriesOutlinePaint(1, Color.red);
}
/**
* Handles an action event.
*
* @param e the event.
*/
void pick1PointerActionPerformed(final ActionEvent e) {
if (CompassDemo.DEBUG) {
System.out.println("compassDemo:pick1PointActionPerformed " + e.getActionCommand() + ",");
}
this.rupassPlot.setSeriesNeedle(0, this.pick1Pointer.getSelectedIndex());
this.rupassPlot.setSeriesPaint(0, Color.blue);
this.rupassPlot.setSeriesOutlinePaint(0, Color.blue);
}
}
JFreeChart: Compass Demo 2
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -----------------
* CompassDemo2.java
* -----------------
* (C) Copyright 2002-2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: CompassDemo2.java,v 1.7 2004/04/26 19:11:53 taqua Exp $
*
* Changes
* -------
* 04-Aug-2003 : Version 1 (DG);
*
*/
package org.jfree.chart.demo;
import java.awt.Color;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.rupassPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to...
*
*/
public class CompassDemo2 extends ApplicationFrame {
/**
* Creates a new demo application.
*
* @param title the frame title.
*/
public CompassDemo2(final String title) {
super(title);
final ValueDataset dataset = new DefaultValueDataset(new Double(45.0));
final JFreeChart chart = createChart(dataset);
// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
chartPanel.setEnforceFileExtensions(false);
setContentPane(chartPanel);
}
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private JFreeChart createChart(final ValueDataset dataset) {
final CompassPlot plot = new CompassPlot(dataset);
plot.setSeriesNeedle(7);
plot.setSeriesPaint(0, Color.red);
plot.setSeriesOutlinePaint(0, Color.red);
final JFreeChart chart = new JFreeChart(plot);
return chart;
}
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE *
// * The JFreeChart Developer Guide, written by David Gilbert, is available *
// * to purchase from Object Refinery Limited: *
// * *
// * http://www.object-refinery.ru/jfreechart/guide.html *
// * *
// * Sales are used to provide funding for the JFreeChart project - please *
// * support us so that we can continue developing free software. *
// ****************************************************************************
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(final String[] args) {
final CompassDemo2 demo = new CompassDemo2("Compass Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
JFreeChart: Compass Format Demo
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ----------------------
* CompassFormatDemo.java
* ----------------------
* (C) Copyright 2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: CompassFormatDemo.java,v 1.4 2004/04/27 14:53:09 mungady Exp $
*
* Changes
* -------
* 18-Feb-2004 : Version 1 (DG);
*
*/
package org.jfree.chart.demo;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.rupassFormat;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A demo of the {@link CompassFormat} class.
*
*/
public class CompassFormatDemo extends ApplicationFrame {
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public CompassFormatDemo(final String title) {
super(title);
final JFreeChart chart = createChart();
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE *
// * The JFreeChart Developer Guide, written by David Gilbert, is available *
// * to purchase from Object Refinery Limited: *
// * *
// * http://www.object-refinery.ru/jfreechart/guide.html *
// * *
// * Sales are used to provide funding for the JFreeChart project - please *
// * support us so that we can continue developing free software. *
// ****************************************************************************
/**
* Creates a sample dataset.
*
* @param count the item count.
*
* @return the dataset.
*/
private XYDataset createDirectionDataset(final int count) {
final TimeSeriesCollection dataset = new TimeSeriesCollection();
final TimeSeries s1 = new TimeSeries("Wind Direction", Minute.class);
RegularTimePeriod start = new Minute();
double direction = 180.0;
for (int i = 0; i < count; i++) {
s1.add(start, direction);
start = start.next();
direction = direction + (Math.random() - 0.5) * 15.0;
if (direction < 0.0) {
direction = direction + 360.0;
}
else if (direction > 360.0) {
direction = direction - 360.0;
}
}
dataset.addSeries(s1);
return dataset;
}
/**
* Creates a sample dataset.
*
* @param count the item count.
*
* @return the dataset.
*/
private XYDataset createForceDataset(final int count) {
final TimeSeriesCollection dataset = new TimeSeriesCollection();
final TimeSeries s1 = new TimeSeries("Wind Force", Minute.class);
RegularTimePeriod start = new Minute();
double force = 3.0;
for (int i = 0; i < count; i++) {
s1.add(start, force);
start = start.next();
force = Math.max(0.5, force + (Math.random() - 0.5) * 0.5);
}
dataset.addSeries(s1);
return dataset;
}
/**
* Creates a sample chart.
*
* @return a sample chart.
*/
private JFreeChart createChart() {
final XYDataset direction = createDirectionDataset(600);
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Time",
"Date",
"Direction",
direction,
true,
true,
false
);
final XYPlot plot = chart.getXYPlot();
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setUpperMargin(0.0);
// configure the range axis to display directions...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setAutoRangeIncludesZero(false);
final TickUnits units = new TickUnits();
units.add(new NumberTickUnit(180.0, new CompassFormat()));
units.add(new NumberTickUnit(90.0, new CompassFormat()));
units.add(new NumberTickUnit(45.0, new CompassFormat()));
units.add(new NumberTickUnit(22.5, new CompassFormat()));
rangeAxis.setStandardTickUnits(units);
// add the wind force with a secondary dataset/renderer/axis
plot.setRangeAxis(rangeAxis);
final XYItemRenderer renderer2 = new XYAreaRenderer();
final ValueAxis axis2 = new NumberAxis("Force");
axis2.setRange(0.0, 12.0);
renderer2.setSeriesPaint(0, new Color(0, 0, 255, 128));
plot.setDataset(1, createForceDataset(600));
plot.setRenderer(1, renderer2);
plot.setRangeAxis(1, axis2);
plot.mapDatasetToRangeAxis(1, 1);
return chart;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(final String[] args) {
final CompassFormatDemo demo = new CompassFormatDemo("Compass Format Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}