我正在尝试在创建XYBarChart时使用ClusteredXYBarRenderer。此图表本身添加了多个数据系列。我的要求是允许用户放大域轴,同时防止在范围轴上缩放。我可以在我的代码中轻松实现。
然而,我坚持的是以下要求:
以下是SSCCE供您参考:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.ClusteredXYBarRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ClusteredBarChart {
private JFreeChart createClusteredChart(String title, String categoryAxisLabel, String valueAxisLabel, IntervalXYDataset dataset) {
NumberAxis domainAxis = new NumberAxis(categoryAxisLabel);
domainAxis.setAutoRangeIncludesZero(false);
domainAxis.setAutoRange(true);
ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
XYBarRenderer renderer = new ClusteredXYBarRenderer();
renderer.setShadowVisible(false);
XYPlot plot = new XYPlot(dataset, domainAxis, valueAxis, renderer);
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setNoDataMessage("=== No Datapoints Detected ====");
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
return chart;
}
private void createDummyData(XYSeriesCollection intervalDataset){
XYSeries series1 = new XYSeries("series1");
XYSeries series2 = new XYSeries("series2");
XYSeries series3 = new XYSeries("series3");
XYSeries series4 = new XYSeries("series4");
XYSeries series5 = new XYSeries("series5");
series1.add(6, 10);
series1.add(7, 11);
series1.add(10, 11);
series1.add(17, 17);
series1.add(21, 15);
series2.add(6, 18);
series2.add(7, 10);
series2.add(10, 14);
series2.add(17, 12);
series2.add(21, 6);
series3.add(6, 12);
series3.add(7, 13);
series3.add(10, 14);
series3.add(17, 16);
series3.add(21, 9);
series4.add(6, 8);
series4.add(7, 22);
series4.add(10, 21);
series4.add(17, 26);
series4.add(21, 24);
series5.add(6, 14);
series5.add(7, 11);
series5.add(10, 31);
series5.add(17, 12);
series5.add(21, 11);
intervalDataset.addSeries(series1);
intervalDataset.addSeries(series2);
intervalDataset.addSeries(series3);
intervalDataset.addSeries(series4);
intervalDataset.addSeries(series5);
}
public void doPlot(String plotTitle) {
XYSeriesCollection intervalXYDataSet = new XYSeriesCollection();
createDummyData(intervalXYDataSet);
JFreeChart chart = createClusteredChart(plotTitle, "Category", "Value", intervalXYDataSet);
ChartPanel chartpanel = new ChartPanel(chart);
chartpanel.setDomainZoomable(true);
chartpanel.setRangeZoomable(false);
JFrame frame = new JFrame();
frame.setTitle(plotTitle);
frame.add(new JScrollPane(chartpanel));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new ClusteredBarChart().doPlot("Test Plot");
}
}
如果运行该示例,您会看到值#6和#7的点看起来非常接近。我想在它们之间添加空间。此外,我想阻止用户放大到6.5或6.75等....换句话说,最大可缩放范围应该是实际数据集中的值。
请注意,此问题引用了this one that I had asked earlier。
任何帮助都会非常感激。谢谢!
答案 0 :(得分:1)
我可以看到两种方法:
使用zoom()
方法的ChartPanel
方法和合适的Rectangle2D
。您需要遍历模型以在数据空间中查找所需的集群边界,并且可以使用valueToJava2D()
NumberAxis
转换为Java 2D空间。您可以从ChartPanel
:
Rectangle2D r = panel.getChartRenderingInfo().getPlotInfo().getDataArea();
将控件添加到ChartPanel
以仅显示感兴趣的群集,不包括模型中的其他群集。这相关的example可以通过多个Box&晶须图可能会有所帮助。