cn.InstFS.wkr.NetworkMining.UIs.TimeSeriesChart1.java Source code

Java tutorial

Introduction

Here is the source code for cn.InstFS.wkr.NetworkMining.UIs.TimeSeriesChart1.java

Source

package cn.InstFS.wkr.NetworkMining.UIs;

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 *
 * ---------------------
 * SWTTimeSeriesDemo.java
 * ---------------------
 * (C) Copyright 2006-2009, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   Henry Proudhon (henry.proudhon AT ensmp.fr);
 *
 * Changes
 * -------
 * 30-Jan-2007 : New class derived from TimeSeriesDemo.java (HP);
 * 
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.ValueMarker;
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.experimental.chart.swt.ChartComposite;
import org.jfree.ui.LengthAdjustmentType;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;

import cn.InstFS.wkr.NetworkMining.DataInputs.DataItem;
import cn.InstFS.wkr.NetworkMining.DataInputs.DataItems;
import associationRules.LinePos;
import associationRules.ProtoclPair;

/**
 * An example of a time series chart. For the most part, default settings are
 * used, except that the renderer is modified to show filled shapes (as well as
 * lines) at each data point.
 */
public class TimeSeriesChart1 extends Composite {

    ArrayList<DataItems> _nor_model_ = null;
    ArrayList<DataItems> _abnor_model_ = null;

    public TimeSeriesChart1(Composite parent, int style, ProtoclPair pp) {
        super(parent, style);
        // ?
        DataItems dataitems1 = DataItemsNormalization(pp.getDataItems1(), 1);
        DataItems dataitems2 = DataItemsNormalization(pp.getDataItems2(), 0);

        /*
         * _nor_model_?DataItem1 _abnor_model_?DataItem2 LinePos
         * ??key?? 
         */
        /*
         * _nor_model_ = new ArrayList<DataItems>(); _abnor_model_ = new
         * ArrayList<DataItems>();
         * 
         * 
         * // LinePos ??key?? ArrayList<LinePos> linepos1 = new
         * ArrayList<LinePos>();
         * 
         * for (Object se : pp.getMapAB().keySet()) { //ArrayList<LinePos>
         * linepos1 = pp.getMapAB().get(se); // System.out.println(" key :" + se
         * + "   " + "value" + s.size()); // linepos1.addAll(s);
         * 
         * Iterator lineposIt1 = linepos1.iterator(); while
         * (lineposIt1.hasNext()) { LinePos temp1 = (LinePos) lineposIt1.next();
         * _nor_model_.add(this.getDataItems(dataitems1, (temp1).A_start,
         * temp1.A_end)); // System.out.println(" A_start=" + (temp1).A_start +
         * "  " // + "A_end=" + temp1.A_end);
         * _abnor_model_.add(this.getDataItems(dataitems2, (temp1).B_start,
         * temp1.B_end)); // System.out.println(" B_start=" + (temp1).B_start +
         * "  " // + "B_end=" + temp1.B_end); //System.out.println(""); } }
         */
        String chartname = "??" + pp.getProtocol1() + "/" + "??" + pp.getProtocol2() + "?"
                + "(" + pp.confidence + ")";
        JFreeChart chart = createChart2(_nor_model_, _abnor_model_, dataitems1, dataitems2, pp.getMapAB(),
                chartname, pp.getProtocol1(), pp.getProtocol2());

        ChartComposite frame = new ChartComposite(this, SWT.NONE, chart, true);
        frame.setDisplayToolTips(true);
        frame.setHorizontalAxisTrace(false);
        frame.setVerticalAxisTrace(false);
        this.setLayout(new FillLayout());
    }

    /*
     * getDataItems start end ?DataItems?DataItems
     */
    public static DataItems getDataItems(DataItems dataitems, int start, int end) {
        DataItems ret = new DataItems();
        // ret.setItems(items);

        for (int i = start; i <= end; i++) {

            ret.add1Data(dataitems.getElementAt(i));
        }

        return ret;

    }

