I'm having some trouble while working on JFreeChart RingPlot. I've managed to put labels inside my chart, yet I can't change their positions as I want. Here where am I right now;
I need to move the labes closer to the edges of the chart so that I can lower the section depth and have a better ring look. So far, I tried to play with setSimpleLabelOffset and setLabelGap methods but didn't work well.
Here is my code;
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Critical", new Integer(5));
dataset.setValue("Important", new Integer(20));
dataset.setValue("Moderate", new Integer(19));
dataset.setValue("Low", new Integer(5));
JFreeChart chart = ChartFactory.createRingChart("", dataset, false, true, false);
RingPlot pie = (RingPlot) chart.getPlot();
pie.setBackgroundPaint(Color.WHITE);
pie.setOutlineVisible(false);
pie.setShadowPaint(null);
pie.setSimpleLabels(true);
pie.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
//pie.setSimpleLabelOffset(new RectangleInsets(1, 1, 1, 1));
//pie.setLabelGap(0.05);
//pie.setLabelPadding(new RectangleInsets(100, 5, 10, 5));
pie.setLabelBackgroundPaint(null);
pie.setLabelOutlinePaint(null);
pie.setLabelShadowPaint(null);
pie.setSectionDepth(0.50);
pie.setSectionOutlinesVisible(false);
pie.setSeparatorsVisible(false);
pie.setIgnoreZeroValues(true);
Any idea how may I achieve this?
Thanks in advance.
Edit: Thanks for the response #trashgod, but something is wrong with my environment iI guess. I copied and pasted the whole code you presented above and what I get is this:
The default RectangleInsets for PiePlot labels are inset relative to the plot's pieArea:
this.simpleLabelOffset = new RectangleInsets(UnitType.RELATIVE, 0.18, 0.18, 0.18, 0.18);
The example below cuts the insets in half and changes the section depth accordingly:
pie.setSimpleLabelOffset(new RectangleInsets(UnitType.RELATIVE, 0.09, 0.09, 0.09, 0.09));
pie.setSectionDepth(0.33);
As tested with jfreechart-1.0.19.jar and jcommon-1.0.23.jar, Java 1.8.0_92, Mac OS X 10.11.5, Windows 10:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.RingPlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.UnitType;
/**
* #see http://stackoverflow.com/a/37414338/230513
*/
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Critical", new Integer(5));
dataset.setValue("Important", new Integer(20));
dataset.setValue("Moderate", new Integer(19));
dataset.setValue("Low", new Integer(5));
JFreeChart chart = ChartFactory.createRingChart("", dataset, false, true, false);
RingPlot pie = (RingPlot) chart.getPlot();
pie.setBackgroundPaint(Color.WHITE);
pie.setOutlineVisible(false);
pie.setShadowPaint(null);
pie.setSimpleLabels(true);
pie.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
pie.setSimpleLabelOffset(new RectangleInsets(
UnitType.RELATIVE, 0.09, 0.09, 0.09, 0.09));
pie.setLabelBackgroundPaint(null);
pie.setLabelOutlinePaint(null);
pie.setLabelShadowPaint(null);
pie.setSectionDepth(0.33);
pie.setSectionOutlinesVisible(false);
pie.setSeparatorsVisible(false);
pie.setIgnoreZeroValues(true);
f.add(new ChartPanel(chart){
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
Related
I'm using Pie-chart of JFreeChart library for my need. And i want to display percentage with total values in pie-chart. So, i searched over google. And i found this solution which is answered by #trashgod.
Now as per answer given by him i created class which is as following :
import java.awt.Color;
import java.awt.Dimension;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class TestPieChart {
private static final String KEY1 = "Datum 1";
public static final String KEY2 = "Datum 2";
public static void main(String[] args) {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue(KEY1, 45045); //49
dataset.setValue(KEY2, 53955); //151
JFreeChart someChart = ChartFactory.createPieChart(
"Header", dataset, true, true, false);
PiePlot plot = (PiePlot) someChart.getPlot();
plot.setSectionPaint(KEY1, Color.green);
plot.setSectionPaint(KEY2, Color.red);
plot.setExplodePercent(KEY1, 0.10);
plot.setSimpleLabels(true);
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
"{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%"));
plot.setLabelGenerator(gen);
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ChartPanel(someChart) {
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
What it gives output as follows:
In which percentage was 55% and 46%, Total is 101% (Because it is taking upper limit for both values like 54.5 then 55)
But at the same time if i take value for KEY1 as 49 and KEY2 as 151, then it is giving accurate result as follows: (Sum of both in this case is 100%)
So, My question is : Why JFreeChart is performing different for different values? and is there any solution to fix this (total percentage will not cross 100) ?
The percentage calculation performed by the label generator's abstract parent, seen here, and the formatter's default rounding, discussed here, are both correct. If desired, you can change the precision of the displayed percentage by specifying a different percentFormat when constructing the StandardPieSectionLabelGenerator:
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
"{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0.0%"));
Note that 45.5% + 54.5% = 100%.
I found a thermometer demo and customized it for my dashboard project. http://www.java2s.com/Code/Java/Chart/JFreeChartThermometerDemo2.htm
In my dashboard I have six thermometer with different Mercury Color. However, I cannot seem to find a way to change the color of the range numbers that are displayed beside the thermometer.
How do I change the range number text color from Black to White?
My Demo Thermometer screenshot
package Thermometers;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import App.App_v2;
public class ThermometerDemo2 extends JPanel
{
private static final long serialVersionUID = 1L;
public ThermometerDemo2(Color color, double maxValue)
{
// create a dataset...
final DefaultValueDataset dataset = new DefaultValueDataset(110);
// create the chart...
final ThermometerPlot plot = new ThermometerPlot(dataset);
plot.setRange(0.0, maxValue);
plot.setSubrange(ThermometerPlot.CRITICAL, 250, 300);
plot.setValueFont(new Font("Georgia", Font.BOLD, 32));
plot.setThermometerStroke(new BasicStroke(2.0f));
plot.setBackgroundPaint(new Color(20,42,60));
plot.setMercuryPaint(color);
final JFreeChart chart = new JFreeChart(plot);
chart.setBorderVisible(false);
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
this.add(chartPanel);
this.setBackground(new Color(20,42,60));
}
public static void main(final String[] args) {
JFrame frame = new JFrame("change the black range color into white");
frame.setVisible(true);
frame.setSize(500, 500);
ThermometerDemo2 demo = new ThermometerDemo2(Color.magenta, 300);
demo.setVisible(true);
frame.add(demo);
frame.pack();
}
}
I am using JFreeChart to make a barchart vs time. For some reason on these charts, the tick labels on the x axis turn into "..." occasionally. There seems like there is plenty of room for the labels to expand, but instead it just cuts off the whole thing. How can I fix this.
I tried uploading a picture using the image button, but it does not seem to be working.
Here is code with a similar set up to my project. Strangely it acted different then what is happening to my build. On mine instead of saying "Hou...", it just says "...". Ignore comments and all other uneeded things please.
package dataDisplay;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
public class mockTest extends JPanel{
ChartPanel chartPanel;
JFreeChart chart;
CategoryAxis domainAxis;
NumberAxis rangeAxis;
public mockTest()
{
//Mock data
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int[] times = new int[]{1,2,3,4,5,6,7,8,9,10,11,12};
for ( int i = 0; i < times.length; i++ ){
dataset.addValue(times[i], "Time", "Houreee" + String.valueOf(i+1));;
}
CategoryPlot plot = new CategoryPlot();
//create the plot
//add the first dataset, and render as bar values
CategoryItemRenderer renderer = new BarRenderer();
plot.setDataset(0,dataset);
plot.setRenderer(0,renderer);
//set axis
domainAxis = new CategoryAxis("Time");
rangeAxis = new NumberAxis("Value");
plot.setDomainAxis(0,domainAxis);
plot.setRangeAxis(rangeAxis);
chart = new JFreeChart(plot);
chartPanel = new ChartPanel( chart );
this.addComponentListener(new ComponentAdapter() {
#Override
/**
* Makes it so it does not stretch out text. Resizes the fonts to scale with the screen width..
*/
public void componentResized(ComponentEvent e) {
chartPanel.setMaximumDrawHeight(e.getComponent().getHeight());
chartPanel.setMaximumDrawWidth(e.getComponent().getWidth());
chartPanel.setMinimumDrawWidth(e.getComponent().getWidth());
chartPanel.setMinimumDrawHeight(e.getComponent().getHeight());
// Makes the font size scale according to the width of the chart panel.
rangeAxis.setLabelFont(new Font("SansSerif", Font.PLAIN,e.getComponent().getWidth()/60));
domainAxis.setTickLabelFont(new Font("SansSerif", Font.PLAIN,e.getComponent().getWidth()/80));
rangeAxis.setTickLabelFont(new Font("SansSerif", Font.PLAIN,e.getComponent().getWidth()/75));
}
});
this.add(chartPanel, "Center");
}
public static void main (String[] args)
{
// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
int scrnWidth= (int)scrnsize.getWidth();
int scrnHeight = (int) scrnsize.getHeight();
JFrame J= new JFrame();
JPanel jP = new JPanel();
J.setContentPane(jP);
J.setSize(scrnWidth, scrnHeight);
jP.setBackground(Color.white);
jP.setBounds(0,0,scrnWidth,scrnHeight);
int xPercent= 50;
int yPercent = 50;
int widthPercent=50;
int heightPercent=43;
jP.setLayout(null);
jP.setSize(scrnWidth, scrnHeight);
mockTest b= new mockTest();
jP.add(b);
b.setBounds(new Rectangle((int)(scrnWidth*((double)xPercent/100)),(int)(scrnHeight*((double)yPercent/100)),(int)(scrnWidth*((double)widthPercent/100)),(int)(scrnHeight*((double)heightPercent/100))));
J.setUndecorated(true);
J.setVisible(true);
}
Don't use a null layout; let the layout manager do the work. The default layout of JPanel is FlowLayout, which ignores your subsequent changes. In the example below,
The chartPanel is given a GridLayout; when added to the enclosing frame's CENTER, the chart will be free to grow as the frame is resized.
Avoid unnecessarily nested panels.
Use setExtendedState() to maximize the frame.
If necessary, use one of the approaches suggested here to alter the chart's initial size.
If you choose to alter a Font, use deriveFont() to avoid abrupt disparities in the user's chosen settings.
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
/** #see https://stackoverflow.com/a/31014252/230513 */
public class Test {
public void display() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < 12; i++) {
dataset.addValue(i, "Time", "Hours" + String.valueOf(i + 1));
}
CategoryPlot plot = new CategoryPlot();
CategoryItemRenderer renderer = new BarRenderer();
plot.setDataset(0, dataset);
plot.setRenderer(0, renderer);
CategoryAxis domainAxis = new CategoryAxis("Time");
NumberAxis rangeAxis = new NumberAxis("Value");
plot.setDomainAxis(0, domainAxis);
plot.setRangeAxis(rangeAxis);
JFreeChart chart = new JFreeChart(plot);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setLayout(new GridLayout());
JFrame f = new JFrame();
f.add(chartPanel);
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
f.setUndecorated(true);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
Suppose that we need to display multiple XYSeries in a single XYSeriesCollection. My problem is that every time I add a XYSeries, the JFreeChart wants to update the chart and that slows down the process of displaying multiple XYSeries.
What I want is similar to this:
// Do not update the chart
XYSeriesCollection.add(XYSeries1)
XYSeriesCollection.add(XYSeries2)
...
XYSeriesCollection.add(XYSeries10)
// Update the chart
How can I do this?
Construct a new XYSeriesCollection having the desired series, and invoke setDataset() on the XYPlot. This will generate a single DatasetChangeEvent.
Addendum: Here's an SSCCE that updates N series, each having N2 values. As this is a performance question, the example may be helpful in profiling.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class ChartPanelTest {
private static final int N = 16;
private static final Random random = new Random();
private static XYDataset createDataset() {
TimeSeriesCollection tsc = new TimeSeriesCollection();
for (int j = 0; j < N; j++) {
TimeSeries series = new TimeSeries("Data" + j);
Day current = new Day();
for (int i = 0; i < N * N; i++) {
series.add(current, random.nextGaussian());
current = (Day) current.next();
}
tsc.addSeries(series);
}
return tsc;
}
private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", "Day", "Value", dataset, false, false, false);
return chart;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
final XYPlot plot = chart.getXYPlot();
ChartPanel chartPanel = new ChartPanel(chart) {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 300);
}
};
f.add(chartPanel);
JPanel p = new JPanel();
p.add(new JButton(new AbstractAction("New") {
#Override
public void actionPerformed(ActionEvent e) {
plot.setDataset(createDataset());
}
}));
f.add(p, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
Looking at the documentation for XYSeriesCollection (assuming add should be addSeries) there is no method addAll (or similar).
If you want, you could extend XYSeriesCollection and implement addAll. In this method, you could temporarily disable all listeners before adding and re-add them after. However, this would probably be a bad idea, and would definitely need to be synchronized:
it would produce unpredictable polymorphism behavior
it would require reflection hacks, as AbstractDataset.listenerList is private
it could generate a security exception
it would not be thread safe
So the short answer is no, it is not feasible.
… but this is a starting point for how you could do it:
Field field = getClass().getDeclaredField("listenerList");
field.setAccessible(true);
EventListenerList ell = field.get(this);
// go from here
field.setAccessible(false);
I have a JFree XY Line chart which always starts at x = 0. Then based on user defined settings from a properties file, the application increments based on that number (this represents the time in minutes).
For example, x = 0 to start the user defined setting is 5 so the scale goes 0, 5, 10, 15, 20…, or the user setting is 3 so it goes 0, 3, 6, 9, 12… Pretty simple.
The issue I am having is the way in which the graph starts. If I start at 0, then 0 is in the middle of the graph rather than at the bottom left with -0.0000005, -0.000004, -0.000003… 0.000000 , 0.000001 , 0.000002… 0.000005
How can I just manually add the scale at the bottom, i.e. define it should be increments of 2 and then maintain it?
You should use NumberAxis, which contains a lot of methods to define the scale of your chart.
Example :
// Create an XY Line chart
XYSeries series = new XYSeries("Random Data");
series.add(1.0, 500.2);
series.add(10.0, 694.1);
XYSeriesCollection data = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("XY Series Demo", "X", "Y", data,
PlotOrientation.VERTICAL,
true, true, false);
// Create an NumberAxis
NumberAxis xAxis = new NumberAxis();
xAxis.setTickUnit(new NumberTickUnit(2));
// Assign it to the chart
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainAxis(xAxis);
Based on this example, here's an sscce that uses setTickUnit() to adjust the domain axis tick unit dynamically, starting from the value 5.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/** #see https://stackoverflow.com/a/14167983/230513 */
public class SSCCE {
private static final int COUNT = 100;
private static final int UNITS = 5;
private static final Random r = new Random();
public static void main(String[] args) {
XYSeries series = new XYSeries("Data");
for (int i = 0; i < COUNT; i++) {
series.add(i, r.nextGaussian());
}
XYSeriesCollection data = new XYSeriesCollection(series);
final JFreeChart chart = ChartFactory.createXYLineChart("TickUnits",
"X", "Y", data, PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
final NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setTickUnit(new NumberTickUnit(UNITS));
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("TickUnitDemo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ChartPanel(chart));
final JSpinner spinner = new JSpinner(
new SpinnerNumberModel(UNITS, 1, COUNT, 1));
spinner.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
JSpinner s = (JSpinner) e.getSource();
Number n = (Number) s.getValue();
xAxis.setTickUnit(new NumberTickUnit(n.intValue()));
}
});
JPanel p = new JPanel();
p.add(new JLabel(chart.getTitle().getText()));
p.add(spinner);
f.add(p, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}