java - 系列线在 JFreeChart 中未正确显示

标签 java r charts jfreechart

我正在处理这个图,其中数据和线条未正确显示。当它有更多数据并用鼠标滚轮缩放时,线条无法正确显示。

我尝试将多边形框放置在系列线周围,认为系列会像 XYTextAnnotation 一样显示,但它不起作用。

import java.awt.Color;
import java.awt.BasicStroke;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.Layer;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.ui.TextAnchor;

public class XYLineChart extends ApplicationFrame {

   public XYLineChart(String applicationTitle, String chartTitle) {
      super(applicationTitle);
      JFreeChart xylineChart = ChartFactory.createXYLineChart(
         chartTitle ,
         "Category" ,
         "Score" ,
         createDataset() ,
         PlotOrientation.VERTICAL ,
         false , true , false);

      ChartPanel chartPanel = new ChartPanel( xylineChart );
      chartPanel.setMouseWheelEnabled(true);
      chartPanel.setBackground(Color.WHITE);
      chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
      final XYPlot plot = xylineChart.getXYPlot( );
      plot.setDomainPannable(true);
      plot.setRangePannable(true);
      plot.setBackgroundPaint(Color.WHITE);
      plot.setDomainGridlinePaint(Color.BLACK);
      plot.setRangeGridlinePaint(Color.BLACK);

//annotations
      XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
      renderer.setSeriesPaint( 0 , Color.BLUE );
      renderer.setSeriesStroke( 0 , new BasicStroke( 1.0f ) );

      XYTextAnnotation a1 = new XYTextAnnotation("data",(double)9.1,9);
      a1.setTextAnchor(TextAnchor.CENTER_LEFT);
      a1.setPaint(Color.BLUE);
      renderer.addAnnotation(a1, Layer.BACKGROUND);

      XYTextAnnotation a2 = new XYTextAnnotation("data",(double)28.2,60);
      a2.setTextAnchor(TextAnchor.CENTER_LEFT);
      a2.setPaint(Color.BLUE);
      renderer.addAnnotation(a2, Layer.BACKGROUND);

      plot.setRenderer(renderer);
      setContentPane( chartPanel ); 
   }

   private XYDataset createDataset( ) {
      final XYSeriesCollection dataset = new XYSeriesCollection( );
      final XYSeries data1 = new XYSeries( "Nothing" );
      data1.add(8,9);
      data1.add(9,9);
      data1.add(7,9);
      dataset.addSeries(data1);
      final XYSeries data2 = new XYSeries( "Nothing1" );
      data2.add(20,60);
      data2.add(28,60);
      dataset.addSeries(data2);
      return dataset;
   }

   public static void main( String[ ] args ) {
      XYLineChart chart = new XYLineChart("",
         "");
      chart.pack();
      RefineryUtilities.centerFrameOnScreen( chart );          
      chart.setVisible( true ); 
   }
}

如果我遗漏了什么,请告诉我。

another point

最佳答案

We can see text in first series (blue), but the line has no meaning.

默认情况下,XYLineAndShapeRenderer渲染器“线条和形状都可见”。看起来问题在于短线上的拥挤形状。一种方法是首先渲染行:

XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);

然后让用户根据需要启用渲染一个或多个系列的形状:

 renderer.setSeriesShapesVisible(1, true);

检查了几种方法here 。这个example说明了如何使用 JCheckBox,而这个 example说明单击图例可切换其状态。

image

关于java - 系列线在 JFreeChart 中未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46440116/

相关文章:

mysql - 谷歌图表不画

java - 底部导航图标仅在单击两次后才会更改颜色?

r - 加速矢量创建

python - 在 python 中精确复制 R 文本预处理

javascript - 使用 Highcharts 修正图 TableView

javascript - 我的图表不会实时更新 - 在 Chart.js 和 Javascript 中完成

java - 客户端无法连接到服务器;抛出 IOException。简单的客户端/服务器问题

java - Spring 和 Hibernate 混搭,是 @Entity 的代理对象,并添加了额外的 @Service

java - Java 中可以进行静态元编程吗?

r - 创建一个方阵,使得每个元素都等于 2^|j-k|在 R