    /*
     * DataItemsList<String>,?addanumber??
     */
    public DataItems DataItemsNormalization(DataItems data, int addanumber) {
        DataItems ret = new DataItems();
        ret.setTime(data.getTime());
        ret.setNonNumData(data.getNonNumData());
        ret.setProbMap(data.getProbMap());
        ret.setVarSet(data.getVarSet());
        ret.setProb(data.getProb());
        ret.setDiscreteNodes(data.getDiscreteNodes());
        ret.setGranularity(data.getGranularity());
        ret.setDiscreteStrings(data.getDiscreteStrings());
        List<String> datanormolization = new ArrayList<String>();
        Iterator it = data.data.iterator();
        // ?
        if (it.hasNext()) {
            double min = Double.valueOf((String) it.next()).doubleValue();
            double max = min;
            while (it.hasNext()) {
                double temp = Double.valueOf((String) it.next()).doubleValue();
                if (max < temp) {
                    max = temp;
                } else if (min > temp) {
                    min = temp;
                }
            }
            // ???????
            double t = max - min;
            // ?
            it = data.data.iterator();
            while (it.hasNext()) {
                double temp2 = Double.valueOf((String) it.next()).doubleValue();
                double temp2normalization = 0;

                if (t != 0) {
                    temp2normalization = (temp2 - min) / t + addanumber;
                }
                datanormolization.add(Double.toString(temp2normalization));
            }
            // ?List<String> datanormolization DataItems
            ret.setData(datanormolization);

        } else {
            System.out.println(" TimeSeriesChart1 function DataItemsNormalization  data is null");
        }
        return ret;

    }

    /*
     * 
     * 
     * @param _nor_model?DataItems1
     * 
     * @param _abnor_model?DataItems2
     */
    public static JFreeChart createChart2(ArrayList<DataItems> _nor_model, ArrayList<DataItems> _abnor_model,
            DataItems nor, DataItems abnor, Map<String, ArrayList<LinePos>> mapAB, String chartname,
            String protocol1, String protocol2) {
        XYDataset xydataset = createNormalDataset(nor, protocol1);
        JFreeChart jfreechart = ChartFactory.createXYLineChart(chartname, "", "", xydataset);
        jfreechart.getLegend().setVisible(false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
        numberaxis.setAutoRangeIncludesZero(false);
        java.awt.geom.Ellipse2D.Double double1 = new java.awt.geom.Ellipse2D.Double(-4D, -4D, 6D, 6D);

        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
        // ??
        xylineandshaperenderer.setSeriesLinesVisible(0, true);
        xylineandshaperenderer.setBaseShapesVisible(false);
        xylineandshaperenderer.setSeriesShape(0, double1);
        xylineandshaperenderer.setSeriesPaint(0, Color.blue);
        xylineandshaperenderer.setSeriesFillPaint(0, Color.blue);
        xylineandshaperenderer.setSeriesOutlinePaint(0, Color.blue);
        xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(0.5F));
        // ?
        // xylineandshaperenderer.setBaseItemLabelGenerator(new
        // StandardXYItemLabelGenerator());
        // xylineandshaperenderer.setBaseItemLabelsVisible(true);
        int datasetcount0 = xyplot.getDatasetCount();

        XYDataset xydataset1 = createNormalDataset(abnor, protocol2);
        // xydataset1.
        XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer();
        int datasetcount = xyplot.getDatasetCount();
        xyplot.setDataset(datasetcount, xydataset1);
        xyplot.setRenderer(datasetcount, xylineandshaperenderer1);
        // ???
        xylineandshaperenderer1.setBaseShapesVisible(false);
        // ??
        xylineandshaperenderer1.setSeriesLinesVisible(0, true);
        xylineandshaperenderer1.setSeriesShape(0, double1);
        // 
        xylineandshaperenderer1.setSeriesPaint(0, Color.green);
        xylineandshaperenderer1.setSeriesFillPaint(0, Color.green);
        xylineandshaperenderer1.setSeriesOutlinePaint(0, Color.green);
        xylineandshaperenderer1.setUseFillPaint(true);

        xylineandshaperenderer1.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
        xylineandshaperenderer1.setSeriesStroke(0, new BasicStroke(0.5F));

        // //?
        /*
         * for (int i = 0; i < _nor_model.size(); i++) { XYDataset xydataset2 =
         * createmodeDataset(_nor_model.get(i), "_nor_model" + i);
         * XYLineAndShapeRenderer xylineandshaperenderer2 = new
         * XYLineAndShapeRenderer(); xyplot.setDataset(i + 2, xydataset2);
         * xyplot.setRenderer(2 + i, xylineandshaperenderer2); // ???
         * xylineandshaperenderer2.setBaseShapesVisible(false); // ??
         * xylineandshaperenderer2.setSeriesLinesVisible(0, true);
         * xylineandshaperenderer2.setSeriesShape(0, double1); // 
         * xylineandshaperenderer2.setSeriesPaint(0, Color.red);
         * xylineandshaperenderer2.setSeriesFillPaint(0, Color.red);
         * xylineandshaperenderer2.setSeriesOutlinePaint(0, Color.red);
         * xylineandshaperenderer2.setUseFillPaint(true);
         * xylineandshaperenderer2 .setBaseItemLabelGenerator(new
         * StandardXYItemLabelGenerator());
         * xylineandshaperenderer2.setSeriesStroke(0, new BasicStroke(2.5F));
         * 
         * } for (int i = 0; i < _abnor_model.size(); i++) { XYDataset
         * xydataset3 = createmodeDataset(_abnor_model.get(i), "_abnor_model" +
         * i); XYLineAndShapeRenderer xylineandshaperenderer3 = new
         * XYLineAndShapeRenderer(); xyplot.setDataset(i + 2 +
         * _nor_model.size(), xydataset3); xyplot.setRenderer(i + 2 +
         * _nor_model.size(), xylineandshaperenderer3); // ???
         * xylineandshaperenderer3.setBaseShapesVisible(false); // ??
         * xylineandshaperenderer3.setSeriesLinesVisible(0, true);
         * xylineandshaperenderer3.setSeriesShape(0, double1); // 
         * xylineandshaperenderer3.setSeriesPaint(0, Color.red);
         * xylineandshaperenderer3.setSeriesFillPaint(0, Color.red);
         * xylineandshaperenderer3.setSeriesOutlinePaint(0, Color.red);
         * xylineandshaperenderer3.setUseFillPaint(true);
         * xylineandshaperenderer3 .setBaseItemLabelGenerator(new
         * StandardXYItemLabelGenerator());
         * xylineandshaperenderer3.setSeriesStroke(0, new BasicStroke(2.5F));
         * 
         * }
         */
        // ??
        // 
        // /////////////////////////////////
        // ?

        XYDataset xydataset4 = createLineDataset(nor, abnor, mapAB, xyplot);

        // ??y=1
        ValueMarker valuemarker = new ValueMarker(1); // 
        valuemarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        valuemarker.setPaint(Color.black); // ?
        valuemarker.setStroke(new BasicStroke(1.0F)); // 
        // valuemarker.setLabel(""); //?
        valuemarker.setLabelFont(new Font("SansSerif", 0, 11)); // ?
        valuemarker.setLabelPaint(Color.red);
        valuemarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        valuemarker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
        xyplot.addRangeMarker(valuemarker);
        // //

        //jfreechart.getLegend().setVisible(true);
        return jfreechart;
    }

