2012-10-03 159 views
1

我發現SWTChart library,只是想知道如何得到一個散點圖,點有不同的顏色和大小,就像這個example一樣。散點圖與不同的點顏色和大小

example http://matplotlib.org/_images/scatter_demo2.png

package org.swtchart.examples; 

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.swtchart.Chart; 
import org.swtchart.ILineSeries; 
import org.swtchart.LineStyle; 
import org.swtchart.ISeries.SeriesType; 

/** 
* An example for scatter chart. 
*/ 
public class ScatterChartExample { 

private static final double[] xSeries = { 0.0, 2.6, 6.5, 4.4, 5.6, 4.3, 
    3.4, 10.8, 2.1, 8.9 }; 
private static final double[] ySeries = { 1.3, 0.0, 3.9, 2.6, 1.1, 0.6, 
    3.1, 3.5, 5.6, 4.4 }; 

/** 
* The main method. 
* 
* @param args 
*   the arguments 
*/ 
public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setText("Scatter Chart"); 
    shell.setSize(500, 400); 
    shell.setLayout(new FillLayout()); 

    createChart(shell); 

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

/** 
* create the chart. 
* 
* @param parent 
*   The parent composite 
* @return The created chart 
*/ 
static public Chart createChart(Composite parent) { 

    // create a chart 
    Chart chart = new Chart(parent, SWT.NONE); 

    // set titles 
    chart.getTitle().setText("Scatter Chart"); 
    chart.getAxisSet().getXAxis(0).getTitle().setText("Score A"); 
    chart.getAxisSet().getYAxis(0).getTitle().setText("Score B"); 

    // create scatter series 
    ILineSeries scatterSeries = (ILineSeries) chart.getSeriesSet() 
     .createSeries(SeriesType.LINE, "scatter series"); 
    scatterSeries.setLineStyle(LineStyle.NONE); 
    scatterSeries.setXSeries(xSeries); 
    scatterSeries.setYSeries(ySeries); 

    // adjust the axis range 
    chart.getAxisSet().adjustRange(); 

    return chart; 
} 
} 

怎麼可能得到一個散點圖,其中分有不同的顏色和大小?

預先感謝您。


我想我只是第一次嘗試從LineSeries這個簡單的代碼

package org.swtchart.examples; 
import org.swtchart.Chart; 
import org.swtchart.internal.series.LineSeries; 

public class ILineSeriesTest extends LineSeries{ 

    protected ILineSeriesTest(Chart chart, String id) { 
     super(chart, id); 
    } 
} 

在示例代碼繼承我把它改爲:

ILineSeriesTest scatterSeries = (ILineSeriesTest) chart.getSeriesSet() 
      .createSeries(SeriesType.LINE, "scatter series"); 

但是,我得到了以下錯誤:

Exception in thread "main" java.lang.ClassCastException: org.swtchart.internal.series.LineSeries cannot be cast to org.swtchart.examples.ILineSeriesTest 
    at org.swtchart.examples.ScatterChartExampleTest.createChart(ScatterChartExampleTest.java:67) 
    at org.swtchart.examples.ScatterChartExampleTest.main(ScatterChartExampleTest.java:38) 

爲什麼它沒有解決?

預先感謝您。

+0

