Exemplo n.º 1
0
  public ChartWindow(String title) {

    this.symbol = title;
    thisp = this;
    // initialize the main layout
    setTitle(title);
    mainpanel = new JPanel();
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    mainpanel.setLayout(gbl);

    // build a toolbar
    //		add a plain status bar
    this.statusLine = new JLabel("Done");
    this.getContentPane().add(statusLine, BorderLayout.SOUTH);

    statusLine.setBackground(new Color(0, 0, 0));
    statusLine.setForeground(new Color(255, 255, 255));
    statusLine.setOpaque(true);

    //
    x1 = new TimeSeries("symbol", FixedMillisecond.class);
    dataset1.addSeries(x1);

    System.out.println("Populated.");

    //
    chart = createChart(dataset1);
    System.out.println("constr.");

    // chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

    chart.setAntiAlias(false);

    chartPanel = new ChartPanel(chart);

    //

    int i = 0;
    gbc.anchor = gbc.NORTHWEST;
    gbc.fill = gbc.BOTH;
    gbc.weightx = 1;
    gbc.gridx = 0;

    gbc.weighty = 1;
    gbc.gridy = i;
    gbl.setConstraints(chartPanel, gbc);

    mainpanel.add(chartPanel);
    // System.out.println("add");
    setVisible(true);
    setSize(new Dimension(400, 300));

    // chartPanel.setPopupMenu(buildPopupMenu());
    chartPanel.setMouseZoomable(true);
    chartPanel.setHorizontalAxisTrace(true);
    chartPanel.setVerticalAxisTrace(true);
    chartPanel.setHorizontalZoom(true);

    chartPanel.setOpaque(true);
    chartPanel.setBackground(new Color(0, 0, 0));

    this.getContentPane().add(mainpanel, BorderLayout.CENTER);

    this.setSize(600, 400);
    this.toFront();
    this.show();
  }
  /**
   * Returns a sequence plot as a ChartPanel.
   *
   * @param aProteinSequencePanelParent the protein sequence panel parent
   * @param sparklineDataset the dataset
   * @param proteinAnnotations the protein annotations
   * @param addReferenceLine if true, a reference line is added
   * @param allowZooming if true, the user can zoom in the created plot/chart
   * @return a sequence plot
   */
  public ChartPanel getSequencePlot(
      ProteinSequencePanelParent aProteinSequencePanelParent,
      JSparklinesDataset sparklineDataset,
      HashMap<Integer, ArrayList<ResidueAnnotation>> proteinAnnotations,
      boolean addReferenceLine,
      boolean allowZooming) {

    this.proteinSequencePanelParent = aProteinSequencePanelParent;
    DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset();
    StackedBarRenderer renderer = new StackedBarRenderer();
    renderer.setShadowVisible(false);
    CategoryToolTipGenerator myTooltips = new ProteinAnnotations(proteinAnnotations);

    // add the data
    for (int i = 0; i < sparklineDataset.getData().size(); i++) {

      JSparklinesDataSeries sparklineDataSeries = sparklineDataset.getData().get(i);

      for (int j = 0; j < sparklineDataSeries.getData().size(); j++) {
        barChartDataset.addValue(sparklineDataSeries.getData().get(j), "" + i, "" + j);
        renderer.setSeriesPaint(i, sparklineDataSeries.getSeriesColor());
        renderer.setSeriesToolTipGenerator(i, myTooltips);
      }
    }

    // create the chart
    JFreeChart chart =
        ChartFactory.createStackedBarChart(
            null, null, null, barChartDataset, PlotOrientation.HORIZONTAL, false, false, false);

    // fine tune the chart properites
    CategoryPlot plot = chart.getCategoryPlot();

    // remove space before/after the domain axis
    plot.getDomainAxis().setUpperMargin(0);
    plot.getDomainAxis().setLowerMargin(0);

    // remove space before/after the range axis
    plot.getRangeAxis().setUpperMargin(0);
    plot.getRangeAxis().setLowerMargin(0);

    renderer.setRenderAsPercentages(true);
    renderer.setBaseToolTipGenerator(new IntervalCategoryToolTipGenerator());

    // add the dataset to the plot
    plot.setDataset(barChartDataset);

    // hide unwanted chart details
    plot.getRangeAxis().setVisible(false);
    plot.getDomainAxis().setVisible(false);
    plot.setRangeGridlinesVisible(false);
    plot.setDomainGridlinesVisible(false);

    // add a reference line in the middle of the dataset
    if (addReferenceLine) {
      DefaultCategoryDataset referenceLineDataset = new DefaultCategoryDataset();
      referenceLineDataset.addValue(1.0, "A", "B");
      plot.setDataset(1, referenceLineDataset);
      LayeredBarRenderer referenceLineRenderer = new LayeredBarRenderer();
      referenceLineRenderer.setSeriesBarWidth(0, referenceLineWidth);
      referenceLineRenderer.setSeriesFillPaint(0, referenceLineColor);
      referenceLineRenderer.setSeriesPaint(0, referenceLineColor);
      plot.setRenderer(1, referenceLineRenderer);
    }

    // set up the chart renderer
    plot.setRenderer(0, renderer);

    // hide the outline
    chart.getPlot().setOutlineVisible(false);

    // make sure the background is the same as the panel
    chart.getPlot().setBackgroundPaint(backgroundColor);
    chart.setBackgroundPaint(backgroundColor);

    final HashMap<Integer, ArrayList<ResidueAnnotation>> blockTooltips = proteinAnnotations;

    // create the chart panel
    ChartPanel chartPanel = new ChartPanel(chart);

    chartPanel.addChartMouseListener(
        new ChartMouseListener() {

          @Override
          public void chartMouseClicked(ChartMouseEvent cme) {
            if (cme.getEntity() != null && cme.getTrigger().getButton() == MouseEvent.BUTTON1) {

              ((CategoryItemEntity) cme.getEntity()).getDataset();
              Integer blockNumber =
                  new Integer((String) ((CategoryItemEntity) cme.getEntity()).getRowKey());

              ArrayList<ResidueAnnotation> annotation = blockTooltips.get(blockNumber);
              if (annotation != null) {
                proteinSequencePanelParent.annotationClicked(annotation, cme);
              }
            }
          }

          @Override
          public void chartMouseMoved(ChartMouseEvent cme) {

            cme.getTrigger()
                .getComponent()
                .setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

            if (cme.getEntity() != null && cme.getEntity() instanceof CategoryItemEntity) {
              ((CategoryItemEntity) cme.getEntity()).getDataset();
              Integer blockNumber =
                  new Integer((String) ((CategoryItemEntity) cme.getEntity()).getRowKey());

              ArrayList<ResidueAnnotation> annotation = blockTooltips.get(blockNumber);
              if (annotation != null && !annotation.isEmpty()) {
                if (blockTooltips.get(blockNumber).get(0).isClickable()) {
                  cme.getTrigger()
                      .getComponent()
                      .setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
                }
              }
            }
          }
        });

    if (!allowZooming) {
      chartPanel.setPopupMenu(null);
      chartPanel.setRangeZoomable(false);
    }

    chartPanel.setBackground(Color.WHITE);

    return chartPanel;
  }