1 46 47 package org.jfree.chart.plot; 48 49 import java.awt.AlphaComposite ; 50 import java.awt.Color ; 51 import java.awt.Composite ; 52 import java.awt.Graphics2D ; 53 import java.awt.Insets ; 54 import java.awt.Paint ; 55 import java.awt.Shape ; 56 import java.awt.geom.Rectangle2D ; 57 import java.util.ResourceBundle ; 58 59 import org.jfree.chart.ChartRenderingInfo; 60 import org.jfree.chart.LegendItemCollection; 61 import org.jfree.chart.axis.AxisSpace; 62 import org.jfree.chart.axis.ValueAxis; 63 import org.jfree.chart.renderer.AbstractRenderer; 64 import org.jfree.data.XYDataset; 65 66 71 public class PeriodMarkerPlot extends XYPlot implements ValueAxisPlot { 72 73 74 static protected ResourceBundle localizationResources = 75 ResourceBundle.getBundle("org.jfree.chart.plot.LocalizationBundle"); 76 77 84 public PeriodMarkerPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis) { 85 super(data, domainAxis, rangeAxis, null); 86 } 87 88 93 public LegendItemCollection getLegendItems() { 94 return null; 95 } 96 97 102 public String getPlotType() { 103 return localizationResources.getString("Period_Marker_Plot"); 104 } 105 106 111 public XYDataset getTempXYDataset() { 112 return getDataset(); 114 } 115 116 123 public void draw(Graphics2D g2, Rectangle2D plotArea, ChartRenderingInfo info) { 124 125 Insets insets = getInsets(); 126 if (insets != null) { 127 plotArea = new Rectangle2D.Double (plotArea.getX() + insets.left, 128 plotArea.getY() + insets.top, 129 plotArea.getWidth() - insets.left - insets.right, 130 plotArea.getHeight() - insets.top - insets.bottom); 131 } 132 133 AxisSpace space = new AxisSpace(); 134 ValueAxis domainAxis = getDomainAxis(); 135 ValueAxis rangeAxis = getRangeAxis(); 136 space = domainAxis.reserveSpace(g2, this, plotArea, getDomainAxisEdge(), space); 137 space = rangeAxis.reserveSpace(g2, this, plotArea, getRangeAxisEdge(), space); 138 Rectangle2D dataArea = space.shrink(plotArea, null); 139 140 drawBackground(g2, dataArea); 141 142 double cursor = 0.0; 144 getDomainAxis().draw(g2, cursor, plotArea, dataArea, getDomainAxisEdge()); 145 getRangeAxis().draw(g2, cursor, plotArea, dataArea, getRangeAxisEdge()); 146 147 Shape originalClip = g2.getClip(); 148 g2.clip(dataArea); 149 150 XYDataset data = getTempXYDataset(); 152 if (data != null) { 153 int seriesCount = data.getSeriesCount(); 154 for (int serie = 0; serie < seriesCount; serie++) { 155 drawMarkedPeriods(data, serie, g2, dataArea); 157 } 158 } 159 160 drawOutline(g2, dataArea); 161 g2.setClip(originalClip); 162 } 163 164 172 private void drawMarkedPeriods(XYDataset data, int serie, Graphics2D g2, Rectangle2D plotArea) { 173 174 Paint thisSeriePaint = Color.gray; g2.setPaint(thisSeriePaint); 176 g2.setStroke(AbstractRenderer.DEFAULT_STROKE); 178 float opacity = 0.1f; 179 if (thisSeriePaint instanceof Color ) { 180 Color thisSerieColor = (Color ) thisSeriePaint; 181 int colorSaturation = thisSerieColor.getRed() 182 + thisSerieColor.getGreen() + thisSerieColor.getBlue(); 183 if (colorSaturation > 255) { 184 opacity = opacity * colorSaturation / 255.0f; 185 } 186 } 187 Composite originalComposite = g2.getComposite(); 188 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity)); 189 190 double minY = plotArea.getMinY(); 191 double maxY = plotArea.getMaxY(); 192 193 int itemCount = data.getItemCount(serie); 194 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) { 195 if (data.getYValue(serie, itemIndex).doubleValue() == 0) { 196 continue; 198 } 199 Number xStart; 200 if (itemIndex > 0) { 201 xStart = new Long ((data.getXValue(serie, itemIndex).longValue() 202 + data.getXValue(serie, itemIndex - 1).longValue()) / 2); 203 } 204 else { 205 xStart = data.getXValue(serie, itemIndex); 206 } 207 int j = itemIndex + 1; 208 while (j < itemCount) { 209 if (data.getYValue(serie, j).doubleValue() == 0) { 210 break; 211 } 212 j++; 213 } 214 itemIndex = j; 215 Number xEnd; 216 if (j < itemCount) { 217 xEnd = new Long ((data.getXValue(serie, j - 1).longValue() 218 + data.getXValue(serie, j).longValue()) / 2); 219 } 220 else { 221 xEnd = data.getXValue(serie, j - 1); 222 } 223 224 double xxStart = getDomainAxis().translateValueToJava2D(xStart.doubleValue(), plotArea, 225 getDomainAxisEdge()); 226 double xxEnd = getDomainAxis().translateValueToJava2D(xEnd.doubleValue(), plotArea, 227 getDomainAxisEdge()); 228 229 markPeriod(xxStart, xxEnd, minY, maxY, g2); 230 } 231 232 g2.setComposite(originalComposite); 233 234 } 235 236 245 private void markPeriod(double xStart, double xEnd, double minY, double maxY, Graphics2D g2) { 246 g2.fill(new Rectangle2D.Double (xStart, minY, xEnd - xStart, maxY - minY)); 247 } 248 249 254 public void zoom(double percent) { 255 } 256 257 } 258 | Popular Tags |