JFreeChart - customizing RingChart - java

I want to make two ring charts that look like the following:
But the RingPlot doesn't seem very customizable. The best I could come up with is this:
Any chance of doing what I want with JFreeChart?

JFreeChart can do most things, this should get you started (I'll probably incorporate the center text feature into the upcoming 1.0.18 release so that the subclassing isn't necessary):
package org.jfree.chart.demo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlotState;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.text.TextUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
/**
* A simple demonstration application showing how to create a ring chart using
* data from a {#link DefaultPieDataset}.
*/
public class RingChartDemo1 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
static class CustomRingPlot extends RingPlot {
private Font centerTextFont;
private Color centerTextColor;
public CustomRingPlot(PieDataset dataset) {
super(dataset);
this.centerTextFont = new Font(Font.SANS_SERIF, Font.BOLD, 24);
this.centerTextColor = Color.LIGHT_GRAY;
}
#Override
protected void drawItem(Graphics2D g2, int section,
Rectangle2D dataArea, PiePlotState state, int currentPass) {
super.drawItem(g2, section, dataArea, state, currentPass);
if (currentPass == 1 && section == 0) {
Number n = this.getDataset().getValue(section);
g2.setFont(this.centerTextFont);
g2.setPaint(this.centerTextColor);
TextUtilities.drawAlignedString(n.toString(), g2,
(float) dataArea.getCenterX(),
(float) dataArea.getCenterY(),
TextAnchor.CENTER);
}
}
}
/**
* Default constructor.
*
* #param title the frame title.
*/
public RingChartDemo1(String title) {
super(title);
setContentPane(createDemoPanel());
}
/**
* Creates a sample dataset.
*
* #return A sample dataset.
*/
private static PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("A", new Double(210));
dataset.setValue("B", new Double(150));
return dataset;
}
/**
* Creates a chart.
*
* #param dataset the dataset.
*
* #return A chart.
*/
private static JFreeChart createChart(PieDataset dataset) {
CustomRingPlot plot = new CustomRingPlot(dataset);
JFreeChart chart = new JFreeChart("Custom Ring Chart",
JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(new GradientPaint(new Point(0, 0),
new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));
// customise the title position and font
TextTitle t = chart.getTitle();
t.setHorizontalAlignment(HorizontalAlignment.LEFT);
t.setPaint(new Color(240, 240, 240));
t.setFont(new Font("Arial", Font.BOLD, 26));
plot.setBackgroundPaint(null);
plot.setOutlineVisible(false);
plot.setLabelGenerator(null);
plot.setSectionPaint("A", Color.ORANGE);
plot.setSectionPaint("B", new Color(100, 100, 100));
plot.setSectionDepth(0.05);
plot.setSectionOutlinesVisible(false);
plot.setShadowPaint(null);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* #return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
chart.setPadding(new RectangleInsets(4, 8, 2, 2));
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
panel.setPreferredSize(new Dimension(600, 300));
return panel;
}
/**
* Starting point for the demonstration application.
*
* #param args ignored.
*/
public static void main(String[] args) {
RingChartDemo1 demo = new RingChartDemo1("JFreeChart: Ring Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}

Related

how to add circular graduations otherwise circles to polar plot?

Starting from the code below , is there a way to add graduation to plot ?
the aim is to obtain a plot like posted.
i found addannotation for linechart plot but not for polar ...
Thank you
package jfreechart;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class JFreeChartPolarChartExample2 extends JFrame {
private static final long serialVersionUID = 1L;
public JFreeChartPolarChartExample2(String applicationTitle) {
super(applicationTitle);
XYSeriesCollection dataSet1 = new XYSeriesCollection();
JFreeChart jfreeChart = ChartFactory.createPolarChart(null, dataSet1, true, true, false);
final XYSeries series = new XYSeries(" serie1");
series.add(10, 0.90);
series.add(100, 0.10);
series.add(195, 0.50);
series.add(295, 0.50);
dataSet1.addSeries(series);
PolarPlot polarPlot = (PolarPlot) jfreeChart.getPlot();
ChartPanel chartPanel = new ChartPanel(jfreeChart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 500));
setContentPane(chartPanel);
}
public static void main(String[] args) {
JFreeChartPolarChartExample2 chart = new JFreeChartPolarChartExample2(null);
chart.pack();
chart.setVisible(true);
}
}

How can I read all values on crossline in JFreeChart? [duplicate]

On an XYPlot, I want the coordinates of mouse displayed as hint near to mouse, but only when (the mouse) move on chart! In another words, when the crosshair moves to another point, the positioning of the coordinate values would move too, following the crosshair.
Also 1 horizontal line and 1 vertical line will be drawn, that intersects specifically at the point that mouse is over on.
Is this possible?
Until now I can get the coordinates and printed on console using ChartMouseListener and chartMouseMoved method.
This is my project and how I want to be the chart with mouse.
JFreeChart has quite flexible support for crosshairs. To do what you described I would use an Overlay on the ChartPanel, and update the crosshairs from your ChartMouseListener. Here is a self-contained example (which I'll add to the collection of demos that we ship with the JFreeChart Developer Guide):
package org.jfree.chart.demo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.panel.CrosshairOverlay;
import org.jfree.chart.plot.Crosshair;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;
/**
* A demo showing crosshairs that follow the data points on an XYPlot.
*/
public class CrosshairOverlayDemo1 extends JFrame implements ChartMouseListener {
private ChartPanel chartPanel;
private Crosshair xCrosshair;
private Crosshair yCrosshair;
public CrosshairOverlayDemo1(String title) {
super(title);
setContentPane(createContent());
}
private JPanel createContent() {
JFreeChart chart = createChart(createDataset());
this.chartPanel = new ChartPanel(chart);
this.chartPanel.addChartMouseListener(this);
CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
this.xCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
this.xCrosshair.setLabelVisible(true);
this.yCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
this.yCrosshair.setLabelVisible(true);
crosshairOverlay.addDomainCrosshair(xCrosshair);
crosshairOverlay.addRangeCrosshair(yCrosshair);
chartPanel.addOverlay(crosshairOverlay);
return chartPanel;
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart("Crosshair Demo",
"X", "Y", dataset);
return chart;
}
private XYDataset createDataset() {
XYSeries series = new XYSeries("S1");
for (int x = 0; x < 10; x++) {
series.add(x, x + Math.random() * 4.0);
}
XYSeriesCollection dataset = new XYSeriesCollection(series);
return dataset;
}
#Override
public void chartMouseClicked(ChartMouseEvent event) {
// ignore
}
#Override
public void chartMouseMoved(ChartMouseEvent event) {
Rectangle2D dataArea = this.chartPanel.getScreenDataArea();
JFreeChart chart = event.getChart();
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis xAxis = plot.getDomainAxis();
double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea,
RectangleEdge.BOTTOM);
double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x);
this.xCrosshair.setValue(x);
this.yCrosshair.setValue(y);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CrosshairOverlayDemo1 app = new CrosshairOverlayDemo1(
"JFreeChart: CrosshairOverlayDemo1.java");
app.pack();
app.setVisible(true);
}
});
}
}

