我需要在没有的情况下把一个卷轴放到一个图表中,但是我不能使用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个项目:
你能帮我写这段代码吗?有没有办法只用swt来做这件事?我不能使用swing..。
谢谢。
发布于 2018-10-23 19:19:19
我们解决了这个问题,解决方案是实现了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();
}https://stackoverflow.com/questions/52942763
复制相似问题