【问题标题】:Create stylish charts in Java eg with JFreeChart在 Java 中创建时尚的图表,例如使用 JFreeChart
【发布时间】:2012-01-06 01:56:20
【问题描述】:

在 Java 中创建漂亮图表的最佳方法是什么?看起来图表的主要选项是 JFreeChart,但不幸的是默认情况下它们看起来很简单。

比较一个 JFreeChart 示例:http://www.jfree.org/jfreechart/images/PriceVolumeDemo1.png 使用 Javascript 图表库之一,例如 http://www.highcharts.com/demo/spline-symbols/grid 或 http://people.iola.dk/olau/flot/examples/graph-types.html

javascript 看起来更好——它们有流畅的线条,默认字体很好,与看起来很普通的 JFreeChart 相比,整体看起来不错。

是否有基于 JFreeChart 构建的图表库,默认情况下看起来不错,或者是否有一些示例代码可以使普通的 JFreeChart 图表(例如折线图)看起来很棒?

【问题讨论】:

    标签: java charts styles jfreechart


    【解决方案1】:

    我有同样的问题。

    这段代码使 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);
    

    【讨论】:

      【解决方案2】:

      试试XChart。 XChart 是一个用于绘制数据的轻量级 Java 库,可以替代 JFreeChart。它的重点是简单性,并不具备 JFreeChart 的所有功能,但它提供了一组丰富的图表功能,包括将不同“皮肤”应用于图表的主题。您可以通过调用chart.setTheme(myTheme) 实现接口并将其应用于图表来轻松创建自己的主题。该 jar 在 2.0.0 版本中只有 ~86 KB,并且没有依赖关系。它在 Apache 2.0 下获得许可,并托管在 Github 上。一些截图可以找到here。免责声明:我是该项目的主要开发者。

      【讨论】:

      • 对 xchart 印象深刻。强烈推荐给所有需要简单图形工具的人。感谢您的工作!
      【解决方案3】:

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

      在那里您可以找到许多示例(您需要下载一个 JFreeChart Demo (web start))。 在使用 jFreeChart 进行一些工作后,我正在考虑迁移到 EasyChart(关注:http://www.objectplanet.com/easycharts/examples.html),但实际上它看起来与 jFreeChart 非常相似。 JFreeChart 很容易写,我不知道 EasyChart。

      但根据您的问题,在 JFreeChart 中更改字体、LineRenderer 或任何内容都没有问题,因此您可以将其修改为与您从 JavaScript 发布的完全一样。

      【讨论】:

      • 我认为有一种方法可以改变字体和线条渲染等大部分样式 - 我想我希望有一些非常漂亮的图表的示例代码可以让我不必玩弄它太多了。 JFreeChart 的 web start demo 中的展示图表是一个好的开始,他们应该使用示例页面上的截图。
      【解决方案4】:

      【讨论】:

        【解决方案5】:

        如果您稍作编辑,但没有 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.
        

        【讨论】:

          猜你喜欢
          • 2012-04-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-11
          • 1970-01-01
          相关资源
          最近更新 更多