0

In JFreeChart, I want to display multiple lines within the same range. However multiple series are not getting painted:

enter image description here

Code to reproduce:

import org.jfree.chart.ChartPanel;

import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class LineChart_AWT extends ApplicationFrame {

    public LineChart_AWT( String applicationTitle , String chartTitle ) {
          super(applicationTitle);
          JFreeChart lineChart = ChartFactory.createLineChart(
             chartTitle,
             "","",
             createDataset(),
             PlotOrientation.VERTICAL,
             true,true,false);

          CategoryPlot plot = (CategoryPlot) lineChart.getPlot();
          plot.getRangeAxis().setAutoRange(true);
          ((NumberAxis)plot.getRangeAxis()).setAutoRangeIncludesZero(false);
          plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
          plot.setDomainGridlinesVisible(true);

          ChartPanel chartPanel = new ChartPanel( lineChart );
          chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
          setContentPane( chartPanel );
       }

       private DefaultCategoryDataset createDataset( ) {
          DefaultCategoryDataset dataset = new DefaultCategoryDataset( );

          dataset.addValue(1.0,"TypeA", "8");
          dataset.addValue(1.0,"TypeB", "8");
          dataset.addValue(1.0,"TypeA","10");
          dataset.addValue(1.0,"TypeA","11");
          dataset.addValue(1.0,"TypeA","13");
          dataset.addValue(2.0,"TypeA","16");
          dataset.addValue(2.0,"TypeA","18");
          dataset.addValue(2.0,"TypeB","19");
          dataset.addValue(5.0,"TypeB","20");

          return dataset;
       }

       public static void main( String[ ] args ) {
          LineChart_AWT chart = new LineChart_AWT(
             "JFreeChart demo" ,
             "Series are not drawn onto each other");

          chart.pack( );
          RefineryUtilities.centerFrameOnScreen( chart );
          chart.setVisible( true );
       }
    }

Why is typeB not painted from "8"?

If I comment out all of TypeA series, TypeB is displayed correctly: enter image description here

How can I achieve to display both TypeA and TypeB onto each other if needed?

Thanks!

Daniel
  • 2,318
  • 2
  • 22
  • 53
  • 1
    Why not use an `XYDataset` with `createXYLineChart()`? – trashgod Aug 19 '19 at 03:28
  • @trashgod: this is a dynamically generated chart where X values can also be strings. Are string values possible for XYDataset as X? – Daniel Aug 19 '19 at 06:00
  • SymbolAxis seems to be the solution and it might also open doors for optimizing the space between labels on X axes (making them not displaying if there is no space). [This post](https://stackoverflow.com/questions/12835369/java-jfreechart-category-step-chart-horizontal-image-to-explane/12839170#12839170) helped me. – Daniel Aug 19 '19 at 06:07
  • It has its uses; also have a look at `org.jfree.data.jdbc` for helpful abstractions. – trashgod Aug 19 '19 at 18:01

0 Answers0