简体   繁体   English

如何使用Java prefuse库创建条形图?

[英]How do I create a bar chart using the Java prefuse library?

I've currently got prefuse to plot a scatter graph, where the X axis is the computer name and the Y axis is its temperature. 我目前很容易绘制散点图,其中X轴是计算机名称,Y轴是其温度。 How do I get it to draw bars showing the values instead of discrete points? 如何绘制显示值而不是离散点的条形图?

I'm currently using the following code to render the points: 我当前正在使用以下代码来渲染点:

ShapeAction shape = new ShapeAction(group, Constants.SHAPE_RECTANGLE);
ColorAction strokeColor = new DataColorAction(group, dataType, Constants.NUMERICAL, VisualItem.STROKECOLOR, colorPalette);

ActionList draw = new ActionList();
draw.add(shape);
draw.add(strokeColor);
draw.add(new ColorAction(group, VisualItem.FILLCOLOR, 0));
draw.add(new RepaintAction());
m_vis.putAction("draw", draw);

How would I adapt this code to get, instead of a small square at each point, a thick bar going from th bottom of the graph to the point? 我将如何修改此代码以获取从图的底部到该点的粗线而不是每个点的小方块?

Thanks. 谢谢。

I think I should probably point out how I did this - Stack Overflow is supposed to be a repository too, after all. 我想我应该指出我是如何做到的-毕竟堆栈溢出也应该是一个存储库。 Earlier in the code was the following: 在代码的早期是以下内容:

m_vis.setRendererFactory(new RendererFactory() {
    Renderer yAxisRenderer = new AxisRenderer(Constants.LEFT, Constants.TOP);
    Renderer xAxisRenderer = new AxisRenderer(Constants.CENTER, Constants.FAR_BOTTOM);
    Renderer barRenderer = new ShapeRenderer();

    public Renderer getRenderer(VisualItem item) {
        return item.isInGroup("yAxis") ? yAxisRenderer :
               item.isInGroup("xAxis") ? xAxisRenderer :
               barRenderer;
    }
});

I extended the shape renderer to always return a rectangle of the correct width and height, and positioned it half a bar to the left of where it was supposed to be. 我扩展了形状渲染器,以始终返回正确宽度和高度的矩形,并将其放置在应该位于其左侧的半格处。 If you want to position your bars in the centre, you need to do that yourself - prefuse won't help you. 如果您想将杠铃放在中间,则需要自己动手-装裱不会帮助您。

m_vis.setRendererFactory(new RendererFactory() {
    Renderer yAxisRenderer = new AxisRenderer(Constants.LEFT, Constants.TOP);
    Renderer xAxisRenderer = new AxisRenderer(Constants.CENTER, Constants.FAR_BOTTOM);
    Renderer barRenderer = new ShapeRenderer() {
        protected Shape getRawShape(VisualItem item) {
            double x = item.getX();
            double y = item.getY();
            if (Double.isNaN(x) || Double.isInfinite(x))
                x = getInsets().left + axisWidth + totalBarWidth / 2;
            if (Double.isNaN(y) || Double.isInfinite(y))
                y = 0;

            double width = totalBarWidth / (barCount + 1) - barGap;
            double height = getHeight() - getInsets().bottom - axisHeight - y;
            x -= width / 2;

            return rectangle(x, y, width, height);
        }
    };

    public Renderer getRenderer(VisualItem item) {
        return item.isInGroup("yAxis") ? yAxisRenderer :
               item.isInGroup("xAxis") ? xAxisRenderer :
               barRenderer;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM