Some problems while creating chart

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

Some problems while creating chart

Post by Avijit » Fri Aug 16, 2002 11:11 pm

Hi:

While I was trying to create a timeseries and a verticalXYBar chart, I am facing some problems,regarding which I have no clue atall.
This is the piece of code I am using to generate two types of graph, depending on the type user specifies:

import com.jrefinery.chart.*;
import com.jrefinery.chart.tooltips.*;
import com.jrefinery.chart.data.*;
import com.jrefinery.data.*;
import com.jrefinery.date.*;
import java.awt.Color;
import java.awt.GradientPaint;

boolean isData = (rpSupTrends.size() > 0);
if (isData)
{
createSupTrendImage(tempRec.mScoringType, rpSupTrends);
}
else
{
showError();
}

protected void createSupTrendImage(int scoringType, Vector rpSupTrends)
throws XCException,XCDoNotProceedException
{
try
{
TimeSeriesCollection data = new TimeSeriesCollection();
BasicTimeSeries t1 = null;
XCUserMgr userMgr = getUserMgr();
CompanyRecord companyRec = userMgr.listCompany(new Long(reqParams.mSupplierID));
JFreeChart chart = null;
if (scoringType == TemplateAttrRecord.SCORETYPE_SCORING)
{
t1 = new BasicTimeSeries("Supplier scoring");
for (int j = 0; j < rpSupTrends.size(); j++)
{
XCRptSupPerfTrend rpTrend = (XCRptSupPerfTrend)rpSupTrends.elementAt(j);
t1.add(new Day((java.util.Date)rpTrend.mScoreDate), rpTrend.mScore);
}
data.addSeries(t1);
XYDataset xyData = data;
chart = ChartFactory.createTimeSeriesChart(companyRec.mName, "Date", "Value",
xyData, true);
XYPlot plot = chart.getXYPlot();
NumberAxis axis = (NumberAxis)plot.getRangeAxis();
axis.setLabel("Supplier scoring");
axis.setAutoRangeIncludesZero(false);
HorizontalDateAxis haxis = (HorizontalDateAxis)plot.getDomainAxis();
haxis.setAutoTickUnitSelection(false);
haxis.setTickUnit(new DateUnit(Calendar.MONTH, 1));
haxis.getTickLabelFormatter().applyPattern("MMM-yyyy");
haxis.setVerticalTickLabels(true);
}
else
{
t1 = new BasicTimeSeries("Supplier rating");
for (int j = 0; j < rpSupTrends.size(); j++)
{
XCRptSupPerfTrend rpTrend = (XCRptSupPerfTrend)rpSupTrends.elementAt(j);
t1.add(new Day((java.util.Date)rpTrend.mScoreDate), rpTrend.mScore);
}
data.addSeries(t1);
IntervalXYDataset xyData = data;
chart = ChartFactory.createVerticalXYBarChart(companyRec.mName, "Date", "Rating",
xyData, true);

XYPlot plot = chart.getXYPlot();
HorizontalDateAxis haxis = (HorizontalDateAxis)plot.getDomainAxis();
haxis.setAutoTickUnitSelection(false);
haxis.setTickUnit(new DateUnit(Calendar.MONTH, 1));
haxis.getTickLabelFormatter().applyPattern("MMM-yyyy");
haxis.setVerticalTickLabels(true);

NumberAxis na = (NumberAxis) plot.getRangeAxis();
na.setTickUnit(new NumberTickUnit(100.0, new java.text.DecimalFormat("000")));
}

int width = 600;
int height = 400;

String imgFilePath = XCConfig.getRequiredString(XCRptUtil.CONFIG_REPORT_IMAGES_DIR);
String fileName = XCUniqueStringAllocator.allocUniqueString(SUP_TREND_IMAGE);
setSessionValue("FileName", fileName);
fileName = imgFilePath + fileName;
File file = new File(fileName);
ChartUtilities.saveChartAsJPEG(file, chart, width, height);
}
catch(exception e)
{
}
}

