系列行未在JFreeChart中正确显示 [英] Series lines not showing up properly in JFreeChart

查看:90
本文介绍了系列行未在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;

公共类XYLineChart扩展了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));
最终XYPlot图= xylineChart.getXYPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);

//注释
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);
}

私人XYDataset createDataset(){
最终XYSeriesCollection数据集=新XYSeriesCollection();
最终XYSeries数据1 =新XYSeries( Nothing);
data1.add(8,9);
data1.add(9,9);
data1.add(7,9);
dataset.addSeries(data1);
最终XYSeries数据2 =新XYSeries( Nothing1);
data2.add(20,60);
data2.add(28,60);
dataset.addSeries(data2);
返回数据集;
}

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

如果我错过了任何东西,请告诉我。



解决方案


我们可以看到第一个系列的文字(蓝色),但该行没有意义。


默认为


I am working with this plot where the data and the lines are not showing properly. When it has more data and while zooming it with mouse wheel, the lines are not showing properly.

I tried placing polygon box around series line thinking that the series will show up like the XYTextAnnotation, but it's not working.

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

In case I am missing anything, please let me know.

解决方案

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

By default, XYLineAndShapeRenderer renderers "with both lines and shapes visible." It looks like crowded shapes on a short line are the problem. One approach would be to render only the lines initially:

XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);

Then let the user enable rendering the shapes for one or more series as desired:

 renderer.setSeriesShapesVisible(1, true);

Several approaches are examined here. This example illustrates using JCheckBox, and this example illustrates clicking on the legend to toggle its state.

这篇关于系列行未在JFreeChart中正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