如何在时间序列jfree图表上添加文本/图像

时间:2014-10-31 06:15:23

标签: java jfreechart

要求:我需要在具有空数据的时间序列图上显示 toolTip(无数据可用)和图像; 灰色区域的图像2
问题:我无法得到它。
图像有些数据即; series1.addOrUpdate(absoluteMSecond,data [i]);
enter image description here

具有空数据的图像即; 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);
  }
}

提前致谢

1 个答案:

答案 0 :(得分:2)

您可以为情节setBackgroundImage()方法指定所需的图片,提及herehere

如果数据集为空,ChartPanelgetToolTipText()的实现将返回null,但您可以覆盖该方法以返回合适的替代字符串。