Wednesday, May 15, 2013

Jfreechart - XYSplineRenderer

        XYSeriesCollection  dataset = new XYSeriesCollection();
        JFreeChart chart = ChartFactory.createXYLineChart(
                null, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, false);

       //add Series
       XYSeries series1= new XYSeries("Series 1");
       Random rn = new Random();
        for (int i = 0; i <=50; i=i+5) {
            int r = rn.nextInt();
            series1.add((double) i,(double) r);
        }
       dataset.addSeries(series1)


        XYPlot xyPlot = chart.getXYPlot();
        //Set plot style
        XYSplineRenderer xySplineRenderer = new XYSplineRenderer();
        xyPlot.setRenderer(xySplineRenderer);

        //get xAxis
        NumberAxis xAxis = (NumberAxis) xyPlot.getDomainAxis();
        //set xAxis range from 0-xMax
        xAxis.setRange(0,xMax);
        //set xAxis unit
       
         //format number of axis
        //NumberFormat numberFormat = NumberFormat.getInstance(Locale.GERMANY);
        xAxis.setTickUnit(new NumberTickUnit(xUnit));
        xAxis.setNumberFormatOverride(new DecimalFormat("###.0"));
       
        //get yAxis
        NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();
        //set yAxis range from 0-yMax
        yAxis.setRange(0,yMax);

        //set yAxis unit
        yAxis.setTickUnit(new NumberTickUnit(yUnit));
        yAxis.setNumberFormatOverride(new DecimalFormat("###.0"));


        //Plot Style Sheet
        //set background background
        xyPlot.setBackgroundPaint(Color.white);

        //Create grid style
        xyPlot.setDomainGridlinePaint(Color.gray);
        xyPlot.setRangeGridlinePaint(Color.gray);
       
        //Set legend Position
        LegendTitle legend = chart.getLegend();
        legend.setPosition(RectangleEdge.BOTTOM);

        //Set series style (color, shape point)
        //Change shape point into circle
        Shape circle  = new Ellipse2D.Double(-2,-2,5,5);
       
        xySplineRenderer.setSeriesPaint(0,Color.green);
        xySplineRenderer.setSeriesShape(0,circle);
       
        xySplineRenderer.setSeriesPaint(1,Color.red);
        xySplineRenderer.setSeriesShape(1,circle);

Related Posts:

  • Jfreechart - XYSplineRenderer        XYSeriesCollection  dataset = new XYSeriesCollection();         JFreeChart chart = ChartFactory.createXYLineChart(         &nbs… Read More
  • Jfreechart - Change line styeThis would make your second series show as a dashed line: plot.getRenderer().setSeriesStroke( 1, new BasicStroke( 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] {6.0f, 6.0f}, 0… Read More
  • Jfreechart - Change Shape PointDiamon/Cross/Triangle Shape renderer.setSeriesShape(seriesIndex,ShapeUtilities.createDiamond(5)); renderer.setSeriesShape(seriesIndex,ShapeUtilities.createDiagonalCross(5,5)); .... Circle Shape circle  = new Ellipse2D.… Read More
  • Jfreechart - Tips Set a FormatOverride on the axis for NumberAxis and a DateAxis   XYPlot plot = (XYPlot) chart.getPlot(); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setNumberFormatOverride( new NumberFormat(){ … Read More

0 comments:

Post a Comment