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: PieChartRenderer.java
021: Created on 7. August 2001, 18:14
022: */
023:
024: package de.progra.charting.render;
025:
026: import java.awt.geom.Ellipse2D;
027: import java.awt.geom.Arc2D;
028: import de.progra.charting.CoordSystem;
029: import java.awt.geom.AffineTransform;
030: import de.progra.charting.PointToPixelTranslator;
031: import java.awt.Graphics2D;
032: import java.awt.RenderingHints;
033: import java.awt.Color;
034: import de.progra.charting.model.ChartDataModel;
035:
036: /**
037: * This renderer creates a PieChart.
038: * @author mueller
039: * @version 1.0
040: */
041: public class PieChartRenderer extends AbstractChartRenderer {
042:
043: /** Creates new PieChartRenderer
044: * @param model the DataModel that should be rendered
045: */
046: public PieChartRenderer(ChartDataModel model) {
047: super (model);
048: }
049:
050: /** Creates new PieChartRenderer
051: * @param cs the CoordSystem used to translate values into points
052: * @param model the DataModel that should be rendered
053: */
054: public PieChartRenderer(CoordSystem cs, ChartDataModel model) {
055: super (cs, model);
056: }
057:
058: /** Finally renders the Object in the Graphics object.
059: * @param g the Graphics2D object in which to render
060: */
061: public void renderChart(Graphics2D g) {
062: Object rh = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
063: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
064: RenderingHints.VALUE_ANTIALIAS_ON);
065: ChartDataModel m = getChartDataModel();
066: RowColorModel rcm = getRowColorModel();
067:
068: double height = this .getBounds().getHeight();
069: double width = this .getBounds().getWidth();
070:
071: int datenreihen = m.getDataSetNumber();
072: // determine shortest dataset length
073: int min_length = Integer.MAX_VALUE;
074:
075: for (int i = 0; i < datenreihen; i++)
076: min_length = Math.min(min_length, m.getDataSetLength(i));
077:
078: double center_y = getBounds().getCenterY();
079: double center_x = getBounds().getCenterX();
080:
081: double rad = Math.min(width * 0.9, height * 0.9);
082: double modelVal = 0.0;
083: for (int reihe = min_length; reihe >= 1; reihe--) {
084:
085: double kreis = (double) rad / min_length * reihe;
086: Ellipse2D.Double circle = new Ellipse2D.Double(
087: (double) center_x - kreis / 2, (double) center_y
088: - kreis / 2, kreis, kreis);
089:
090: double sum = 0;
091: double start = 0.0;
092:
093: // Paint data
094: for (int i = 0; i < datenreihen; i++) {
095: modelVal = m.getValueAt(i, reihe - 1).doubleValue();
096:
097: // Catch modelVal == Not A Number
098: if (modelVal != modelVal)
099: continue;
100: sum += modelVal;
101: }
102:
103: for (int i = 0; i < datenreihen; i++) {
104: double value = m.getValueAt(i, reihe - 1).doubleValue();
105:
106: // Catch value == Not A Number
107: if (value != value)
108: value = 0.0;
109:
110: Arc2D.Double arc = new Arc2D.Double(circle
111: .getBounds2D(), (double) start, 360.0
112: * (double) value / (double) sum, Arc2D.PIE);
113: start += 360 * (double) value / (double) sum;
114:
115: g.setColor(rcm.getColor(i));
116: g.fill(arc);
117: }
118: g.setColor(Color.black);
119: g.draw(circle);
120: }
121:
122: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, rh);
123: }
124: }
|