Source Code Cross Referenced for LogAxis.java in  » Chart » jfreechart » org » jfree » experimental » chart » axis » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chart » jfreechart » org.jfree.experimental.chart.axis 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ===========================================================
002:         * JFreeChart : a free chart library for the Java(tm) platform
003:         * ===========================================================
004:         *
005:         * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006:         *
007:         * Project Info:  http://www.jfree.org/jfreechart/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it
010:         * under the terms of the GNU Lesser General Public License as published by
011:         * the Free Software Foundation; either version 2.1 of the License, or
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025:         * in the United States and other countries.]
026:         *
027:         * ------------
028:         * LogAxis.java
029:         * ------------
030:         * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   -;
034:         *
035:         * $Id: LogAxis.java,v 1.1.2.2 2007/03/22 16:39:18 mungady Exp $
036:         *
037:         * Changes
038:         * -------
039:         * 24-Aug-2006 : Version 1 (DG);
040:         * 22-Mar-2007 : Use defaultAutoArrange attribute (DG);
041:         *
042:         */
043:
044:        package org.jfree.experimental.chart.axis;
045:
046:        import java.awt.Graphics2D;
047:        import java.awt.geom.Rectangle2D;
048:        import java.text.NumberFormat;
049:        import java.util.ArrayList;
050:        import java.util.List;
051:
052:        import org.jfree.chart.axis.AxisState;
053:        import org.jfree.chart.axis.LogarithmicAxis;
054:        import org.jfree.chart.axis.NumberAxis;
055:        import org.jfree.chart.axis.NumberTick;
056:        import org.jfree.chart.axis.NumberTickUnit;
057:        import org.jfree.chart.axis.ValueAxis;
058:        import org.jfree.chart.event.AxisChangeEvent;
059:        import org.jfree.chart.plot.Plot;
060:        import org.jfree.chart.plot.PlotRenderingInfo;
061:        import org.jfree.chart.plot.ValueAxisPlot;
062:        import org.jfree.data.Range;
063:        import org.jfree.ui.RectangleEdge;
064:        import org.jfree.ui.TextAnchor;
065:
066:        /**
067:         * A numerical axis that uses a logarithmic scale.  The plan is for this class
068:         * to replace the {@link LogarithmicAxis} class.
069:         * 
070:         * WARNING: THIS CLASS IS NOT PART OF THE STANDARD JFREECHART API AND IS 
071:         * SUBJECT TO ALTERATION OR REMOVAL.  DO NOT RELY ON THIS CLASS FOR 
072:         * PRODUCTION USE.  Please experiment with this code and provide feedback.
073:         */
074:
075:        // TODO: support for margins that get inherited from ValueAxis
076:        // TODO: add auto tick unit selection
077:        // TODO: number formatting options
078:        // TODO: write JUnit tests
079:        public class LogAxis extends ValueAxis {
080:
081:            /** The logarithm base. */
082:            private double base = 10.0;
083:
084:            /** The logarithm of the base value - cached for performance. */
085:            private double baseLog = Math.log(10.0);
086:
087:            /**  The smallest value permitted on the axis. */
088:            private double smallestValue = 1E-100;
089:
090:            /** The current tick unit. */
091:            private NumberTickUnit tickUnit;
092:
093:            /** The override number format. */
094:            private NumberFormat numberFormatOverride;
095:
096:            /** The number of minor ticks per major tick unit. */
097:            private int minorTickCount;
098:
099:            /**
100:             * Creates a new <code>LogAxis</code> with no label.
101:             */
102:            public LogAxis() {
103:                this (null);
104:            }
105:
106:            /**
107:             * Creates a new <code>LogAxis</code> with the given label.
108:             * 
109:             * @param label  the axis label (<code>null</code> permitted).
110:             */
111:            public LogAxis(String label) {
112:                super (label, NumberAxis.createIntegerTickUnits());
113:                setDefaultAutoRange(new Range(0.01, 1.0));
114:                this .tickUnit = new NumberTickUnit(1.0);
115:                this .minorTickCount = 10;
116:                this .setTickMarksVisible(false);
117:            }
118:
119:            /**
120:             * Returns the base for the logarithm calculation.
121:             * 
122:             * @return The base for the logarithm calculation.
123:             */
124:            public double getBase() {
125:                return this .base;
126:            }
127:
128:            /**
129:             * Sets the base for the logarithm calculation and sends an 
130:             * {@link AxisChangeEvent} to all registered listeners.
131:             * 
132:             * @param base  the base value (must be > 1.0).
133:             */
134:            public void setBase(double base) {
135:                if (base <= 1.0) {
136:                    throw new IllegalArgumentException("Requires 'base' > 1.0.");
137:                }
138:                this .base = base;
139:                this .baseLog = Math.log(base);
140:                notifyListeners(new AxisChangeEvent(this ));
141:            }
142:
143:            /**
144:             * Returns the smallest value represented by the axis.
145:             * 
146:             * @return The smallest value represented by the axis.
147:             */
148:            public double getSmallestValue() {
149:                return this .smallestValue;
150:            }
151:
152:            /**
153:             * Sets the smallest value represented by the axis.
154:             * 
155:             * @param value  the value.
156:             */
157:            public void setSmallestValue(double value) {
158:                if (value <= 0.0) {
159:                    throw new IllegalArgumentException(
160:                            "Requires 'value' > 0.0.");
161:                }
162:                this .smallestValue = value;
163:            }
164:
165:            /**
166:             * Returns the current tick unit.
167:             * 
168:             * @return The current tick unit.
169:             */
170:            public NumberTickUnit getTickUnit() {
171:                return this .tickUnit;
172:            }
173:
174:            /**
175:             * Sets the tick unit for the axis and sends an {@link AxisChangeEvent} to 
176:             * all registered listeners.  A side effect of calling this method is that
177:             * the "auto-select" feature for tick units is switched off (you can 
178:             * restore it using the {@link ValueAxis#setAutoTickUnitSelection(boolean)}
179:             * method).
180:             *
181:             * @param unit  the new tick unit (<code>null</code> not permitted).
182:             */
183:            public void setTickUnit(NumberTickUnit unit) {
184:                // defer argument checking...
185:                setTickUnit(unit, true, true);
186:            }
187:
188:            /**
189:             * Sets the tick unit for the axis and, if requested, sends an 
190:             * {@link AxisChangeEvent} to all registered listeners.  In addition, an 
191:             * option is provided to turn off the "auto-select" feature for tick units 
192:             * (you can restore it using the 
193:             * {@link ValueAxis#setAutoTickUnitSelection(boolean)} method).
194:             *
195:             * @param unit  the new tick unit (<code>null</code> not permitted).
196:             * @param notify  notify listeners?
197:             * @param turnOffAutoSelect  turn off the auto-tick selection?
198:             */
199:            public void setTickUnit(NumberTickUnit unit, boolean notify,
200:                    boolean turnOffAutoSelect) {
201:
202:                if (unit == null) {
203:                    throw new IllegalArgumentException("Null 'unit' argument.");
204:                }
205:                this .tickUnit = unit;
206:                if (turnOffAutoSelect) {
207:                    setAutoTickUnitSelection(false, false);
208:                }
209:                if (notify) {
210:                    notifyListeners(new AxisChangeEvent(this ));
211:                }
212:
213:            }
214:
215:            /**
216:             * Returns the number format override.  If this is non-null, then it will 
217:             * be used to format the numbers on the axis.
218:             *
219:             * @return The number formatter (possibly <code>null</code>).
220:             */
221:            public NumberFormat getNumberFormatOverride() {
222:                return this .numberFormatOverride;
223:            }
224:
225:            /**
226:             * Sets the number format override.  If this is non-null, then it will be 
227:             * used to format the numbers on the axis.
228:             *
229:             * @param formatter  the number formatter (<code>null</code> permitted).
230:             */
231:            public void setNumberFormatOverride(NumberFormat formatter) {
232:                this .numberFormatOverride = formatter;
233:                notifyListeners(new AxisChangeEvent(this ));
234:            }
235:
236:            /**
237:             * Returns the number of minor tick marks to display.
238:             * 
239:             * @return The number of minor tick marks to display.
240:             */
241:            public int getMinorTickCount() {
242:                return this .minorTickCount;
243:            }
244:
245:            /**
246:             * Sets the number of minor tick marks to display.
247:             * 
248:             * @param count  the count.
249:             */
250:            public void setMinorTickCount(int count) {
251:                if (count <= 0) {
252:                    throw new IllegalArgumentException("Requires 'count' > 0.");
253:                }
254:                this .minorTickCount = count;
255:                notifyListeners(new AxisChangeEvent(this ));
256:            }
257:
258:            /**
259:             * Calculates the log of the given value, using the current base.
260:             * 
261:             * @param value  the value.
262:             * 
263:             * @return The log of the given value.
264:             * 
265:             * @see #getBase()
266:             */
267:            public double calculateLog(double value) {
268:                return Math.log(value) / this .baseLog;
269:            }
270:
271:            /**
272:             * Calculates the value from a given log.
273:             * 
274:             * @param log  the log value (must be > 0.0).
275:             * 
276:             * @return The value with the given log.
277:             */
278:            public double calculateValue(double log) {
279:                return Math.pow(this .base, log);
280:            }
281:
282:            /**
283:             * Converts a Java2D coordinate to an axis value, assuming that the
284:             * axis covers the specified <code>edge</code> of the <code>area</code>.
285:             * 
286:             * @param java2DValue  the Java2D coordinate.
287:             * @param area  the area.
288:             * @param edge  the edge that the axis belongs to.
289:             * 
290:             * @return A value along the axis scale.
291:             */
292:            public double java2DToValue(double java2DValue, Rectangle2D area,
293:                    RectangleEdge edge) {
294:
295:                Range range = getRange();
296:                double axisMin = calculateLog(range.getLowerBound());
297:                double axisMax = calculateLog(range.getUpperBound());
298:
299:                double min = 0.0;
300:                double max = 0.0;
301:                if (RectangleEdge.isTopOrBottom(edge)) {
302:                    min = area.getX();
303:                    max = area.getMaxX();
304:                } else if (RectangleEdge.isLeftOrRight(edge)) {
305:                    min = area.getMaxY();
306:                    max = area.getY();
307:                }
308:                double log = 0.0;
309:                if (isInverted()) {
310:                    log = axisMax - (java2DValue - min) / (max - min)
311:                            * (axisMax - axisMin);
312:                } else {
313:                    log = axisMin + (java2DValue - min) / (max - min)
314:                            * (axisMax - axisMin);
315:                }
316:                return calculateValue(log);
317:            }
318:
319:            /**
320:             * Converts a value on the axis scale to a Java2D coordinate relative to 
321:             * the given <code>area</code>, based on the axis running along the 
322:             * specified <code>edge</code>.
323:             * 
324:             * @param value  the data value.
325:             * @param area  the area.
326:             * @param edge  the edge.
327:             * 
328:             * @return The Java2D coordinate corresponding to <code>value</code>.
329:             */
330:            public double valueToJava2D(double value, Rectangle2D area,
331:                    RectangleEdge edge) {
332:
333:                Range range = getRange();
334:                double axisMin = calculateLog(range.getLowerBound());
335:                double axisMax = calculateLog(range.getUpperBound());
336:                value = calculateLog(value);
337:
338:                double min = 0.0;
339:                double max = 0.0;
340:                if (RectangleEdge.isTopOrBottom(edge)) {
341:                    min = area.getX();
342:                    max = area.getMaxX();
343:                } else if (RectangleEdge.isLeftOrRight(edge)) {
344:                    max = area.getMinY();
345:                    min = area.getMaxY();
346:                }
347:                if (isInverted()) {
348:                    return max - ((value - axisMin) / (axisMax - axisMin))
349:                            * (max - min);
350:                } else {
351:                    return min + ((value - axisMin) / (axisMax - axisMin))
352:                            * (max - min);
353:                }
354:            }
355:
356:            /**
357:             * Configures the axis.  This method is typically called when an axis
358:             * is assigned to a new plot.
359:             */
360:            public void configure() {
361:                if (isAutoRange()) {
362:                    autoAdjustRange();
363:                }
364:            }
365:
366:            /**
367:             * Adjusts the axis range to match the data range that the axis is
368:             * required to display.
369:             */
370:            protected void autoAdjustRange() {
371:                Plot plot = getPlot();
372:                if (plot == null) {
373:                    return; // no plot, no data
374:                }
375:
376:                if (plot instanceof  ValueAxisPlot) {
377:                    ValueAxisPlot vap = (ValueAxisPlot) plot;
378:
379:                    Range r = vap.getDataRange(this );
380:                    if (r == null) {
381:                        r = getDefaultAutoRange();
382:                    }
383:
384:                    double upper = r.getUpperBound();
385:                    double lower = r.getLowerBound();
386:                    double range = upper - lower;
387:
388:                    // if fixed auto range, then derive lower bound...
389:                    double fixedAutoRange = getFixedAutoRange();
390:                    if (fixedAutoRange > 0.0) {
391:                        lower = Math.max(upper - fixedAutoRange,
392:                                this .smallestValue);
393:                    } else {
394:                        // ensure the autorange is at least <minRange> in size...
395:                        double minRange = getAutoRangeMinimumSize();
396:                        if (range < minRange) {
397:                            double expand = (minRange - range) / 2;
398:                            upper = upper + expand;
399:                            lower = lower - expand;
400:                        }
401:
402:                        // apply the margins - these should apply to the exponent range
403:                        //                upper = upper + getUpperMargin() * range;
404:                        //                lower = lower - getLowerMargin() * range;
405:                    }
406:
407:                    setRange(new Range(lower, upper), false, false);
408:                }
409:
410:            }
411:
412:            /**
413:             * Draws the axis on a Java 2D graphics device (such as the screen or a 
414:             * printer).
415:             *
416:             * @param g2  the graphics device (<code>null</code> not permitted).
417:             * @param cursor  the cursor location (determines where to draw the axis).
418:             * @param plotArea  the area within which the axes and plot should be drawn.
419:             * @param dataArea  the area within which the data should be drawn.
420:             * @param edge  the axis location (<code>null</code> not permitted).
421:             * @param plotState  collects information about the plot 
422:             *                   (<code>null</code> permitted).
423:             * 
424:             * @return The axis state (never <code>null</code>).
425:             */
426:            public AxisState draw(Graphics2D g2, double cursor,
427:                    Rectangle2D plotArea, Rectangle2D dataArea,
428:                    RectangleEdge edge, PlotRenderingInfo plotState) {
429:
430:                AxisState state = null;
431:                // if the axis is not visible, don't draw it...
432:                if (!isVisible()) {
433:                    state = new AxisState(cursor);
434:                    // even though the axis is not visible, we need ticks for the 
435:                    // gridlines...
436:                    List ticks = refreshTicks(g2, state, dataArea, edge);
437:                    state.setTicks(ticks);
438:                    return state;
439:                }
440:                state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea,
441:                        edge);
442:                state = drawLabel(getLabel(), g2, plotArea, dataArea, edge,
443:                        state);
444:                return state;
445:            }
446:
447:            /**
448:             * Calculates the positions of the tick labels for the axis, storing the 
449:             * results in the tick label list (ready for drawing).
450:             *
451:             * @param g2  the graphics device.
452:             * @param state  the axis state.
453:             * @param dataArea  the area in which the plot should be drawn.
454:             * @param edge  the location of the axis.
455:             * 
456:             * @return A list of ticks.
457:             *
458:             */
459:            public List refreshTicks(Graphics2D g2, AxisState state,
460:                    Rectangle2D dataArea, RectangleEdge edge) {
461:
462:                List result = new java.util.ArrayList();
463:                if (RectangleEdge.isTopOrBottom(edge)) {
464:                    result = refreshTicksHorizontal(g2, dataArea, edge);
465:                } else if (RectangleEdge.isLeftOrRight(edge)) {
466:                    result = refreshTicksVertical(g2, dataArea, edge);
467:                }
468:                return result;
469:
470:            }
471:
472:            /**
473:             * Returns a list of ticks for an axis at the top or bottom of the chart.
474:             * 
475:             * @param g2  the graphics device.
476:             * @param dataArea  the data area.
477:             * @param edge  the edge.
478:             * 
479:             * @return A list of ticks.
480:             */
481:            protected List refreshTicksHorizontal(Graphics2D g2,
482:                    Rectangle2D dataArea, RectangleEdge edge) {
483:                Range range = getRange();
484:                List ticks = new ArrayList();
485:                double start = Math.floor(calculateLog(getLowerBound()));
486:                double end = Math.ceil(calculateLog(getUpperBound()));
487:                double current = start;
488:                while (current <= end) {
489:                    double v = calculateValue(current);
490:                    if (range.contains(v)) {
491:                        ticks.add(new NumberTick(new Double(v),
492:                                createTickLabel(v), TextAnchor.TOP_CENTER,
493:                                TextAnchor.CENTER, 0.0));
494:                    }
495:                    // add minor ticks (for gridlines)
496:                    double next = Math.pow(this .base, current
497:                            + this .tickUnit.getSize());
498:                    for (int i = 1; i < this .minorTickCount; i++) {
499:                        double minorV = v + i
500:                                * ((next - v) / this .minorTickCount);
501:                        if (range.contains(minorV)) {
502:                            ticks.add(new NumberTick(new Double(minorV), "",
503:                                    TextAnchor.TOP_CENTER, TextAnchor.CENTER,
504:                                    0.0));
505:                        }
506:                    }
507:                    current = current + this .tickUnit.getSize();
508:                }
509:                return ticks;
510:            }
511:
512:            /**
513:             * Returns a list of ticks for an axis at the left or right of the chart.
514:             * 
515:             * @param g2  the graphics device.
516:             * @param dataArea  the data area.
517:             * @param edge  the edge.
518:             * 
519:             * @return A list of ticks.
520:             */
521:            protected List refreshTicksVertical(Graphics2D g2,
522:                    Rectangle2D dataArea, RectangleEdge edge) {
523:                Range range = getRange();
524:                List ticks = new ArrayList();
525:                double start = Math.floor(calculateLog(getLowerBound()));
526:                double end = Math.ceil(calculateLog(getUpperBound()));
527:                double current = start;
528:                while (current <= end) {
529:                    double v = calculateValue(current);
530:                    if (range.contains(v)) {
531:                        ticks.add(new NumberTick(new Double(v),
532:                                createTickLabel(v), TextAnchor.CENTER_RIGHT,
533:                                TextAnchor.CENTER, 0.0));
534:                    }
535:                    // add minor ticks (for gridlines)
536:                    double next = Math.pow(this .base, current
537:                            + this .tickUnit.getSize());
538:                    for (int i = 1; i < this .minorTickCount; i++) {
539:                        double minorV = v + i
540:                                * ((next - v) / this .minorTickCount);
541:                        if (range.contains(minorV)) {
542:                            ticks.add(new NumberTick(new Double(minorV), "",
543:                                    TextAnchor.CENTER_RIGHT, TextAnchor.CENTER,
544:                                    0.0));
545:                        }
546:                    }
547:                    current = current + this .tickUnit.getSize();
548:                }
549:                return ticks;
550:            }
551:
552:            /**
553:             * Creates a tick label for the specified value.
554:             * 
555:             * @param value  the value.
556:             * 
557:             * @return The label.
558:             */
559:            private String createTickLabel(double value) {
560:                if (this.numberFormatOverride != null) {
561:                    return this.numberFormatOverride.format(value);
562:                } else {
563:                    return this.tickUnit.valueToString(value);
564:                }
565:            }
566:
567:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.