How to change the side range value colors in thermometer in JFreeChart

I found a thermometer demo and customized it for my dashboard project. http://www.java2s.com/Code/Java/Chart/JFreeChartThermometerDemo2.htm
In my dashboard I have six thermometer with different Mercury Color. However, I cannot seem to find a way to change the color of the range numbers that are displayed beside the thermometer.
How do I change the range number text color from Black to White?
My Demo Thermometer screenshot
package Thermometers;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import App.App_v2;
public class ThermometerDemo2 extends JPanel
{
private static final long serialVersionUID = 1L;
public ThermometerDemo2(Color color, double maxValue)
{
// create a dataset...
final DefaultValueDataset dataset = new DefaultValueDataset(110);
// create the chart...
final ThermometerPlot plot = new ThermometerPlot(dataset);
plot.setRange(0.0, maxValue);
plot.setSubrange(ThermometerPlot.CRITICAL, 250, 300);
plot.setValueFont(new Font("Georgia", Font.BOLD, 32));
plot.setThermometerStroke(new BasicStroke(2.0f));
plot.setBackgroundPaint(new Color(20,42,60));
plot.setMercuryPaint(color);
final JFreeChart chart = new JFreeChart(plot);
chart.setBorderVisible(false);
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
this.add(chartPanel);
this.setBackground(new Color(20,42,60));
}
public static void main(final String[] args) {
JFrame frame = new JFrame("change the black range color into white");
frame.setVisible(true);
frame.setSize(500, 500);
ThermometerDemo2 demo = new ThermometerDemo2(Color.magenta, 300);
demo.setVisible(true);
frame.add(demo);
frame.pack();
}
}

My x-axis integer values are prefixed with "19:00:00."

