Pydi
Pydi

Reputation: 77

How to add text/image on time series jfree chart

Requirement: i need to display toolTip(No data available) and image on time series chart which have null data ie; Image 2 on gray color area.
Problem:I am not able to get it.
Image With some data ie; series1.addOrUpdate(absoluteMSecond, data[i]);
enter image description here

Image with null data ie; series1.addOrUpdate(absoluteMSecond, null);
enter image description here

COde:

import java.util.Calendar;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class TextOnJFreeChart extends ApplicationFrame {

  public TextOnJFreeChart(final String title) {
    super(title);
    final XYDataset data = createDataset();
    final JFreeChart chart = createChart(data);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
  }

  private JFreeChart createChart(final XYDataset data) {

    final JFreeChart chart = ChartFactory.createTimeSeriesChart("Text/ToolTip Trying Demo", "X", "Y", data, true, true, true);
    final XYPlot plot = chart.getXYPlot();
    plot.getRenderer().setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
    plot.setNoDataMessage("Hai it is ok if i get this message.......");

    final DateAxis domainAxis = new DateAxis("Time");
    domainAxis.setUpperMargin(0.50);
    plot.setDomainAxis(domainAxis);

    final ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setUpperMargin(0.30);
    rangeAxis.setLowerMargin(0.50);

    return chart;
  }

  private XYDataset createDataset() {

    final TimeSeriesCollection result = new TimeSeriesCollection();
    result.addSeries(createSupplier1Bids());
    return result;
  }

  private TimeSeries createSupplier1Bids() 
  {

    double[] data = {200.0, 195.0, 190.0, 188.0, 185.0, 180.0};
    long timeStamp = System.currentTimeMillis();
    Millisecond absoluteMSecond = getTimeInMillisecondFormat(timeStamp, 0L);
    final TimeSeries series1 = new TimeSeries("Supplier 1", Millisecond.class);
    for(int i = 0; i < data.length; i++)
    {
      absoluteMSecond = getTimeInMillisecondFormat(timeStamp + i * 1000, 0L);
      //series1.addOrUpdate(absoluteMSecond, data[i]);
      series1.addOrUpdate(absoluteMSecond, null);
    }
    return series1;
  }

  public Millisecond getTimeInMillisecondFormat(long timeStamp, long startTime)
  {
    try
    {
      long diff = timeStamp - startTime;
      Calendar calender = Calendar.getInstance();
      calender.setTimeInMillis(diff);
      Millisecond elapsedMSecond = new Millisecond(calender.getTime());
      return (elapsedMSecond);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }

  public static void main(final String[] args) {

    final TextOnJFreeChart demo = new TextOnJFreeChart("Text/ToolTip Trying Demo");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
  }
}

Thanks in advance

Upvotes: 1

Views: 972

Answers (1)

trashgod
trashgod

Reputation: 205805

You can specify the desired image to the plot's setBackgroundImage() method, mentioned here and here.

The implementation of getToolTipText() in ChartPanel will return null if the dataset is empty, but you can override the method to return a suitable alternative string.

Upvotes: 2

Related Questions