display of graphs using data from database : Some Issues

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sang

display of graphs using data from database : Some Issues

Post by sang » Fri May 17, 2002 12:32 am

Hi,
I am working on dynamically displaying the graphs by retrieving data from the oracle database based on different queries. My aim is that with the same data retrieved I should be able to display it in different types of graphs, this is something like we have in the JFreeChartDemo eample but there we have fixed data for the graphs. I tried with the code but was not able to figure out how to go about it. Since different type of graphs need different types of datasets as input. I have some questions :
- Is there some restrictions on the data that a particular type of graph can uses for display
- Are some datautilities under development/developed which I am not aware of to convert data to make it suitable for other type of graphs.
- I looked at CVS code dowladed the JdbcCategoryDataset, JdbcPieDataset and JdbcXYPlotDataset, PieDataset, DefaultPieDataset but was not able to compile and run the previous example code for Piechart examples. What other changed files I need to download to compile the code for these new Jdbc... files

- The release code works fine for each of the graph.

I will appreciate if anybody gives some suggestions on this.

Thanks
Sang

Bryan

Re: display of graphs using data from database : Some Issues

Post by Bryan » Fri May 17, 2002 3:08 am

Sang

I am actually working on the jdbc datasets today. I have found a couple of problems with them, which I have fixed. I will probably send DG an update tomorrow with these changes for inclusion in the CVS. These though should not have stopped you compiling. I have not been writing against the CVS, but rather against the 0.8.1 release base.

Bryan

Andrew Perepelytsya

Re: display of graphs using data from database : Some Issues

Post by Andrew Perepelytsya » Fri May 17, 2002 7:22 pm

Hi, there.

Concerning using more or less the same data for different graphs. Here's some code snippet from one of my servlets. My web app retrieves data dynamically from some remote site, parses it and then uses it in the main workflow.

===============================

...

JFreeChart chart = null;
Dataset data = null;

try { // Catch NullPointerException
// If any of the Datasets is null, i.e. was not set

switch (chartType) {
case Chart.HILO_CHART:
data = (HighLowDataset) session.getAttribute("com.dronski.stock.HiLowDataset");
chart = ChartFactory.createHighLowChart(
symbol,
Chart.HORIZONTAL_LABEL,
Chart.VERTICAL_LABEL,
(HighLowDataset) data,
Chart.LEGEND);
break;

case Chart.CANDLE_CHART:
data = (HighLowDataset) session.getAttribute("com.dronski.stock.HiLowDataset");
ValueAxis timeAxis = new HorizontalDateAxis(Chart.HORIZONTAL_LABEL);
NumberAxis valueAxis = new VerticalNumberAxis(Chart.VERTICAL_LABEL);
XYPlot plot = new XYPlot(timeAxis, valueAxis);
plot.setXYItemRenderer(new CandlestickRenderer(8));
chart = new JFreeChart(data, plot, symbol, JFreeChart.DEFAULT_TITLE_FONT, Chart.LEGEND);
chart.getPlot().setSeriesPaint(new Paint[] { Chart.CANDLE_STROKE_COLOR });
/*
chart = ChartFactory.createCandlestickChart(
symbol,
Chart.HORIZONTAL_LABEL,
Chart.VERTICAL_LABEL,
(HighLowDataset) data,
Chart.LEGEND);
*/
break;

case Chart.TIME_SERIES_CHART:
TimeSeriesCollection seriesData = (TimeSeriesCollection) session.getAttribute("com.dronski.stock.TimeSeriesCollection");
// Provide moving average only if we have more than 30 days of data
if (seriesData.getItemCount(0) > 30) {
MovingAveragePlotFitAlgorithm mavg = new MovingAveragePlotFitAlgorithm();
mavg.setPeriod(30);
PlotFit pf = new PlotFit((XYDataset) data, mavg);
data = pf.getFit();
}
chart = ChartFactory.createTimeSeriesChart(
symbol,
Chart.HORIZONTAL_LABEL,
Chart.VERTICAL_LABEL,
seriesData,
Chart.LEGEND);
break;
}
} // END try{}

catch (NullPointerException npex) {
// if we have no dataset, i.e. session wasn't created or due to an error
if (data == null) {
response.reset();
response.setContentType("text/plain");
out.write("Error: no data specified for a chart.".getBytes());
out.close();
return;
}
}

Andrew Perepelytsya

Re: display of graphs using data from database : Some Issues

Post by Andrew Perepelytsya » Fri May 17, 2002 7:31 pm

Impossible to present the source nicely, but...

Ok, the main idea behind all this is all datasets implement the Dataset interface, so you treat them more or less the same way. When you need some particular functionality, downcast the dataset.

I have a class parsing the stock data (HLOCV). It's methods allow me to take only the data I need for my graphs. Usually it's every field for stock charts, and Date[] and closePrice[] arrays for any other. This way I can plot charts like HiLowClose, Candlestick, TimeSeries, some others from one and the same datasource.
(the class uses gnu-regexp package if somebody's interested)

Though, I'm not sure if it is applicable to PieCharts. In general, it works for me, I'd be happy if it works for somebody else.

Ok, hope this helps a bit ;) Cheers

sang

Re: display of graphs using data from database : Some Issues

Post by sang » Fri May 17, 2002 7:57 pm

creating data separtely for each chart is fine, but I want to use the same data to display different types of chart, for example same data as input for bar chart and pie chart, where bar chart uses categoryDataset and pieChart uses pieDataset. The data comes as a result of oracle query.

Thanks
Sang

sang

Re: display of graphs using data from database : Some Issues

Post by sang » Fri May 17, 2002 8:21 pm

I will try this
Thanks
Sangeeta

David Gilbert

Re: display of graphs using data from database : Some Issues

Post by David Gilbert » Fri May 17, 2002 9:29 pm

There are a couple of methods in the DatasetUtilities class that create a PieDataset from a subset of the data in a CategoryDataset.

Regards,

DG.

Locked