How do I change a JFreeChart's size
When you create your ChartPanel
, you have several options that affect the result:
Accept the
DEFAULT_WIDTH
andDEFAULT_HEIGHT
: 680 x 420.Specify the preferred
width
andheight
in the constructor.Invoke
setPreferredSize()
explicitly if appropriate.Override
getPreferredSize()
to calculate the size dynamically.@Override
public Dimension getPreferredSize() {
// given some values of w & h
return new Dimension(w, h);
}Choose the layout of the container to which the
ChartPanel
will be added. Note that the default layout ofJPanel
isFlowLayout
, while that ofJFrame
isBorderLayout
. As a concrete example,ThermometerDemo
uses both preferred values in the constructor and aGridLayout
for the container to allow dynamic resizing.
How to change marker size in XYPlot of jfreechart
You can achieve this by XYItemRenderer.setSeriesShape
. Please refer to how DefaultDrawingSupplier .createStandardSeriesShapes()
works.
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
double size = 20.0;
double delta = size / 2.0;
Shape shape1 = new Rectangle2D.Double(-delta, -delta, size, size);
Shape shape2 = new Ellipse2D.Double(-delta, -delta, size, size);
renderer.setSeriesShape(0, shape1);
renderer.setSeriesShape(1, shape2);
Reference: JFreeChart: Increase Size of Data Point
Set item shape size in jFreechart
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
.
How to change point size or shape in FastScatterPlot (JFreeChart)?
For speed, you might try the alternate FastScatterPlot
calculation suggested in the render()
method's source comments; profile to compare. For size, you can change the rendered size in the same method; the following would quadruple the size of each point.
g2.fillRect(transX, transY, 2, 2);
Java&JFreeChart - How to set a JFreeChart's ChartPanel resize with it's container ( say JPanel )?
Try my code and it works fine.
Note that :
I have one frame which contains a panelMain, A panelMain contains a subPanel and a subPanel contains ChartPanel.
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
JPanel panelMain = new JPanel(new GridLayout(0,2));
ChartPanel chartPanel = createChart();
JPanel subPanel = new JPanel(new BorderLayout());
subPanel.setBorder(BorderFactory.createTitledBorder("Consommation"));
subPanel.setPreferredSize(new Dimension(400, 200));
subPanel.add(chartPanel);
panelMain.add(subPanel);
frame.add(panelMain);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
And now when you resize your window application. Your chartPanel will resized automatically. Hope this helps.
Resize JFreeChart.ChartPanel on maximizing screen
Calling ChartPanel.setSize
after ChartPanel.setPreferredSize
and JFrame.validate
did the job! See the new code:
frame = new JFrame();
frame.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
// frame.repaint();
ChartPanel cp = (ChartPanel)panel.getComponents()[0];
cp.setPreferredSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight()));
cp.setSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight()));
// frame.invalidate();
frame.validate();
// frame.repaint();
}
...
});
...
Recompute JFreeChart display on ChartPanel resize
Solving Common Layout Problems suggests, "be sure that your component's container uses a layout manager that respects the requested size of the component." Your fragment uses BorderLayout.CENTER
, which is a good choice: it allows the the ChartPanel
to resize smoothly as the frame is resized, but it ignores the panel's minimum size. Eventually, resampling artifact and distortion appear. As you want to retain the resize behavior, one approach is to set the chart panel's minimum draw width and heigh to zero and (optionally) limit the frame's minimum size accordingly:
cp.setMinimumDrawWidth(0);
cp.setMinimumDrawHeight(0);
// optionally
f.setMinimumSize(new Dimension(
cp.getMinimumDrawWidth(),
cp.getMinimumDrawHeight()));
The draw width and heigh may also be specified in the constructor, as shown here. In the example below, note:
Override
getPreferredSize()
to establish the initial preferred size, also discussed here.Construct and manipulate Swing GUI objects only on the event dispatch thread.
Preferred size:
Smaller size:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* @see https://stackoverflow.com/q/69720552/230513
*/
public class ChartTest {
private static final int W = 320;
private static final int H = 240;
private void display() {
JFrame f = new JFrame("JFreeChart Resizing");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CategoryDataset ds = new DefaultCategoryDataset();
JFreeChart chart = ChartFactory.createLineChart(
"Chart Title", "X axis", "Y axis", ds);
ChartPanel cp = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(W, H);
}
};
f.add(cp, BorderLayout.CENTER);
f.add(new JLabel("Java v" + System.getProperty("java.version")
+ "; JFreeChart 1.5.3", JLabel.CENTER), BorderLayout.PAGE_END);
cp.setMinimumDrawWidth(0);
cp.setMinimumDrawHeight(0);
f.setMinimumSize(new Dimension(cp.getMinimumDrawWidth(), cp.getMinimumDrawHeight()));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new ChartTest()::display);
}
}
Related Topics
Failed to Load the Jni Shared Library (Jdk)
How to Parse a Local JSON File from Assets Folder into a Listview
Java 7 Language Features with Android
Android JSONobject - How to Loop Through a Flat JSON Object to Get Each Key and Value
Tomcat 7: How to Set Initial Heap Size Correctly
File.Encoding Has No Effect, Lc_All Environment Variable Does It
Installer/Packager for a Java Application for Ubuntu and Suse
How to Build Netty-Transport-Native-Epoll-4.0.32.Final-Linux-X86_64.Jar
How to Install Java Application to My Linux System
Converting a String to Hexadecimal in Java
Why Can't Static Methods Be Abstract in Java
Replacing a Fragment with Another Fragment Inside Activity Group
How to Convert a Color Integer to a Hex String in Android
How Does Activity.Finish() Work in Android
How to Check If a File Is Open by Another Process (Java/Linux)
Can't Start Cassandra (Single-Node Cluster on Centos7)
"Java.Sql.Sqlexception: I/O Error: Connection Reset" in Linux Server