KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > XYTickLabelDemo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * --------------------
23  * XYTickLabelDemo.java
24  * --------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: Matthias Rose;
28  * Contributor(s): -;
29  *
30  * $Id: XYTickLabelDemo.java,v 1.2 2003/11/24 16:22:23 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 15-Jul-2002 : Version 1 (MR);
35  *
36  */

37
38
39 package org.jfree.chart.demo;
40 import java.awt.BorderLayout JavaDoc;
41 import java.awt.Color JavaDoc;
42 import java.awt.Font JavaDoc;
43 import java.awt.event.ActionEvent JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45
46 import javax.swing.JCheckBox JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JPanel JavaDoc;
49 import javax.swing.JTextField JavaDoc;
50
51 import org.jfree.chart.ChartFactory;
52 import org.jfree.chart.ChartPanel;
53 import org.jfree.chart.JFreeChart;
54 import org.jfree.chart.Spacer;
55 import org.jfree.chart.axis.AxisLocation;
56 import org.jfree.chart.axis.DateAxis;
57 import org.jfree.chart.axis.NumberAxis;
58 import org.jfree.chart.axis.SymbolicAxis;
59 import org.jfree.chart.axis.ValueAxis;
60 import org.jfree.chart.plot.PlotOrientation;
61 import org.jfree.chart.plot.XYPlot;
62 import org.jfree.chart.renderer.StandardXYItemRenderer;
63 import org.jfree.data.XYSeries;
64 import org.jfree.data.XYSeriesCollection;
65 import org.jfree.ui.ApplicationFrame;
66 import org.jfree.ui.RefineryUtilities;
67
68 /**
69  * An example which shows some bugs with tick labels in version 0.9.13
70  *
71  * @author Matthias Rose
72  */

73 public class XYTickLabelDemo extends ApplicationFrame implements ActionListener JavaDoc {
74
75     private static int DEFAULT_FONT_SIZE = 13; // causes some overlapping
76

77     /** the chart */
78     JFreeChart chart;
79
80     /** Tick labels vertical? */
81     private JCheckBox JavaDoc verticalTickLabelsCheckBox;
82
83     /** Plot horizontal? */
84     private JCheckBox JavaDoc horizontalPlotCheckBox;
85
86     /** SymbolicAxes? */
87     private JCheckBox JavaDoc symbolicAxesCheckBox;
88
89     /** Tick labels font size entry field */
90     private JTextField JavaDoc fontSizeTextField;
91
92     /**
93      * A demonstration application showing some bugs with tick labels in version 0.9.13
94      *
95      * @param title the frame title.
96      */

97     public XYTickLabelDemo(String JavaDoc title) {
98
99         super(title);
100         chart = createChart();
101         ChartPanel chartPanel = new ChartPanel(chart);
102         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(600, 270));
103
104         JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
105         setContentPane(mainPanel);
106         mainPanel.add(chartPanel);
107
108         JPanel JavaDoc optionsPanel = new JPanel JavaDoc();
109         mainPanel.add(optionsPanel, BorderLayout.SOUTH);
110         
111         symbolicAxesCheckBox = new JCheckBox JavaDoc("Symbolic axes");
112         symbolicAxesCheckBox.addActionListener(this);
113         optionsPanel.add(symbolicAxesCheckBox);
114
115         verticalTickLabelsCheckBox = new JCheckBox JavaDoc("Tick labels vertical");
116         verticalTickLabelsCheckBox.addActionListener(this);
117         optionsPanel.add(verticalTickLabelsCheckBox);
118
119         fontSizeTextField = new JTextField JavaDoc(3);
120         fontSizeTextField.addActionListener(this);
121         optionsPanel.add(new JLabel JavaDoc("Font size:"));
122         optionsPanel.add(fontSizeTextField);
123         ValueAxis axis = chart.getXYPlot().getDomainAxis();
124         fontSizeTextField.setText(DEFAULT_FONT_SIZE+"");
125
126         XYPlot plot = chart.getXYPlot();
127         Font JavaDoc ft = axis.getTickLabelFont();
128         ft = ft.deriveFont((float) DEFAULT_FONT_SIZE);
129         plot.getDomainAxis().setTickLabelFont(ft);
130         plot.getRangeAxis().setTickLabelFont(ft);
131         plot.getSecondaryDomainAxis(0).setTickLabelFont(ft);
132         plot.getSecondaryRangeAxis(0).setTickLabelFont(ft);
133
134         
135         horizontalPlotCheckBox = new JCheckBox JavaDoc("Plot horizontal");
136         horizontalPlotCheckBox.addActionListener(this);
137         optionsPanel.add(horizontalPlotCheckBox);
138     }
139
140     /**
141      * When a checkbox is changed ...
142      *
143      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
144      */

