Exemplo n.º 1
0
  public ChartPanel getChartPanel() {
    JFreeChart c = null;
    CombinedDomainCategoryPlot combinedP = null;

    for (final String valueProperty : valueP) {
      final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
      for (int i = 0; i < set.getNumResults(); i++)
        dataset.addValue(
            (Double) set.getResultValue(i, valueProperty),
            set.getResultValue(i, seriesP).toString(),
            set.getResultValue(i, categoryP).toString());
      JFreeChart chart =
          ChartFactory.createLineChart(
              title,
              xAxisLabel,
              ((yAxisLabel == null) ? set.getNiceProperty(valueProperty) : yAxisLabel)
                  + yAxisLabelSuffix,
              dataset,
              PlotOrientation.VERTICAL,
              showLegend,
              tooltips,
              false /*urls*/);

      chart.getPlot().setBackgroundPaint(Color.WHITE);
      ((CategoryPlot) chart.getPlot()).setRangeGridlinePaint(Color.GRAY);
      //			((CategoryPlot) chart.getPlot()).setDomainGridlinesVisible(true);
      //			((CategoryPlot) chart.getPlot()).setDomainGridlinePaint(Color.GRAY);
      chart.setBackgroundPaint(new Color(0, 0, 0, 0));
      CategoryPlot plot = (CategoryPlot) chart.getPlot();

      Color cols[];
      Shape shapes[] = null;
      if (dataset.getRowCount() == 2) {
        cols =
            new Color[] {
              ColorUtil.bright(FreeChartUtil.COLORS[0]), ColorUtil.dark(FreeChartUtil.COLORS[1])
            };
        shapes =
            new Shape[] {ShapeUtilities.createDiamond(4.5f), new Ellipse2D.Float(-3f, -3f, 6f, 6f)};
      } else if (dataset.getRowCount() == 3) {
        Color orange = new Color(244, 125, 43);
        // Color orange = new Color(255, 145, 63);
        Color red = new Color(238, 46, 47);
        Color blue = new Color(24, 90, 169);
        // Color blue = new Color(4, 70, 149);
        cols = new Color[] {red, orange, blue};

        //
        //				Color bright = Color.MAGENTA;
        //				Color medium = Color.RED;
        //				Color dark = new Color(0, 0, 75);
        //				cols = new Color[] { bright, medium, dark };
      } else cols = FreeChartUtil.COLORS;

      //			for (int i = 0; i < cols.length; i++)
      //			{
      //				cols[i] = ColorUtil.grayscale(cols[i]);
      //				int color = cols[i].getRGB();
      //				int red = (color >>> 16) & 0xFF;
      //				int green = (color >>> 8) & 0xFF;
      //				int blue = (color >>> 0) & 0xFF;
      //				float luminance = (red * 0.2126f + green * 0.7152f + blue * 0.0722f) / 255;
      //				System.out.println(luminance);
      //			}

      //			for (int i = 0; i < dataset.getColumnCount(); i++)
      //			{
      //				//							int seriesIdx = -1;
      //				//							for (int j = 0; j < dataset.getRowCount(); j++)
      //				//							{
      //				//								String seriesValue = dataset.getRowKey(j).toString();
      //				//								String categoryValue = dataset.getColumnKey(i).toString();
      //				//								if ((drawShape.containsKey(valueProperty) &&
      // seriesValue.equals(drawShape.get(valueProperty).get(
      //				//										categoryValue))))
      //				//								{
      //				//									seriesIdx = j;
      //				//									break;
      //				//								}
      //				//							}
      //				//
      //				//							if (seriesIdx != -1)
      //				//							{
      //				CategoryLineAnnotation anno = new CategoryLineAnnotation(dataset.getColumnKey(i),
      // dataset
      //						.getValue(0, i).doubleValue(), dataset.getColumnKey(i), dataset.getValue(1,
      // i).doubleValue(),
      //						ColorUtil.transparent(Color.GRAY, 200), new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
      //								BasicStroke.JOIN_ROUND, 5.0f, new float[] { 2.0f, 2.0f }, 5.0f));
      //				int thick = 10;
      //				Color back = new Color(255, 255, 255);
      //
      //				//				CategoryLineAnnotation anno = new CategoryLineAnnotation(dataset.getColumnKey(i),
      // dataset
      //				//						.getValue(0, i).doubleValue(), dataset.getColumnKey(i), dataset.getValue(1,
      // i).doubleValue(),
      //				//						ColorUtil.transparent(back, 200), new BasicStroke(thick, BasicStroke.CAP_ROUND,
      //				//								BasicStroke.JOIN_ROUND));
      //				plot.addAnnotation(anno);
      //
      //				if (dataset.getValue(0, i).doubleValue() > dataset.getValue(1, i).doubleValue())
      //				{
      //					addAnnotation(plot, dataset, i, 1, cols, back, thick, true, valueProperty);
      //					addAnnotation(plot, dataset, i, 0, cols, back, thick, false, valueProperty);
      //				}
      //				else
      //				{
      //					addAnnotation(plot, dataset, i, 0, cols, back, thick, false, valueProperty);
      //					addAnnotation(plot, dataset, i, 1, cols, back, thick, true, valueProperty);
      //
      //				}
      //			}

      LineAndShapeRenderer renderer =
          new LineAndShapeRenderer() {
            @Override
            public boolean getItemShapeVisible(int series, int item) {
              //					//					return dataset.getValue(series, item).doubleValue() > dataset.getValue(1
              // - series, item)
              //					//							.doubleValue();
              //
              //					String seriesValue = dataset.getRowKey(series).toString();
              //					String categoryValue = dataset.getColumnKey(item).toString();
              //					return (drawShape.containsKey(valueProperty) &&
              // seriesValue.equals(drawShape.get(valueProperty)
              //							.get(categoryValue)));
              //					//					return new Random().nextBoolean();

              return false;
            }

            @Override
            public boolean getItemShapeFilled(int series, int item) {
              String seriesValue = dataset.getRowKey(series).toString();
              String categoryValue = dataset.getColumnKey(item).toString();
              return (drawShape.containsKey(valueProperty)
                  && seriesValue.equals(drawShape.get(valueProperty).get(categoryValue)));
            }

            @Override
            public Boolean getSeriesLinesVisible(int series) {
              return true;
            }
          };

      //			Polygon p1 = new Polygon();
      //			p1.addPoint(0, -9);
      //			p1.addPoint(-3, -5);
      //			p1.addPoint(3, -5);
      //			renderer.setBaseShape(p1);

      plot.setRenderer(renderer);
      //			LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
      //			renderer.set

      if (markers != null && markers.containsKey(valueProperty))
        for (String category : markers.keySet2(valueProperty)) {
          CategoryMarker marker = new CategoryMarker(category);
          marker.setOutlinePaint(null);
          marker.setPaint(new Color(150, 150, 150));
          marker.setDrawAsLine(true);
          marker.setLabel(" " + markers.get(valueProperty, category));

          //				marker.setLabelFont(plot.getDomainAxis().getLabelFont());
          //				marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
          //				marker.setLabelOffset(new RectangleInsets(10.0, 0.0, 0.0, 0.0));

          marker.setLabelAnchor(RectangleAnchor.BOTTOM);
          marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
          marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT);

          plot.addDomainMarker(marker, Layer.BACKGROUND);
        }
      //
      //			marker = new CategoryMarker("elephant");
      //			marker.setOutlinePaint(null);
      //			marker.setPaint(new Color(150, 150, 150));
      //			plot.addDomainMarker(marker, Layer.BACKGROUND);

      //			CategoryPointerAnnotation p = new CategoryPointerAnnotation("", "mouse", 0.5,
      // Math.toRadians(270.0));
      //			p.setPaint(Color.RED);
      //			plot.addAnnotation(p);

      //			for (int i = 0; i < cols.length; i++)
      //				cols[i] = ColorUtil.grayscale(cols[i]);

      for (int i = 0; i < dataset.getRowCount(); i++) {
        //				renderer.setSeriesShape(i, p1);

        //				double ratio = i / (double) (dataset.getRowCount() - 1);
        //				System.out.println(i + " " + ratio);
        //				//			renderer.setSeriesPaint(i, ColorGradient.get2ColorGradient(ratio, Color.BLACK,
        // Color.LIGHT_GRAY));
        //				//				renderer.setSeriesPaint(i, new ColorGradient(new Color(200, 0, 0),
        // Color.LIGHT_GRAY, new Color(150,
        //				//						150, 255)).getColor(ratio));
        //				renderer.setSeriesPaint(i, new ColorGradient(new Color(0, 0, 200), Color.LIGHT_GRAY,
        // new Color(150,
        //						150, 255)).getColor(ratio));
        renderer.setSeriesPaint(i, cols[i]);

        renderer.setSeriesOutlinePaint(i, Color.BLACK);
        renderer.setSeriesFillPaint(i, Color.BLACK);

        if (shapes != null) renderer.setSeriesShape(i, shapes[i]);

        float thick = 1.0f;

        //				if ((dataset.getRowCount() == 2 || dataset.getRowCount() == 3) && i == 1)
        //					renderer.setSeriesStroke(i, new BasicStroke(thick, BasicStroke.CAP_ROUND,
        // BasicStroke.JOIN_ROUND,
        //							1.0f, new float[] { 6f * thick, 1.5f * thick }, 3f * thick));
        //				else if ((dataset.getRowCount() == 2 || dataset.getRowCount() == 3) && i == 2)
        //					renderer.setSeriesStroke(i, new BasicStroke(thick, BasicStroke.CAP_ROUND,
        // BasicStroke.JOIN_ROUND,
        //							1.0f, new float[] { 1.5f * thick, 1.5f * thick }, 3f * thick));
        //				else
        renderer.setSeriesStroke(
            i, new BasicStroke(thick, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

        // , 1.0f,new float[] { dash, 2 * thick }, dash / 2));

      }

      if (yAxisRange != null)
        ((NumberAxis) plot.getRangeAxis()).setRange(yAxisRange[0], yAxisRange[1]);
      if (yAxisRangePerValue.containsKey(valueProperty))
        ((NumberAxis) plot.getRangeAxis())
            .setRange(
                yAxisRangePerValue.get(valueProperty)[0], yAxisRangePerValue.get(valueProperty)[1]);
      if (yAxisTickUnitsPerValue.containsKey(valueProperty))
        ((NumberAxis) plot.getRangeAxis())
            .setTickUnit(new NumberTickUnit(yAxisTickUnitsPerValue.get(valueProperty)));

      // ((NumberAxis) plot.getRangeAxis()).setAutoRangeIncludesZero(true);

      CategoryAxis axis = plot.getDomainAxis();

      //			axis.setTickLabelsVisible(true);

      if (c == null) {
        c = chart;
      } else if (combinedP == null) {
        combinedP = new CombinedDomainCategoryPlot(new CategoryAxis(xAxisLabel));
        combinedP.setOrientation(PlotOrientation.VERTICAL);
        combinedP.add(c.getCategoryPlot());
        combinedP.add(chart.getCategoryPlot());
        c = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, combinedP, false);
        c.setBackgroundPaint(new Color(0, 0, 0, 0));
        c.addLegend(chart.getLegend());
        axis = plot.getDomainAxis();
      } else {
        combinedP.add(chart.getCategoryPlot());
      }

      if (rotateXLabels == XLabelsRotation.diagonal)
        axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
      if (rotateXLabels == XLabelsRotation.vertical) {
        axis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
        axis.setMaximumCategoryLabelWidthRatio(1.0f);
      }
      axis.setLowerMargin(0);
      axis.setUpperMargin(0);
      if (!showDomainAxis) axis.setVisible(false);
    }
    ChartPanel cp = new ChartPanel(c);
    return cp;
  }
  /** Confirm that cloning works. */
  public void testCloning() {
    LineAndShapeRenderer r1 = new LineAndShapeRenderer();
    Rectangle2D shape = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
    Rectangle2D baseShape = new Rectangle2D.Double(11.0, 12.0, 13.0, 14.0);
    r1.setShape(shape);
    r1.setBaseShape(baseShape);
    r1.setBaseLegendShape(new Rectangle(4, 3, 2, 1));
    r1.setBaseLegendTextFont(new Font("Dialog", Font.PLAIN, 3));
    r1.setBaseLegendTextPaint(new Color(1, 2, 3));

    LineAndShapeRenderer r2 = null;
    try {
      r2 = (LineAndShapeRenderer) r1.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }
    assertTrue(r1 != r2);
    assertTrue(r1.getClass() == r2.getClass());
    assertTrue(r1.equals(r2));

    r1.setSeriesVisible(0, Boolean.FALSE);
    assertFalse(r1.equals(r2));
    r2.setSeriesVisible(0, Boolean.FALSE);
    assertTrue(r1.equals(r2));

    r1.setSeriesVisibleInLegend(0, Boolean.FALSE);
    assertFalse(r1.equals(r2));
    r2.setSeriesVisibleInLegend(0, Boolean.FALSE);
    assertTrue(r1.equals(r2));

    r1.setSeriesPaint(0, Color.black);
    assertFalse(r1.equals(r2));
    r2.setSeriesPaint(0, Color.black);
    assertTrue(r1.equals(r2));

    r1.setSeriesFillPaint(0, Color.yellow);
    assertFalse(r1.equals(r2));
    r2.setSeriesFillPaint(0, Color.yellow);
    assertTrue(r1.equals(r2));

    r1.setSeriesOutlinePaint(0, Color.yellow);
    assertFalse(r1.equals(r2));
    r2.setSeriesOutlinePaint(0, Color.yellow);
    assertTrue(r1.equals(r2));

    r1.setSeriesStroke(0, new BasicStroke(2.2f));
    assertFalse(r1.equals(r2));
    r2.setSeriesStroke(0, new BasicStroke(2.2f));
    assertTrue(r1.equals(r2));

    r1.setSeriesOutlineStroke(0, new BasicStroke(2.2f));
    assertFalse(r1.equals(r2));
    r2.setSeriesOutlineStroke(0, new BasicStroke(2.2f));
    assertTrue(r1.equals(r2));

    shape.setRect(4.0, 3.0, 2.0, 1.0);
    assertFalse(r1.equals(r2));
    r2.setShape(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
    assertTrue(r1.equals(r2));

    baseShape.setRect(4.0, 3.0, 2.0, 1.0);
    assertFalse(r1.equals(r2));
    r2.setBaseShape(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
    assertTrue(r1.equals(r2));

    r1.setSeriesShape(0, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
    assertFalse(r1.equals(r2));
    r2.setSeriesShape(0, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
    assertTrue(r1.equals(r2));

    r1.setSeriesItemLabelsVisible(0, Boolean.TRUE);
    assertFalse(r1.equals(r2));
    r2.setSeriesItemLabelsVisible(0, Boolean.TRUE);
    assertTrue(r1.equals(r2));

    r1.setSeriesItemLabelPaint(0, Color.red);
    assertFalse(r1.equals(r2));
    r2.setSeriesItemLabelPaint(0, Color.red);
    assertTrue(r1.equals(r2));

    r1.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition());
    assertFalse(r1.equals(r2));
    r2.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition());
    assertTrue(r1.equals(r2));

    r1.setSeriesNegativeItemLabelPosition(0, new ItemLabelPosition());
    assertFalse(r1.equals(r2));
    r2.setSeriesNegativeItemLabelPosition(0, new ItemLabelPosition());
    assertTrue(r1.equals(r2));

    r1.setSeriesCreateEntities(0, Boolean.FALSE);
    assertFalse(r1.equals(r2));
    r2.setSeriesCreateEntities(0, Boolean.FALSE);
    assertTrue(r1.equals(r2));

    r1.setLegendShape(0, new Rectangle(9, 7, 3, 4));
    assertFalse(r1.equals(r2));
    r2.setLegendShape(0, new Rectangle(9, 7, 3, 4));
    assertTrue(r1.equals(r2));

    r1.setBaseLegendShape(new Rectangle(3, 4, 1, 5));
    assertFalse(r1.equals(r2));
    r2.setBaseLegendShape(new Rectangle(3, 4, 1, 5));
    assertTrue(r1.equals(r2));

    r1.setLegendTextFont(1, new Font("Dialog", Font.PLAIN, 33));
    assertFalse(r1.equals(r2));
    r2.setLegendTextFont(1, new Font("Dialog", Font.PLAIN, 33));
    assertTrue(r1.equals(r2));

    r1.setBaseLegendTextFont(new Font("Dialog", Font.PLAIN, 11));
    assertFalse(r1.equals(r2));
    r2.setBaseLegendTextFont(new Font("Dialog", Font.PLAIN, 11));
    assertTrue(r1.equals(r2));

    r1.setLegendTextPaint(3, Color.red);
    assertFalse(r1.equals(r2));
    r2.setLegendTextPaint(3, Color.red);
    assertTrue(r1.equals(r2));

    r1.setBaseLegendTextPaint(Color.green);
    assertFalse(r1.equals(r2));
    r2.setBaseLegendTextPaint(Color.green);
    assertTrue(r1.equals(r2));
  }