コード例 #1
0
  private void updateChart(Security security) {
    ISeries series = chart.getSeriesSet().getSeries(Messages.ColumnQuote);
    if (series != null) chart.getSeriesSet().deleteSeries(Messages.ColumnQuote);
    chart.clearMarkerLines();

    if (security == null || security.getPrices().isEmpty()) {
      chart.getTitle().setText(security == null ? "..." : security.getName()); // $NON-NLS-1$
      chart.redraw();
      return;
    }

    chart.getTitle().setText(security.getName());

    List<SecurityPrice> prices = security.getPrices();

    int index;
    Date[] dates;
    double[] values;

    if (chartPeriod == null) {
      index = 0;
      dates = new Date[prices.size()];
      values = new double[prices.size()];
    } else {
      index =
          Math.abs(
              Collections.binarySearch(
                  prices, new SecurityPrice(chartPeriod, 0), new SecurityPrice.ByDate()));

      if (index >= prices.size()) {
        // no data available
        chart.redraw();
        return;
      }

      dates = new Date[prices.size() - index];
      values = new double[prices.size() - index];
    }

    for (int ii = 0; index < prices.size(); index++, ii++) {
      SecurityPrice p = prices.get(index);
      dates[ii] = p.getTime();
      values[ii] = p.getValue() / Values.Quote.divider();
    }

    ILineSeries lineSeries =
        (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, Messages.ColumnQuote);
    lineSeries.setXDateSeries(dates);
    lineSeries.setLineWidth(2);
    lineSeries.enableArea(true);
    lineSeries.setSymbolType(PlotSymbolType.NONE);
    lineSeries.setYSeries(values);
    lineSeries.setAntialias(SWT.ON);

    chart.getAxisSet().adjustRange();

    for (Portfolio portfolio : getClient().getPortfolios()) {
      for (PortfolioTransaction t : portfolio.getTransactions()) {
        if (t.getSecurity() == security
            && (chartPeriod == null || chartPeriod.before(t.getDate()))) {
          String label = Values.Share.format(t.getShares());
          switch (t.getType()) {
            case BUY:
            case TRANSFER_IN:
            case DELIVERY_INBOUND:
              chart.addMarkerLine(t.getDate(), new RGB(0, 128, 0), label);
              break;
            case SELL:
            case TRANSFER_OUT:
            case DELIVERY_OUTBOUND:
              chart.addMarkerLine(t.getDate(), new RGB(128, 0, 0), "-" + label); // $NON-NLS-1$
              break;
            default:
              throw new UnsupportedOperationException();
          }
        }
      }
    }

    chart.redraw();
  }
コード例 #2
0
  protected void createBottomTable(Composite parent) {
    SashForm sash = new SashForm(parent, SWT.HORIZONTAL);

    // folder
    CTabFolder folder = new CTabFolder(sash, SWT.BORDER);

    // latest
    latest = new SecurityDetailsViewer(sash, SWT.BORDER, getClient());
    latest.getControl().pack();
    int width = latest.getControl().getBounds().width;
    sash.setWeights(new int[] {parent.getParent().getParent().getBounds().width - width, width});

    // tab 1: chart
    CTabItem item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.SecurityTabChart);

    Composite chartComposite = new Composite(folder, SWT.NONE);
    GridLayoutFactory.fillDefaults().numColumns(2).spacing(0, 0).applyTo(chartComposite);
    item.setControl(chartComposite);

    chart = new TimelineChart(chartComposite);
    chart.getTitle().setText("..."); // $NON-NLS-1$
    GridDataFactory.fillDefaults().grab(true, true).applyTo(chart);

    Composite buttons = new Composite(chartComposite, SWT.NONE);
    buttons.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
    GridDataFactory.fillDefaults().grab(false, true).applyTo(buttons);
    RowLayoutFactory.fillDefaults().type(SWT.VERTICAL).spacing(2).fill(true).applyTo(buttons);

    addButton(buttons, Messages.SecurityTabChart1M, Calendar.MONTH, -1);
    addButton(buttons, Messages.SecurityTabChart2M, Calendar.MONTH, -2);
    addButton(buttons, Messages.SecurityTabChart6M, Calendar.MONTH, -6);
    addButton(buttons, Messages.SecurityTabChart1Y, Calendar.YEAR, -1);
    addButton(buttons, Messages.SecurityTabChart2Y, Calendar.YEAR, -2);
    addButton(buttons, Messages.SecurityTabChart3Y, Calendar.YEAR, -3);
    addButton(buttons, Messages.SecurityTabChart5Y, Calendar.YEAR, -5);
    addButton(buttons, Messages.SecurityTabChart10Y, Calendar.YEAR, -10);

    Button button = new Button(buttons, SWT.FLAT);
    button.setText(Messages.SecurityTabChartAll);
    button.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            chartPeriod = null;

            Security security = (Security) prices.getData(Security.class.toString());
            updateChart(security);
          }
        });

    // tab 2: historical quotes
    item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.SecurityTabHistoricalQuotes);
    item.setControl(createPricesTable(folder));

    // tab 3: transactions
    item = new CTabItem(folder, SWT.NONE);
    item.setText(Messages.SecurityTabTransactions);
    item.setControl(createTransactionTable(folder));

    folder.setSelection(0);
  }