Nicolas S.Xu
Nicolas S.Xu

Reputation: 14524

Set item shape size in jFreechart

I am plotting a line chart using jFreeChart, which is a series of (x, y) value connected by a straight line.

But the problem is the shape, a circle or a rect that represents a data point is too big, since I have a lot of values in one series. You can see how it looks like in this screenshot:

enter image description here

Also, I learned the shapes that represent data point do not scale as the rest of the plot does when I resize the chart panel.

Question:

How to make the shape(the circles and rects that represent data point) smaller so that they won't clog together?

Here is my code to generate the chart, and I used a customized renderer to draw different color for different part of the line in one data series.

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

Upvotes: 2

Views: 3209

Answers (1)

trashgod
trashgod

Reputation: 205785

You can override getItemShape() in your XYLineAndShapeRenderer to return any desired Shape, as shown here. Alternatively, invoke one of the abstract parent's methods, setSeriesShape(), setBaseShape() etc.

@Nicolas S.Xu reports using code similar to the following:

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

See also ShapeUtilities.

Upvotes: 3

Related Questions