将ChartComposite转换为scrolledComposite

时间:2018-10-23 06:47:09

标签: swt jfreechart scrolledcomposite

我需要将滚动条放入图表中,但不能使用javax.swing,而只能使用swt。

我想在类别轴上放置无数个项目,可能是100或200,显然,您需要滚动查看x轴上的所有数据。

我已经使用SlidingCategoryDataset实现了图表的数据集,但是滚动仅适用于项目的切片部分,

这些是创建图表和数据集的方法:

private static CategoryDataset createDataset() {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int i = 0; i < 50; i++)
        dataset.addValue(Math.random() * 100D, "S1", "S" + i);

    return dataset;
}

private static JFreeChart createGraficaY(SlidingCategoryDataset slidingDataSet) {
    JFreeChart chart = ChartFactory.createAreaChart(
            "", 
            "", 
            "Y", 
            slidingDataSet, 
            PlotOrientation.VERTICAL,
            true, 
            true, 
            false 
    );      


    CategoryPlot plot = (CategoryPlot) chart.getPlot();


    int alpha = 127;
    Paint serie_2017 = new Color(0,150,194,alpha);
    Paint serie_2018 = new Color(0,216,180,alpha);


    AreaRenderer r = new AreaRenderer();                
    r.setSeriesPaint(0, serie_2017);
    r.setSeriesPaint(1, serie_2018);    


    plot.setRenderer(r);


    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);        
    plot.setBackgroundPaint(Color.white);       
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.BLACK);
    plot.setDomainGridlinesVisible(false);
    plot.setDomainGridlinePaint(Color.BLACK);
    plot.setOutlineVisible(false);
    plot.setOutlinePaint(Color.white);

    chart.getLegend().setFrame(BlockBorder.NONE);       

    return chart;
}

这是主要课程:

public static void main(String[] args) {

    Display display = new Display();
    Shell shell = new Shell( display );
    shell.setLayout( new FillLayout() );
    final ScrolledComposite scrolledComposite = new ScrolledComposite( shell, SWT.H_SCROLL);
    scrolledComposite.setExpandVertical( true );
    scrolledComposite.setExpandHorizontal( true );
    scrolledComposite.setAlwaysShowScrollBars( true );


    SlidingCategoryDataset dataset = new SlidingCategoryDataset(createDataset(), 0, 10);
    JFreeChart chart =createGraficaY(dataset);
    chart.removeLegend();


    final ChartComposite chartComposite = new ChartComposite(scrolledComposite, SWT.NONE, chart,
            true);  

    scrolledComposite.setContent(chartComposite);

    scrolledComposite.setExpandVertical(true); 
    scrolledComposite.setExpandHorizontal(true); 
    scrolledComposite.setMinSize(chartComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

    scrolledComposite.addListener( SWT.Resize, event -> {
      int width = scrolledComposite.getClientArea().width;
      scrolledComposite.setMinSize( shell.computeSize( width, SWT.DEFAULT ) );      
    } );

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

}

此代码不符合我的要求,因为首先滚动10个项目:

ScrollCompositeChart_1

ScrollCompositeChart_2

您能帮忙这段代码吗? 有什么办法只能用swt做到这一点吗?我不能用秋千...

谢谢。

1 个答案:

答案 0 :(得分:1)

我们解决了该问题,该解决方案是使用Slider而不是scrolledComposite实现的:

public static void main(String[] args) {

    Display display = new Display();
    Shell shell = new Shell( display );
    GridLayout gridLayout = new GridLayout(1,true);
    shell.setLayout(gridLayout);
    GridData gridDataGeneral=new GridData(SWT.FILL,SWT.FILL,true,true);
    shell.setLayoutData(gridDataGeneral);


    //Creamos la grafica y lo ponemos en el composite
    Grafica graficaImpl = new GraficaImpl();

    final SlidingCategoryDataset dataset = new SlidingCategoryDataset(createDataset(), 0, 10);
    JFreeChart chart =graficaImpl.crearGraficaY(dataset);
    chart.removeLegend();


    //CHARTCOMPOSITE
    //Se coloca en el chartcomposite
    final ChartComposite chartComposite = new ChartComposite(shell, SWT.NONE, chart,
            true);  
    GridData gridDatachartComposite=new GridData(SWT.FILL,SWT.FILL,true,true);
    chartComposite.setLayoutData(gridDatachartComposite);


    //SLIDER
    final Slider slider = new Slider(shell, SWT.HORIZONTAL);
    slider.setMaximum(100);
    slider.setMinimum(0);
    slider.setSelection(0);
    slider.setIncrement(1);


    SelectionListener listener = new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
            dataset.setFirstCategoryIndex(slider.getSelection());
         }


    };


    slider.addSelectionListener(listener);

    GridData gridDataSlider=new GridData(SWT.FILL,SWT.BEGINNING,true,true);
    slider.setLayoutData(gridDataSlider);

    /* */

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

}