在jFreechart中设置项目形状大小

时间:2015-07-05 09:10:22

标签: java jfreechart

我正在使用jFreeChart绘制折线图,​​这是一系列由直线连接的(x,y)值。

但问题是表示数据点的形状,圆形或矩形太大,因为我在一个系列中有很多值。你可以在这个截图中看到它的样子:

enter image description here

此外,我了解到,当我调整图表面板的大小时,表示数据点的形状不会按照图表的其余部分进行缩放。

问题

如何使形状(代表数据点的圆圈和圆圈)变小,以免它们一起堵塞?

这是我生成图表的代码,我使用自定义渲染器在一个数据系列中为该行的不同部分绘制不同的颜色。

    private JFreeChart createChart(XYDataset dataCollection) {

    // create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(
            fileName, // chart title
            "Count", // x axis label
            "Price", // y axis label
            dataCollection, // data
            PlotOrientation.VERTICAL,
            true, // include legend
            true, // tooltips
            false // urls
    );

    // customize chart
    XYPlot plot = (XYPlot) chart.getPlot();
    // find out the max and min value for price series
    XYSeriesCollection collection = (XYSeriesCollection)plot.getDataset();
    XYSeries priceSeries = collection.getSeries(0);
    double maxY = priceSeries.getMaxY();
    double minY = priceSeries.getMinY();
    // set max and min for range axis (y axis)
    NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
    rangeAxis.setRange(minY, maxY);

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer() {

        public Color getItemColor(int series, int item) {
            // modify code here to change color for different part of the line in one serie line
            if(series == 1) {
                System.out.println("getting color at: " + item);
                int isBuy = 0;
                if(item < kFilter.buySellSignal.size()) {
                    isBuy = kFilter.buySellSignal.get(item);
                }

                if(isBuy == 1) {
                    return Color.red;
                } else {
                    return Color.green;
                }
            } else {
                return Color.yellow;
            }



        }

        @Override
        protected void drawFirstPassShape(Graphics2D g2, int pass, int series, int item, Shape shape) {
            super.drawFirstPassShape(g2, pass, series, item, shape);

            //g2.setStroke(getItemStroke(series, item));
            Color c1 = getItemColor(series, item - 1);
            Color c2 = getItemColor(series, item);

            GradientPaint linePaint = new GradientPaint(0, 0, c1, 0, 300, c2);
            g2.setPaint(linePaint);
            g2.draw(shape);
        }
    };

    plot.setRenderer(renderer);

    return chart;
}

1 个答案:

答案 0 :(得分:2)

您可以覆盖getItemShape()中的XYLineAndShapeRenderer以返回所需的Shape,如图所示here。或者,调用抽象父方法之一,setSeriesShape()setBaseShape()等。

@Nicolas S.Xu报告使用类似于以下内容的代码:

Rectangle rect = new Rectangle(2, 2);
renderer.setSeriesShape(1, rect);

另见ShapeUtilities