Ejemplo n.º 1
0
  public GenericScatterGraph(XYSeries series, String xAxis, String yAxis) {
    super();

    XYDataset xyDataset = new XYSeriesCollection(series);

    JFreeChart chart =
        ChartFactory.createScatterPlot(
            "", // chart title
            xAxis, // x axis label
            yAxis, // y axis label
            null,
            PlotOrientation.VERTICAL,
            false, // include legend
            true, // tooltips
            false // urls
            );

    chart.setBackgroundPaint(Color.darkGray);
    plot = chart.getXYPlot();
    plot.setDataset(0, xyDataset);
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);

    // Shape cross = ShapeUtilities.createDiamond(0.5f);
    Shape cross = ShapeUtilities.createDiagonalCross(0.5f, 0.5f);
    XYItemRenderer renderer = plot.getRenderer();
    renderer.setSeriesPaint(0, new Color(252, 141, 89));
    renderer.setSeriesShape(0, cross);

    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setTickLabelPaint(Color.white);
    domainAxis.setLabelPaint(Color.white);

    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setTickLabelPaint(Color.white);
    rangeAxis.setLabelPaint(Color.white);

    chartPanel = new ChartPanel(chart);
    chartPanel.setSize(100, 800);

    setLayout(new BorderLayout());
    add(chartPanel, BorderLayout.CENTER);
    setBackground(Color.black);

    chartPanel.revalidate();
    setVisible(true);
  }
 private BufferedImage generarGraficoLineal(
     String titulo,
     String ejeHorizontal,
     String ejeVertical,
     double[] valores,
     String[] funciones,
     boolean porcentaje,
     boolean leyenda,
     int anchoImagen,
     int altoImagen) {
   XYSeries frecObservadas = new XYSeries("Cantidades de resoluciones por calificación");
   // Calculamos las frecuencias por intervalo
   for (int i = 0; i < valores.length; i++) {
     frecObservadas.add(i, valores[i]);
   }
   XYDataset dataset = new XYSeriesCollection(frecObservadas);
   // Creamos el gráfico
   JFreeChart chart =
       ChartFactory.createXYLineChart(
           titulo,
           ejeHorizontal,
           ejeVertical,
           null,
           PlotOrientation.VERTICAL,
           leyenda,
           false,
           true);
   // Seteamos color de título
   chart.setBackgroundPaint(null);
   chart.setBorderVisible(false);
   chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL);
   chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE);
   if (leyenda) {
     chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR);
     chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO);
   }
   // Seteamos datasets
   XYPlot xyplot = chart.getXYPlot();
   xyplot.setDataset(1, dataset);
   xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
   // Seteamos características de la función lineal
   XYItemRenderer xyLineRendererObs = new XYLineAndShapeRenderer();
   xyLineRendererObs.setSeriesShape(0, new Line2D.Double(0.0, 0.0, 0.0, 0.0));
   xyLineRendererObs.setSeriesStroke(0, new BasicStroke(2.5f));
   xyLineRendererObs.setBasePaint(generarColorAleatorio(Color.orange));
   xyplot.setRenderer(1, xyLineRendererObs);
   // Seteamos color de labels de ejes
   ValueAxis axisX = new SymbolAxis(ejeHorizontal, funciones);
   axisX.setVerticalTickLabels(true);
   xyplot.setDomainAxis(axisX);
   xyplot.getDomainAxis().setTickLabelPaint(Color.BLACK);
   xyplot.getDomainAxis().setLabelPaint(Color.BLACK);
   xyplot.getDomainAxis().setTickLabelFont(LookAndFeelEntropy.FUENTE_REGULAR);
   xyplot.getRangeAxis().setTickLabelPaint(Color.BLACK);
   xyplot.getRangeAxis().setLabelPaint(Color.BLACK);
   xyplot.getRangeAxis().setTickLabelFont(LookAndFeelEntropy.FUENTE_REGULAR);
   // Seteamos porcentaje
   final NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis();
   if (porcentaje) {
     rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
   } else {
     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
   }
   // Seteamos transparencia de fondo
   Plot plot = chart.getPlot();
   plot.setBackgroundPaint(null);
   plot.setBackgroundImageAlpha(0.0f);
   plot.setOutlineVisible(false);
   // Generamos una imagen
   this.lastChart = chart;
   return chart.createBufferedImage(anchoImagen, altoImagen);
 }
  /**
   * @param axis Axis name to wich the new series belongs
   * @param cs Series Coinfiguration
   */
  @Override
  public final void addSeries(String axis, SimpleSeriesConfiguration cs) {
    for (int i = 0; i < axes.size(); i++) {
      if (axes.get(i).getName().equals(axis)) {
        String strColor;
        javafx.scene.paint.Color color;
        int indice = seriesList.size();
        if (cs.getColor() == null) {
          color = getColor(indice);
        } else {
          color = cs.getColor();
        }
        strColor = color.toString();

        XYSeriesCollection dataset = datasetList.get(i);
        Series series =
            new Series(
                cs.getName(),
                "color: "
                    + strColor
                    + ";width: "
                    + String.valueOf(cs.getLineWidth())
                    + ";shape: "
                    + cs.getShapeName()
                    + ";",
                i,
                dataset.getSeriesCount());
        dataset.addSeries(series);

        XYItemRenderer renderer = plot.getRenderer(i);
        renderer.setSeriesPaint(dataset.getSeriesCount() - 1, scene2awtColor(color));

        SeriesShape simb =
            new SeriesShape(
                cs.getShapeName(), javafx.scene.paint.Color.web(strColor.replace("#", "0x")));

        if (cs.getLineWidth() > 0) {
          ((XYLineAndShapeRenderer) renderer)
              .setSeriesLinesVisible(dataset.getSeriesCount() - 1, true);
          renderer.setSeriesStroke(
              dataset.getSeriesCount() - 1, new BasicStroke(cs.getLineWidth()));
        } else {
          ((XYLineAndShapeRenderer) renderer)
              .setSeriesLinesVisible(dataset.getSeriesCount() - 1, false);
        }

        if (cs.getShapeName().equals("null")) {
          renderer.setSeriesShape(dataset.getSeriesCount() - 1, null);
          ((XYLineAndShapeRenderer) renderer)
              .setSeriesShapesVisible(dataset.getSeriesCount() - 1, false);
        } else {
          renderer.setSeriesShape(dataset.getSeriesCount() - 1, simb.getShapeAWT());
          ((XYLineAndShapeRenderer) renderer)
              .setSeriesShapesVisible(dataset.getSeriesCount() - 1, true);
          if (cs.getShapeName().contains("empty")) {
            ((XYLineAndShapeRenderer) renderer)
                .setSeriesShapesFilled(dataset.getSeriesCount() - 1, false);
          } else {
            ((XYLineAndShapeRenderer) renderer)
                .setSeriesShapesFilled(dataset.getSeriesCount() - 1, true);
          }
        }

        if (i == 0) {
          plot.setRenderer(renderer);
        } else {
          plot.setRenderer(i, renderer);
        }

        seriesList.add(series);

        final LegendAxis le = getLegendAxis(axis);
        final Label label = new Label(cs.toString());
        Platform.runLater(
            () -> {
              label.setStyle(
                  "fondo: "
                      + strChartBackgroundColor
                      + ";-fx-background-color: fondo;-fx-text-fill: ladder(fondo, white 49%, black 50%);-fx-padding:5px;-fx-background-radius: 5;-fx-font-size: "
                      + String.valueOf(fontSize)
                      + "px");
            });

        label.setOnMouseClicked(
            (MouseEvent t) -> {
              if (t.getClickCount() == 2) {
                for (int i1 = 0; i1 < seriesList.size(); i1++) {
                  if (seriesList.get(i1).getKey().toString().equals(label.getText())) {
                    editSeries(seriesList.get(i1));
                    break;
                  }
                }
              }
            });
        label.setOnMouseExited(
            (MouseEvent t) -> {
              label.setStyle(
                  label
                      .getStyle()
                      .replace("-fx-background-color: blue", "-fx-background-color: fondo"));
            });
        label.setOnMouseEntered(
            (MouseEvent t) -> {
              label.setStyle(
                  label
                      .getStyle()
                      .replace("-fx-background-color: fondo", "-fx-background-color: blue"));
              for (Node le1 : legendFrame.getChildren()) {
                if (le1 instanceof LegendAxis) {
                  le1.setStyle("-fx-background-color:" + strBackgroundColor);
                  ((LegendAxis) le1).selected = false;
                }
              }
            });
        label.setStyle(
            "fondo: "
                + strChartBackgroundColor
                + ";-fx-text-fill: white;-fx-background-color: fondo;-fx-padding:5px;-fx-background-radius: 5;-fx-font-size: "
                + String.valueOf(fontSize)
                + "px");

        le.getChildren().add(label);
        label.setGraphic(simb.getShapeGraphic());

        break;
      }
    }
  }