Hide labels on jfreechart/PiePlot3D piechart
-
05-07-2019 - |
Question
I'm trying to get the jfreechart PieChart3D to hide labels. I can't find anything in the documentation.
Anyone know how to do this?
<% response.setContentType("image/png"); %><%@page import="org.jfree.data.general.*"%><%@page import="org.jfree.chart.*"%><%@page import="org.jfree.chart.plot.*"%><%@page import="java.awt.Color" %><%
DefaultPieDataset ds = (DefaultPieDataset)session.getAttribute("usagePieOutputDataset");
JFreeChart chart = ChartFactory.createPieChart3D
(
null, // Title
ds, // Dataset
false, // Show legend
false, // Use tooltips
false // Configure chart to generate URLs?
);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(false);
PiePlot3D plot = ( PiePlot3D )chart.getPlot();
plot.setDepthFactor(0.0);
plot.setLabelOutlinePaint(Color.LIGHT_GRAY);
plot.setLabelFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 10));
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 150, 144);
%>
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/PiePlot3D.html
Solution
This will hide all labels:
plot.setLabelGenerator(null); //null means no labels
OTHER TIPS
Actually this seems to be an even easier way and also much shorter.
Just do the label distribution yourself (anonymous implementation) and pretend that there are no labels to display by returning a zero in getItemCount()
.
plot.setLabelDistributor(new AbstractPieLabelDistributor() {
public int getItemCount() { return 0; }
public void distributeLabels(double minY,double height) {}
});
Old solution:
Don't know if there is an easier way but this should work. Should be self-explanatory. Don't show links set some colors transparent and don't generate labels. Else just ask.
Color transparent = new Color(0.0f,0.0f,0.0f,0.0f);
plot.setLabelLinksVisible(Boolean.FALSE);
plot.setLabelOutlinePaint(transparent);
plot.setLabelBackgroundPaint(transparent);
plot.setLabelShadowPaint(transparent);
plot.setLabelGenerator(new PieSectionLabelGenerator(){
@Override
public AttributedString generateAttributedSectionLabel(PieDataset dataset, Comparable key) {
return new AttributedString("");
}
@Override
public String generateSectionLabel(PieDataset dataset, Comparable key) {
return "";
}
});
This work for me:
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickLabelsVisible(false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow