如何使用 xybubblerenderer 实现类别图

发布于 2024-12-01 16:11:54 字数 174 浏览 3 评论 0原文

如何在 JFreechart 中使用 xybubblerenderer 实现类别图? 我想要一个类别图,其中显示每个类别的几个气泡。 使用气泡的 x 值将这些气泡放置在图的 y 轴上,气泡的 y 值确定其面积。 所以每个类别中都存在泡沫。它们在图上的 x 位置可以是随机的。一个值控制它们在图上的 y 位置,另一个值控制它们的面积。

how to implement a category plot with xybubblerenderer in JFreechart?
I want to have a category plot which shows several bubbles for each category.
These bubbles are placed on the y axis of the plot using the x value of the bubble and y value of the bubble determines its area.
So there are bubbles within each category. Their x position on the plot can be random. One value controls their y position on the plot and another value controls their area.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2024-12-08 16:11:54

可以使用 SymbolAxis 和 XYBubbleRenderer 来完成。必须确保气泡的 x 值在 [0,n] 范围内,并且 [0,n] 已在符号轴中分配了正确的标签。

It can be done using the SymbolAxis and XYBubbleRenderer. It has to be ensured that the x values of the bubbles are in the range [0,n] and [0,n] have been assigned proper labels in symbol axis.

┼── 2024-12-08 16:11:54

否则你必须编写自己的 CategoryBubbleRenderer,我已经这样做了并且它有效,这里是我的代码

/**
 * Title: CategoryBubbleRenderer
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 *
 * Based on XYBubbleRenderer and BarRenderer
 *
 */



import org.jfree.chart.LegendItem;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.util.PublicCloneable;

import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

  /**
   * A renderer that draws a circle at each data point with a diameter that is
   * determined by the z-value in the dataset.
   */
  public class CategoryBubbleRenderer extends AbstractCategoryItemRenderer
          implements Cloneable, PublicCloneable/*, Serializable*/ {

    /** For serialization. */
    //public static final long serialVersionUID = -5221991598674249125L;



    public CategoryBubbleRenderer() {
      super();
      setBaseLegendShape(new Ellipse2D.Double(-4.0,-4.0,8.0,8.0));
    }



    /*public void drawItem(Graphics2D g2, XYItemRendererState state,
                         Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
                         ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
                         int series, int item, CrosshairState crosshairState, int pass) {*/
    public void drawItem(Graphics2D g2,
                         CategoryItemRendererState state,
                         Rectangle2D dataArea,
                         CategoryPlot plot,
                         CategoryAxis domainAxis,
                         ValueAxis rangeAxis,
                         CategoryDataset dataset,
                         int row,
                         int column,
                         int pass) {



      // nothing is drawn if the row index is not included in the list with
      // the indices of the visible rows...
      int visibleRow = state.getVisibleSeriesIndex(row);
      if (visibleRow < 0) {
        return;
      }
      // nothing is drawn for null values...
      Number dataValue = dataset.getValue(row, column);
      if (dataValue == null) {
        return;
      }
      int series = row;
      int item = column;

      PlotOrientation orientation = plot.getOrientation();

      // get the data point...
      //double x = dataset.getXValue(series, item);
      //double y = dataset.getYValue(series, item);
      double y = dataValue.doubleValue();
      double z = Double.NaN;


      double d1 = rangeAxis.getUpperBound();
      double d2 = rangeAxis.getLowerBound();
      z = (d2-d1)/10;//just an attempt to calculate  a good default value

      if (!Double.isNaN(z)) {
        RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
        RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
        //double transX = domainAxis.valueToJava2D(x, dataArea, domainAxisLocation);
        //double transX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());
        double transX = domainAxis.getCategoryMiddle(column, getColumnCount(),
                dataArea, plot.getDomainAxisEdge()) - state.getBarWidth()
                / 2.0;
        double transY = rangeAxis.valueToJava2D(y, dataArea, rangeAxisLocation);


        double zero2 = rangeAxis.valueToJava2D(0.0, dataArea,
                rangeAxisLocation);
        double transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea,
                rangeAxisLocation);
        double transDomain = transRange;


        transDomain = Math.abs(transDomain);
        transRange = Math.abs(transRange);
        Ellipse2D circle = null;
        if (orientation == PlotOrientation.VERTICAL) {
          circle = new Ellipse2D.Double(transX - transDomain / 2.0,
                  transY - transRange / 2.0, transDomain, transRange);
        }
        else if (orientation == PlotOrientation.HORIZONTAL) {
          circle = new Ellipse2D.Double(transY - transRange / 2.0,
                  transX - transDomain / 2.0, transRange, transDomain);
        }
        g2.setPaint(getItemPaint(series, item));
        g2.fill(circle);
        g2.setStroke(getItemOutlineStroke(series, item));
        g2.setPaint(getItemOutlinePaint(series, item));
        g2.draw(circle);

        if (isItemLabelVisible(series, item)) {
          if (orientation == PlotOrientation.VERTICAL) {
            drawItemLabel(g2, orientation, dataset, series, item,
                    transX, transY, false);
          }
          else if (orientation == PlotOrientation.HORIZONTAL) {
            drawItemLabel(g2, orientation, dataset, series, item,
                    transY, transX, false);
          }
        }

        // add an entity if this info is being collected

        /*EntityCollection entities = null;
        if (info != null) {
          entities = info.getOwner().getEntityCollection();
          if (entities != null && circle.intersects(dataArea)) {
            addEntity(entities, circle, dataset, series, item,
                    circle.getCenterX(), circle.getCenterY());
          }
        }

        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, x, y, domainAxisIndex,
                rangeAxisIndex, transX, transY, orientation);
        */
      }

    }


    /**
     * Returns a legend item for the specified series.  The default method
     * is overridden so that the legend displays circles for all series.
     *
     * @param datasetIndex  the dataset index (zero-based).
     * @param series  the series index (zero-based).
     *
     * @return A legend item for the series.
     */
    public LegendItem getLegendItem(int datasetIndex, int series) {
      LegendItem result = null;
      CategoryPlot plot = getPlot();
      if (plot == null) {
        return null;
      }

      CategoryDataset dataset = plot.getDataset(datasetIndex);
      if (dataset != null) {
        if (getItemVisible(series, 0)) {
          String label = getLegendItemLabelGenerator().generateLabel(
                  dataset, series);
          String description = label;
          String toolTipText = null;
          if (getLegendItemToolTipGenerator() != null) {
            toolTipText = getLegendItemToolTipGenerator().generateLabel(
                    dataset, series);
          }
          String urlText = null;
          if (getLegendItemURLGenerator() != null) {
            urlText = getLegendItemURLGenerator().generateLabel(
                    dataset, series);
          }
          Shape shape = lookupLegendShape(series);
          Paint paint = lookupSeriesPaint(series);
          Paint outlinePaint = lookupSeriesOutlinePaint(series);
          Stroke outlineStroke = lookupSeriesOutlineStroke(series);
          result = new LegendItem(label, description, toolTipText,
                  urlText, shape, paint, outlineStroke, outlinePaint);
          result.setLabelFont(lookupLegendTextFont(series));
          Paint labelPaint = lookupLegendTextPaint(series);
          if (labelPaint != null) {
            result.setLabelPaint(labelPaint);
          }
          result.setDataset(dataset);
          result.setDatasetIndex(datasetIndex);
          //result.setSeriesKey(dataset.getSeriesKey(series));
          result.setSeriesKey(dataset.getRowKey(series));
          result.setSeriesIndex(series);
        }
      }
      return result;
    }


    public boolean equals(Object obj) {
      if (obj == this) {
        return true;
      }
      if (!(obj instanceof CategoryBubbleRenderer)) {
        return false;
      }
      CategoryBubbleRenderer that = (CategoryBubbleRenderer) obj;
      return super.equals(obj);
    }


    public Object clone() throws CloneNotSupportedException {
      return super.clone();
    }

  }

Otherwise you have to write your own CategoryBubbleRenderer, I have done so and it works, here my code

/**
 * Title: CategoryBubbleRenderer
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 *
 * Based on XYBubbleRenderer and BarRenderer
 *
 */



import org.jfree.chart.LegendItem;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.util.PublicCloneable;

import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

  /**
   * A renderer that draws a circle at each data point with a diameter that is
   * determined by the z-value in the dataset.
   */
  public class CategoryBubbleRenderer extends AbstractCategoryItemRenderer
          implements Cloneable, PublicCloneable/*, Serializable*/ {

    /** For serialization. */
    //public static final long serialVersionUID = -5221991598674249125L;



    public CategoryBubbleRenderer() {
      super();
      setBaseLegendShape(new Ellipse2D.Double(-4.0,-4.0,8.0,8.0));
    }



    /*public void drawItem(Graphics2D g2, XYItemRendererState state,
                         Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
                         ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
                         int series, int item, CrosshairState crosshairState, int pass) {*/
    public void drawItem(Graphics2D g2,
                         CategoryItemRendererState state,
                         Rectangle2D dataArea,
                         CategoryPlot plot,
                         CategoryAxis domainAxis,
                         ValueAxis rangeAxis,
                         CategoryDataset dataset,
                         int row,
                         int column,
                         int pass) {



      // nothing is drawn if the row index is not included in the list with
      // the indices of the visible rows...
      int visibleRow = state.getVisibleSeriesIndex(row);
      if (visibleRow < 0) {
        return;
      }
      // nothing is drawn for null values...
      Number dataValue = dataset.getValue(row, column);
      if (dataValue == null) {
        return;
      }
      int series = row;
      int item = column;

      PlotOrientation orientation = plot.getOrientation();

      // get the data point...
      //double x = dataset.getXValue(series, item);
      //double y = dataset.getYValue(series, item);
      double y = dataValue.doubleValue();
      double z = Double.NaN;


      double d1 = rangeAxis.getUpperBound();
      double d2 = rangeAxis.getLowerBound();
      z = (d2-d1)/10;//just an attempt to calculate  a good default value

      if (!Double.isNaN(z)) {
        RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
        RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
        //double transX = domainAxis.valueToJava2D(x, dataArea, domainAxisLocation);
        //double transX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());
        double transX = domainAxis.getCategoryMiddle(column, getColumnCount(),
                dataArea, plot.getDomainAxisEdge()) - state.getBarWidth()
                / 2.0;
        double transY = rangeAxis.valueToJava2D(y, dataArea, rangeAxisLocation);


        double zero2 = rangeAxis.valueToJava2D(0.0, dataArea,
                rangeAxisLocation);
        double transRange = zero2 - rangeAxis.valueToJava2D(z, dataArea,
                rangeAxisLocation);
        double transDomain = transRange;


        transDomain = Math.abs(transDomain);
        transRange = Math.abs(transRange);
        Ellipse2D circle = null;
        if (orientation == PlotOrientation.VERTICAL) {
          circle = new Ellipse2D.Double(transX - transDomain / 2.0,
                  transY - transRange / 2.0, transDomain, transRange);
        }
        else if (orientation == PlotOrientation.HORIZONTAL) {
          circle = new Ellipse2D.Double(transY - transRange / 2.0,
                  transX - transDomain / 2.0, transRange, transDomain);
        }
        g2.setPaint(getItemPaint(series, item));
        g2.fill(circle);
        g2.setStroke(getItemOutlineStroke(series, item));
        g2.setPaint(getItemOutlinePaint(series, item));
        g2.draw(circle);

        if (isItemLabelVisible(series, item)) {
          if (orientation == PlotOrientation.VERTICAL) {
            drawItemLabel(g2, orientation, dataset, series, item,
                    transX, transY, false);
          }
          else if (orientation == PlotOrientation.HORIZONTAL) {
            drawItemLabel(g2, orientation, dataset, series, item,
                    transY, transX, false);
          }
        }

        // add an entity if this info is being collected

        /*EntityCollection entities = null;
        if (info != null) {
          entities = info.getOwner().getEntityCollection();
          if (entities != null && circle.intersects(dataArea)) {
            addEntity(entities, circle, dataset, series, item,
                    circle.getCenterX(), circle.getCenterY());
          }
        }

        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, x, y, domainAxisIndex,
                rangeAxisIndex, transX, transY, orientation);
        */
      }

    }


    /**
     * Returns a legend item for the specified series.  The default method
     * is overridden so that the legend displays circles for all series.
     *
     * @param datasetIndex  the dataset index (zero-based).
     * @param series  the series index (zero-based).
     *
     * @return A legend item for the series.
     */
    public LegendItem getLegendItem(int datasetIndex, int series) {
      LegendItem result = null;
      CategoryPlot plot = getPlot();
      if (plot == null) {
        return null;
      }

      CategoryDataset dataset = plot.getDataset(datasetIndex);
      if (dataset != null) {
        if (getItemVisible(series, 0)) {
          String label = getLegendItemLabelGenerator().generateLabel(
                  dataset, series);
          String description = label;
          String toolTipText = null;
          if (getLegendItemToolTipGenerator() != null) {
            toolTipText = getLegendItemToolTipGenerator().generateLabel(
                    dataset, series);
          }
          String urlText = null;
          if (getLegendItemURLGenerator() != null) {
            urlText = getLegendItemURLGenerator().generateLabel(
                    dataset, series);
          }
          Shape shape = lookupLegendShape(series);
          Paint paint = lookupSeriesPaint(series);
          Paint outlinePaint = lookupSeriesOutlinePaint(series);
          Stroke outlineStroke = lookupSeriesOutlineStroke(series);
          result = new LegendItem(label, description, toolTipText,
                  urlText, shape, paint, outlineStroke, outlinePaint);
          result.setLabelFont(lookupLegendTextFont(series));
          Paint labelPaint = lookupLegendTextPaint(series);
          if (labelPaint != null) {
            result.setLabelPaint(labelPaint);
          }
          result.setDataset(dataset);
          result.setDatasetIndex(datasetIndex);
          //result.setSeriesKey(dataset.getSeriesKey(series));
          result.setSeriesKey(dataset.getRowKey(series));
          result.setSeriesIndex(series);
        }
      }
      return result;
    }


    public boolean equals(Object obj) {
      if (obj == this) {
        return true;
      }
      if (!(obj instanceof CategoryBubbleRenderer)) {
        return false;
      }
      CategoryBubbleRenderer that = (CategoryBubbleRenderer) obj;
      return super.equals(obj);
    }


    public Object clone() throws CloneNotSupportedException {
      return super.clone();
    }

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文