如何在 Java 中将 NA 值表示为 HeatMap 的空像素(白色)?

How to represent NA values as empty pixels (white) for HeatMap in Java?

我正在使用 Java HeatMap 库 (http://www.mbeckler.org/heatMap/) 为我的数据生成热图。 我正在使用具有 NA 值的数据集。所以,基本上我不希望具有 NA 值的像素有颜色(白色)。但是,不幸的是,这个库不支持具有 NA 值的数据,我得到的是一个具有基色的图像计划块。我试着查看源代码,以便进行一些更改。在代码中,drawData() 方法用于为 bufferedImage 中的每个像素着色(可能!)。有人可以帮助我如何实现对 NA 值的支持并以无颜色显示它们吗?我对 BufferedImageGraphics2D class.

几乎没有经验

这里是库源代码中的drawData()方法:

/**
     * Creates a BufferedImage of the actual data plot.
     *
     * After doing some profiling, it was discovered that 90% of the drawing
     * time was spend drawing the actual data (not on the axes or tick marks).
     * Since the Graphics2D has a drawImage method that can do scaling, we are
     * using that instead of scaling it ourselves. We only need to draw the
     * data into the bufferedImage on startup, or if the data or gradient
     * changes. This saves us an enormous amount of time. Thanks to
     * Josh Hayes-Sheen (grey@grevian.org) for the suggestion and initial code
     * to use the BufferedImage technique.
     *
     * Since the scaling of the data plot will be handled by the drawImage in
     * paintComponent, we take the easy way out and draw our bufferedImage with
     * 1 pixel per data point. Too bad there isn't a setPixel method in the
     * Graphics2D class, it seems a bit silly to fill a rectangle just to set a
     * single pixel...
     *
     * This function should be called whenever the data or the gradient changes.
     */
    private void drawData()
    {

      //  System.out.println("Column: " + data.length + " row: " + data[0].length);
        bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
        bufferedGraphics = bufferedImage.createGraphics();

        for (int x = 0; x < data.length; x++)
        {
            for (int y = 0; y < data[0].length; y++)
            {
                bufferedGraphics.setColor(colors[dataColorIndices[x][y]]);
                bufferedGraphics.fillRect(x, y, 1, 1);
            }
        }
    }

示例数据可在以下位置找到:http://www.filedropper.com/data_13

所以,这是目前的样子:

来自 Java:

来自 R:

请忽略两张图片的方向

由于 BufferedImage 被创建为 ARGB,您可以不在那些应该未定义的像素上绘制任何东西,它们将保持透明。类似于:

public static int NA = ...;

private void drawData()
{
    bufferedImage = new BufferedImage(data.length,data[0].length, BufferedImage.TYPE_INT_ARGB);
    Graphics2D bufferedGraphics = bufferedImage.createGraphics();
    try {
        for (int x = 0; x < data.length; x++)
        {
            for (int y = 0; y < data[0].length; y++)
            {
                int colorIndex = dataColorIndices[x][y];
                if (colorIndex != NA) {
                    bufferedGraphics.setColor(colors[colorIndex]);
                    bufferedGraphics.fillRect(x, y, 1, 1);
                }
// Alternate flow, if you really want the pixels to be white
//                else {
//                    bufferedGraphics.setColor(Color.WHITE);
//                    bufferedGraphics.fillRect(x, y, 1, 1);
//                }
            }
        }
    }
    finally {
        bufferedGraphics.dispose();
    }
}

我还处理了 Graphics2D 实例,以避免资源泄漏。