1 23 24 package de.progra.charting.render; 25 26 import java.awt.font.FontRenderContext ; 27 import java.awt.font.TextLayout ; 28 import java.awt.geom.Ellipse2D ; 29 import java.awt.geom.Arc2D ; 30 import java.awt.geom.GeneralPath ; 31 import java.awt.geom.Line2D ; 32 33 import de.progra.charting.CoordSystem; 34 import java.awt.geom.AffineTransform ; 35 import de.progra.charting.PointToPixelTranslator; 36 37 import java.awt.Font ; 38 import java.awt.Graphics2D ; 39 import java.awt.Paint ; 40 import java.awt.RenderingHints ; 41 import java.awt.Color ; 42 import de.progra.charting.model.ChartDataModel; 43 44 49 public class RadarChartRenderer extends AbstractChartRenderer { 50 51 54 public RadarChartRenderer(ChartDataModel model) { 55 super(model); 56 } 57 58 62 public RadarChartRenderer(CoordSystem cs, ChartDataModel model) { 63 super(cs, model); 64 } 65 66 69 public void renderChart(Graphics2D g) { 70 Object rh = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 73 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 74 75 ChartDataModel m = getChartDataModel(); 77 RowColorModel rcm = getRowColorModel(); 78 79 double height = this.getBounds().getHeight(); 81 double width = this.getBounds().getWidth(); 82 83 int lNumberOfRows = m.getDataSetNumber(); 85 86 int lNumberOfColumns = Integer.MAX_VALUE; 88 for(int i = 0; i < lNumberOfRows; i++) 89 lNumberOfColumns = Math.min(lNumberOfColumns, m.getDataSetLength(i)); 90 91 double[] maxvalues = new double[lNumberOfColumns]; 92 93 double center_y = getBounds().getCenterY(); 95 double center_x = getBounds().getCenterX(); 96 97 double lRadius = Math.min(width * 0.9, height * 0.9) / 2; 99 100 for(int lRow = 0; lRow < lNumberOfRows; lRow++) 102 { 103 GeneralPath filledPolygon = new GeneralPath (GeneralPath.WIND_EVEN_ODD, lNumberOfColumns); 105 for (int lCol = 0; lCol < lNumberOfColumns; lCol++) 106 { 107 double lValue = m.getValueAt(lRow, lCol).doubleValue(); 109 110 double lMaxValue = maxvalues[lCol]; 112 113 if(lMaxValue == 0.0) { 114 for(int row = 0; row < lNumberOfRows; row++) 115 lMaxValue = Math.max(lMaxValue, m.getValueAt(row, lCol).doubleValue() * 1.1); 116 117 maxvalues[lCol] = lMaxValue; 118 } 119 120 double lScaledValue = lValue / lMaxValue; 121 double lLineValue = lRadius * lScaledValue; 122 123 double lRotation = (-1 * (2 * Math.PI / lNumberOfColumns) * lCol) + Math.PI; 127 128 double lX = center_x + (lLineValue * Math.sin(lRotation)); 130 double lY = center_y + (lLineValue * Math.cos(lRotation)); 131 132 Line2D lLine = new Line2D.Double (center_x, center_y, lX, lY); 134 g.setColor(Color.black); 135 g.draw(lLine); 136 137 if (lCol == 0) filledPolygon.moveTo((float)lX, (float)lY); 139 else filledPolygon.lineTo((float)lX, (float)lY); 140 } 141 142 filledPolygon.closePath(); 144 g.setPaint( rcm.getColor(lRow) ); 145 g.draw(filledPolygon); 146 } 147 148 double lRotation; 149 double lX; 150 double lY; 151 TextLayout lLabel; 152 153 for (int lCol = 0; lCol < lNumberOfColumns; lCol++) 155 { 156 lRotation = (-1 * (2 * Math.PI / lNumberOfColumns) * lCol) + Math.PI; 161 162 lX = center_x + (lRadius * Math.sin(lRotation)); 164 lY = center_y + (lRadius * Math.cos(lRotation)); 165 166 Line2D lLine = new Line2D.Double (center_x, center_y, lX, lY ); 168 g.setColor(Color.black); 169 g.draw(lLine); 170 171 lLabel = new TextLayout ("" + model.getColumnValueAt(lCol), new Font ("Courier", Font.BOLD, 9), new FontRenderContext (null, true, false)); 173 g.setColor(Color.black); 174 175 if ((lRotation <= Math.PI / 2) && (lRotation >= -Math.PI / 2)) 177 lY += lLabel.getBounds().getHeight(); 178 179 if (lRotation <= 0) 181 lX -= lLabel.getBounds().getWidth(); 182 183 lLabel.draw(g, (float)lX, (float)lY); 184 } 185 186 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, rh); 188 } 189 } 190 | Popular Tags |