I am currently working with a multiseries plot. I have a array of plotSeries
Code: Select all
for(int ch = 0; ch < MAXCHANNELS; ch++) plotSeries[ch] = new XYSeries("Series " + ch);
Code: Select all
XYSeriesCollection data = new XYSeriesCollection();
XYLineAndShapeRenderer r = new XYLineAndShapeRenderer();
for(int ch = 0; ch < MAXCHANNELS; ch++) {
data.addSeries(plotSeries[ch]);
r.setSeriesStroke(ch, new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
r.setSeriesShape(ch, new Rectangle(new Dimension(0, 0)));
r.setSeriesPaint(ch, plotColor[ch]);
r.setSeriesToolTipGenerator(ch, new StandardXYToolTipGenerator() {
private static final long serialVersionUID = 1L;
public String generateToolTip(XYDataset dataset, int series, int item) {
String toolTipStr = "asdf";
return toolTipStr;
}
});
}
chart = ChartFactory.createXYLineChart("", "", "", data, PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = chart.getXYPlot();
plot.setRenderer(r);
I don't know if this is important, but I am also changing the content of the plotseries during runtime do display different kind of data. At the initialization, the plotSeries are empty.
Any help would be appreciated.
Cheers, Stefan
EDIT: Oh and I use jfreechart-1.0.13.jar and jcommon-1.0.16.jar
Ok, I created a runalone code example to show you what I mean. The example is a simple chart with 2 series in a dataset displayed. I also apply a renderer to control color/shape of the two curves and I also tried to create a tooltip (as the default one would not show up). But this tooltip does not show as well.
What I want is to create a tooltip which I can control what is written in it and which shows for all series.
Code: Select all
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.CustomXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
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;
public class PlotDemo extends JFrame {
private static final long serialVersionUID = 1L;
private XYSeries plotSeries1, plotSeries2;
public PlotDemo() {
XYSeriesCollection data = new XYSeriesCollection();
XYLineAndShapeRenderer r = new XYLineAndShapeRenderer();
plotSeries1 = new XYSeries("demoSeries1");
plotSeries2 = new XYSeries("demoSeries2");
for(int i = 0; i < 500; i++) {
plotSeries1.add(i, Math.sin(i / 30.0));
plotSeries2.add(i, Math.sin(i / 50.0));
}
data.addSeries(plotSeries1);
data.addSeries(plotSeries2);
r.setSeriesStroke(0, new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
r.setSeriesShape(0, new Rectangle(new Dimension(0, 0)));
r.setSeriesPaint(0, new Color(0.3f, 0.5f, 0.0f));
r.setSeriesStroke(1, new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
r.setSeriesShape(1, new Rectangle(new Dimension(0, 0)));
r.setSeriesPaint(1, new Color(1.0f, 0.1f, 0.0f));
r.setBaseToolTipGenerator(new CustomXYToolTipGenerator());
XYToolTipGenerator tt = new XYToolTipGenerator() {
@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return String.format("%.2fHz\n%.2fHz", arg0.getXValue(arg1, arg2), arg0.getYValue(arg1, arg2));
}
};
r.setSeriesToolTipGenerator(0, tt);
r.setSeriesToolTipGenerator(1, tt);
JFreeChart chart = ChartFactory.createXYLineChart("", "", "",
data, PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = chart.getXYPlot();
plot.setRenderer(r);
ValueAxis domain, range;
domain = new NumberAxis();
domain.setAutoRange(true);
range = new NumberAxis();
range.setAutoRange(true);
plot.setDomainAxis(domain);
plot.setRangeAxis(range);
ChartPanel phnPlotPanel = new ChartPanel(chart);
add(phnPlotPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
PlotDemo main = new PlotDemo();
main.setVisible(true);
main.setSize(new Dimension(400, 400));
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}