0

I have a line chart and have it creating a smooth curve instead of a jagged graph. (I am doing this via renderers, XYSplineRenderer as an example)

The curve should have inflection points at the data points but rises above or below and does not look good and is misleading (implying that some data exists above the max and/or below the min) enter image description here

Code that produces this:

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class LineChart {

    public String saveToFile(String path,String pathColor) {
        try {

            JFreeChart chart = getChart(pathColor,"","","",false);
            Point p = new Point(400,300);
            File chartFile = new File(path);
            ChartUtils.writeChartAsPNG(new FileOutputStream(chartFile), chart, Double.valueOf(p.getX()).intValue(), Double.valueOf(p.getY()).intValue(),false,0);

        } catch (Throwable t) {
            t.printStackTrace();
        }
        return null;
    }

    private JFreeChart getChart(
            String hexColor,
            String title,
            String category,
            String value,
            boolean includeLegend) {

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        TimeSeries series1 = new TimeSeries("Total");
        series1.add(new Day(7,10,2019),0);
        series1.add(new Day(8,10,2019),6);
        series1.add(new Day(9,10,2019),0);
        series1.add(new Day(10,10,2019),2);
        series1.add(new Day(11,10,2019),0);

        dataset.addSeries(series1);

        XYSplineRenderer renderer = new XYSplineRenderer();
        renderer.setDrawOutlines(true);
        renderer.setPrecision(1000);

        renderer.setDrawSeriesLineAsPath(true);

        renderer.setSeriesPaint(0, Color.decode(hexColor)); 
        renderer.setSeriesShapesVisible(0,  true);
        renderer.setSeriesItemLabelsVisible(0, true);
        renderer.setSeriesItemLabelPaint(0, Color.decode(hexColor));
        renderer.setSeriesShape(0, new Ellipse2D.Double(-3, -3, 6, 6));

        renderer.setSeriesItemLabelGenerator(0, new XYItemLabelGenerator() {

            @Override
            public String generateLabel(XYDataset dataset, int series, int item) {
                Double d = dataset.getYValue(series, item);
                return "" + d.intValue();
            }
        });

        JFreeChart chart = ChartFactory.createTimeSeriesChart(title, category, value, null,includeLegend, true, false);
        chart.setAntiAlias(true);
        XYPlot plot = (XYPlot)chart.getPlot();
        plot.setRangeGridlinePaint(new Color(155, 155, 155) );
        plot.setBackgroundPaint(new Color(0, 0, 0, 0));

        final TickUnits standardUnits = new TickUnits();
        standardUnits.add(
                new DateTickUnit(DateTickUnitType.DAY, 1, new SimpleDateFormat("dd E"))
        );

        DateAxis domainAxis = (DateAxis)plot.getDomainAxis();
        domainAxis.setTickLabelFont(new Font("Montserrat",Font.BOLD, 10));
        domainAxis.setTickLabelPaint(Color.decode("#9b9b9b"));

        domainAxis.setStandardTickUnits(standardUnits);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setTickLabelFont(new Font("Montserrat",Font.BOLD, 10));
        rangeAxis.setTickLabelPaint(Color.decode("#9b9b9b"));

        plot.setDataset(0, dataset);
        plot.setRenderer(0, renderer);
        plot.setOutlinePaint(null);
        return chart;
    }
}
Jeff I
  • 396
  • 1
  • 2
  • 13
  • Possible duplicate of [*JFreeChart adding trend line outside of actual values?*](https://stackoverflow.com/q/23452585/230513) – trashgod Oct 26 '19 at 00:46
  • Does this answer your question? [JFreeChart adding trendline outside of actual values?](https://stackoverflow.com/questions/23452585/jfreechart-adding-trendline-outside-of-actual-values) – Catalina Island Nov 16 '19 at 16:00

0 Answers0