Q: Domain axis = Date axis with YIntervalSeries

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Chavadam
Posts: 4
Joined: Sat Mar 03, 2007 4:12 pm
Location: Belgium

Q: Domain axis = Date axis with YIntervalSeries

Post by Chavadam » Fri Dec 20, 2013 10:06 am

Dear JFreeChart connoisseurs,

It is the first time I come in this forum. I'm not a graduated software developer, but only an hobbyist keen of Java, Swing and now JFreeChart, and using them in my free time. I'm using JFreeChart 1.0.10 (with NetBeans IDE v.7.3.1) with enthusiam, but I have a question :

I'm designing a chart based on YIntervalChart-Demo1 with the following codes for the dataset

Code: Select all

    private static IntervalXYDataset createDataset_Sem(Semaine sem)
    {   
        YIntervalSeries sérieHPl = new YIntervalSeries("H. pleines");
                for (short noDeSem = 0 ; noDeSem <= (short) semaines.nbreTotDeSem -1 ; noDeSem++)
        {   
            BigDecimal y_HPl_PMin = semaines.listeSem.get(noDeSem).hPleines_PuissMin;
            BigDecimal y_HPl_PMax = semaines.listeSem.get(noDeSem).hPleines_PuissMax;
            if (y_HPl_PMin != null && y_HPl_PMax != null)
            {
                sérieHPl.add(noDeSem,           // X
                        (y_HPl_PMax.doubleValue() - y_HPl_PMin.doubleValue())/2,      // Y  Any value
                         y_HPl_PMin.doubleValue(),       // Y-low
                         y_HPl_PMax.doubleValue()        // Y-high
                            );
            }
            
        YIntervalSeriesCollection dataset = new YIntervalSeriesCollection();
        dataset.addSeries(sérieHPl);
        // dataset.setDomainIsPointsInTime(true);
        return dataset;
    }
and for the chart construction :

Code: Select all

    private static JFreeChart createYIntervalChart(IntervalXYDataset dataset, String période)
    {
        // Create the chart ...
        chart = ChartFactory.createScatterPlot
            (   "Consommation d'électricité",   // chart title
                période,                  // domain axis label - X : Time
                "Puissance - KW",         // range axis label - Y : Value
                dataset,                  // data
                PlotOrientation.VERTICAL,
                true,                     // Include legend ?
                true,                     // Use tooltips ?
                false                     // Configure chart to generate URLs ?
            );
        
        // Here some customization of the chart ...
        chart.setBackgroundPaint(Color.lightGray);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.darkGray);
        plot.setRangeGridlinePaint(Color.darkGray);
        plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 5.0, 5.0));
        
        // plot.setRenderer(new YIntervalRenderer());
        // YIntervalRenderer rendu = new YIntervalRenderer();
        XYItemRenderer xyitemRend = plot.getRenderer();
        if (xyitemRend instanceof XYLineAndShapeRenderer)
        {
            XYLineAndShapeRenderer xyLSRend = (XYLineAndShapeRenderer) xyitemRend;
            xyLSRend.setBaseShapesVisible(true);
            xyLSRend.setBaseShapesFilled(false);
        }
        
        ValueAxis domainAxis = new DateAxis("Une année");
        // Exception : org.jfree.chart.axis.NumberAxis cannot be cast to org.jfree.chart.axis.DateAxis
     /* DateAxis axis = (DateAxis) plot.getDomainAxis();
          axis.setDateFormatOverride(formateurDeDate2);
      */
        TextTitle titreGraphique = chart.getTitle();
     //   titreGraphique.setFont(new Font(? ?));		// Normal

        return chart;
    }
... and I get the nice chart as I want it, thanks JFreeChart.
Oeps ! I had show you a picture of the chart, but I see it has to be accessible via http ([img]http://image_url[/img]); it cannot from my "Mydocuments" folder in the HDD op my PC ...[img]C:\\Users\\....png[/img]

But I had like to have a domain axis with month dates (jan feb mar ...) instead of numbers of the week in the year (1 2 3 ... 52). How to achieve it ?
How to create a

Code: Select all

    TimeSeriesCollection dataset = new TimeSeriesCollection()
    dataset.addSeries(serieHPl); 
with series of class YIntevalSeries instead of TimeSeries ?
In 'createDataset_Sem(...)', I tried

Code: Select all

    sérieHPl.add(new Week(noDeSem, année), ...
, but this causes
No suitable method found for add(Week,double,double,double)
Is it possible ?

And eventually this : How to get smaller pads for the max and min values of each vertical line ? (XYLineAndShapeRenderer rendu hereabove + ...)

Thanks in advance for your help.
Skype : Chavadam

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Q: Domain axis = Date axis with YIntervalSeries

Post by david.gilbert » Fri Dec 27, 2013 2:37 pm

The ChartFactory.createScatterPlot() method will return you a chart object that has a NumberAxis for the x-axis. You can replace that with a DateAxis very easily:

Code: Select all

plot.setDomainAxis(new DateAxis("X"));
Now the x-values in your dataset will be presented as though they are "milliseconds since 1-Jan-1970" (the encoding used by java.util.Date). So when you create your dataset, you need to somehow compute a millisecond that falls within the week you have a data observation for. One way to do that is to instantiate a Week object, then call the getMiddleMillisecond() method and use that value for your x-value.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked