3

我在这里有一个简单的甘特图,它由许多任务组成,就像这样:

TaskSeries s1 = new TaskSeries("Planned Tasks");
Task newTask = new Task("Task" +
String.valueOf(taskIndex),
new
SimpleTimePeriod(currentTime,
currentTime +
(int) distributionTime)
);
s1.add(newTask) 
final TaskSeriesCollection collection = new TaskSeriesCollection();
collection.add(s1);

JFreeChart chart = ChartFactory.createGanttChart(
"Distribution ", 
"Task", 
"Time", 
collection, 
true, 
true, 
false 
);

有没有办法在每个栏内写一些东西,代表一个任务?例如,如果任务由两个子任务组成,是否可以用标签标记它们,以便在绘图上看到它们的名称?提前致谢!

4

2 回答 2

8

要在每个项目中添加 aa 标签,setBaseItemLabelGenerator在这种情况下我使用的是 aIntervalCategoryItemLabelGenerator但你可以通过扩展来实现你自己的 CategoryItemLabelGenerator.

使用此代码:

CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setBaseItemLabelGenerator( new IntervalCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelPaint(Color.BLACK);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.INSIDE6, TextAnchor.BOTTOM_CENTER));

然后你会得到这个:b

带有 BaseItemLabelGenerator 的甘特图

自定义标签实现CategoryItemLabelGenerator

renderer.setBaseItemLabelGenerator( new CategoryItemLabelGenerator(){

    @Override
    public String generateRowLabel(CategoryDataset dataset, int row) {
        return "Your Row Text  " + row;
    }

    @Override
    public String generateColumnLabel(CategoryDataset dataset, int column) {
        return "Your Column Text  " + column;
    }

    @Override
    public String generateLabel(CategoryDataset dataset, int row, int column) {
        return "Your Label Text  " + row + "," + column;
    }


});

在此示例generateLabel中,控制条中的标签,可CategoryDataset用于确定要标记的条rowcolumn

在此处输入图像描述

于 2012-07-03T10:34:21.643 回答
1

最终我找到了这个问题的解决方案。把它贴在这里也许它会帮助别人。

