use of org.jfree.chart.util.RectangleInsets in project graphcode2vec by graphcode2vec.
the class MeterPlot method draw.
/**
* Draws the plot on a Java 2D graphics device (such as the screen or a
* printer).
*
* @param g2 the graphics device.
* @param area the area within which the plot should be drawn.
* @param anchor the anchor point (<code>null</code> permitted).
* @param parentState the state from the parent plot, if there is one.
* @param info collects info about the drawing.
*/
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
if (info != null) {
info.setPlotArea(area);
}
// adjust for insets...
RectangleInsets insets = getInsets();
insets.trim(area);
area.setRect(area.getX() + 4, area.getY() + 4, area.getWidth() - 8, area.getHeight() - 8);
// draw the background
if (this.drawBorder) {
drawBackground(g2, area);
}
// adjust the plot area by the interior spacing value
double gapHorizontal = (2 * DEFAULT_BORDER_SIZE);
double gapVertical = (2 * DEFAULT_BORDER_SIZE);
double meterX = area.getX() + gapHorizontal / 2;
double meterY = area.getY() + gapVertical / 2;
double meterW = area.getWidth() - gapHorizontal;
double meterH = area.getHeight() - gapVertical + ((this.meterAngle <= 180) && (this.shape != DialShape.CIRCLE) ? area.getHeight() / 1.25 : 0);
double min = Math.min(meterW, meterH) / 2;
meterX = (meterX + meterX + meterW) / 2 - min;
meterY = (meterY + meterY + meterH) / 2 - min;
meterW = 2 * min;
meterH = 2 * min;
Rectangle2D meterArea = new Rectangle2D.Double(meterX, meterY, meterW, meterH);
Rectangle2D.Double originalArea = new Rectangle2D.Double(meterArea.getX() - 4, meterArea.getY() - 4, meterArea.getWidth() + 8, meterArea.getHeight() + 8);
double meterMiddleX = meterArea.getCenterX();
double meterMiddleY = meterArea.getCenterY();
// plot the data (unless the dataset is null)...
ValueDataset data = getDataset();
if (data != null) {
double dataMin = this.range.getLowerBound();
double dataMax = this.range.getUpperBound();
Shape savedClip = g2.getClip();
g2.clip(originalArea);
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
if (this.dialBackgroundPaint != null) {
fillArc(g2, originalArea, dataMin, dataMax, this.dialBackgroundPaint, true);
}
drawTicks(g2, meterArea, dataMin, dataMax);
drawArcForInterval(g2, meterArea, new MeterInterval("", this.range, this.dialOutlinePaint, new BasicStroke(1.0f), null));
Iterator iterator = this.intervals.iterator();
while (iterator.hasNext()) {
MeterInterval interval = (MeterInterval) iterator.next();
drawArcForInterval(g2, meterArea, interval);
}
Number n = data.getValue();
if (n != null) {
double value = n.doubleValue();
drawValueLabel(g2, meterArea);
if (this.range.contains(value)) {
g2.setPaint(this.needlePaint);
g2.setStroke(new BasicStroke(2.0f));
double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE + 15;
double valueAngle = valueToAngle(value);
double valueP1 = meterMiddleX + (radius * Math.cos(Math.PI * (valueAngle / 180)));
double valueP2 = meterMiddleY - (radius * Math.sin(Math.PI * (valueAngle / 180)));
Polygon arrow = new Polygon();
if ((valueAngle > 135 && valueAngle < 225) || (valueAngle < 45 && valueAngle > -45)) {
double valueP3 = (meterMiddleY - DEFAULT_CIRCLE_SIZE / 4);
double valueP4 = (meterMiddleY + DEFAULT_CIRCLE_SIZE / 4);
arrow.addPoint((int) meterMiddleX, (int) valueP3);
arrow.addPoint((int) meterMiddleX, (int) valueP4);
} else {
arrow.addPoint((int) (meterMiddleX - DEFAULT_CIRCLE_SIZE / 4), (int) meterMiddleY);
arrow.addPoint((int) (meterMiddleX + DEFAULT_CIRCLE_SIZE / 4), (int) meterMiddleY);
}
arrow.addPoint((int) valueP1, (int) valueP2);
g2.fill(arrow);
Ellipse2D circle = new Ellipse2D.Double(meterMiddleX - DEFAULT_CIRCLE_SIZE / 2, meterMiddleY - DEFAULT_CIRCLE_SIZE / 2, DEFAULT_CIRCLE_SIZE, DEFAULT_CIRCLE_SIZE);
g2.fill(circle);
}
}
g2.setClip(savedClip);
g2.setComposite(originalComposite);
}
if (this.drawBorder) {
drawOutline(g2, area);
}
}
use of org.jfree.chart.util.RectangleInsets in project graphcode2vec by graphcode2vec.
the class MultiplePiePlot method draw.
/**
* Draws the plot on a Java 2D graphics device (such as the screen or a
* printer).
*
* @param g2 the graphics device.
* @param area the area within which the plot should be drawn.
* @param anchor the anchor point (<code>null</code> permitted).
* @param parentState the state from the parent plot, if there is one.
* @param info collects info about the drawing.
*/
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
// adjust the drawing area for the plot insets (if any)...
RectangleInsets insets = getInsets();
insets.trim(area);
drawBackground(g2, area);
drawOutline(g2, area);
// check that there is some data to display...
if (DatasetUtilities.isEmptyOrNull(this.dataset)) {
drawNoDataMessage(g2, area);
return;
}
int pieCount = 0;
if (this.dataExtractOrder == TableOrder.BY_ROW) {
pieCount = this.dataset.getRowCount();
} else {
pieCount = this.dataset.getColumnCount();
}
// the columns variable is always >= rows
int displayCols = (int) Math.ceil(Math.sqrt(pieCount));
int displayRows = (int) Math.ceil((double) pieCount / (double) displayCols);
// swap rows and columns to match plotArea shape
if (displayCols > displayRows && area.getWidth() < area.getHeight()) {
int temp = displayCols;
displayCols = displayRows;
displayRows = temp;
}
prefetchSectionPaints();
int x = (int) area.getX();
int y = (int) area.getY();
int width = ((int) area.getWidth()) / displayCols;
int height = ((int) area.getHeight()) / displayRows;
int row = 0;
int column = 0;
int diff = (displayRows * displayCols) - pieCount;
int xoffset = 0;
Rectangle rect = new Rectangle();
for (int pieIndex = 0; pieIndex < pieCount; pieIndex++) {
rect.setBounds(x + xoffset + (width * column), y + (height * row), width, height);
String title = null;
if (this.dataExtractOrder == TableOrder.BY_ROW) {
title = this.dataset.getRowKey(pieIndex).toString();
} else {
title = this.dataset.getColumnKey(pieIndex).toString();
}
this.pieChart.setTitle(title);
PieDataset piedataset = null;
PieDataset dd = new CategoryToPieDataset(this.dataset, this.dataExtractOrder, pieIndex);
if (this.limit > 0.0) {
piedataset = DatasetUtilities.createConsolidatedPieDataset(dd, this.aggregatedItemsKey, this.limit);
} else {
piedataset = dd;
}
PiePlot piePlot = (PiePlot) this.pieChart.getPlot();
piePlot.setDataset(piedataset);
piePlot.setPieIndex(pieIndex);
// update the section colors to match the global colors...
for (int i = 0; i < piedataset.getItemCount(); i++) {
Comparable key = piedataset.getKey(i);
Paint p;
if (key.equals(this.aggregatedItemsKey)) {
p = this.aggregatedItemsPaint;
} else {
p = (Paint) this.sectionPaints.get(key);
}
piePlot.setSectionPaint(key, p);
}
ChartRenderingInfo subinfo = null;
if (info != null) {
subinfo = new ChartRenderingInfo();
}
this.pieChart.draw(g2, rect, subinfo);
if (info != null) {
info.getOwner().getEntityCollection().addAll(subinfo.getEntityCollection());
info.addSubplotInfo(subinfo.getPlotInfo());
}
++column;
if (column == displayCols) {
column = 0;
++row;
if (row == displayRows - 1 && diff != 0) {
xoffset = (diff * width) / 2;
}
}
}
}
use of org.jfree.chart.util.RectangleInsets in project graphcode2vec by graphcode2vec.
the class PiePlot3D method draw.
/**
* Draws the plot on a Java 2D graphics device (such as the screen or a
* printer). This method is called by the
* {@link org.jfree.chart.JFreeChart} class, you don't normally need
* to call it yourself.
*
* @param g2 the graphics device.
* @param plotArea the area within which the plot should be drawn.
* @param anchor the anchor point.
* @param parentState the state from the parent plot, if there is one.
* @param info collects info about the drawing
* (<code>null</code> permitted).
*/
public void draw(Graphics2D g2, Rectangle2D plotArea, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
// adjust for insets...
RectangleInsets insets = getInsets();
insets.trim(plotArea);
Rectangle2D originalPlotArea = (Rectangle2D) plotArea.clone();
if (info != null) {
info.setPlotArea(plotArea);
info.setDataArea(plotArea);
}
drawBackground(g2, plotArea);
Shape savedClip = g2.getClip();
g2.clip(plotArea);
// adjust the plot area by the interior spacing value
double gapPercent = getInteriorGap();
double labelPercent = 0.0;
if (getLabelGenerator() != null) {
labelPercent = getLabelGap() + getMaximumLabelWidth();
}
double gapHorizontal = plotArea.getWidth() * (gapPercent + labelPercent) * 2.0;
double gapVertical = plotArea.getHeight() * gapPercent * 2.0;
if (DEBUG_DRAW_INTERIOR) {
double hGap = plotArea.getWidth() * getInteriorGap();
double vGap = plotArea.getHeight() * getInteriorGap();
double igx1 = plotArea.getX() + hGap;
double igx2 = plotArea.getMaxX() - hGap;
double igy1 = plotArea.getY() + vGap;
double igy2 = plotArea.getMaxY() - vGap;
g2.setPaint(Color.lightGray);
g2.draw(new Rectangle2D.Double(igx1, igy1, igx2 - igx1, igy2 - igy1));
}
double linkX = plotArea.getX() + gapHorizontal / 2;
double linkY = plotArea.getY() + gapVertical / 2;
double linkW = plotArea.getWidth() - gapHorizontal;
double linkH = plotArea.getHeight() - gapVertical;
// make the link area a square if the pie chart is to be circular...
if (isCircular()) {
// is circular?
double min = Math.min(linkW, linkH) / 2;
linkX = (linkX + linkX + linkW) / 2 - min;
linkY = (linkY + linkY + linkH) / 2 - min;
linkW = 2 * min;
linkH = 2 * min;
}
PiePlotState state = initialise(g2, plotArea, this, null, info);
// the link area defines the dog leg points for the linking lines to
// the labels
Rectangle2D linkAreaXX = new Rectangle2D.Double(linkX, linkY, linkW, linkH * (1 - this.depthFactor));
state.setLinkArea(linkAreaXX);
if (DEBUG_DRAW_LINK_AREA) {
g2.setPaint(Color.blue);
g2.draw(linkAreaXX);
g2.setPaint(Color.yellow);
g2.draw(new Ellipse2D.Double(linkAreaXX.getX(), linkAreaXX.getY(), linkAreaXX.getWidth(), linkAreaXX.getHeight()));
}
// the explode area defines the max circle/ellipse for the exploded pie
// sections.
// it is defined by shrinking the linkArea by the linkMargin factor.
double hh = linkW * getLabelLinkMargin();
double vv = linkH * getLabelLinkMargin();
Rectangle2D explodeArea = new Rectangle2D.Double(linkX + hh / 2.0, linkY + vv / 2.0, linkW - hh, linkH - vv);
state.setExplodedPieArea(explodeArea);
// the pie area defines the circle/ellipse for regular pie sections.
// it is defined by shrinking the explodeArea by the explodeMargin
// factor.
double maximumExplodePercent = getMaximumExplodePercent();
double percent = maximumExplodePercent / (1.0 + maximumExplodePercent);
double h1 = explodeArea.getWidth() * percent;
double v1 = explodeArea.getHeight() * percent;
Rectangle2D pieArea = new Rectangle2D.Double(explodeArea.getX() + h1 / 2.0, explodeArea.getY() + v1 / 2.0, explodeArea.getWidth() - h1, explodeArea.getHeight() - v1);
// the link area defines the dog-leg point for the linking lines to
// the labels
int depth = (int) (pieArea.getHeight() * this.depthFactor);
Rectangle2D linkArea = new Rectangle2D.Double(linkX, linkY, linkW, linkH - depth);
state.setLinkArea(linkArea);
state.setPieArea(pieArea);
state.setPieCenterX(pieArea.getCenterX());
state.setPieCenterY(pieArea.getCenterY() - depth / 2.0);
state.setPieWRadius(pieArea.getWidth() / 2.0);
state.setPieHRadius((pieArea.getHeight() - depth) / 2.0);
// get the data source - return if null;
PieDataset dataset = getDataset();
if (DatasetUtilities.isEmptyOrNull(getDataset())) {
drawNoDataMessage(g2, plotArea);
g2.setClip(savedClip);
drawOutline(g2, plotArea);
return;
}
// if too any elements
if (dataset.getKeys().size() > plotArea.getWidth()) {
String text = "Too many elements";
Font sfont = new Font("Tahoma", Font.BOLD, 10);
g2.setFont(sfont);
FontMetrics fm = g2.getFontMetrics(sfont);
int stringWidth = fm.stringWidth(text);
g2.drawString(text, (int) (plotArea.getX() + (plotArea.getWidth() - stringWidth) / 2), (int) (plotArea.getY() + (plotArea.getHeight() / 2)));
return;
}
// effect.
if (isCircular()) {
double min = Math.min(plotArea.getWidth(), plotArea.getHeight()) / 2;
plotArea = new Rectangle2D.Double(plotArea.getCenterX() - min, plotArea.getCenterY() - min, 2 * min, 2 * min);
}
// get a list of keys...
List sectionKeys = dataset.getKeys();
if (sectionKeys.size() == 0) {
return;
}
// establish the coordinates of the top left corner of the drawing area
double arcX = pieArea.getX();
double arcY = pieArea.getY();
// g2.clip(clipArea);
Composite originalComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
double totalValue = DatasetUtilities.calculatePieDatasetTotal(dataset);
double runningTotal = 0;
if (depth < 0) {
// if depth is negative don't draw anything
return;
}
ArrayList arcList = new ArrayList();
Arc2D.Double arc;
Paint paint;
Paint outlinePaint;
Stroke outlineStroke;
Iterator iterator = sectionKeys.iterator();
while (iterator.hasNext()) {
Comparable currentKey = (Comparable) iterator.next();
Number dataValue = dataset.getValue(currentKey);
if (dataValue == null) {
arcList.add(null);
continue;
}
double value = dataValue.doubleValue();
if (value <= 0) {
arcList.add(null);
continue;
}
double startAngle = getStartAngle();
double direction = getDirection().getFactor();
double angle1 = startAngle + (direction * (runningTotal * 360)) / totalValue;
double angle2 = startAngle + (direction * (runningTotal + value) * 360) / totalValue;
if (Math.abs(angle2 - angle1) > getMinimumArcAngleToDraw()) {
arcList.add(new Arc2D.Double(arcX, arcY + depth, pieArea.getWidth(), pieArea.getHeight() - depth, angle1, angle2 - angle1, Arc2D.PIE));
} else {
arcList.add(null);
}
runningTotal += value;
}
Shape oldClip = g2.getClip();
Ellipse2D top = new Ellipse2D.Double(pieArea.getX(), pieArea.getY(), pieArea.getWidth(), pieArea.getHeight() - depth);
Ellipse2D bottom = new Ellipse2D.Double(pieArea.getX(), pieArea.getY() + depth, pieArea.getWidth(), pieArea.getHeight() - depth);
Rectangle2D lower = new Rectangle2D.Double(top.getX(), top.getCenterY(), pieArea.getWidth(), bottom.getMaxY() - top.getCenterY());
Rectangle2D upper = new Rectangle2D.Double(pieArea.getX(), top.getY(), pieArea.getWidth(), bottom.getCenterY() - top.getY());
Area a = new Area(top);
a.add(new Area(lower));
Area b = new Area(bottom);
b.add(new Area(upper));
Area pie = new Area(a);
pie.intersect(b);
Area front = new Area(pie);
front.subtract(new Area(top));
Area back = new Area(pie);
back.subtract(new Area(bottom));
// draw the bottom circle
int[] xs;
int[] ys;
arc = new Arc2D.Double(arcX, arcY + depth, pieArea.getWidth(), pieArea.getHeight() - depth, 0, 360, Arc2D.PIE);
int categoryCount = arcList.size();
for (int categoryIndex = 0; categoryIndex < categoryCount; categoryIndex++) {
arc = (Arc2D.Double) arcList.get(categoryIndex);
if (arc == null) {
continue;
}
Comparable key = getSectionKey(categoryIndex);
paint = lookupSectionPaint(key, false);
outlinePaint = lookupSectionOutlinePaint(key, false);
outlineStroke = lookupSectionOutlineStroke(key, false);
g2.setPaint(paint);
g2.fill(arc);
g2.setPaint(outlinePaint);
g2.setStroke(outlineStroke);
g2.draw(arc);
g2.setPaint(paint);
Point2D p1 = arc.getStartPoint();
// draw the height
xs = new int[] { (int) arc.getCenterX(), (int) arc.getCenterX(), (int) p1.getX(), (int) p1.getX() };
ys = new int[] { (int) arc.getCenterY(), (int) arc.getCenterY() - depth, (int) p1.getY() - depth, (int) p1.getY() };
Polygon polygon = new Polygon(xs, ys, 4);
g2.setPaint(java.awt.Color.lightGray);
g2.fill(polygon);
g2.setPaint(outlinePaint);
g2.setStroke(outlineStroke);
g2.draw(polygon);
g2.setPaint(paint);
}
g2.setPaint(Color.gray);
g2.fill(back);
g2.fill(front);
// cycle through once drawing only the sides at the back...
int cat = 0;
iterator = arcList.iterator();
while (iterator.hasNext()) {
Arc2D segment = (Arc2D) iterator.next();
if (segment != null) {
Comparable key = getSectionKey(cat);
paint = lookupSectionPaint(key, false);
outlinePaint = lookupSectionOutlinePaint(key, false);
outlineStroke = lookupSectionOutlineStroke(key, false);
drawSide(g2, pieArea, segment, front, back, paint, outlinePaint, outlineStroke, false, true);
}
cat++;
}
// cycle through again drawing only the sides at the front...
cat = 0;
iterator = arcList.iterator();
while (iterator.hasNext()) {
Arc2D segment = (Arc2D) iterator.next();
if (segment != null) {
Comparable key = getSectionKey(cat);
paint = lookupSectionPaint(key, false);
outlinePaint = lookupSectionOutlinePaint(key, false);
outlineStroke = lookupSectionOutlineStroke(key, false);
drawSide(g2, pieArea, segment, front, back, paint, outlinePaint, outlineStroke, true, false);
}
cat++;
}
g2.setClip(oldClip);
// draw the sections at the top of the pie (and set up tooltips)...
Arc2D upperArc;
for (int sectionIndex = 0; sectionIndex < categoryCount; sectionIndex++) {
arc = (Arc2D.Double) arcList.get(sectionIndex);
if (arc == null) {
continue;
}
upperArc = new Arc2D.Double(arcX, arcY, pieArea.getWidth(), pieArea.getHeight() - depth, arc.getAngleStart(), arc.getAngleExtent(), Arc2D.PIE);
Comparable currentKey = (Comparable) sectionKeys.get(sectionIndex);
paint = lookupSectionPaint(currentKey, true);
outlinePaint = lookupSectionOutlinePaint(currentKey, false);
outlineStroke = lookupSectionOutlineStroke(currentKey, false);
g2.setPaint(paint);
g2.fill(upperArc);
g2.setStroke(outlineStroke);
g2.setPaint(outlinePaint);
g2.draw(upperArc);
// add a tooltip for the section...
if (info != null) {
EntityCollection entities = info.getOwner().getEntityCollection();
if (entities != null) {
String tip = null;
PieToolTipGenerator tipster = getToolTipGenerator();
if (tipster != null) {
// @mgs: using the method's return value was missing
tip = tipster.generateToolTip(dataset, currentKey);
}
String url = null;
if (getURLGenerator() != null) {
url = getURLGenerator().generateURL(dataset, currentKey, getPieIndex());
}
PieSectionEntity entity = new PieSectionEntity(upperArc, dataset, getPieIndex(), sectionIndex, currentKey, tip, url);
entities.add(entity);
}
}
}
List keys = dataset.getKeys();
Rectangle2D adjustedPlotArea = new Rectangle2D.Double(originalPlotArea.getX(), originalPlotArea.getY(), originalPlotArea.getWidth(), originalPlotArea.getHeight() - depth);
if (getSimpleLabels()) {
drawSimpleLabels(g2, keys, totalValue, adjustedPlotArea, linkArea, state);
} else {
drawLabels(g2, keys, totalValue, adjustedPlotArea, linkArea, state);
}
g2.setClip(savedClip);
g2.setComposite(originalComposite);
drawOutline(g2, originalPlotArea);
}
use of org.jfree.chart.util.RectangleInsets in project graphcode2vec by graphcode2vec.
the class PolarPlot method draw.
/**
* Draws the plot on a Java 2D graphics device (such as the screen or a
* printer).
* <P>
* This plot relies on a {@link PolarItemRenderer} to draw each
* item in the plot. This allows the visual representation of the data to
* be changed easily.
* <P>
* The optional info argument collects information about the rendering of
* the plot (dimensions, tooltip information etc). Just pass in
* <code>null</code> if you do not need this information.
*
* @param g2 the graphics device.
* @param area the area within which the plot (including axes and
* labels) should be drawn.
* @param anchor the anchor point (<code>null</code> permitted).
* @param parentState ignored.
* @param info collects chart drawing information (<code>null</code>
* permitted).
*/
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) {
// if the plot area is too small, just return...
boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
if (b1 || b2) {
return;
}
// record the plot area...
if (info != null) {
info.setPlotArea(area);
}
// adjust the drawing area for the plot insets (if any)...
RectangleInsets insets = getInsets();
insets.trim(area);
Rectangle2D dataArea = area;
if (info != null) {
info.setDataArea(dataArea);
}
// draw the plot background and axes...
drawBackground(g2, dataArea);
double h = Math.min(dataArea.getWidth() / 2.0, dataArea.getHeight() / 2.0) - MARGIN;
Rectangle2D quadrant = new Rectangle2D.Double(dataArea.getCenterX(), dataArea.getCenterY(), h, h);
AxisState state = drawAxis(g2, area, quadrant);
if (this.renderer != null) {
Shape originalClip = g2.getClip();
Composite originalComposite = g2.getComposite();
g2.clip(dataArea);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
this.angleTicks = refreshAngleTicks();
drawGridlines(g2, dataArea, this.angleTicks, state.getTicks());
// draw...
render(g2, dataArea, info);
g2.setClip(originalClip);
g2.setComposite(originalComposite);
}
drawOutline(g2, dataArea);
drawCornerTextItems(g2, dataArea);
}
use of org.jfree.chart.util.RectangleInsets in project graphcode2vec by graphcode2vec.
the class ChartFactory method createPieChart.
/**
* Creates a pie chart with default settings that compares 2 datasets.
* The colour of each section will be determined by the move from the value
* for the same key in <code>previousDataset</code>. ie if value1 > value2
* then the section will be in green (unless <code>greenForIncrease</code>
* is <code>false</code>, in which case it would be <code>red</code>).
* Each section can have a shade of red or green as the difference can be
* tailored between 0% (black) and percentDiffForMaxScale% (bright
* red/green).
* <p>
* For instance if <code>percentDiffForMaxScale</code> is 10 (10%), a
* difference of 5% will have a half shade of red/green, a difference of
* 10% or more will have a maximum shade/brightness of red/green.
* <P>
* The chart object returned by this method uses a {@link PiePlot} instance
* as the plot.
* <p>
* Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
* Xhenseval</a>.
*
* @param title the chart title (<code>null</code> permitted).
* @param dataset the dataset for the chart (<code>null</code> permitted).
* @param previousDataset the dataset for the last run, this will be used
* to compare each key in the dataset
* @param percentDiffForMaxScale scale goes from bright red/green to black,
* percentDiffForMaxScale indicate the change
* required to reach top scale.
* @param greenForIncrease an increase since previousDataset will be
* displayed in green (decrease red) if true.
* @param legend a flag specifying whether or not a legend is required.
* @param locale the locale (<code>null</code> not permitted).
* @param subTitle displays a subtitle with colour scheme if true
* @param showDifference create a new dataset that will show the %
* difference between the two datasets.
*
* @return A pie chart.
*
* @since 1.0.7
*/
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset, int percentDiffForMaxScale, boolean greenForIncrease, boolean legend, Locale locale, boolean subTitle, boolean showDifference) {
PiePlot plot = new PiePlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
List keys = dataset.getKeys();
DefaultPieDataset series = null;
if (showDifference) {
series = new DefaultPieDataset();
}
double colorPerPercent = 255.0 / percentDiffForMaxScale;
for (Iterator it = keys.iterator(); it.hasNext(); ) {
Comparable key = (Comparable) it.next();
Number newValue = dataset.getValue(key);
Number oldValue = previousDataset.getValue(key);
if (oldValue == null) {
if (greenForIncrease) {
plot.setSectionPaint(key, Color.green);
} else {
plot.setSectionPaint(key, Color.red);
}
if (showDifference) {
series.setValue(key + " (+100%)", newValue);
}
} else {
double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255 : Math.abs(percentChange) * colorPerPercent);
if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue() || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
plot.setSectionPaint(key, new Color(0, (int) shade, 0));
} else {
plot.setSectionPaint(key, new Color((int) shade, 0, 0));
}
if (showDifference) {
series.setValue(key + " (" + (percentChange >= 0 ? "+" : "") + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")", newValue);
}
}
}
if (showDifference) {
plot.setDataset(series);
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
if (subTitle) {
TextTitle subtitle = null;
subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-" + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+" + percentDiffForMaxScale + "%", new Font("Tahoma", Font.PLAIN, 10));
chart.addSubtitle(subtitle);
}
currentTheme.apply(chart);
return chart;
}
Aggregations