Plotting points on domain and range axis in Line chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Scarlett
Posts: 11
Joined: Fri Sep 29, 2006 11:21 am

Plotting points on domain and range axis in Line chart

Post by Scarlett » Tue Oct 10, 2006 6:54 am

Hello,

I want to plot points on the X and Y axis in a line chart.

e.g p1(20,0) or p2(0,20)

Currently I can plot the points only if the X and the Y co-ordinates are not zero.

If any one has any idea, let me know of how it can be done.

Thanks,
Scarlett

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Plotting points on domain and range axis in Line chart

Post by david.gilbert » Tue Oct 10, 2006 9:20 am

Scarlett wrote:Currently I can plot the points only if the X and the Y co-ordinates are not zero.
Can you explain the problem? It shouldn't matter if one or the other coordinate is zero.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Scarlett
Posts: 11
Joined: Fri Sep 29, 2006 11:21 am

Creation of Line chart

Post by Scarlett » Tue Oct 10, 2006 9:53 am

//First step is creation of Dataset.
DefaultCategoryDataset dataset = new DefaultCategoryDataset()

dataset.addValue(10, " Classes", "0");
dataset.addValue(15, " Classes", "10");

//Second step is creating the Line chart with the above dataset.
I am using
ChartFactory.createLineChart(....);

When I see the above chart, I see the origin as (0,0) and one more point on X Axis as "0" above which the the Yvaue for the first point "10" gets plotted.

Where as what I want is that the first point should appear on the YAxis.

Let me know of how it can be done.

Thanks,
Scarlett

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Oct 10, 2006 10:09 am

Don't use a CategoryPlot for this, use an XYPlot:

Code: Select all

/* 
 * (C)opyright 2006, by Object Refinery Limited
 */

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

/**
 * A very simple line chart.
 */
public class XYLineChartDemo extends JFrame {

    /**
     * A demonstration application showing how to create a simple time series 
     * chart.  This example uses monthly data.
     *
     * @param title  the frame title.
     */
    public XYLineChartDemo(String title) {
        super(title);
        JFreeChart chart = createChart(createDataset());
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);
    }

    /**
     * Creates a chart.
     * 
     * @param dataset  a dataset.
     * 
     * @return A chart.
     */
    private static JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "Line Chart",  // title
            "X",             // x-axis label
            "Y",   // y-axis label
            dataset,            // data
            PlotOrientation.VERTICAL, 
            true,               // create legend?
            true,               // generate tooltips?
            false               // generate URLs?
        );
        return chart;
    }
    
    /**
     * Creates a dataset.
     *
     * @return The dataset.
     */
    private static XYDataset createDataset() {
        XYSeries s1 = new XYSeries("Series 1");
        s1.add(0.0, 10.0);
        s1.add(10.0, 15.0);
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s1);
        return dataset;
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        XYLineChartDemo demo = new XYLineChartDemo("Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Scarlett
Posts: 11
Joined: Fri Sep 29, 2006 11:21 am

Post by Scarlett » Tue Oct 10, 2006 11:16 am

Hey Dave!,

Thanks a lot!

Your tip was certainly very helpful :D

Thanks,
Scarlett

Locked