Bar Chart with months and double values only

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mmrs
Posts: 1
Joined: Sun Apr 11, 2010 4:16 pm
antibot: No, of course not.

Bar Chart with months and double values only

Post by mmrs » Sun Apr 11, 2010 4:47 pm

Hi all,
I'm new to JFree Chart. And I'm trying to construct one with months [jan, feb, mar, ...] as the x axis
and double values (balances) for those months .
the problem is that i need only months on the x- axis.

i tried this :

public JFreeChart createChart(String title) {
TimeSeries time = new TimeSeries("Annual", "Month", "Value", Year.class);
time.add(new Month(Month.JANUARY ,null), new Double(50.1));
time.add(new Month(Month.FEBRUARY ,null), new Double(62.1));
time.add(new Month(Month.MARCH ,null), new Double(15.1));
time.add(new Month(Month.APRIL ,null), new Double(59.1));
time.add(new Month(Month.MAY ,null), new Double(43.1));
time.add(new Month(Month.JUNE ,null), new Double(95.1));
time.add(new Month(Month.JULY,null), new Double(19.1));
time.add(new Month(Month.AUGUST,null), new Double(29.1));

TimeSeriesCollection timeData = new TimeSeriesCollection(time);
timeData.setDomainIsPointsInTime(false);

JFreeChart chart = ChartFactory.createXYBarChart(
title,
"Periods",
true,
"Balances",
timeData,
PlotOrientation.VERTICAL,
true,
false,
false
);

return chart;
}



but didn't work out .

i know this might silly thread ^^", but to me help is highly appreciated
thanks in advance.

gengar
Posts: 26
Joined: Thu Mar 18, 2010 12:28 am
antibot: No, of course not.

Re: Bar Chart with months and double values only

Post by gengar » Tue Apr 13, 2010 12:53 am

You don't need a time series for this, just use DefaultCategoryDataset for your bar chart.
Make sure you add values to the dataset in the order from Jan to Dec.

It would be something like this:

Code: Select all

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, "series 1", "Jan");
dataset.addValue(2.0, "series 1", "Feb");
dataset.addValue(3.0, "series 1", "Mar");
dataset.addValue(4.0, "series 1", "April");
dataset.addValue(5.0, "series 1", "May");
...

JFreeChart chart = ChartFactory.createBarChart(
            "Title",
            "Month",
            "Value",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
);

Locked