1

I have faced with a problem of putting date on my JFreeChart graph. I read the double value and date value from my local MySql database but I cannot put date onto the graph.

Here is my useless tryings

    XYSeries series = new XYSeries("Dynamic");

    for (int i = 0; i < ch.size(); i++) {
        series.add(ch.get(i) * * *.get_date() * * *
        ,ch.get(i).get_pro());
    }

    XYDataset xyDataset = new XYSeriesCollection(series);

    JFreeChart chart = ChartFactory.createXYLineChart("Title", "Date", "Buy",
            xyDataset,
            PlotOrientation.VERTICAL,
            true, true, true);
    JFrame add = new DynamicCurrency(ch);
    add.getContentPane().add(new ChartPanel(chart));
    add.setVisible(true);

I bold the place where the mistake occurs. Thanks a lot

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

You can use the getTime() method which returns a long and cast it to double:

series.add((double) ch.get(i).get_date().getTime(), ch.get(i).get_pro());
amrfaissal
  • 1,060
  • 1
  • 7
  • 18
  • Do you know whether is ts possible to convert date to normal format when showing it on graph? – user2491039 Jun 16 '13 at 15:59
  • Thanks once more for your advice but I again have a mistake=( – user2491039 Jun 16 '13 at 17:53
  • I add DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss")); – user2491039 Jun 16 '13 at 17:53
  • and got org.jfree.chart.axis.NumberAxis cannot be cast to org.jfree.chart.axis.DateAxis – user2491039 Jun 16 '13 at 17:54
  • The `getDomainAxis()` method must return a `ValueAxis` which is a superclass of `NumberAxis` and `DateAxis`. – amrfaissal Jun 16 '13 at 19:11
  • I see but nevertheless I got this mistake – user2491039 Jun 16 '13 at 19:28
  • org.jfree.chart.axis.NumberAxis cannot be cast to org.jfree.chart.axis.DateAxis – user2491039 Jun 16 '13 at 19:29
  • import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; – user2491039 Jun 16 '13 at 19:57
  • DateFormat TIMESTAMP = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ValueAxis axis = (ValueAxis) chart.getXYPlot().getDomainAxis(); ((DateAxis) axis).setDateFormatOverride(TIMESTAMP); – user2491039 Jun 16 '13 at 19:59