example of Bar Chart(between value and time)?

Discussion about JFreeChart related to stockmarket charts.
Locked
ahmadgee
Posts: 3
Joined: Fri Feb 29, 2008 2:56 pm

example of Bar Chart(between value and time)?

Post by ahmadgee » Fri Feb 29, 2008 3:01 pm

Hi every body,

I am looking an example of Bar Chart(between value and time). I have explored the JFreeChart but there is example of Bar Chart(between value and category). Can any body provide me the usefull information or example?

Thanks
Best regards,

RoyW
Posts: 93
Joined: Wed Apr 23, 2008 7:42 pm
Contact:

Time is relative

Post by RoyW » Sat Jul 12, 2008 4:10 am

Time changes...
So time = ?, Years, Months, Days, Seconds, Milliseconds...?

Code: Select all

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.TooltipChartPanel;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;

import javax.swing.*;
import java.awt.*;

public class BarChartTime extends JFrame {
    public BarChartTime() {
        super("BarChartTime");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DateAxis domainAxis      = new DateAxis("Date");
        NumberAxis rangeAxis     = new NumberAxis("y");
        XYBarRenderer renderer   = new XYBarRenderer(0.1);
        XYDataset dataset        = getMonthDataset();
//        XYDataset dataset        = getDayDataset();
//        XYDataset dataset        = getSecondDataset();

        XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

        mainPlot.setDomainGridlinesVisible(true);
        domainAxis.setTickMarkPosition( DateTickMarkPosition.MIDDLE );

        JFreeChart chart = new JFreeChart(null, null, mainPlot, false);
        ChartPanel chartPanel = new TooltipChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(800, 300));

        this.add(chartPanel);
        this.pack();
    }

    private XYDataset getMonthDataset() {

            TimeSeries s1 = new TimeSeries("Series 1", Month.class);
            s1.add(new Month(1, 2001), 142);
            s1.add(new Month(2, 2001), 181);
            s1.add(new Month(3, 2001), 167);
            s1.add(new Month(4, 2001), 153);
            s1.add(new Month(5, 2001), 167);
            s1.add(new Month(6, 2001), 158);
            s1.add(new Month(7, 2001), 148);
            s1.add(new Month(8, 2001), 153);
            s1.add(new Month(9, 2001), 142);
            s1.add(new Month(10, 2001), 123);
            s1.add(new Month(11, 2001), 131);
            s1.add(new Month(12, 2001), 139);


            final TimeSeriesCollection dataset = new TimeSeriesCollection();
            dataset.addSeries(s1);

            return dataset;
    }

    private XYDataset getDayDataset() {

            TimeSeries s1 = new TimeSeries("Series 1", Day.class);
            s1.add(new Day(1, 1, 2001), 142);
            s1.add(new Day(2, 1, 2001), 181);
            s1.add(new Day(3, 1, 2001), 167);
            s1.add(new Day(4, 1, 2001), 153);
            s1.add(new Day(5, 1, 2001), 167);
            s1.add(new Day(6, 1, 2001), 158);
            s1.add(new Day(7, 1, 2001), 148);
            s1.add(new Day(8, 1, 2001), 153);
            s1.add(new Day(9, 1, 2001), 142);
            s1.add(new Day(10, 1, 2001), 123);
            s1.add(new Day(11, 1, 2001), 131);
            s1.add(new Day(12, 1, 2001), 139);


            final TimeSeriesCollection dataset = new TimeSeriesCollection();
            dataset.addSeries(s1);

            return dataset;
    }
    private XYDataset getSecondDataset() {

            TimeSeries s1 = new TimeSeries("Series 1", Second.class);
            s1.add(new Second(1, 1, 1, 1, 1, 2001), 142);
            s1.add(new Second(2, 1, 1, 1, 1, 2001), 181);
            s1.add(new Second(3, 1, 1, 1, 1, 2001), 167);
            s1.add(new Second(4, 1, 1, 1, 1, 2001), 153);
            s1.add(new Second(5, 1, 1, 1, 1, 2001), 167);
            s1.add(new Second(6, 1, 1, 1, 1, 2001), 158);
            s1.add(new Second(7, 1, 1, 1, 1, 2001), 148);
            s1.add(new Second(8, 1, 1, 1, 1, 2001), 153);
            s1.add(new Second(9, 1, 1, 1, 1, 2001), 142);
            s1.add(new Second(10, 1, 1, 1, 1, 2001), 123);
            s1.add(new Second(11, 1, 1, 1, 1, 2001), 131);
            s1.add(new Second(12, 1, 1, 1, 1, 2001), 139);


            final TimeSeriesCollection dataset = new TimeSeriesCollection();
            dataset.addSeries(s1);

            return dataset;
    }

    public static void main(String[] args) {
        new BarChartTime().setVisible(true);
    }
}
(btw i never use the supplied default datasets/dataseries, it is much better to implement your own)
The answer does not come from thinking outside the box, rather the answer comes from realizing the truth; There is no Box. my js site

