XYLineChart sorting the data by the x values

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
dms489
Posts: 2
Joined: Fri Aug 21, 2020 6:05 pm
antibot: No, of course not.

XYLineChart sorting the data by the x values

Post by dms489 » Thu Sep 03, 2020 11:22 pm

I am trying to use JFreeChart to plot the locations of network devices in four sub-networks. Each data set has the devices in some kind of circular configuration. However, when I plot the data, the devices in each sub-network are sorted on X so that the lines are all monotonically increasing. How do I fix it? The code is attached.

Code: Select all

package simplenet;


import java.awt.BasicStroke;
import java.awt.Color;
import java.util.LinkedList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A demo application.
 *
 */
public class SimpleNet extends ApplicationFrame {

    public static enum Doing {
        NONE, INDEX, DEVICES, NET_DATA, COMPUTE, SUMMARY
    };
    private static LinkedList<Integer[]> net_list = null;
    private static Doing test = Doing.INDEX;
    public static int ipx[] = null;
    public static int ipy[] = null;
    /**
     * Creates a new instance.
     *
     * @param title  the frame title.
     */
    public SimpleNet(final String title) {

        super(title);
        readFile();
        final XYDataset data = createNetworkData();
        final JFreeChart chart = createNetworkChart(data);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(700, 500));
//        chartPanel.setVerticalZoom(true);
  //      chartPanel.setHorizontalZoom(true);
        setContentPane(chartPanel);

    }

    /**
     * Creates a network dataset.
     *
     * @return a network dataset.
     */
    private static XYDataset createNetworkData() {
        int nLines = net_list.size();
        XYSeries series[] = new XYSeries[nLines + 1];
        XYSeriesCollection dataset = new XYSeriesCollection();
        // net_list is an array ar of Integer[]
        // each ar[] is at 0: its index
        //                 1..n a device index
        //  ipx[], ipy[] are the device positions
        try {
        for(int ln = 0; ln < nLines; ln++) {
            series[ln] = new XYSeries("Network " + ln);
            Integer ar[] = net_list.get(ln);
            for(int i = 1; i < ar.length; i++) {
                int ndx = ar[i] - 1;
                series[ln].add(ipx[ndx], ipy[ndx]);
            }
            dataset.addSeries(series[ln]);
        }
        series[nLines] = new XYSeries("All Devices");
        for(int i = 0; i < ipx.length; i++) {
            series[nLines].add(ipx[i], ipy[i]);
        }
        dataset.addSeries(series[nLines]);
        } catch(Exception e) {
            int sigh = 1;
        }
        return dataset;
    }

    private static JFreeChart createNetworkChart(XYDataset dataset) {
        final float dash1[] = {10.0f, 10.0f};
        BasicStroke bs = new BasicStroke(1.0f,
                BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_MITER,
                10.0f, dash1, 0.0f); 
        // create the chart...
        JFreeChart chart = ChartFactory.createXYLineChart(
            "Networks",      // chart title
            "X",                      // x axis label
            "Y",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            false,                     // tooltips
            false                     // urls
        );
        chart.setTitle("Network Devices");
        // get a reference to the plot for further customisation...
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer
                = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setDrawSeriesLineAsPath(false);
        // sets paint color for each series
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesPaint(1, Color.GREEN);
        renderer.setSeriesPaint(2, Color.BLUE);
        renderer.setSeriesPaint(3, Color.YELLOW);
        renderer.setSeriesPaint(4, Color.BLACK);
        renderer.setSeriesStroke(0, bs);
        renderer.setSeriesStroke(1, bs);
        renderer.setSeriesStroke(2, bs);
        renderer.setSeriesStroke(3, bs);
        renderer.setSeriesStroke(4, bs);
        renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesLinesVisible(1, true);
        renderer.setSeriesLinesVisible(2, true);
        renderer.setSeriesLinesVisible(3, true);
        renderer.setSeriesLinesVisible(4, false);
        renderer.setSeriesShapesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);
        renderer.setSeriesShapesVisible(2, false);
        renderer.setSeriesShapesVisible(3, false);
        renderer.setSeriesShapesVisible(4, true);
        // change the auto tick unit selection to integer units only...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        return chart;
    }

    public void readFile() {
        int x[] = {18,84,125,68,80,30,96,73,9,62,59,47,65,43,6,91};
        ipx = x;
        int y[] = {16,37,38,11,26,7,44,30,36,33,40,22,49,55,26,41};
        ipy = y;
        Integer a[] = {1,9,12,6,1,15};
        Integer b[] = {2,10,8,5,4,12};
        Integer c[] = {3,14,13,10,11};
        Integer d[] = {4,2,16,7,3,5,8};
        net_list = new LinkedList<Integer[]>();
        net_list.add(a);
        net_list.add(b);
        net_list.add(c);
        net_list.add(d);
    }
    
    public static void main(final String[] args) {
        final SimpleNet demo = new SimpleNet("Simple Network Figure");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}

Post Reply