001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: DefaultChart.java
021: Created on 26. Juni 2001, 22:49
022: */
023:
024: package de.progra.charting;
025:
026: import de.progra.charting.model.ChartDataModel;
027: import java.util.*;
028: import de.progra.charting.render.AbstractChartRenderer;
029: import de.progra.charting.render.RowColorModel;
030: import java.awt.geom.AffineTransform;
031: import java.awt.Rectangle;
032: import java.awt.Graphics2D;
033: import java.awt.Dimension;
034: import java.awt.Color;
035:
036: /** The Default class to create a chart.
037: * @author mueller
038: * @version 1.0
039: */
040: public class DefaultChart extends AbstractChart {
041:
042: public static int LINEAR_X_LINEAR_Y = 0;
043: public static int NO_COORDINATE_SYSTEM = 1;
044:
045: /** Creates new empty DefaultChart.*/
046: protected DefaultChart() {
047: }
048:
049: /** Creates a new DefaultChart with the given model
050: * and title string and no coordinate system.
051: * @param model the ChartDataModel
052: * @param title the title String
053: */
054: public DefaultChart(ChartDataModel model, String title) {
055: this ();
056: setChartDataModel(model);
057: setRowColorModel(new RowColorModel(model));
058:
059: setLegend(new Legend(getRowColorModel()));
060: setTitle(new Title(title));
061: }
062:
063: /** Creates a new DefaultChart with the given model
064: * and title string and a coordinate system.
065: * @param model the ChartDataModel
066: * @param title the title String
067: * @param coord the id of the coordinate system configuration
068: */
069: public DefaultChart(ChartDataModel model, String title, int coord) {
070: this (model, title);
071:
072: if (coord == this .LINEAR_X_LINEAR_Y)
073: this .setCoordSystem(new CoordSystem(model));
074: }
075:
076: /** Creates a new DefaultChart with the given model
077: * and title string and a coordinate system.
078: * @param model the ChartDataModel
079: * @param title the title String
080: * @param coord the id of the coordinate system configuration
081: * @param xaxis the x-axis' unit
082: * @param yaxis the y-axis' unit
083: */
084: public DefaultChart(ChartDataModel model, String title, int coord,
085: String xaxis, String yaxis) {
086: this (model, title, coord);
087:
088: this .getCoordSystem().setXAxisUnit(xaxis);
089: this .getCoordSystem().setYAxisUnit(yaxis);
090: }
091:
092: /** Should compute the preferred size of the Chart
093: * @return <CODE>null</CODE>
094: */
095: public Dimension getPreferredSize() {
096: return null;
097: }
098:
099: /** Does the layout of the title, legend and coordinate system and
100: * calls the render method of all those including the ChartRenderers.
101: * @param g the <CODE>Graphics2D</CODE> object to paint in.
102: */
103: public void render(Graphics2D g) {
104:
105: //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
106: // RenderingHints.VALUE_ANTIALIAS_ON);
107:
108: int width = (int) getBounds().getWidth();
109: int height = (int) getBounds().getHeight();
110:
111: Title t = getTitle();
112: Legend l = getLegend();
113: CoordSystem c = getCoordSystem();
114: Collection renderer = getChartRenderer().values();
115:
116: g.setColor(Color.white);
117: g.fillRect(0, 0, width, height);
118:
119: g.setColor(Color.black);
120:
121: int titleheight = 0;
122: int legendwidth = 0;
123:
124: if (t != null) {
125: Dimension title = t.getPreferredSize();
126: t.setBounds(new Rectangle((int) (width / 2 - (int) (title
127: .getWidth() / 2)), 0, (int) title.getWidth(),
128: (int) title.getHeight()));
129: t.render(g);
130: titleheight = (int) t.getBounds().getHeight();
131: }
132: if (l != null) {
133: Dimension legend = l.getPreferredSize();
134: l
135: .setBounds(new Rectangle((int) (width - legend
136: .getWidth()), (int) (height / 2
137: - legend.getHeight() / 2 + titleheight),
138: (int) legend.getWidth(), (int) legend
139: .getHeight()));
140: l.render(g);
141: legendwidth = (int) l.getBounds().getWidth();
142: }
143: if (c != null) {
144: c.setBounds(new Rectangle(0, (int) titleheight,
145: (int) (width - legendwidth),
146: (int) (height - titleheight)));
147: }
148: if (!renderer.isEmpty()) {
149: Iterator i = renderer.iterator();
150: while (i.hasNext()) {
151: AbstractChartRenderer cr = (AbstractChartRenderer) i
152: .next();
153: cr.setBounds(new Rectangle(0, (int) titleheight,
154: (int) (width - legendwidth),
155: (int) (height - titleheight) - 5));
156:
157: // cr.setPointToPixelTranslator()
158:
159: cr.render(g);
160: }
161: }
162: if (c != null)
163: c.render(g);
164:
165: /*
166: g.setColor(Color.pink);
167:
168: g.draw(t.getBounds());
169: g.draw(l.getBounds());
170: g.draw(c.getBounds());
171: g.draw(c.getInnerBounds());
172: */
173: //System.out.println("** Bounds: "+c.getBounds());
174: //System.out.println("** InnerBounds: "+c.getInnerBounds());
175: }
176:
177: }
|