I am new to jFreeChart. I am trying to run the example code provided with the download. It is working fine. I want to display integer values on the x-axis. I changed the program to use NumberAxis instead of DateAxis. However, when I generated the graph, it displays my integer x values prefixed with "19:00:00." (it should be 5, 10 but it shows 19:00:00.005, 19:00:00.010). What am I doing wrong here?
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.io.File;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.ChartUtilities;
/**
* An example of a time series chart. For the most part, default settings are
* used, except that the renderer is modified to show filled shapes (as well as
* lines) at each data point.
*/
public class PlotExample3 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
{
// set a theme using the new shadow generator feature available in
// 1.0.14 - for backwards compatibility it is not enabled by default
ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",
true));
}
/**
* A demonstration application showing how to create a simple time series
* chart. This example uses monthly data.
*
* #param title the frame title.
*/
public PlotExample3(String title) {
super(title);
ChartPanel chartPanel = (ChartPanel) createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a chart.
*
* #param dataset a dataset.
*
* #return A chart.
*/
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", // title
"X", // x-axis label
"Y", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
chart.setBackgroundPaint(Color.white);
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(1, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return chart;
}
/**
* Creates a dataset, consisting of two series of monthly data.
*
* #return The dataset.
*/
private static XYDataset createDataset() {
XYSeries s1 = new XYSeries("Test");
s1.add(1, 181.8);
s1.add(2, 167.3);
s1.add(3, 153.8);
s1.add(4, 167.6);
s1.add(5, 158.8);
s1.add(7, 148.3);
s1.add(8, 153.9);
s1.add(9, 142.7);
s1.add(10,123.2);
s1.add(11,131.8);
s1.add(12,139.6);
// ******************************************************************
// More than 150 demo applications are included with the JFreeChart
// Developer Guide...for more information, see:
//
// > http://www.object-refinery.com/jfreechart/guide.html
//
// ******************************************************************
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* #return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
try {
ChartUtilities.saveChartAsPNG(new File("C:/Users/pb/Pictures/MyChart.png"),chart,1000,1000);
} catch(Exception e) {
System.err.println(e.getMessage());
}
panel.setFillZoomRectangle(true);
panel.setMouseWheelEnabled(true);
return panel;
}
/**
* Starting point for the demonstration application.
*
* #param args ignored.
*/
public static void main(String[] args) {
PlotExample3 demo = new PlotExample3("Time Series Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
ChartFactory.createTimeSeriesChart() creates a chart with a DateAxis for the x-axis and a NumberAxis for the y-axis. The Javadocs for this method even say that: "A time series chart is an XYPlot with a DateAxis for the x-axis and a NumberAxis for the y-axis."
You should try ChartFactory.createXYLineChart() which will give you a NumberAxis for the x-axis.

JFreechart series tool tip above shape annotation

I have an XYPlot on which are series and a couple of dynamically added shape annotations with no fill (hence each of the series points are visible). Is it possible to display the series tool tips(that show the coordinate of the series point over which the mouse pointer is currently pointing to) over the annotations? Or how can I re-arrange the elements in order to make the tooltip visible.
I suspect you are adding the shape annotations to the plot, where they are drawn last. Instead, add them to the renderer in Layer.BACKGROUND. As shown below, the circle does not obscure the tool tip at (20, 20). Note also how (10, 10) is not affected by the line annotation, while (30, 30) is obscured by the arc.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.Layer;
/**
* #see http://stackoverflow.com/questions/6797012
* #see http://stackoverflow.com/questions/6604211
*/
public class ArcTest {
private static final Random r = new Random();
private static final Color blue = Color.blue;
private static final BasicStroke stroke = new BasicStroke(2.0f);
private static final double PI = 180d;
private static final int X = 8;
private static final int Y = 0;
private static final int W = 6 * X;
private static final int H = 3 * X;
public static void main(String[] args) {
JFreeChart chart = ChartFactory.createXYLineChart(
"ArcTest", "X", "Y", createDataset(),
PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer =
(XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
Ellipse2D.Double circle = new Ellipse2D.Double(X, X, 20, 20);
renderer.addAnnotation(new XYShapeAnnotation(
circle, stroke, blue), Layer.BACKGROUND);
XYLineAnnotation line = new XYLineAnnotation(X, Y, X, H, stroke, blue);
plot.addAnnotation(line);
Arc2D.Double arc = new Arc2D.Double(X, Y, W, 2 * H, PI, PI, Arc2D.OPEN);
plot.addAnnotation(new XYShapeAnnotation(arc, stroke, blue));
ChartFrame frame = new ChartFrame("Test", chart);
frame.pack();
frame.setVisible(true);
}
private static XYDataset createDataset() {
XYSeriesCollection result = new XYSeriesCollection();
XYSeries series = new XYSeries("ArcTest");
series.add(0, 0);
series.add(10, 10);
series.add(20, 20);
series.add(30, 30);
series.add(W, W);
result.addSeries(series);
return result;
}
}

Categories

Resources