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

别等时光非礼了梦想. 提交于 2019-12-19 10:53:27

问题


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?

Thanks.


回答1:


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


来源:https://stackoverflow.com/questions/793712/how-do-i-create-a-bar-chart-using-the-java-prefuse-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!