I am trying to generate a donut or ring chart using jfree chart library. Ringchart was generated successfully but only the problem is centered text inside ring was not displaying. The following is the sample snippet. chart generation code
JFreeChart chart = ChartFactory.createRingChart(heading, dataSet, legend, tooltips, urls);
And centered text related code is like below
RingPlot pie = (RingPlot) chart.getPlot(); pie.setBackgroundPaint(Color.WHITE); pie.setOutlineVisible(false); pie.setShadowPaint(null); pie.setLabelGenerator(null); pie.setCenterTextMode(CenterTextMode.VALUE); Font font = new Font("Arial",1,30); pie.setCenterTextFont(font); pie.setCenterTextColor(Color.getHSBColor(222, 1, 1)); pie.setSectionDepth(0.1); pie.setSectionOutlinesVisible(false); pie.setSeparatorsVisible(false); pie.setIgnoreZeroValues(false);
I am using jfreechart verison 1.5.0
Advertisement
Answer
It’s not clear where your fragment is going awry, but this minimal complete example gives the expected result. As an aside, note the use of Font.BOLD
for clarity and the use of deriveFont()
to minimize the risk of an unfortunate font substitution; see also *Initial Threads*.
pie.setCenterTextMode(CenterTextMode.VALUE); pie.setCenterTextFont(pie.getCenterTextFont().deriveFont(Font.BOLD, 30f)); pie.setCenterTextColor(Color.getHSBColor(0, 1, 1));
I tried
pie.setCenterText("Vijay");
Instead of CenterTextMode.VALUE
; specify CenterTextMode.FIXED
:
pie.setCenterTextMode(CenterTextMode.FIXED); pie.setCenterText("Vijay");
The above code is not setting any value if the first dataset value is zero.
Correct. RingPlot::drawItem()
ignores center text unless values exceed the rendering threshold; you can specify a value that passes the threshold yet displays correctly when formatted:
dataset.setValue("Critical", RingPlot.DEFAULT_MINIMUM_ARC_ANGLE_TO_DRAW);
import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CenterTextMode; import org.jfree.chart.plot.RingPlot; import org.jfree.data.general.DefaultPieDataset; /** * @see https://stackoverflow.com/a/56672573/230513 * @see https://stackoverflow.com/a/37414338/230513 */ public class TestRing { private void display() { JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("Critical", Integer.valueOf(42)); dataset.setValue("Important", Integer.valueOf(21)); dataset.setValue("Moderate", Integer.valueOf(7)); dataset.setValue("Low", Integer.valueOf(3)); JFreeChart chart = ChartFactory.createRingChart( "Test", dataset, false, true, false); RingPlot pie = (RingPlot) chart.getPlot(); pie.setSimpleLabels(true); pie.setCenterTextMode(CenterTextMode.VALUE); pie.setCenterTextFont(pie.getCenterTextFont().deriveFont(Font.BOLD, 30f)); pie.setCenterTextColor(Color.getHSBColor(0, 1, 1)); 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 TestRing()::display); } }