Avoid filling the points in a XYLineChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Avoid filling the points in a XYLineChart

Post by DerPe » Mon Aug 14, 2017 10:20 am

Hi guys,

I just began to use JFreeChart and I have two questions:

1) I use a XLChart for displaying a timeserie with 1440 entries. The problem is that the lines are quite fat. Changing the series stroke does not have an effect:

Code: Select all

 XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
I think the problem is that the points are filled. I also used the methode " setShapesFilled(false)" but this also did not help. Would anyone mind telling me what I have to do in order to get thin lines?

2) I just copied an example from the internet to get started:

Code: Select all

package testing;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import generateProfiles.Reader_ExternalSignals;

public class LineChartEx extends JFrame {

    public LineChartEx() {

        initUI();
    }

    private void initUI() {

        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        chartPanel.setBackground(Color.white);
        add(chartPanel);

        pack();
        setTitle("Line chart");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private XYDataset createDataset() {

        XYSeries series = new XYSeries("2016");
     
        String inputFile1 = "src/testing/testTemperature.csv";
        double[] heatDemand = Reader_ExternalSignals.readAggregatedExternalSignal(1, inputFile1);
        
for (int i=0; i<1440; i++) {
	series.add(i,heatDemand[i]);
}
        
	XYSeries series2 = new XYSeries("2017");
	
	 series2.add(18, 40);
     series2.add(20, 42);
     series2.add(25, 50);
     series2.add(30, 48);
     series2.add(40, 43);
     series2.add(1345, 47);


        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        dataset.addSeries(series2);

        return dataset;
    }


	private JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "Temperature of the hot water tank", 
                "Minute of the day", 
                "Temperature in °C", 
                dataset, 
                PlotOrientation.VERTICAL,
                true, 
                true, 
                false 
        );

        XYPlot plot = chart.getXYPlot();

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        

        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.white);

        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);

        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.BLACK);

        chart.getLegend().setFrame(BlockBorder.NONE);

        chart.setTitle(new TextTitle("Temperature of the hot water tank",
                        new Font("Serif", java.awt.Font.BOLD, 18)
                )
        );

        return chart;

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            LineChartEx ex = new LineChartEx();
            ex.setVisible(true);
        });
    }

I would like to know how you can use the chart as an element of a GUI? I would like to embed the chart into a Container of a GUI. For example it could be stored in a JLabel (or something similar). Is there a way how to create a chart and store it as an element in the code which then can be used for typical GUI elements?

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Avoid filling the points in a XYLineChart

Post by John Matthews » Mon Aug 14, 2017 11:16 am

1) The setShapesFilled() method is deprecated. Try one of these:

Code: Select all

renderer.setSeriesShapesFilled(series, false);
renderer.setBaseShapesFilled(false);
2) The examples in org.jfree.chart.demo, included in the distribution, are typical. Like your example, each creates a ChartPanel and adds it to the enclosing ApplicationFrame , a subclass of JFrame.

DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Re: Avoid filling the points in a XYLineChart

Post by DerPe » Mon Aug 14, 2017 3:53 pm

Hi John Matthews, thanks for your answer,

When I try "renderer.setBaseShapesFilled(false);" nothing changes at all. The lines are still as thick as the used to be. After inserting "renderer.setSeriesShapesFilled(series, false);" I get either the error message that rederer cannot be resolved or that series cannot be resolved (depending on where I insert the line in the cost that I posted).

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Avoid filling the points in a XYLineChart

Post by John Matthews » Mon Aug 14, 2017 5:03 pm

Starting from the example cited, I get the following result with the following modification to createChart(). Note that ChartFactory.createXYLineChart() already supplies an instance of XYLineAndShapeRenderer, which may be modified in situ.

Code: Select all

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesFilled(false);
renderer.setDrawOutlines(false);
…
//plot.setRenderer(renderer);
Image

DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Re: Avoid filling the points in a XYLineChart

Post by DerPe » Tue Aug 15, 2017 9:45 am

Thanks John Matthews for your answer,

now it looks good :P .

Nonetheless I have some further questions:
1)On the left and right side of your diagramm you can see an empty space within the chart. How can you eliminte those? (I am not talking about the border; I am talking about the empty space directly next to the red curve. So basically the diagramm should stop at Age=50 and not at Age=52).
2) How can you alter the range and descpriton of the y-axis? Let's say I want the y-Axis (Salary in your case) to start at 400 € and I want to have a description for every 100 € (not every 200€ as in your example). What do I have to do?
3) How can you generally decrease the size of the whole diagramm? Let's say I want the daigramm to be 20 %smaller in both directions. What would be the command for this (if there is one)?

Thanks for your attention. I'd appreciate input.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Avoid filling the points in a XYLineChart

Post by John Matthews » Tue Aug 15, 2017 5:50 pm

1) You can adjust the lower and upper margins of the relevant axis.

2) I usually let the axis auto range, but you can setAutoRange(false) and adjust the bounds manually.

3) I habitually override the ChartPanel method getPreferredSize() and specify the desired Dimension.

Locked