2

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

Vijay Raju
  • 150
  • 1
  • 12

1 Answers1

2

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

ring plot with value

I tried pie.setCenterText("Vijay");

Instead of CenterTextMode.VALUE; specify CenterTextMode.FIXED:

pie.setCenterTextMode(CenterTextMode.FIXED);
pie.setCenterText("Vijay");

ring plot with fixed text

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

ring plot zero

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

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • tq for the reply.With the above code we can only set dataset values right.I want to set a default text as vijay instead of dataset value?.And the above code is not setting any value if the first dataset value is zero .And I tried pie.setCenterText("vijay"); for default text but it was not working empty space was displaying.any suggestion plz – Vijay Raju Jun 20 '19 at 14:50
  • I've elaborated above. – trashgod Jun 20 '19 at 17:02
  • @trashgod.is it possible to align the text in two lines in the center area. Like text in one line and number in another line. p.setCenterText("total\n"+count) ; – Vijay Raju Aug 12 '19 at 10:29
  • @VijayRaju: At a guess, you'd have to override `RingPlot::drawItem` to draw a second line using `TextUtils.drawAlignedString`. – trashgod Aug 14 '19 at 00:38
  • Tq@trashgod :Its working (mult line center text) by overriding drawItem method. But how to apply different font objects for each line. It's only taking last font object. Is it possible to add multiple fonts for each line? – Vijay Raju Jul 20 '20 at 14:05
  • @VijayRaju: `drawItem()` invokes `drawAlignedString()`, which uses an `AttributedString`, for [example](https://stackoverflow.com/a/29808858/230513), which may offer additional flexibility. – trashgod Jul 20 '20 at 18:30
  • 1
    Excellent @trashgod: I enabled TextUtils.setDrawStringsWithFontAttributes(true) ;then I passed multiple font objects to Graphics2D class font setters. Now all working as expected. – Vijay Raju Jul 21 '20 at 11:48