2014-10-10

XYGraph

Nebula Visualization Widgets の XYGraph を試してみました。

XYGraph を利用するには、まず Eclipse のマーケットプレイスから、Nebula をインストールします。

プロジェクトのビルドパスに org.eclipse.draw2d_XXXX.....jarorg.eclipse.nebula.visualization.xygraph_XXXX....jar を加ます

参考サイトで紹介されていた Snippet をベースにした簡単なサンプルを以下に示しました。

List: XYGraphSample.java
package nebula;

/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.nebula.visualization.xygraph.dataprovider.CircularBufferDataProvider;
import org.eclipse.nebula.visualization.xygraph.figures.ToolbarArmedXYGraph;
import org.eclipse.nebula.visualization.xygraph.figures.Trace;
import org.eclipse.nebula.visualization.xygraph.figures.XYGraph;
import org.eclipse.nebula.visualization.xygraph.figures.Trace.PointStyle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * A simple XYGraph with toolbar.
 * 
 * Original name of the class was 'SimpleToolbarArmedXYGraphExample' and
 * modified by Fuhito Suguri, 10-Oct-2014
 * 
 * @author Xihui Chen
 *
 */
public class XYGraohSample {
    Shell shell;

    public XYGraohSample(Display display) {
        shell = new Shell(display);
        shell.setText("XYGraphSample");
        shell.setSize(600, 400);

        shell.setLayout(new FillLayout());

        initUI();

        shell.setLocation(200, 200);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public void initUI() {
        // use LightweightSystem to create the bridge between SWT and draw2D
        final LightweightSystem lws = new LightweightSystem(shell);

        // create a new XYGraph
        XYGraph xyGraph = new XYGraph();
        xyGraph.setTitle("XYgraph の簡単な例");

        xyGraph.primaryXAxis.setTitle("X軸");
        xyGraph.primaryYAxis.setTitle("Y軸");

        xyGraph.primaryXAxis.setShowMajorGrid(true);
        xyGraph.primaryYAxis.setShowMajorGrid(true);

        // add ToolbarArmedXYGraph
        ToolbarArmedXYGraph toolbarArmedXYGraph = new ToolbarArmedXYGraph(xyGraph);

        // set it as the content of LightwightSystem
        lws.setContents(toolbarArmedXYGraph);

        xyGraph.primaryXAxis.setShowMajorGrid(true);
        xyGraph.primaryYAxis.setShowMajorGrid(true);

        // create a trace data provider, which will provide the data to the trace
        CircularBufferDataProvider traceDataProvider = new CircularBufferDataProvider(false);
        traceDataProvider.setBufferSize(100);
        traceDataProvider.setCurrentXDataArray(new double[] { 10, 23, 34, 45, 56, 78, 88, 99 });
        traceDataProvider.setCurrentYDataArray(new double[] { 11, 44, 55, 45, 88, 98, 52, 23 });

        // create the trace
        Trace trace = new Trace("データ", xyGraph.primaryXAxis, xyGraph.primaryYAxis, traceDataProvider);

        // set trace property
        trace.setPointStyle(PointStyle.CIRCLE);
        trace.setPointSize(10);

        // add the trace to xyGraph
        xyGraph.addTrace(trace);
    }

    public static void main(String[] args) {
        Display display = new Display();
        new XYGraohSample(display);
        display.dispose();
    }
}

実行例

参考サイト

  1. Nebula Visualization Widgets
  2. Overview (SWT XYGraph API)

0 件のコメント: