【问题标题】:How to plot scatter plot on an image?如何在图像上绘制散点图?
【发布时间】:2016-08-12 13:16:10
【问题描述】:

我正在尝试将我在 Matlab 中的 GUI 转换为 JavaFX 8 小程序。我想要一个散点图,其中的图位于图像上,如下所示。

如上图所示,CIE 图与散点图集成在一起。我尝试使用堆栈窗格,但由于图像在其上方,所以绘图不可见。

编辑-

试图覆盖 layoutplotchildren():

class SuperScatterChart extends ScatterChart{

        public SuperScatterChart(Axis arg0, Axis arg1) {
            super(arg0, arg1 );
            // TODO Auto-generated constructor stub
        }




        @Override
        protected void layoutPlotChildren(){

            ImageView iv1 = new ImageView(new Image("file:///C:/Desktop/cie.png",450,450,true,true));
           // How do I add iv1 to plot children?
            super.layoutPlotChildren();

        }



    }

【问题讨论】:

  • 我想到了两种方法。 1.使用CSS:.chart-plot-background nodeRegion,所以你可以在CSS上设置一个-fx-background-image。 2. 继承ScatterChart 并覆盖layoutPlotChildren() 将图像添加到绘图区域,然后调用super.layoutPlotChildren() 添加散点。请参阅stackoverflow.com/questions/38871202 了解一些类似的内容。
  • 另外:“我尝试使用堆栈窗格,但由于图像在它上面,所以绘图不可见。” - 如果您将图像添加到堆栈窗格在图表之前,则不应该发生这种情况。
  • 我尝试了第一种方法,但是图像位于散点图后面。至于第二种方法,将图像更改为覆盖 layoutPlotChildren() 中的绘图区域的代码语法是什么?谢谢@James_D
  • 在调用super.layoutPlotChildren()之前,只需将包含所需图像的ImageView 添加到情节儿童中即可
  • edit 你的问题,不要在 cmets 中发布代码。会发生什么?

标签: java user-interface javafx-8 scatter-plot


【解决方案1】:

您将图像视图添加到情节孩子

getPlotChildren().add(iv1);

您可能不需要在重写的 layoutPlotChildren() 方法中执行此操作:只需在构造函数中执行一次就足够了:

class SuperScatterChart extends ScatterChart<Number, Number>{

    private final ImageView iv1 ;

    public SuperScatterChart(NumberAxis xAxis, NumberAxis yAxis) {
        super(xAxis, yAxis);
        iv1 = new ImageView(new Image("file:///C:/Desktop/cie.png",450,450,true,true));
        getPlotChildren().add(iv1);
    }

}

这会将图像放置在默认位置(我认为绘图区域没有任何默认布局,所以我认为它只会位于左上角)。如果需要移动,添加

@Override
protected void layoutPlotChildren() {
    double x = ... ; // x coordinate of image in xAxis coordinates
    double y = ... ; // y coordinate of image in yAxis coordinates

    double layoutX = getXAxis().getDisplayPosition(x);
    double layoutY = getYAxis().getDisplayPosition(y);

    iv1.setLayoutX(layoutX);
    iv1.setLayoutY(layoutY);

    super.layoutPlotChildren();
}

请注意,这使您有机会使用“绘图坐标”轻松定位图像,即图表轴的坐标系,而不是基于像素的坐标系。

【讨论】:

  • 谢谢!想弄清楚这个问题很久了。 @James_D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 2015-05-21
  • 2020-08-17
相关资源
最近更新 更多