如何使用Java prefuse库创建条形图? [英] How do I create a bar chart using the Java prefuse library?

查看:122
本文介绍了如何使用Java prefuse库创建条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在预先添加以绘制散点图,其中X轴是计算机名称, Y轴是它的温度。如何让它绘制显示值而不是离散点的条形图?

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. 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?

谢谢。

推荐答案

我想我应该指出我是如何做到的 - 毕竟Stack Overflow也应该是一个存储库。代码中的早期代码如下:

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;
    }
});

我将形状渲染器扩展为始终返回正确宽度和高度的矩形,并将其定位为一半它左边应该是一个酒吧。如果你想把你的酒吧放在中心,你需要自己做 - prefuse对你没有帮助。

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;
    }
});

这篇关于如何使用Java prefuse库创建条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