gdongre
Posts: 2
Joined: Mon Jul 21, 2008 6:13 pm

Post by gdongre » Wed Jul 30, 2008 12:01 pm

Hi,
Not sure where is this ToolTipChartPanel class, referred in the code below. In which Jar is this? I tried the latest version of JFree also.

Thanks,
Ganesh.

RoyW
Posts: 93
Joined: Wed Apr 23, 2008 7:42 pm
Contact:

Post by RoyW » Wed Jul 30, 2008 1:38 pm

Actually, TooltipChartPanel was something I posted in this thread
http://www.jfree.org/phpBB2/viewtopic.p ... 2&start=45

It is not required for this demo.

Replace

Code: Select all

    ChartPanel chartPanel = new TooltipChartPanel(chart); 
with

Code: Select all

    CharttPanel chartPanel = new ChartPanel(chart); 
and it should work.
The answer does not come from thinking outside the box, rather the answer comes from realizing the truth; There is no Box. my js site

RTJava
Posts: 4
Joined: Tue Mar 02, 2010 2:19 pm
antibot: No, of course not.

Re: example of Bar Chart(between value and time)?

Post by RTJava » Tue Mar 02, 2010 2:49 pm

Dear Experts,
I have short question related to below listed example. When you add second series which values are lower than first series, it hides lower values series by XYBarRenderer, therefore it cannot be seen on the chart. For example:
TimeSeries s2 = new TimeSeries("Series 2", Month.class);
s2.add(new Month(1, 2001), 112);
s2.add(new Month(2, 2001), 11);
s2.add(new Month(3, 2001), 17);
s2.add(new Month(4, 2001), 13);
s2.add(new Month(5, 2001), 17);
s2.add(new Month(6, 2001), 18);
s2.add(new Month(7, 2001), 18);
s2.add(new Month(8, 2001), 13);
s2.add(new Month(9, 2001), 12);
s2.add(new Month(10, 2001), 13);
s2.add(new Month(11, 2001), 11);
s2.add(new Month(12, 2001), 19);

dataset.addSeries(s1);
dataset.addSeries(s2);
// dataset.addSeries(s1);

s1 covers s2 values. If you change order first s2 then s1, i can be seen. Why and do you know any workaround?

Briefly, I have one table which includes Date(TIMESTAMP), X (double), Y(double), Z(double) data. Based on Date(Daily, Monthly, Yearly), I try to plot different type of charts. Such as, Bar chart, Pie chart, ... so on. TimeSeriesCollection samples have mostly line plot. Do you have any suggestions?
Thank you in advance.
Regards,

SuperAs
Posts: 1
Joined: Thu Dec 16, 2010 1:28 pm
antibot: No, of course not.

Re: example of Bar Chart(between value and time)?

Post by SuperAs » Thu Dec 16, 2010 1:35 pm

RTJava, I am on the same situation, S2 covers S1... did you finally got the solution to show the 2 series fine?

Thanks.

Locked