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);
}
}