    /*
     * ??DataItems?
     * 
     * @param mapAB ????
     */
    public static XYDataset createLineDataset(DataItems dataitems1, DataItems dataitems2,
            Map<String, ArrayList<LinePos>> mapAB, XYPlot xyplot) {
        // ????
        int modelcount = mapAB.keySet().size();
        long off1 = dataitems1.getElementAt(0).getTime().getTime();
        long off2 = dataitems2.getElementAt(0).getTime().getTime();
        long unit = 0;
        if (dataitems1.getLength() > 0) {
            unit = dataitems1.getElementAt(1).getTime().getTime() - off1;
        } else {
            unit = 3600000;
        }

        XYSeriesCollection xyseriescollection = new XYSeriesCollection();
        // ??

        int colorindex = 0;
        int renderercount = 2;
        for (Object se : mapAB.keySet()) {
            ArrayList<LinePos> s = mapAB.get(se);
            int oneModelCount = s.size();

            ArrayList<DataItems> dslist = new ArrayList<DataItems>();
            Iterator it = s.iterator();

            System.out.println("**************");
            System.out.println("?=" + se + ":=" + oneModelCount);

            XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
            // ???
            xylineandshaperenderer.setBaseShapesVisible(false);
            // ??
            xylineandshaperenderer.setSeriesLinesVisible(0, true);
            // xylineandshaperenderer.setSeriesShape(0, double1); //?
            // 
            xylineandshaperenderer.setSeriesPaint(0, getcolor(colorindex % 9));
            xylineandshaperenderer.setSeriesFillPaint(0, getcolor(colorindex % 9));
            xylineandshaperenderer.setSeriesOutlinePaint(0, getcolor(colorindex % 9));
            colorindex++;
            xylineandshaperenderer.setUseFillPaint(true);
            xylineandshaperenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());

            xylineandshaperenderer.setBaseShapesVisible(false);
            xylineandshaperenderer.setSeriesStroke(0,
                    new BasicStroke(1.0F, 1, 1, 1.0F, new float[] { 15F, 12F }, 0.0F)); // 
            xylineandshaperenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
            xylineandshaperenderer.setBaseItemLabelsVisible(false);

            while (it.hasNext()) {
                LinePos temp = (LinePos) it.next();

                DataItem d1 = new DataItem();
                d1 = dataitems1.getElementAt(temp.A_start);
                DataItem d2 = new DataItem();
                d2 = dataitems2.getElementAt(temp.B_start);
                DataItems ds1 = new DataItems();
                ds1.add1Data(d1);
                ds1.add1Data(d2);
                // dslist.add(ds1);
                XYDataset xydataset1 = createmodeDataset(ds1, off1, se.toString() + ":" + 1, unit);

                DataItem d3 = new DataItem();
                d3 = dataitems1.getElementAt(temp.A_end);
                DataItem d4 = new DataItem();
                d4 = dataitems2.getElementAt(temp.B_end);
                DataItems ds2 = new DataItems();
                ds2.add1Data(d3);
                ds2.add1Data(d4);
                // dslist.add(ds2);
                XYDataset xydataset2 = createmodeDataset(ds2, off1, se.toString() + ":" + 2, unit);

                int datasetCount = xyplot.getDatasetCount();
                XYTextAnnotation localXYTextAnnotation = null;
                int modelx = (temp.A_start + temp.B_end) / 2;
                double modely = (Double.parseDouble(dataitems2.getElementAt(temp.A_start).getData())
                        + Double.parseDouble(dataitems1.getElementAt(temp.B_end).getData())) / 2;
                /*System.out.println("se=" + se + "::" + "x=" + modelx + "y="
                      + modely);*/
                localXYTextAnnotation = new XYTextAnnotation("" + se + ":" + oneModelCount, modelx, modely);
                xyplot.addAnnotation(localXYTextAnnotation);
                /*System.out.println("datasetCount=" + datasetCount);*/

                xyplot.setDataset(datasetCount, xydataset1);
                xyplot.setRenderer(datasetCount, xylineandshaperenderer);
                xyplot.setDataset(datasetCount + 1, xydataset2);
                xyplot.setRenderer(datasetCount + 1, xylineandshaperenderer);

            }

        }
        return xyseriescollection;
    }

    /*
     * DataItems??
     */
    public static XYDataset createNormalDataset(DataItems normal, String protocol1) {
        // ???
        int length = normal.getLength();
        int time[] = new int[length];
        XYSeries xyseries = new XYSeries(protocol1);
        XYSeriesCollection xyseriescollection = new XYSeriesCollection();

        // ??

        for (int i = 0; i < length; i++) {
            DataItem temp = new DataItem();

            temp = normal.getElementAt(i);

            //System.out.println("DataItem.time=" + temp.getTime().getTime());

            xyseries.add(i, Double.parseDouble(temp.getData())); // 

        }
        xyseriescollection.addSeries(xyseries);

        return xyseriescollection;
    }

    // 

    // ?DataItemsyXYDataset
    public static XYDataset createmodeDataset(DataItems normal, long offset1, String protocol2, long unit) {
        // ???
        int length = normal.getLength();
        int time[] = new int[length];
        XYSeries xyseries = new XYSeries(protocol2);
        XYSeriesCollection xyseriescollection = new XYSeriesCollection();
        // ??
        long offset2 = normal.getElementAt(0).getTime().getTime();
        int off = (int) ((offset2 - offset1) / unit);
        for (int i = 0; i < length; i++) {
            DataItem temp = new DataItem();

            temp = normal.getElementAt(i);
            /*
             * if(i==0){ System.out.println("DataItem.time="+temp.getTime());
             * }
             */

            xyseries.add(i + off, Double.parseDouble(temp.getData())); // 

        }
        xyseriescollection.addSeries(xyseries);
        return xyseriescollection;
    }

    // ?
    /*
     * public static XYDataset createAbnormalDataset(DataItems abnor) { //
     *  int length = abnor.getLength(); XYSeries xyseries = new
     * XYSeries("??");
     * 
     * XYSeriesCollection xyseriescollection = new XYSeriesCollection();
     * 
     * // ?
     * 
     * for (int i = 0; i < length; i++) {
     * 
     * DataItem temp = new DataItem(); temp = abnor.getElementAt(i);
     * xyseries.add((double) temp.getTime().getTime(),
     * Double.parseDouble(temp.getData())); // ??? xyseries.add((double)
     * temp.getTime().getTime(), Double.parseDouble(temp.getData()));
     * 
     * } xyseriescollection.addSeries(xyseries); return xyseriescollection; }
     */
    // ?

    /*
     * ?0
     */
    public static Color getcolor(int i) {
        switch (i) {
        case 1:
            return Color.red;
        case 2:
            return Color.blue;
        case 3:
            return Color.cyan;
        case 4:
            return Color.gray;
        case 5:
            return Color.green;
        case 6:
            return Color.magenta;
        case 7:
            return Color.orange;
        case 8:
            return Color.yellow;
        case 9:
            return Color.white;
        case 0:
            return Color.pink;
        default:
            return Color.black;
        }
    }

    public static void main(String[] args) {

        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(600, 300);
        shell.setLayout(new FillLayout());
        shell.setText("Time series demo  ");
        DataItems dataitem = new DataItems();

        // TimeSeriesChart1 s=new TimeSeriesChart1(shell, SWT.NULL,dataitem);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

    public class MyDataSet extends XYSeriesCollection {

    }

}