我需要将滚动放入图表中,但我不能使用 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 做到这一点?我不能使用摇摆...
谢谢。