ДиаграммаКомпозит в прокручиваемыйКомпозит
Мне нужно поместить свиток в диаграмму без, но я не могу использовать javax.swing, просто используя SWT.
Я хочу поместить ограниченное количество элементов на оси категорий, возможно, 100 или 200, очевидно, вам нужен прокрутка для просмотра всех данных на оси х.
Я реализовал набор данных моей диаграммы с 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? Я не могу использовать качели...
Благодарю.
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();
}