Search code examples
javajfreechart

Y tags in XYSeriesCollection (JFreeChart)


I have an XY chart where I want to represent X values along some dates. The creation of the dataset is simple:

XYSeries serie = new XYSeries("valor");
for(int i=0;i<lista.size();i++){
    serie.add(i,lista.get(i).getValue());

}
dataset.addSeries(serie);

where serie.add uses as arguments (y.value, x.value). I am representing the x value along time, but it appears as just the index of the arraylist (obvious, I use i as the first parameter). My question is, how could I show dates (or Strings) as the Y values? I know that this possible to be done with a BarChart, for instance:

DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.addValue(51, "series", "Colonel Forbin");
        dataSet.addValue(92, "series", "The Lizards");

Something like this is what I need to do, hope you could help, thanks


Solution

  • As shown here, if the range values in your series represent milliseconds from the Java epoch, a DateAxis should display the values correctly.

    NumberAxis domain = new NumberAxis("X");
    DateAxis range = new DateAxis("Y");
    domain.setAutoRangeIncludesZero(false);
    XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
    XYPlot plot = new XYPlot(dataset, domain, range, renderer);
    JFreeChart chart = new JFreeChart(
        "Title", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    

    Addendum: I just want to set … tags that change dynamically.

    It looks like you want SymbolAxis, seen here.