Now the problems are
1. a) When there is a single data for a supplier,the TimeSeries graph does not show anything.Say, the query returns the following result:

value date
25 08/05/02

the graph places a horizontal gradient(dotted line) at 25 on the Y-axis and on the x-axis it does not show the date.What I was looking for is, like a dot, at the value of 25, that matches the color in the legend for the supplier.
b) In the case of a verticalXYBar chart I get a bar as wide as the graph.However, as the number of values returned increases, the width gradually diminishes.Can I have the width of the bar same, irrespective of the number of values returned?
2. When I have data spanning across months,I am getting x-axis labels like jan-2002,feb-2002 etc as per my code. But when I have a report with all the values within a single month,the x-axis is not showing any date.
3.I want to put a log line of test at the bottom of the chart, and I was using the following code, but the line is going out of the chart (because, may be it does not fit in the chart ).So,can I alling it properly in may be a multiline test string if required?

String DisclaimerStr = "...scorecard data cannot be directly compared because performance data for the selected suppliers is based on different scorecard templates";
TextTitle Disclaimer = new TextTitle(DisclaimerStr, new Font("SansSerif", Font.PLAIN, 12));
Disclaimer.setPosition(TextTitle.BOTTOM);
Disclaimer.setHorizontalAlignment(TextTitle.CENTER);
chart.addTitle(Disclaimer);

Please help me in suggesting any possible solution in these problems.I am a biginner in Java and JFreechart.
Thanks
-avijit

David Gilbert

Re: Some problems while creating chart

Post by David Gilbert » Mon Aug 19, 2002 9:30 am

Avijit wrote:
> Now the problems are
> 1. a) When there is a single data for a supplier,the
> TimeSeries graph does not show anything.Say, the query
> returns the following result:
>
> value date
> 25 08/05/02
>
> the graph places a horizontal gradient(dotted line) at 25 on
> the Y-axis and on the x-axis it does not show the date.What I
> was looking for is, like a dot, at the value of 25, that
> matches the color in the legend for the supplier.

The time series chart just draws lines between points, and it doesn't handle the case where there is just one point. You could try changing the renderer to draw shapes and lines:

XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES, new StandardXYToolTipGenerator()));
XYPlot plot = myChart.getXYPlot();
plot.setXYItemRenderer(renderer);


> b) In the case of a verticalXYBar chart I get a bar as wide
> as the graph.However, as the number of values returned
> increases, the width gradually diminishes.Can I have the
> width of the bar same, irrespective of the number of values
> returned?

No, because the bar widths are determined by the dataset. Take a look at the getStartXValue(...) and getEndXValue(...) methods in the IntervalXYDataset interface for details.

> 2. When I have data spanning across months,I am getting
> x-axis labels like jan-2002,feb-2002 etc as per my code. But
> when I have a report with all the values within a single
> month,the x-axis is not showing any date.

You've set a fixed tick unit size of one month in your code. JFreeChart won't override that.

> 3.I want to put a log line of test at the bottom of the
> chart, and I was using the following code, but the line is
> going out of the chart (because, may be it does not fit in
> the chart ).So,can I alling it properly in may be a multiline
> test string if required?
>
> String DisclaimerStr = "...scorecard data cannot be directly
> compared because performance data for the selected suppliers
> is based on different scorecard templates";
> TextTitle Disclaimer = new TextTitle(DisclaimerStr, new
> Font("SansSerif", Font.PLAIN, 12));
> Disclaimer.setPosition(TextTitle.BOTTOM);
>
> Disclaimer.setHorizontalAlignment(TextTitle.CENTER);
> chart.addTitle(Disclaimer);
>

At the moment, the best you could do is split the text and add two subtitles. At some point, the TextTitle class will be improved to allow for multiline text.

Regards,

DG

Locked