1

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.

another point

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
ch.Joshi elijah
  • 103
  • 2
  • 11
  • 1
    Please [edit] your question to include a [mcve] that exhibits the problem you describe. Cross-posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&p=182635). – trashgod Sep 27 '17 at 11:30
  • I have a edited to be more clear @trashgod – ch.Joshi elijah Sep 29 '17 at 09:42
  • Your example works as expected for me. I see both series and both annotations. What's wrong? – trashgod Sep 29 '17 at 13:31
  • when we add series with more distance between each series items the plot looks ugly and hardly see those lines.I want to make the plot look professional and clearly.the text showing right but not the lines with mouse wheel change.Am i clear @trashgod – ch.Joshi elijah Sep 30 '17 at 13:06
  • Sorry, not clear at all; _professional_ is too subjective for any useful answer; the chart zooms and pans as expected; as an aside, see [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) and these [sizing alternatives](https://stackoverflow.com/a/10277372/230513). – trashgod Sep 30 '17 at 15:00
  • thanks for you answers so far trashgod, My chart is dynamic if i had an another line and text in place of the red rectangle shown above,the text beside line is showing ok as there are positioned, but the lines are srinking according to the tick of axis.can i make those points plot just like the annotations ? are is there any way to make those lines plot just like annotated text ? – ch.Joshi elijah Oct 03 '17 at 05:29
  • [`SlidingXYDataset`](https://stackoverflow.com/a/46130344/230513)? – trashgod Oct 03 '17 at 11:14
  • am afraid its a no.Please observe text annotations beside lines while zooming in and out they don't move with respective to x and y tick axis,but lines do.I want lines just as annotations.Is it possible ? And can i add some points to you for your response i want to give you some am new hear. – ch.Joshi elijah Oct 03 '17 at 12:37
  • I see the series lines and annotations moving together as I zoom and/or pan. – trashgod Oct 04 '17 at 12:04
  • Yes trashgod,lines move based on tick units of x and y axis so its shrinking, but text move according to that point and anchor(I guess) which is not shrinking. please look at that attached graph we can see text in first series(blue one) but the line has no meaning.My intention is to make that line look like text so that user can clearly see at least not that meaningless line. – ch.Joshi elijah Oct 04 '17 at 12:56

1 Answers1

1

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.

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045