JFreeChart 允许您在每个绘图中拥有多个渲染器。您可以将 aXYStepRenderer
与 a结合使用XYLineAndShapeRenderer
,此示例改编自 JFreeChart Demos
import java.awt.BasicStroke;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class XY2RendererDemo extends ApplicationFrame {
public XY2RendererDemo(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
setContentPane(chartPanel);
}
private static JFreeChart createChart(XYDataset[] dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"XYStepRenderer & XYLineAndShapeRenderer",
"X",
"Y",
dataset[0],
PlotOrientation.VERTICAL,
true,
true,
false
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
{
XYStepRenderer renderer = new XYStepRenderer();
renderer.setBaseShapesVisible(true);
renderer.setSeriesStroke(0, new BasicStroke(2.0f));
renderer.setSeriesStroke(1, new BasicStroke(2.0f));
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
renderer.setDefaultEntityRadius(6);
plot.setRenderer(renderer);
}
{
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
plot.setDataset(1, dataset[1]);
plot.setRenderer(1, renderer2);
}
return chart;
}
private static XYDataset[] createDataset() {
XYSeries series1 = new XYSeries("Series 1");
series1.add(1.0, 3.0);
series1.add(2.0, 4.0);
series1.add(3.0, 2.0);
series1.add(6.0, 3.0);
XYSeries series2 = new XYSeries("Series 2");
series2.add(1.0, 7.0);
series2.add(2.0, 6.0);
series2.add(3.0, 9.0);
series2.add(4.0, 5.0);
series2.add(6.0, 4.0);
XYSeriesCollection dataset1 = new XYSeriesCollection();
dataset1.addSeries(series1);
XYSeriesCollection dataset2 = new XYSeriesCollection();
dataset2.addSeries(series2);
return new XYDataset[]{dataset1,dataset2};
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
public static void main(String[] args) {
XY2RendererDemo demo = new XY2RendererDemo(
"JFreeChart: Using 2 Renderers");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
在此示例中,我将 theplots initial Renderer 替换为 a XYStepRenderer
,然后为 second 添加第二XYLineAndShapeRenderer
个XYDataset
。一个更简单的方法是添加第二个 Renderer,因为 ChartFactory#createXYLineChart()XYLineAndShapeRenderer
默认会创建一个。