JFreeChart connect one point to all other around

2020-02-14 18:06发布

is it possible to connect one point to all others around in JFreeChart
here how it should looks enter image description here

so all the points around connected to X point

chart.setBackgroundPaint(Color.white);

        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        Shape cross = ShapeUtilities.createDiagonalCross(3, 1);
        Shape somehing = ShapeUtilities.createDiamond(4);



        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesLinesVisible(1, false);
        renderer.setSeriesLinesVisible(2, false);
        renderer.setSeriesLinesVisible(3, false);

        renderer.setSeriesShape(0, cross);
        renderer.setSeriesShape(1, somehing);
        renderer.setSeriesShape(2, somehing);
        renderer.setSeriesShape(3, somehing);

        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesPaint(1, Color.BLUE);
        renderer.setSeriesPaint(2, Color.YELLOW);
        renderer.setSeriesPaint(2, Color.green);
        plot.setRenderer(renderer);


        plot.setBackgroundPaint(Color.BLACK);
        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;

thank You

1条回答
贼婆χ
2楼-- · 2020-02-14 19:00

You'll need a custom renderer. This minimal example overrides the XYLineAndShapeRenderer method drawPrimaryLine(). It draws the lines relative to the item having anchor as its series index. You'll need to recapitulate the existing implementation, replacing the lines shown below.

Addendum: The example simply passes anchor as a constructor parameter, but you can extend XYDataset to include a unique value for each series.

image

MyRenderer r = new MyRenderer(8);
XYPlot plot = new XYPlot(dataset, new NumberAxis("X"), new NumberAxis("Y"), r);
JFreeChart chart = new JFreeChart(plot);
…
private static class MyRenderer extends XYLineAndShapeRenderer {

    private final int anchor;

    public MyRenderer(int acnchor) {
        this.anchor = acnchor;
    }

    @Override
    protected void drawPrimaryLine(XYItemRendererState state, Graphics2D g2,
        XYPlot plot, XYDataset dataset, int pass, int series, int item,
        ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {
        if (item == anchor) {
            return;
        }
        …
        double x0 = dataset.getXValue(series, anchor);
        double y0 = dataset.getYValue(series, anchor);
        …
    }
}
查看更多
登录 后发表回答