David:
If I am only interested in the percentage of the pie chart values, would 0.01 workaround to the normalized values ?
For example, if I have 10 data values: v1, v2, ..., v10, which are actual
values that are mostly greater than 1. Now in the DefaultPieDataset.setValue() call, use t1, t2, ..., t10, where
ti = vi/vtotal. vtotal = (v1 + v2 + ... + v10). Note now ti is always in the range of 0 - 1. Now for any ti > 0.01, we set it to be 0. Would this the
same as your 2) workaround ?
I will try to write a test to loop through all possible values from 0 - 1 with 10 values, incremental by 0.00001 to see if this workaround covers all.
I will let you know the result.
Pie Chart Crashes Java VM
unit test to verify the workaround of 0.001 data threshold
I have written a JUnit test that proves the 0.001 threshold is safe as
a workaround to prevent the crash without JFreeChart code/JVM upgrade.
Here is the source. It is based on the PieChartDemo1.
public class PieChartCrashTest extends TestBase {
public PieChartCrashTest(String name) {
super(name);
}
/**
* Starting point for the demonstration application.
*/
public void testCreateChart() throws Exception {
debugMsg("\n\nTesting creation of Pie chart");
debugMsg("==============================");
double[] values = {
0.0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
int numOfUnits = 10000;
double unit = 0.0001;
for (int i=0; i< numOfUnits; i++) {
values[0] = unit*i;
values[9] = 10.0 - values[0];
debugMsg(i+": value="+values[0]);
PieChart demo = new PieChart("Pie Chart Demo 1", values);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
// Thread.sleep(2000); // pause 2 seconds
}
}
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public static Test suite() {
TestSuite suite = new TestSuite("PieChartCrashTest Tests");
suite.addTest(new PieChartCrashTest("testCreateChart"));
return suite;
}
private class PieChart extends ApplicationFrame {
public PieChart(String title, double[] values) {
super(title);
// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();
double total = 0;
for (int i=0; i<values.length; i++) {
total += values;
}
for (int i=0; i<values.length; i++) {
double finalValue = values/total;
if (finalValue < 0.001) {
finalValue = 0;
}
data.setValue("Slice "+(i+1), new Double(finalValue));
}
// create the chart...
JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart title
data, // data
true // include legend
);
// set the background color for the chart...
chart.setBackgroundPaint(Color.yellow);
PiePlot plot = (PiePlot)chart.getPlot();
plot.setSectionLabelType(5);
//plot.setNoDataMessage("No data available");
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
this.setContentPane(chartPanel);
}
}
}
a workaround to prevent the crash without JFreeChart code/JVM upgrade.
Here is the source. It is based on the PieChartDemo1.
public class PieChartCrashTest extends TestBase {
public PieChartCrashTest(String name) {
super(name);
}
/**
* Starting point for the demonstration application.
*/
public void testCreateChart() throws Exception {
debugMsg("\n\nTesting creation of Pie chart");
debugMsg("==============================");
double[] values = {
0.0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
int numOfUnits = 10000;
double unit = 0.0001;
for (int i=0; i< numOfUnits; i++) {
values[0] = unit*i;
values[9] = 10.0 - values[0];
debugMsg(i+": value="+values[0]);
PieChart demo = new PieChart("Pie Chart Demo 1", values);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
// Thread.sleep(2000); // pause 2 seconds
}
}
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public static Test suite() {
TestSuite suite = new TestSuite("PieChartCrashTest Tests");
suite.addTest(new PieChartCrashTest("testCreateChart"));
return suite;
}
private class PieChart extends ApplicationFrame {
public PieChart(String title, double[] values) {
super(title);
// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();
double total = 0;
for (int i=0; i<values.length; i++) {
total += values;
}
for (int i=0; i<values.length; i++) {
double finalValue = values/total;
if (finalValue < 0.001) {
finalValue = 0;
}
data.setValue("Slice "+(i+1), new Double(finalValue));
}
// create the chart...
JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart title
data, // data
true // include legend
);
// set the background color for the chart...
chart.setBackgroundPaint(Color.yellow);
PiePlot plot = (PiePlot)chart.getPlot();
plot.setSectionLabelType(5);
//plot.setNoDataMessage("No data available");
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
this.setContentPane(chartPanel);
}
}
}