问题是在 GanttRenderer 中现有的 drawTasks 函数中没有显示标签的代码。所以我们必须使用新的 drawTasks 函数来扩展 GanttRenderer 类:

   public class MyGanttRenderer extends GanttRenderer {
      private transient Paint completePaint;
      private transient Paint incompletePaint;
      private double startPercent;
      private double endPercent;

      public MyGanttRenderer() {
         super();
         setIncludeBaseInRange(false);
         this.completePaint = Color.green;
         this.incompletePaint = Color.red;
         this.startPercent = 0.35;
         this.endPercent = 0.65;
      }

      protected void drawTasks(Graphics2D g2,
            CategoryItemRendererState state,
            Rectangle2D dataArea,
            CategoryPlot plot,
            CategoryAxis domainAxis,
            ValueAxis rangeAxis,
            GanttCategoryDataset dataset,
            int row,
            int column) {

         int count = dataset.getSubIntervalCount(row, column);
         if (count == 0) {
            drawTask(g2, state, dataArea, plot, domainAxis, rangeAxis, 
                  dataset, row, column);
         }

         for (int subinterval = 0; subinterval < count; subinterval++) {

            RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();

            // value 0
            Number value0 = dataset.getStartValue(row, column, subinterval);
            if (value0 == null) {
               return;
            }
            double translatedValue0 = rangeAxis.valueToJava2D(
                  value0.doubleValue(), dataArea, rangeAxisLocation);

            // value 1
            Number value1 = dataset.getEndValue(row, column, subinterval);
            if (value1 == null) {
               return;
            }
            double translatedValue1 = rangeAxis.valueToJava2D(
                  value1.doubleValue(), dataArea, rangeAxisLocation);

            if (translatedValue1 < translatedValue0) {
               double temp = translatedValue1;
               translatedValue1 = translatedValue0;
               translatedValue0 = temp;
            }

            double rectStart = calculateBarW0(plot, plot.getOrientation(), 
                  dataArea, domainAxis, state, row, column);
            double rectLength = Math.abs(translatedValue1 - translatedValue0);
            double rectBreadth = state.getBarWidth();

            // DRAW THE BARS...
            Rectangle2D bar = null;

            if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
               bar = new Rectangle2D.Double(translatedValue0, rectStart, 
                     rectLength, rectBreadth);
            }
            else if (plot.getOrientation() == PlotOrientation.VERTICAL) {
               bar = new Rectangle2D.Double(rectStart, translatedValue0, 
                     rectBreadth, rectLength);
            }

            Rectangle2D completeBar = null;
            Rectangle2D incompleteBar = null;
            Number percent = dataset.getPercentComplete(row, column, 
                  subinterval);
            double start = getStartPercent();
            double end = getEndPercent();
            if (percent != null) {
               double p = percent.doubleValue();
               if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
                  completeBar = new Rectangle2D.Double(translatedValue0, 
                        rectStart + start * rectBreadth, rectLength * p, 
                        rectBreadth * (end - start));
                  incompleteBar = new Rectangle2D.Double(translatedValue0 
                        + rectLength * p, rectStart + start * rectBreadth, 
                        rectLength * (1 - p), rectBreadth * (end - start));
               }
               else if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                  completeBar = new Rectangle2D.Double(rectStart + start 
                        * rectBreadth, translatedValue0 + rectLength 
                        * (1 - p), rectBreadth * (end - start), 
                        rectLength * p);
                  incompleteBar = new Rectangle2D.Double(rectStart + start 
                        * rectBreadth, translatedValue0, rectBreadth 
                        * (end - start), rectLength * (1 - p));
               }

            }

            Paint seriesPaint = getItemPaint(row, column);
            g2.setPaint(seriesPaint);
            g2.fill(bar);

            if (completeBar != null) {
               g2.setPaint(getCompletePaint());
               g2.fill(completeBar);
            }
            if (incompleteBar != null) {
               g2.setPaint(getIncompletePaint());
               g2.fill(incompleteBar);
            }
            if (isDrawBarOutline() 
                  && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
               g2.setStroke(getItemStroke(row, column));
               g2.setPaint(getItemOutlinePaint(row, column));
               g2.draw(bar);
            }

            CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
            if (generator != null && isItemLabelVisible(row, column)) {
               drawItemLabel(g2, dataset, row, column, plot, generator, bar, false);
            }

            // collect entity and tool tip information...
            if (state.getInfo() != null) {
               EntityCollection entities = state.getEntityCollection();
               if (entities != null) {
                  String tip = null;
                  if (getToolTipGenerator(row, column) != null) {
                     tip = getToolTipGenerator(row, column).generateToolTip(
                           dataset, row, column);
                  }
                  String url = null;
                  if (getItemURLGenerator(row, column) != null) {
                     url = getItemURLGenerator(row, column).generateURL(
                           dataset, row, column);
                  }
                  CategoryItemEntity entity = new CategoryItemEntity(
                        bar, tip, url, dataset, dataset.getRowKey(row), 
                        dataset.getColumnKey(column));
                  entities.add(entity);
               }
            }
         }
      }
   }

要在每个栏(包括子任务)上显示标签,只需执行以下操作:

 CategoryPlot plot = (CategoryPlot) chart.getPlot();

 MyGanttRenderer renderer = new MyGanttRenderer();
 plot.setRenderer(renderer);

 renderer.setBaseItemLabelGenerator(new CategoryItemLabelGenerator() {

      public String generateLabel(CategoryDataset dataSet, int series, int categories) {
       /* your code to get the label */
       return label;
      }

      public String generateColumnLabel(CategoryDataset dataset, int categories) {
          return dataset.getColumnKey(categories).toString();
      }

      public String generateRowLabel(CategoryDataset dataset, int series) {
          return dataset.getRowKey(series).toString();
      }
 });

 renderer.setBaseItemLabelsVisible(true);
 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_LEFT));

就是这样。希望它对某些人有用。

于 2012-07-05T07:59:47.073 回答