0

Is it possible having the x-axis labeled with time intervals that are not periodic in a Timeseries-Chart drawn with JFreechart?

I have made up a compilable minimal example. Inside this example are the comments which point out wich intervals i want to have.

What i already tried is extending the RegularTimePeriod-class. Due to some errors i dropped this approach and instead wanted to ask if there is an easier way to do it.

Here is the minimal example (Note: it lacks the imports):

public class MyTimeSeriesGraphMinimalExample {
    public static void main(String args[]) {
        TimeSeries timeseries = new TimeSeries("Series 1");

    //these two should form one interval
    timeseries.add(new Day(1,1,2013),10);//1
    timeseries.add(new Day(5,1,2013),10);//2

    //these two should form one interval
    timeseries.add(new Day(11,1,2013),9);//1
    timeseries.add(new Day(14,1,2013),9);//2
    TimeSeries timeseries1 = new TimeSeries("Series 2");

    //these two should form one interval
    timeseries1.add(new Day(1, 1,2013), 5);//1
    timeseries1.add(new Day(5, 1,2013), 5);//2

    //these two should form one interval
    timeseries1.add(new Day(11, 1,2013), 8);//1
    timeseries1.add(new Day(14, 1,2013), 8);//2

    TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
    timeseriescollection.addSeries(timeseries);
    timeseriescollection.addSeries(timeseries1);
    XYDataset xydataset = timeseriescollection;

    //chart-visual-property-settings
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
            "Time Series Demo 3", "Time", "Value", xydataset, true, true,
            false);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
    dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1,
            new SimpleDateFormat("dd-MMM")));
    dateaxis.setVerticalTickLabels(true);
    XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
            .getRenderer();
    xylineandshaperenderer.setBaseShapesVisible(true);
    xylineandshaperenderer.setSeriesFillPaint(0, Color.red);
    xylineandshaperenderer.setSeriesFillPaint(1, Color.green);
    xylineandshaperenderer.setSeriesPaint(0, Color.red);
    xylineandshaperenderer.setSeriesPaint(1, Color.green);
    xylineandshaperenderer.setUseFillPaint(true);
    xylineandshaperenderer
            .setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator(
                    "Tooltip {0}"));
    //draw
    try {
        ChartUtilities.saveChartAsJPEG(new File("C:/minimalExampleChart.jpeg"),
                jfreechart, 600, 500);
    } catch (Exception e) {
        // TODO: handle exception
    }
        }
}
kiltek
  • 3,183
  • 6
  • 47
  • 70
  • You want labels for days 1, 5, 11 & 14 only? – trashgod Jul 04 '13 at 10:03
  • i am sorry if it wasnt clear enough: no, i want Day 1 to Day 5 to be an interval sharing the same tick on the x-axis. The same with days 11 and 14. the same tick each of these two. – kiltek Jul 04 '13 at 18:55
  • Do you mean like a Gantt chart? – trashgod Jul 04 '13 at 18:59
  • Frankly, Gantt-charts can be extremely varying in appearance, i dont know what kind of Gantt-Chart you reffering to. – kiltek Jul 04 '13 at 19:03
  • A `JFreeChart` Gantt chart, for [example](http://stackoverflow.com/a/8949913/230513). – trashgod Jul 04 '13 at 19:05
  • actually, i also need an y-axis that has numerical values associated to it. The link doesnt seem to accomodate for that. – kiltek Jul 04 '13 at 20:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32906/discussion-between-kiltek-and-trashgod) – kiltek Jul 04 '13 at 21:05

0 Answers0