gpt4 book ai didi

java - JFreeChart - XYBarChart 为每个系列显示单独的条形图

转载 作者:搜寻专家 更新时间:2023-11-01 01:55:20 25 4
gpt4 key购买 nike

我正在使用 JFreeChart 创建 XYBarChart 并向其添加多个系列。目前,对于给定的 x 值和系列中的不同 Y 值,所有这些值都相互堆叠。

对于给定的 x 值,是否可以将每个系列显示为不同的条形图?

编辑:我发布了用于创建图表的代码的相关部分供您引用。

请注意我不能使用CategoryDataset,因为它不提供域轴上的缩放功能。这是我实现的基本要求。

XYSeriesCollection intervalXYDataSet  = new XYSeriesCollection();
int countPlotPoints = populateBandData(intervalXYDataSet, optionList); //optionList is a HashMap<Integer,ArrayList<Integer>> where key = seriesKey, and ArrayList<Integer> builds up the value for each series

if (countPlotPoints == 0) {
print("No options selected.\n");
JOptionPane.showMessageDialog(this, "No Plot Points Were Selected!", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}

/**
* Chart Creation Section
*/
JFreeChart chart = ChartFactory.createXYBarChart(tabTitle, "Frequency Bands", false, "rxlev", intervalXYDataSet, PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
plot.getDomainAxis().setAutoRange(true);

//
final XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setShadowVisible(false);
renderer.setMargin(0.2);
renderer.setDefaultShadowsVisible(false);

ChartPanel chartpanel = new ChartPanel(chart);
chartpanel.setDefaultDirectoryForSaveAs(new File(lastAnalyzedPath));

JFrame frame = new JFrame();
frame.setTitle(plotTitle);
frame.add(new JScrollPane(chartpanel));
frame.pack();
frame.setVisible(true);

frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent evt) {
try {
System.out.println(":: Clearning Memory ::");
System.out.println("\tFree Memory (Before cleanup): "+Runtime.getRuntime().freeMemory());
Component component = getComponent(0);
if(component instanceof ChartPanel){
JFreeChart chart = ((ChartPanel) component).getChart();
XYPlot plot = (XYPlot) chart.getPlot();
plot = null;
chart = null;
component = null;
}
} finally {
System.runFinalization();
System.gc();
System.out.println("\tFree Memory (Post cleanup): "+Runtime.getRuntime().freeMemory());
}
}
});

Here's a screenshot它目前的表现

最佳答案

编辑 使用 ClusteredXYBarRenderer 而不是 XYBarRenderer。这会绘制相邻的条形图(而不是 StackedBars)并提供缩放功能。

ChartFactory 中没有工厂方法来创建ClusteredXYBarRenderer

使用以下方法创建条形图。

private static JFreeChart createClusteredChart(String title, String categoryAxisLabel, String valueAxisLabel, IntervalXYDataset dataset) {

NumberAxis domainAxis = new NumberAxis(categoryAxisLabel);
domainAxis.setAutoRangeIncludesZero(false);

ValueAxis valueAxis = new NumberAxis(valueAxisLabel);

XYBarRenderer renderer = new ClusteredXYBarRenderer();

XYPlot plot = new XYPlot(dataset, domainAxis, valueAxis, renderer);
plot.setOrientation(PlotOrientation.VERTICAL);

JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

return chart;
}

XYBarRenderer 您使用的是正确的。但我认为您应该使用 CategoryDataSet 而不是 XYSeriesCollection。尝试使用以下数据集代替 XYSeriesCollection。这将生成相邻条而不是堆叠条。

private static CategoryDataset createDataset() {
String series1 = "First";
String series2 = "Second";
String series3 = "Third";

String category1 = "Category 1";
String category2 = "Category 2";
String category3 = "Category 3";
String category4 = "Category 4";
String category5 = "Category 5";

DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(1.0D, series1, category1);
dataset.addValue(4.0D, series1, category2);
dataset.addValue(3.0D, series1, category3);
dataset.addValue(5.0D, series1, category4);
dataset.addValue(5.0D, series1, category5);

dataset.addValue(5.0D, series2, category1);
dataset.addValue(7.0D, series2, category2);
dataset.addValue(6.0D, series2, category3);
dataset.addValue(8.0D, series2, category4);
dataset.addValue(4.0D, series2, category5);

dataset.addValue(4.0D, series3, category1);
dataset.addValue(3.0D, series3, category2);
dataset.addValue(2.0D, series3, category3);
dataset.addValue(3.0D, series3, category4);
dataset.addValue(6.0D, series3, category5);

return dataset;
}

关于java - JFreeChart - XYBarChart 为每个系列显示单独的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11366717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com