使用jfreechart standardxyitemrenderer如何设置线条和/或形状可见性

k10s72fa  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(443)

我用jfreechart来创建一个简单的情节。我不得不从 XYLineAndShapeRendererStandardXYItemRenderer 防止渲染器在打印序列数据之前对其进行排序。现在,我不能用 setSeriesLinesVisible 或者 setSeriesShapesVisible 控制系列中的可见性。代码如下:

package usexyseries;

import java.awt.Color; 
import java.awt.BasicStroke; 

import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.ui.ApplicationFrame; 
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.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeriesCollection; 

public class UseXYSeries extends ApplicationFrame {

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

      ChartPanel chartPanel = new ChartPanel( xylineChart );
      chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
      final XYPlot plot = xylineChart.getXYPlot( );
// this renderer sorted the series data      
//      XYLineAndShapeRenderer  renderer = new XYLineAndShapeRenderer ( );
// this renderer can't set the visibility of lines and shapes (sigh!)
      StandardXYItemRenderer  renderer = new StandardXYItemRenderer ( );
      renderer.setSeriesPaint( 0 , Color.RED );
      renderer.setSeriesPaint( 1 , Color.GREEN );
      renderer.setSeriesPaint( 2 , Color.YELLOW );
      renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
      renderer.setSeriesStroke( 1 , new BasicStroke( 3.0f ) );
      renderer.setSeriesStroke( 2 , new BasicStroke( 2.0f ) );
//      renderer.setSeriesLinesVisible(0, true);
//      renderer.setSeriesLinesVisible(1, true);
//      renderer.setSeriesLinesVisible(2, false);
//      renderer.setSeriesShapesVisible(0, false);
//      renderer.setSeriesShapesVisible(1, false);
//      renderer.setSeriesShapesVisible(2, false);
      plot.setRenderer( renderer ); 
      setContentPane( chartPanel ); 
   }

   private XYDataset createDataset( ) {
      final XYSeries firefox = new XYSeries( "Firefox", false );          
      firefox.add( 3.0 , 4.0 );          
      firefox.add( 5.0 , 3.0 );          
      firefox.add( 1.0 , 1.0 );          
      firefox.add( 1.0 , 3.0 );          

      final XYSeries chrome = new XYSeries( "Chrome" );          
      chrome.add( 1.0 , 4.0 );          
      chrome.add( 2.0 , 5.0 );          
      chrome.add( 3.0 , 6.0 );          

      final XYSeries iexplorer = new XYSeries( "InternetExplorer" );          
      iexplorer.add( 3.0 , 4.0 );          
      iexplorer.add( 4.0 , 5.0 );          
      iexplorer.add( 5.0 , 4.0 );          

      final XYSeriesCollection dataset = new XYSeriesCollection( );          
      dataset.addSeries( firefox );          
      dataset.addSeries( chrome );          
      dataset.addSeries( iexplorer );
      return dataset;
   }

   public static void main( String[ ] args ) {
      UseXYSeries chart = new UseXYSeries("Browser Usage Statistics",
         "Which Browser are you using?");
      chart.pack( );          
      RefineryUtilities.centerFrameOnScreen( chart );          
      chart.setVisible( true ); 
   }
}
cnwbcb6i

cnwbcb6i1#

您使用哪个jfreechart版本?
我看不出使用 XYLineAndShapeRenderer 以及 StandardXYItemRenderer 运行测试程序。所以你应该可以使用 XYLineAndShapeRenderer 以及 setSeriesLinesVisible / setSeriesShapesVisible .

uttx8gqw

uttx8gqw2#

啊,你给我指出了正确的方向。我没有意识到xyseries构造函数使用了第二个参数,该参数启用(默认)或禁用了对数据序列的排序。谢谢。

相关问题