【问题标题】:Set item shape size in jFreechart在 jFreechart 中设置项目形状大小
【发布时间】:2015-07-05 09:10:21
【问题描述】:

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

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

另外,当我调整图表面板的大小时,我了解到表示数据点的形状不会像绘图的其余部分那样缩放。

问题

如何使形状(代表数据点的圆形和矩形)更小,以免它们阻塞在一起?

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

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

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

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

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

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

    另见ShapeUtilities

    【讨论】:

    • 矩形 rect = new Rectangle();维度 dd = new Dimension(); dd.setSize(2, 2); rect.setSize(dd); renderer.setSeriesShape(1, rect);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多