use of org.jfree.chart.plot.PiePlot in project SIMVA-SoS by SESoS.
the class PieChartTest method testReplaceDatasetOnPieChart.
/**
* Using a regular pie chart, we replace the dataset with null. Expect to
* receive notification of a chart change event, and (of course) the
* dataset should be null.
*/
@Test
public void testReplaceDatasetOnPieChart() {
LocalListener l = new LocalListener();
this.pieChart.addChangeListener(l);
PiePlot plot = (PiePlot) this.pieChart.getPlot();
plot.setDataset(null);
assertEquals(true, l.flag);
assertNull(plot.getDataset());
}
use of org.jfree.chart.plot.PiePlot in project SIMVA-SoS by SESoS.
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 tooltips configure chart to generate tool tips?
* @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, boolean tooltips, 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));
if (tooltips) {
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) {
// suppresses compiler warning
assert series != null;
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) {
// suppresses compiler warning
assert series != null;
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 = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-" + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+" + percentDiffForMaxScale + "%", new Font("SansSerif", Font.PLAIN, 10));
chart.addSubtitle(subtitle);
}
currentTheme.apply(chart);
return chart;
}
use of org.jfree.chart.plot.PiePlot in project SIMVA-SoS by SESoS.
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 tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
* @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.
*/
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset, int percentDiffForMaxScale, boolean greenForIncrease, boolean legend, boolean tooltips, boolean urls, boolean subTitle, boolean showDifference) {
PiePlot plot = new PiePlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
}
if (urls) {
plot.setURLGenerator(new StandardPieURLGenerator());
}
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) {
// suppresses compiler warning
assert series != null;
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) {
// suppresses compiler warning
assert series != null;
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 = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-" + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+" + percentDiffForMaxScale + "%", new Font("SansSerif", Font.PLAIN, 10));
chart.addSubtitle(subtitle);
}
currentTheme.apply(chart);
return chart;
}
use of org.jfree.chart.plot.PiePlot in project SIMVA-SoS by SESoS.
the class PieChartFXDemo1 method createChart.
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(PieDataset dataset) {
JFreeChart chart = ChartFactory.createPieChart(// chart title
"Smart Phones Manufactured / Q3 2011", // data
dataset, // no legend
false, // tooltips
true, // no URL generation
false);
// set a custom background for the chart
chart.setBackgroundPaint(new GradientPaint(new Point(0, 0), new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));
// customise the title position and font
TextTitle t = chart.getTitle();
t.setHorizontalAlignment(HorizontalAlignment.LEFT);
t.setPaint(new Color(240, 240, 240));
t.setFont(new Font("Arial", Font.BOLD, 26));
PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(null);
plot.setInteriorGap(0.04);
plot.setOutlineVisible(false);
// use gradients and white borders for the section colours
plot.setSectionPaint("Others", createGradientPaint(new Color(200, 200, 255), Color.BLUE));
plot.setSectionPaint("Samsung", createGradientPaint(new Color(255, 200, 200), Color.RED));
plot.setSectionPaint("Apple", createGradientPaint(new Color(200, 255, 200), Color.GREEN));
plot.setSectionPaint("Nokia", createGradientPaint(new Color(200, 255, 200), Color.YELLOW));
plot.setBaseSectionOutlinePaint(Color.WHITE);
plot.setSectionOutlinesVisible(true);
plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f));
// customise the section label appearance
plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
plot.setLabelLinkPaint(Color.WHITE);
plot.setLabelLinkStroke(new BasicStroke(2.0f));
plot.setLabelOutlineStroke(null);
plot.setLabelPaint(Color.WHITE);
plot.setLabelBackgroundPaint(null);
// add a subtitle giving the data source
TextTitle source = new TextTitle("Source: http://www.bbc.co.uk/news/business-15489523", new Font("Courier New", Font.PLAIN, 12));
source.setPaint(Color.WHITE);
source.setPosition(RectangleEdge.BOTTOM);
source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
chart.addSubtitle(source);
return chart;
}
use of org.jfree.chart.plot.PiePlot in project SIMVA-SoS by SESoS.
the class ScrollHandlerFX method handleScroll.
@Override
public void handleScroll(ChartCanvas canvas, ScrollEvent e) {
JFreeChart chart = canvas.getChart();
Plot plot = chart.getPlot();
if (plot instanceof Zoomable) {
Zoomable zoomable = (Zoomable) plot;
handleZoomable(canvas, zoomable, e);
} else if (plot instanceof PiePlot) {
PiePlot pp = (PiePlot) plot;
pp.handleMouseWheelRotation((int) e.getDeltaY());
}
}
Aggregations