Пример #1
0
  public JPanel createChart(
      Date from,
      Date to,
      StockInfo stock1,
      StockInfo stock2,
      Color color1,
      Color color2,
      StockPriceType priceType) {

    LOG.info(
        "Generating chart for tickers:"
            + stock1.getTickerSymbol()
            + " & "
            + stock2.getTickerSymbol());

    String chartTitle = df.format(from) + " to " + df.format(to) + " " + priceType;
    XYDataset dataset1 = createDataset(stock1, priceType);
    XYDataset dataset2 = createDataset(stock2, priceType);

    JFreeChart chart =
        ChartFactory.createTimeSeriesChart(
            chartTitle,
            DOMAIN_AXIS_TITLE,
            stock1.getTickerSymbol() + RANGE_AXIS_SUFFIX,
            dataset1,
            true,
            true,
            false);

    XYPlot plot = chart.getXYPlot();
    NumberAxis axis2 = new NumberAxis(stock2.getTickerSymbol() + RANGE_AXIS_SUFFIX);
    Font tickLabelFont = axis2.getTickLabelFont().deriveFont(11.0F);
    Font labelFont = axis2.getLabelFont().deriveFont(15.0F).deriveFont(Font.BOLD);
    axis2.setTickLabelFont(tickLabelFont);
    axis2.setLabelFont(labelFont);
    axis2.setAutoRangeIncludesZero(false);
    plot.setRangeAxis(1, axis2);
    plot.setDataset(1, dataset2);
    plot.mapDatasetToRangeAxis(1, 1);

    StandardXYItemRenderer renderer = new StandardXYItemRenderer();
    renderer.setSeriesPaint(0, color1);
    renderer.setBaseShapesVisible(true);
    renderer.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
    plot.setRenderer(renderer);

    StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
    renderer2.setSeriesPaint(0, color2);
    renderer2.setBaseShapesVisible(true);
    renderer2.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
    plot.setRenderer(1, renderer2);

    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(df);

    ChartPanel chartPanel = new ChartPanel(chart);

    return chartPanel;
  }
Пример #2
0
  private XYDataset createDataset(StockInfo stock, StockPriceType priceType) {

    TimeSeries timeSeries = new TimeSeries(stock.getTickerSymbol());
    for (StockData sd : stock.getStockData()) {
      cal.setTime(sd.getDate());
      int month = cal.get(Calendar.MONTH);
      int day = cal.get(Calendar.DAY_OF_MONTH);
      int year = cal.get(Calendar.YEAR);
      timeSeries.add(new Day(day, month + 1, year), priceType.get(sd));
      // + 1 since calendar month starts at 0
    }

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(timeSeries);

    return dataset;
  }