另外考慮['XYBubbleRenderer'](http://www.jfree.org/jfreechart/api/javadoc/org /jfree/chart/renderer/xy/XYBubbleRenderer.html)。 – trashgod

+0

@trashgod OP正在使用SWTChart,而不是JFreeChart。 – Baz

回答

1

您可以通過使用setSymbolColors(Color[] colors)實現個人色彩:

scatterSeries.setSymbolColors(new Color[] { ColorConstants.red, 
      ColorConstants.red, ColorConstants.red, ColorConstants.red, 
      ColorConstants.green, ColorConstants.green, 
      ColorConstants.green, ColorConstants.blue, ColorConstants.blue, 
      ColorConstants.blue }); 

注意,這個例子使用的Draw2DColorConstants

至於尺寸:你將不得不實施自己的ISeries它可以照顧的尺寸。

enter image description here

+0

謝謝你,它正在工作。但是,我不明白ISeries如何幫助我。你能否提供更多提示?先謝謝你。 – user977828

+0

@ user977828在您的代碼示例中,您使用['ILineSeries'](http://www.swtchart.org/doc/javadoc/org/swtchart/ILineSeries.html),它有一個方法['setSymbolSize(int)'] (http://www.swtchart.org/doc/javadoc/org/swtchart/ILineSeries.html#setSymbolSize(int))。你所要做的就是實現類似的東西,即採用「int []」而不是「int」的東西。 – Baz

+0

我以爲我只是先嚐試​​繼承[LineSeries](http://swt-chart.svn.sourceforge.net/viewvc/swt-chart/trunk/org.swtchart/src/org/swtchart/internal/series/ LineSeries.java?revision=283&view=markup)與這個簡單的代碼。請在下面找到問題。 – user977828

1

孝SWTchart的顯影劑提供了以下解決方案:

package org.swtchart.examples; 

import org.eclipse.draw2d.ColorConstants; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.graphics.Color; 
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.swtchart.Chart; 
import org.swtchart.IAxis; 
import org.swtchart.ICustomPaintListener; 
import org.swtchart.ILineSeries; 
import org.swtchart.ILineSeries.PlotSymbolType; 
import org.swtchart.IPlotArea; 
import org.swtchart.LineStyle; 
import org.swtchart.ISeries.SeriesType; 

/** 
* An example for scatter chart. 
*/ 
public class ScatterChartExampleTest { 

    private static final double[] xSeries = { 0.0, 2.6, 6.5, 4.4, 5.6, 4.3, 
      3.4, 10.8, 2.1, 8.9 }; 
    private static final double[] ySeries = { 1.3, 0.0, 3.9, 2.6, 1.1, 0.6, 
      3.1, 3.5, 5.6, 4.4 }; 

    /** 
    * The main method. 
    * 
    * @param args 
    *   the arguments 
    */ 
    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setText("Scatter Chart"); 
     shell.setSize(500, 400); 
     shell.setLayout(new FillLayout()); 

     createChart(shell); 

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

    /** 
    * create the chart. 
    * 
    * @param parent 
    *   The parent composite 
    * @return The created chart 
    */ 
    static public Chart createChart(Composite parent) { 

     // create a chart 
     final Chart chart = new Chart(parent, SWT.NONE); 

     // set titles 
     chart.getTitle().setText("Scatter Chart"); 
     chart.getAxisSet().getXAxis(0).getTitle().setText("Score A"); 
     chart.getAxisSet().getYAxis(0).getTitle().setText("Score B"); 

     // create scatter series 
     ILineSeries scatterSeries = (ILineSeries) chart.getSeriesSet() 
       .createSeries(SeriesType.LINE, "scatter series"); 
     scatterSeries.setLineStyle(LineStyle.NONE); 
     scatterSeries.setXSeries(xSeries); 
     scatterSeries.setYSeries(ySeries); 

     // hide default plot symbols 
     scatterSeries.setSymbolType(PlotSymbolType.NONE); 

     // assuming that double[] xSeries, double[] ySeries, int[] sizes, 
     // Color[] colors are available 
     final Color colors[] = new Color[] { ColorConstants.red, ColorConstants.red, 
       ColorConstants.red, ColorConstants.red, ColorConstants.green, 
       ColorConstants.green, ColorConstants.green, 
       ColorConstants.blue, ColorConstants.blue, ColorConstants.blue }; 
     final int sizes[] = new int[] { 10, 11, 12, 14, 15, 16, 17, 18, 19, 20}; 


     IPlotArea plotArea = (IPlotArea) chart.getPlotArea(); 

     plotArea.addCustomPaintListener(new ICustomPaintListener() { 

      public void paintControl(PaintEvent e) { 
       IAxis xAxis = chart.getAxisSet().getXAxis(0); 
       IAxis yAxis = chart.getAxisSet().getYAxis(0); 

       for (int i = 0; i < xSeries.length; i++) { 
        int x = xAxis.getPixelCoordinate(xSeries[i]); 
        int y = yAxis.getPixelCoordinate(ySeries[i]); 

        // draw symbol with any color and shape 
        e.gc.setBackground(colors[i]); 
        e.gc.fillOval(x, y, sizes[i], sizes[i]); 
       } 
      } 

      public boolean drawBehindSeries() { 
       return false; 
      } 
     }); 

     // adjust the axis range 
     chart.getAxisSet().adjustRange(); 

     return chart; 
    } 
}