在Java中创建时尚图表,例如使用JFreeChart。

7
什么是在Java中创建漂亮图表的最佳方式?看起来绘图的主要选项是JFreeChart,但不幸的是,它们默认情况下看起来相当平淡。
将JFreeChart的样本http://www.jfree.org/jfreechart/images/PriceVolumeDemo1.png与Javascript绘图库之一进行比较,例如http://www.highcharts.com/demo/spline-symbols/gridhttp://people.iola.dk/olau/flot/examples/graph-types.html
Javascript的图表看起来更好 - 它们具有平滑的线条,漂亮的默认字体,总体而言比JFreeChart好看得多,后者看起来非常普通。
是否有在JFreeChart基础上构建的图表库可以默认情况下看起来不错,或者有些示例代码可以使普通的JFreeChart图表(例如折线图)看起来很棒?
5个回答

13

我有同样的问题。

这段代码可以让JFreeChart看起来像Highcharts(目前只支持条形图)。它可以很容易地变得更加高效 :)

    String fontName = "Lucida Sans";
    JFreeChart chart = ChartFactory.createBarChart(null, "", "", dataset, PlotOrientation.VERTICAL, false, true, false );

    StandardChartTheme theme = (StandardChartTheme)org.jfree.chart.StandardChartTheme.createJFreeTheme();

    theme.setTitlePaint( Color.decode( "#4572a7" ) );
    theme.setExtraLargeFont( new Font(fontName,Font.PLAIN, 16) ); //title
    theme.setLargeFont( new Font(fontName,Font.BOLD, 15)); //axis-title
    theme.setRegularFont( new Font(fontName,Font.PLAIN, 11));
    theme.setRangeGridlinePaint( Color.decode("#C0C0C0"));
    theme.setPlotBackgroundPaint( Color.white );
    theme.setChartBackgroundPaint( Color.white );
    theme.setGridBandPaint( Color.red );
    theme.setAxisOffset( new RectangleInsets(0,0,0,0) );
    theme.setBarPainter(new StandardBarPainter());
    theme.setAxisLabelPaint( Color.decode("#666666")  );
    theme.apply( chart );
    chart.getCategoryPlot().setOutlineVisible( false );
    chart.getCategoryPlot().getRangeAxis().setAxisLineVisible( false );
    chart.getCategoryPlot().getRangeAxis().setTickMarksVisible( false );
    chart.getCategoryPlot().setRangeGridlineStroke( new BasicStroke() );
    chart.getCategoryPlot().getRangeAxis().setTickLabelPaint( Color.decode("#666666") );
    chart.getCategoryPlot().getDomainAxis().setTickLabelPaint( Color.decode("#666666") );
    chart.setTextAntiAlias( true );
    chart.setAntiAlias( true );
    chart.getCategoryPlot().getRenderer().setSeriesPaint( 0, Color.decode( "#4572a7" ));
    BarRenderer rend = (BarRenderer) chart.getCategoryPlot().getRenderer();
    rend.setShadowVisible( true );
    rend.setShadowXOffset( 2 );
    rend.setShadowYOffset( 0 );
    rend.setShadowPaint( Color.decode( "#C0C0C0"));
    rend.setMaximumBarWidth( 0.1);

在此输入图片描述


9
试试 XChart。XChart 是一个轻量级的 Java 库,用于绘制数据图表,可以作为 JFreeChart 的潜在替代品。它的重点在于简单性,并没有 JFreeChart 拥有的全部功能,但它提供了丰富的图表特性,包括主题(Themes),可应用不同的“外皮”到图表上。您可以通过实现一个接口并将其应用到 Chart 中来创建自己的 Theme 并调用 chart.setTheme(myTheme)。截至 2.0.0 版本发布时,该 jar 文件仅 ~86 KB,没有任何依赖关系。它使用 Apache 2.0 许可证,并托管在 Github 上。一些屏幕截图可以在这里找到。免责声明:我是该项目的首席开发人员。

enter image description here


1
非常 impressed 关于 xchart。强烈推荐给所有需要简单图表工具的人。感谢您的工作! - ruX

3

http://www.jfree.org/jfreechart/samples.html

在这里,您可以找到许多示例(您需要下载JFreeChart演示版(Web Start))。在使用jFreeChart进行一些工作后,我考虑转向EasyChart(请参见:http://www.objectplanet.com/easycharts/examples.html),但实际上它看起来非常类似于jFreeChart。 JFreeChart相当容易编写,但我不知道EasyChart怎么样。但是根据您的问题,在JFreeChart中更改字体,LineRenderer或任何设计中的内容都没有问题,因此您可以将其修改为与您从JavaScript发布的内容完全相同。


1
我原以为有一种方法可以改变大部分样式,比如字体和线条渲染 - 我想我希望能够得到一些真正漂亮的图表示例代码,这样就不必花太多时间去尝试了。JFreeChart Web Start演示中展示的图表是一个很好的开始,他们应该在示例页面上使用这些截图。 - user1043466
还可以参考这个答案,以及org.jfree.chart.annotations - trashgod

2

0

如果您稍作编辑,上述代码将正常工作,但没有BarRenderer。经过编辑的代码也适用于饼图和折线图。

StandardChartTheme theme = new StandardChartTheme(ch.toString());

theme.setTitlePaint( Color.decode( "#4572a7" ) );
theme.setRangeGridlinePaint( Color.decode("#C0C0C0"));
theme.setPlotBackgroundPaint( Color.BLACK );
theme.setChartBackgroundPaint( Color.BLACK );
theme.setGridBandPaint( Color.red );
theme.setAxisOffset( new RectangleInsets(0,0,0,0) );
theme.setBarPainter(new StandardBarPainter());
theme.setAxisLabelPaint( Color.decode("#666666")  );
theme.apply( ch );
ch.getCategoryPlot().setOutlineVisible( false );
ch.getCategoryPlot().getRangeAxis().setAxisLineVisible( false );
ch.getCategoryPlot().getRangeAxis().setTickMarksVisible( false );
ch.getCategoryPlot().setRangeGridlineStroke( new BasicStroke() );
ch.getCategoryPlot().getRangeAxis().setTickLabelPaint( Color.decode("#666666") );
ch.getCategoryPlot().getDomainAxis().setTickLabelPaint( Color.decode("#666666") );
ch.setTextAntiAlias( true );
ch.setAntiAlias( true );
ch.getCategoryPlot().getRenderer().setSeriesPaint( 0, Color.decode( "#4572a7" ));

// Note- The ch is a chart variable.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接