3

I need to get A diamond shapes on my Timeseries in JFreechart, but I am unable to do it. Can someone please guide as to what code should be added to the code below to achieve the Diamond shape points and also how to change colours of the lines?

(The program uses rs and stmt and other things which are derived from the DB and are defined somewhere else. The program works properly right now, only problem is that it looks super boring.)

TimeSeries s1 = new TimeSeries("Technology", Day.class);
TimeSeries s2 = new TimeSeries("Entertainment", Day.class);
TimeSeries s3 = new TimeSeries("Soap", Day.class);
TimeSeries s4 = new TimeSeries("Music", Day.class);
TimeSeries s5 = new TimeSeries("Native", Day.class);
TimeSeries s6 = new TimeSeries("Speciality", Day.class);
TimeSeries s7 = new TimeSeries("Science", Day.class);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date plotdate;


if (!(combo_individualid.getModel().getSize() == 0)) {
    String sql = ""
            + "SELECT * "
            + "FROM   `customerbasetag` "
            + "WHERE  `individual_idindividual` =? ";

    try {
        stmt = conn.prepareStatement(sql);
        stmt.setString(1, combo_individualid.getSelectedItem().toString());
        rs = stmt.executeQuery();

        while (rs.next()) {

            try {
                plotdate = sdf.parse(rs.getString("session_date"));

                s1.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("technology"))));
                s2.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("entertainment"))));
                s3.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("soap"))));
                s4.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("music"))));
                s5.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("native"))));
                s6.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("speciality"))));
                s7.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("science"))));


            } catch (ParseException ex) {
                JOptionPane.showMessageDialog(null,
                    "Parse Exception" + ex.getMessage());
            }
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null,
            "Error During Session Select" + ex.getMessage());
    }


    /*NOTE: Chart plotting here*/
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);
    dataset.addSeries(s3);
    dataset.addSeries(s4);
    dataset.addSeries(s5);
    dataset.addSeries(s6);
    dataset.addSeries(s7);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "TS Chart", "Date", "Value", dataset, true, true, false);

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    ChartFrame f = new ChartFrame("Individual Choice Evaluation", chart);
    f.setVisible(true);
    f.setSize(800, 600);
    f.setLocationRelativeTo(null);

} else {

    JOptionPane.showMessageDialog(null, "Please Select an Individual");
}

I have updated code but it does not work properly still, and I keep getting the old chart back. heres the code.

TimeSeriesCollection dataset = new TimeSeriesCollection();
            dataset.addSeries(s1);
            dataset.addSeries(s2);
            dataset.addSeries(s3);
            dataset.addSeries(s4);
            dataset.addSeries(s5);
            dataset.addSeries(s6);
            dataset.addSeries(s7);
            JFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Chart for Individual id: "+combo_individualid.getSelectedItem().toString() , "Date", "Value", dataset, true, true, false);



            Shape theShape = ShapeUtilities.createDiamond(1);


            XYPlot plot = (XYPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.lightGray);
            plot.setDomainGridlinePaint(Color.white);
            plot.setRangeGridlinePaint(Color.white);
            plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
            plot.setDomainCrosshairVisible(true);
            plot.setRangeCrosshairVisible(true);



            XYItemRenderer renderer = plot.getRenderer();

            renderer.setSeriesShape(0, theShape);
            renderer.setSeriesShape(1, theShape);
            renderer.setSeriesShape(2, theShape);
            renderer.setSeriesShape(3, theShape);
            renderer.setSeriesShape(4, theShape);
            renderer.setSeriesShape(5, theShape);
            renderer.setSeriesShape(6, theShape);





            ChartFrame f = new ChartFrame("Individual Choice Evaluation", chart);
            f.setVisible(true);
            f.setSize(800, 600);
            f.setLocationRelativeTo(null);
chettyharish
  • 1,704
  • 7
  • 27
  • 41

1 Answers1

9

ShapeUtilities.createDiamond() can create a diamond shape; you can apply it as shown in this example of a different Shape.

Addendum: By default, createTimeSeriesChart() creates an XYLineAndShapeRenderer that you can modify as shown below and here.

XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
r.setSeriesShapesVisible(0, true);

image

SSCCE:

import java.awt.Dimension;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.util.ShapeUtilities;

/** @see https://stackoverflow.com/a/14822991/230513 */
public class Test {

    private static final int N = 16;
    private static final Random random = new Random();

    private static XYDataset createDataset() {

        final TimeSeries series = new TimeSeries("Data");
        Day current = new Day();
        for (int i = 0; i < N; i++) {
            series.add(current, Math.abs(random.nextGaussian()));
            current = (Day) current.next();
        }
        return new TimeSeriesCollection(series);
    }

    private static JFreeChart createChart(final XYDataset dataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Test", "Day", "Value", dataset, false, false, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
        r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
        r.setSeriesShapesVisible(0, true);
        return chart;
    }

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(630, 480);
            }
        };
        f.add(chartPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I have edited the code and it still does not work. I am doing something wrong. Can you please tell me whats my mistake? – chettyharish Feb 12 '13 at 13:03