145     public void actionPerformed(ActionEvent JavaDoc evt)
146     {
147         ValueAxis[] axes = new ValueAxis[4];
148         XYPlot plot = chart.getXYPlot();
149         axes[0] = plot.getDomainAxis();
150         axes[1] = plot.getRangeAxis();
151         axes[2] = plot.getSecondaryDomainAxis(0);
152         axes[3] = plot.getSecondaryRangeAxis(0);
153
154         Object JavaDoc source = evt.getSource();
155         
156         if(source == symbolicAxesCheckBox) {
157
158             boolean val = symbolicAxesCheckBox.isSelected();
159         
160             for(int i = 0; i < axes.length; i++)
161             {
162                 ValueAxis axis = axes[i];
163                 String JavaDoc label = axis.getLabel();
164                 int maxTick = (int) axis.getUpperBound();
165                 String JavaDoc[] tickLabels = new String JavaDoc[maxTick];
166                 Font JavaDoc ft = axis.getTickLabelFont();
167                 for(int itk = 0; itk < maxTick; itk++)
168                     tickLabels[itk] = "Label " + itk;
169                 axis = val ? new SymbolicAxis(label, tickLabels) : new NumberAxis(label);
170                 axis.setTickLabelFont(ft);
171                 axes[i] = axis;
172             }
173             plot.setDomainAxis(axes[0]);
174             plot.setRangeAxis(axes[1]);
175             plot.setSecondaryDomainAxis(0, axes[2]);
176             plot.setSecondaryRangeAxis(0, axes[3]);
177
178         }
179         
180         if(source == symbolicAxesCheckBox || source == verticalTickLabelsCheckBox) {
181             boolean val = verticalTickLabelsCheckBox.isSelected();
182                         
183             for(int i = 0; i < axes.length; i++)
184                 axes[i].setVerticalTickLabels(val);
185             
186         } else if (source == symbolicAxesCheckBox || source == horizontalPlotCheckBox) {
187             
188             PlotOrientation val = horizontalPlotCheckBox.isSelected() ?
189             PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL;
190             chart.getXYPlot().setOrientation(val);
191             
192         } else if (source == symbolicAxesCheckBox || source == fontSizeTextField) {
193             String JavaDoc s = fontSizeTextField.getText();
194             if(s.length() > 0) {
195                 float sz = Float.parseFloat(s);
196                 for(int i = 0; i < axes.length; i++) {
197                     ValueAxis axis = axes[i];
198                     Font JavaDoc ft = axis.getTickLabelFont();
199                     ft = ft.deriveFont(sz);
200                     axis.setTickLabelFont(ft);
201                 }
202             }
203         }
204     }
205
206     /**
207      * Creates the demo chart.
208      *
209      * @return The chart.
210      */

211     private JFreeChart createChart() {
212
213         // create some sample data
214

215         XYSeries series1 = new XYSeries("Something");
216         series1.add(0.0, 30.0);
217         series1.add(1.0, 10.0);
218         series1.add(2.0, 40.0);
219         series1.add(3.0, 30.0);
220         series1.add(4.0, 50.0);
221         series1.add(5.0, 50.0);
222         series1.add(6.0, 70.0);
223         series1.add(7.0, 70.0);
224         series1.add(8.0, 80.0);
225
226         XYSeriesCollection dataset1 = new XYSeriesCollection();
227         dataset1.addSeries(series1);
228   
229         XYSeries series2 = new XYSeries("Something else");
230         series2.add(0.0, 5.0);
231         series2.add(1.0, 4.0);
232         series2.add(2.0, 1.0);
233         series2.add(3.0, 5.0);
234         series2.add(4.0, 0.0);
235
236         XYSeriesCollection dataset2 = new XYSeriesCollection();
237         dataset2.addSeries(series2);
238   
239         // create the chart
240

241         JFreeChart chart = ChartFactory.createXYLineChart(
242             "Tick Label Demo",
243             "Domain Axis 1",
244             "Range Axis 1",
245             dataset1,
246             PlotOrientation.VERTICAL,
247             false,
248             true,
249             false
250         );
251
252         chart.setBackgroundPaint(Color.white);
253         XYPlot plot = chart.getXYPlot();
254         plot.setOrientation(PlotOrientation.VERTICAL);
255         plot.setBackgroundPaint(Color.lightGray);
256         plot.setDomainGridlinePaint(Color.white);
257         plot.setRangeGridlinePaint(Color.white);
258         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
259         
260         StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
261         renderer.setPaint(Color.black);
262        
263         // DOMAIN AXIS 2
264
NumberAxis xAxis2 = new NumberAxis("Domain Axis 2");
265         xAxis2.setAutoRangeIncludesZero(false);
266         plot.setSecondaryDomainAxis(0, xAxis2);
267        
268         // RANGE AXIS 2
269
DateAxis yAxis1 = new DateAxis("Range Axis 1");
270         plot.setRangeAxis(yAxis1);
271         
272         DateAxis yAxis2 = new DateAxis("Range Axis 2");
273         plot.setSecondaryRangeAxis(0, yAxis2);
274         plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT);
275
276         plot.setSecondaryDataset(0, dataset2);
277         plot.mapSecondaryDatasetToDomainAxis(0, new Integer JavaDoc(0));
278         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
279         
280         return chart;
281     }
282
283     /**
284      * Starting point for the demonstration application.
285      *
286      * @param args ignored.
287      */

288     public static void main(String JavaDoc[] args) {
289
290         XYTickLabelDemo demo = new XYTickLabelDemo("Tick Label Demo");
291         demo.pack();
292         RefineryUtilities.centerFrameOnScreen(demo);
293         demo.setVisible(true);
294
295     }
296 }
297
298
Popular Tags