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.