我有一个条形图,其中有一个名为"Brutto"的"系列"(在jfree paralance中)和许多类别;自2008年以来,每月一次。我已经轮换了UP-90的月份标签,但它们都挤在一起,很难阅读,所以我想跳过其中的一些标签(例如每隔一个或每三个标签,但这并不重要,只要它整洁可读,最好是动态的,以备将来添加)。
我使用的是JfreeChart 1.0.16,当我在SourceForge上看到Ray Lukas的补丁和jfree论坛上的相关帖子时,我以为我已经找到了答案:http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=15345&启动=15
不幸的是,这个补丁/类似乎不适用于1.0.16;我已经尝试了演示代码,并根据我的用例调整了演示,虽然它没有抛出任何错误,但它似乎对我的图表毫无帮助。从SourceForge看来,该补丁上次更新是在2012年。
有没有办法跳过1.0.16或1.0.17中的类别标签?如果没有,是否有变通办法或更新Ray的补丁以使用1.0.16?
我已经支付了JFree开发指南和演示源代码的费用,但还没有看到任何有用的东西——如果我遗漏了什么,请告诉我。
更新
对于要玩的代码,您可以使用Ray的演示代码和更类似于我的数据集:
public class CategoryLabelDemo extends ApplicationFrame {
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public CategoryLabelDemo(String title) {
super(title);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(-0.0023, value, Jun 2008);
dataset.addValue(-0.0039089907, value, Jul 2008);
dataset.addValue(-0.0035218354, value, Aug 2008);
dataset.addValue(-0.0092901141, value, Sep 2008);
dataset.addValue(-0.0245642646, value, Okt 2008);
dataset.addValue(-0.0032392894, value, Nov 2008);
dataset.addValue(0.0024111542, value, Dez 2008);
dataset.addValue(0.0062748379, value, Jan 2009);
dataset.addValue(0.0107046352, value, Feb 2009);
dataset.addValue(-0.0101799486, value, Mrz 2009);
dataset.addValue(6.233119E-4, value, Apr 2009);
dataset.addValue(0.0117317276, value, Mai 2009);
dataset.addValue(0.0069779374, value, Jun 2009);
dataset.addValue(0.0132477326, value, Jul 2009);
dataset.addValue(0.0062355426, value, Aug 2009);
dataset.addValue(0.0181909045, value, Sep 2009);
dataset.addValue(0.0049082164, value, Okt 2009);
dataset.addValue(0.0186578099, value, Nov 2009);
dataset.addValue(0.0059455313, value, Dez 2009);
dataset.addValue(8.5796E-4, value, Jan 2010);
dataset.addValue(0.0048576055, value, Feb 2010);
dataset.addValue(0.0230331754, value, Mrz 2010);
dataset.addValue(0.0128787177, value, Apr 2010);
dataset.addValue(-0.0101536773, value, Mai 2010);
dataset.addValue(0.0017558451, value, Jun 2010);
dataset.addValue(-0.0010147601, value, Jul 2010);
dataset.addValue(0.0093268076, value, Aug 2010);
dataset.addValue(0.0082342177, value, Sep 2010);
dataset.addValue(0.0078039927, value, Okt 2010);
dataset.addValue(7.203314E-4, value, Nov 2010);
dataset.addValue(0.0071081519, value, Dez 2010);
dataset.addValue(-0.0108996694, value, Jan 2011);
dataset.addValue(0.0059615211, value, Feb 2011);
dataset.addValue(-0.0045793302, value, Mrz 2011);
dataset.addValue(0.0128991521, value, Apr 2011);
dataset.addValue(0.0040074806, value, Mai 2011);
dataset.addValue(-0.0117083555, value, Jun 2011);
dataset.addValue(0.0096033028, value, Jul 2011);
dataset.addValue(-0.0115565828, value, Aug 2011);
dataset.addValue(-0.0178972929, value, Sep 2011);
dataset.addValue(0.0091575092, value, Okt 2011);
dataset.addValue(-0.0153357532, value, Nov 2011);
dataset.addValue(0.0110588886, value, Dez 2011);
dataset.addValue(0.0148573512, value, Jan 2012);
dataset.addValue(0.0104185378, value, Feb 2012);
dataset.addValue(-0.0038222222, value, Mrz 2012);
dataset.addValue(0.0026768984, value, Apr 2012);
dataset.addValue(-0.006763371, value, Mai 2012);
dataset.addValue(8.063794E-4, value, Jun 2012);
dataset.addValue(0.0189794091, value, Jul 2012);
dataset.addValue(7.028642E-4, value, Aug 2012);
dataset.addValue(0.0049165935, value, Sep 2012);
dataset.addValue(-0.0038441377, value, Okt 2012);
dataset.addValue(0.0055253464, value, Nov 2012);
dataset.addValue(-0.0018316616, value, Dez 2012);
dataset.addValue(0.0015728766, value, Jan 2013);
dataset.addValue(0.0035770372, value, Feb 2013);
dataset.addValue(0.0103451274, value, Mrz 2013);
dataset.addValue(-7.743934E-4, value, Apr 2013);
dataset.addValue(0.0051666236, value, Mai 2013);
dataset.addValue(-0.0231303007, value, Jun 2013);
dataset.addValue(0.0109620275, value, Jul 2013);
dataset.addValue(-0.0020818876, value, Aug 2013);
dataset.addValue(0.0067802503, value, Sep 2013);
dataset.addValue(0.0087204283, value, Okt 2013);
dataset.addValue(0.0053924506, value, Nov 2013);
dataset.addValue(-0.0103865146, value, Dez 2013);
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart = null;
try {
BarRenderer barRenderer = new BarRenderer();
CategoryAxis customCategoryAxis = new CategoryAxisSkipLabels(); // new CategoryAxis("Category");
((CategoryAxisSkipLabels)customCategoryAxis).setTickMarksVisible(true);
((CategoryAxisSkipLabels)customCategoryAxis).setDisplaySkippedTickMarks(true);
// ****************** set Layout Type and AlgorithmType
((CategoryAxisSkipLabels)customCategoryAxis).setAlgorithmType(CategoryAxisSkipLabels.N_STEP_ALGO);
// this must be the last call to category axis as it may return CategoryAxis instead of CategoryAxisSkipLabels
//customCategoryAxis = ((CategoryAxisSkipLabels)customCategoryAxis).setTruncate(true);
customCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
NumberAxis numberAxis = new NumberAxis("Value");
CategoryPlot categoryPlot = new CategoryPlot(
dataset, customCategoryAxis, numberAxis, barRenderer
);
chart = new JFreeChart("Bar Chart", categoryPlot);
} catch (Exception e) {
System.out.print("crash");
}
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
LegendTitle legend = (LegendTitle) chart.getLegend();
legend.setPosition(RectangleEdge.BOTTOM);
legend.setHorizontalAlignment(HorizontalAlignment.LEFT);
legend.setBorder(new BlockBorder(Color.white));
legend.setBackgroundPaint(Color.white);
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator("Tooltip: {0}"));
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
CategoryLabelDemo demo = new CategoryLabelDemo("Bar Chart Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
public static void run(String title) throws Exception {
CategoryLabelDemo demo = new CategoryLabelDemo(title);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
这里我给你一个代码试一下,可能会奏效,会给你想要的。
示例:
final JFreeChart chart = ChartFactory.createStackedBarChart("", "", "",
dataset, PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
plot.getDomainAxis(1).setVisible(false);
尝试删除"类别"轴标签。