使用 jFreeChart 的 StandardXYItemRenderer,我如何设置线条和/或形状的可见性。

huangapple go评论75阅读模式
英文:

with jFreeChart StandardXYItemRenderer how do I set line and/or shape visibility

问题

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.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( );
      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 ) );
      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 ); 
   }
}
英文:

I'm using jFreeChart to create what I thought would be a simple plot. I had to switch from XYLineAndShapeRenderer to StandardXYItemRenderer to prevent the renderer from sorting the series data before plotting it. Now, I can't use setSeriesLinesVisible or setSeriesShapesVisible to control visibility within a series. Here is the code:

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 ); 
}
}

答案1

得分: 1

你使用的是哪个 JFreeChart 版本?

在运行你的测试程序时,我没有看到使用 XYLineAndShapeRendererStandardXYItemRenderer 之间有任何区别。因此,你应该能够使用 XYLineAndShapeRenderer 以及 setSeriesLinesVisible/setSeriesShapesVisible

英文:

Which JFreeChart version do you use?

I don't see any difference between using the XYLineAndShapeRenderer and the StandardXYItemRenderer running your test program. So you should be able to use XYLineAndShapeRenderer and setSeriesLinesVisible/setSeriesShapesVisible.

答案2

得分: 1

Ah,你指导我朝着正确的方向。我没有意识到XYSeries的构造函数需要第二个参数,该参数可以启用(默认)或禁用对数据系列进行排序。谢谢。

英文:

Ah, you pointed me in the right direction. I didn't realize that the XYSeries constructor took a second parameter which enabled (default) or disabled sorting the data series.
Thanks.

huangapple
  • 本文由 发表于 2020年9月5日 22:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63754885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定