Search code examples
javachartsjfreechartcategories

Java JFreeChart Category Step Chart horizontal (image to explane)



i need the following type of chart:
It should be a "steped" line chart with categories on the vertical axis, like this:

this is how it shold be

I found this example of an Category Step Chart, but it the orientation is not right for my purpose.

http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/renderer/category/CategoryStepRenderer.html


All i have done so far is this, but as you can see the red line does not fit to the orientation of the chart(should be horizontal):

current state

The corresponding code to this:

   DefaultCategoryDataset ds = new DefaultCategoryDataset(); 

// create dataset

    for (int k = 0; k < ffCount; k++) {
                  StateSignal ss1 = (StateSignal) this.ffDSet.getFframes().get(k).getSignals().get(i);
                  ds.setValue((double) k + 1, ss1.getName(), ss1.getStates().get(0).getStatus());

     }
    CategoryStepRenderer categorysteprenderer = new CategoryStepRenderer(false);
    categorysteprenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    CategoryAxis categoryaxis = new CategoryAxis("Category");
    NumberAxis numberaxis = new NumberAxis("Value");
    CategoryPlot categoryplot = new CategoryPlot(ds, categoryaxis, numberaxis, categorysteprenderer);
    categoryplot.setRangePannable(true);
    categoryplot.setOrientation(PlotOrientation.HORIZONTAL);
    chart = new JFreeChart("test", null, categoryplot, true);

I don´t get it to work. Any ideas?

Thanks in advance!


Solution

  • It looks like you need to use a standard XYLineChart with a XYStepRenderer and a SymbolAxis to replace the default Range Axis rather than a CategoryStepRenderer and a horizontal plot orientation

    If you associate Status A and B with a numerical value say 1 and 2 you can create a chart like this:

    enter image description here

    Using this a XYStepRenderer

      XYStepRenderer renderer = new XYStepRenderer();
      renderer.setBaseShapesVisible(true);
      renderer.setSeriesStroke(0, new BasicStroke(2.0f));
      renderer.setSeriesStroke(1, new BasicStroke(2.0f));
      renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
      renderer.setDefaultEntityRadius(6);
      plot.setRenderer(renderer);
    

    and a Symbol Axis

      String[] grade =  new String[3];
      grade[0] = "";
      grade[1] = "Status A";
      grade[2] = "Status B";
      SymbolAxis rangeAxis = new SymbolAxis("", grade);
      rangeAxis.setTickUnit(new NumberTickUnit(1));
      rangeAxis.setRange(0,3);
      plot.setRangeAxis(rangeAxis);
    

    In this example the SymbolAxis provides an alternative label for each value in the Axis