我使用JFreeChart与JavaFX,我有我的图表看起来几乎正是我想要的.最后一部分是这个小的边界,仍然围绕着图表.我似乎不知道如何使图表与窗格齐平.

enter image description here

到目前为止,我找到的大多数答案都只涉及将填充或不透明嵌入设置为0,但这似乎并不能完全解决问题.我也不确定这是否与ChartViewer增加了一些额外的填充物有关.

到目前为止,这是我的代码.

public class TestApp extends Application
{

    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        TimeSeries throttleSeries = new TimeSeries("");
        throttleSeries.setMaximumItemCount(200);

        long currentTime = System.currentTimeMillis();
        for (int i = 200; i > 0; i--)
        {
            long now = currentTime - (i * 33L);
            throttleSeries.add(new FixedMillisecond(new Date(now)), 0.0);
        }

        TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
        timeSeriesCollection.addSeries(throttleSeries);

        Color throttleColor = Color.GREEN;

        XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer();
        lineRenderer.setDefaultShapesVisible(false);
        lineRenderer.setSeriesStroke(0, new BasicStroke(2f));
        lineRenderer.setSeriesPaint(0, throttleColor);

        XYAreaRenderer areaRenderer = new XYAreaRenderer();
        areaRenderer.setSeriesVisible(0, true);
        areaRenderer.setSeriesPaint(0, throttleColor);

        var areaChart = ChartFactory.createTimeSeriesChart("", "", "", timeSeriesCollection, false, false, false);
        var lineChart = ChartFactory.createTimeSeriesChart("", "", "", timeSeriesCollection, false, false, false);

        setJFreeChartStyle(lineChart);
        lineChart.getXYPlot().setRenderer(lineRenderer);

        setJFreeChartStyle(areaChart);
        areaChart.getXYPlot().setRenderer(areaRenderer);

        StackPane stackPane = new StackPane();
        stackPane.setStyle("-fx-background-color: black;");

        ChartViewer bottomChart = new ChartViewer(areaChart);
        bottomChart.setOpacity(0.3);
        bottomChart.setPadding(Insets.EMPTY);
        bottomChart.setOpaqueInsets(Insets.EMPTY);
        bottomChart.setBorder(Border.EMPTY);
        bottomChart.setStyle("-fx-background-color: transparent;");

        stackPane.getChildren().add(bottomChart);
        StackPane.setAlignment(bottomChart, Pos.CENTER);

        ChartViewer topChart = new ChartViewer(lineChart);
        topChart.setPadding(Insets.EMPTY);
        topChart.setOpaqueInsets(Insets.EMPTY);
        topChart.setBorder(Border.EMPTY);
        topChart.setStyle("-fx-background-color: transparent;");

        stackPane.getChildren().add(topChart);
        StackPane.setAlignment(topChart, Pos.CENTER);

        Scene scene = new Scene(stackPane, 400, 150);
        primaryStage.setScene(scene);
        primaryStage.show();

        Thread thread = new Thread(() -> {

            int counter = 0;
            while (true)
            {
                try
                {
                    Thread.sleep(33);
                }
                catch (InterruptedException ignored)
                {
                }

                int finalCounter = counter;
                Platform.runLater(() -> throttleSeries.addOrUpdate(new FixedMillisecond(),
                                                                   Math.sin(2 * Math.PI * 0.25 * (finalCounter * 0.033)) *
                                                                   0.5 +
                                                                   0.5));

                counter++;
            }
        });

        thread.setDaemon(true);
        thread.start();
    }

    protected void setJFreeChartStyle(JFreeChart chart)
    {
        chart.setBackgroundPaint(new java.awt.Color(0, 0, 0, 0));
        chart.setBorderVisible(false);
        chart.getXYPlot().getRangeAxis().setRange(0, 1);
        chart.getXYPlot().setOutlineVisible(false);

        chart.getXYPlot().getDomainAxis().setLowerMargin(0.0);
        chart.getXYPlot().getDomainAxis().setUpperMargin(0.0);
        chart.setPadding(RectangleInsets.ZERO_INSETS);
        chart.getXYPlot().setBackgroundAlpha(0);
        chart.getXYPlot().setInsets(RectangleInsets.ZERO_INSETS);

        chart.getXYPlot().setRangeCrosshairVisible(false);
        chart.getXYPlot().setRangeZeroBaselineVisible(false);
        chart.getXYPlot().setRangeGridlinesVisible(false);
        chart.getXYPlot().setRangeMinorGridlinesVisible(false);

        chart.getXYPlot().setDomainCrosshairVisible(false);
        chart.getXYPlot().setDomainZeroBaselineVisible(false);
        chart.getXYPlot().setDomainGridlinesVisible(false);
        chart.getXYPlot().setDomainMinorGridlinesVisible(false);

        chart.getXYPlot().getDomainAxis().setVisible(false);
        chart.getXYPlot().getRangeAxis().setVisible(false);
    }

推荐答案

如图here所示,XYPlothere,CategoryPlothere,您可以如下所示删除绘图的轴偏移:

var plot = chart.getXYPlot();
plot.setAxisOffset(RectangleInsets.ZERO_INSETS);

Borderless plot

Java相关问答推荐

Android -如何修复Java.time.zone. ZoneRulesExcept:未知时区ID:Europe/Kyiv

Java应用程序崩溃时试图读取联系人从电话

如何在返回bigint []值的子查询中使用any?

为什么Java的代码工作(if condition内部的实例)

为什么我的ArrayList索引的索引总是返回-1?

如何才能使我的程序不会要求两次输入?

无法了解Java线程所消耗的时间

Java流传输一个列表并创建单个对象

用OSQL创建索引

每次FXMLLoader调用ApplationConext.getBean(类)时创建@Component的新实例

内存和硬盘中的Zip不同,这会导致下载后的Zip损坏

Kotlin Val是否提供了与Java最终版相同的可见性保证?

如何在太阳系模拟器中添加月球?

如何创建模块信息类文件并将其添加到JAR中?

Spring Boot中的应用程序.properties文件中未使用的属性

如何在透视表中添加对计数列的筛选?

Java中HashSet的搜索时间与TreeSet的搜索时间

JavaFX标签中的奇怪字符

将java.util.Date(01.01.0001)转换为java.time.LocalDate将返回29.12.0000

如何正确使用java.time类?