本文介绍了JFreeChart - 自定义RingChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想制作两个如下所示的环形图:
I want to make two ring charts that look like the following:
但是RingPlot似乎不是很可定制的。我能想到的最好的是:
But the RingPlot doesn't seem very customizable. The best I could come up with is this:
有机会用JFreeChart做我想做的事吗?
Any chance of doing what I want with JFreeChart?
推荐答案
可以做大多数事情,这应该让你开始(我可能会将中心文本功能纳入即将发布的1.0.18版本中,以便不需要子类化):
JFreeChart can do most things, this should get you started (I'll probably incorporate the center text feature into the upcoming 1.0.18 release so that the subclassing isn't necessary):
package org.jfree.chart.demo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlotState;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.text.TextUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
/**
* A simple demonstration application showing how to create a ring chart using
* data from a {@link DefaultPieDataset}.
*/
public class RingChartDemo1 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
static class CustomRingPlot extends RingPlot {
private Font centerTextFont;
private Color centerTextColor;
public CustomRingPlot(PieDataset dataset) {
super(dataset);
this.centerTextFont = new Font(Font.SANS_SERIF, Font.BOLD, 24);
this.centerTextColor = Color.LIGHT_GRAY;
}
@Override
protected void drawItem(Graphics2D g2, int section,
Rectangle2D dataArea, PiePlotState state, int currentPass) {
super.drawItem(g2, section, dataArea, state, currentPass);
if (currentPass == 1 && section == 0) {
Number n = this.getDataset().getValue(section);
g2.setFont(this.centerTextFont);
g2.setPaint(this.centerTextColor);
TextUtilities.drawAlignedString(n.toString(), g2,
(float) dataArea.getCenterX(),
(float) dataArea.getCenterY(),
TextAnchor.CENTER);
}
}
}
/**
* Default constructor.
*
* @param title the frame title.
*/
public RingChartDemo1(String title) {
super(title);
setContentPane(createDemoPanel());
}
/**
* Creates a sample dataset.
*
* @return A sample dataset.
*/
private static PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("A", new Double(210));
dataset.setValue("B", new Double(150));
return dataset;
}
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(PieDataset dataset) {
CustomRingPlot plot = new CustomRingPlot(dataset);
JFreeChart chart = new JFreeChart("Custom Ring Chart",
JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(new GradientPaint(new Point(0, 0),
new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));
// customise the title position and font
TextTitle t = chart.getTitle();
t.setHorizontalAlignment(HorizontalAlignment.LEFT);
t.setPaint(new Color(240, 240, 240));
t.setFont(new Font("Arial", Font.BOLD, 26));
plot.setBackgroundPaint(null);
plot.setOutlineVisible(false);
plot.setLabelGenerator(null);
plot.setSectionPaint("A", Color.ORANGE);
plot.setSectionPaint("B", new Color(100, 100, 100));
plot.setSectionDepth(0.05);
plot.setSectionOutlinesVisible(false);
plot.setShadowPaint(null);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
chart.setPadding(new RectangleInsets(4, 8, 2, 2));
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
panel.setPreferredSize(new Dimension(600, 300));
return panel;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
RingChartDemo1 demo = new RingChartDemo1("JFreeChart: Ring Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
这篇关于JFreeChart - 自定义RingChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!