ガント チャートを使用してタスク スケジューラ チャートを生成しようとしていますが、問題が発生しました。X 軸を日付ではなく数値として表示する方法を教えてください。Task(String ,data,data) の代わりに関数 TaskWhatIWant(String , int ,int) を記述して、Gantt のようなタスク スケジューラを生成する方法はありますか
または、このようなタスク スケジューリング チャートを生成するより良い方法はありますか?
package plugtest;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class GanttDemo extends ApplicationFrame
{
public GanttDemo(String s)
{
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}
private static JFreeChart createChart(IntervalCategoryDataset intervalcategorydataset)
{
JFreeChart jfreechart = ChartFactory.createGanttChart("Gantt Chart Demo", "Task", "Date", intervalcategorydataset, true, true, false);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setRangePannable(true);
categoryplot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10F);
CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
categoryitemrenderer.setSeriesPaint(0, Color.blue);
return jfreechart;
}
private static IntervalCategoryDataset createDataset()
{
TaskSeries taskseries = new TaskSeries("Scheduled");
/* which i want it to be
* **Task' task'=new Task'(String taskName, int start,int end);**
*/
Task task = new Task("task1", date(1, 3, 2001), date(5, 3, 2001));
task.setPercentComplete(1.0D);
taskseries.add(task);
Task task1 = new Task("task2", date(9, 3, 2001), date(11, 5, 2001));
taskseries.add(task1);
Task task2 = new Task("task3", date(10, 3, 2001), date(5, 4, 2001));
taskseries.add(task2);
TaskSeriesCollection taskseriescollection = new TaskSeriesCollection();
taskseriescollection.add(taskseries);
return taskseriescollection;
}
private static Date date(int i, int j, int k)
{
Calendar calendar = Calendar.getInstance();
calendar.set(k, j, i);
Date date1 = calendar.getTime();
return date1;
}
public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart(createDataset());
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setMouseWheelEnabled(true);
return chartpanel;
}
public static void main(String args[])
{
GanttDemo ganttdemo = new GanttDemo("gantt demo");
ganttdemo.pack();
RefineryUtilities.centerFrameOnScreen(ganttdemo);
ganttdemo.setVisible(true);
}
}
提案してくれた@Bazに感謝します。
これが sulotion とデモです
変更された関数 createGanttChart:
public static JFreeChart createGanttChart(String title,
String categoryAxisLabel,
String dateAxisLabel,
IntervalCategoryDataset dataset,
boolean legend,
boolean tooltips,
boolean urls) {
CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
ValueAxis dateAxis = new NumberAxis(dateAxisLabel);//important,change axis from data to value
CategoryItemRenderer renderer = new GanttRenderer();
if (tooltips) {
renderer.setBaseToolTipGenerator(
(CategoryToolTipGenerator) new IntervalCategoryToolTipGenerator(
"{1}Schedule Time: {3}ms - {4}ms", NumberFormat .getNumberInstance()));
}
if (urls) {
renderer.setBaseItemURLGenerator(
new StandardCategoryURLGenerator());
}
CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, dateAxis,
renderer);
plot.setOrientation(PlotOrientation.HORIZONTAL);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
return chart;
}
}
変更されたクラス タスク
package plugtest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.data.time.TimePeriod;
import org.jfree.util.ObjectUtilities;
import org.jfree.util.PublicCloneable;
public class Task implements Cloneable, PublicCloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = 1094303785346988894L;
private String description;
private double start; //start Time
private double end; //end Time
public double getStart() {
return start;
}
public void setStart(double start) {
this.start = start;
}
public double getEnd() {
return end;
}
public void setEnd(double end) {
this.end = end;
}
/** The percent complete (<code>null</code> is permitted). */
private Double percentComplete;
/** Storage for the sub-tasks (if any). */
private List subtasks;
public Task(String description, double start , double end) {
if (description == null) {
throw new IllegalArgumentException("Null 'description' argument.");
}
this.description = description;
this.start=start;
this.end=end;
this.percentComplete = null;
this.subtasks = new java.util.ArrayList();
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
if (description == null) {
throw new IllegalArgumentException("Null 'description' argument.");
}
this.description = description;
}
public Double getPercentComplete() {
return this.percentComplete;
}
public void setPercentComplete(Double percent) {
this.percentComplete = percent;
}
public void setPercentComplete(double percent) {
setPercentComplete(new Double(percent));
}
public void addSubtask(Task subtask) {
if (subtask == null) {
throw new IllegalArgumentException("Null 'subtask' argument.");
}
this.subtasks.add(subtask);
}
public void removeSubtask(Task subtask) {
this.subtasks.remove(subtask);
}
public int getSubtaskCount() {
return this.subtasks.size();
}
public Task getSubtask(int index) {
return (Task) this.subtasks.get(index);
}
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (!(object instanceof Task)) {
return false;
}
Task that = (Task) object;
if (!ObjectUtilities.equal(this.description, that.description)) {
return false;
}
if (!ObjectUtilities.equal(this.start, that.start)) {
return false;
}
if (!ObjectUtilities.equal(this.end, that.end)) {
return false;
}
if (!ObjectUtilities.equal(this.percentComplete,
that.percentComplete)) {
return false;
}
if (!ObjectUtilities.equal(this.subtasks, that.subtasks)) {
return false;
}
return true;
}
public Object clone() throws CloneNotSupportedException {
Task clone = (Task) super.clone();
return clone;
}
}
クラス TaskSeriesCollection の関数 getStartValue(Comparable,Comparable)、getEndValue(Comparable,Comparable)、getStartValue(Comparable,Comparable,int)、getEndValue(Comparable,Comparable,int) を変更しました。
public Number getStartValue(Comparable rowKey, Comparable columnKey) {
Number result = null;
int row = getRowIndex(rowKey);
TaskSeries series = (TaskSeries) this.data.get(row);
Task task = series.get(columnKey.toString());
if (task != null) {
result = task.getStart(); //return the start time of task
}
return result;
}
public Number getEndValue(Comparable rowKey, Comparable columnKey) {
Number result = null;
int row = getRowIndex(rowKey);
TaskSeries series = (TaskSeries) this.data.get(row);
Task task = series.get(columnKey.toString());
if (task != null) {
result = task.getEnd(); //return the end time of task
}
return result;
}
public Number getStartValue(Comparable rowKey, Comparable columnKey,
int subinterval) {
Number result = null;
int row = getRowIndex(rowKey);
TaskSeries series = (TaskSeries) this.data.get(row);
Task task = series.get(columnKey.toString());
if (task != null) {
Task sub = task.getSubtask(subinterval);
if (sub != null) {
result = sub.getStart();
}
}
return result;
}
public Number getEndValue(Comparable rowKey, Comparable columnKey,
int subinterval) {
Number result = null;
int row = getRowIndex(rowKey);
TaskSeries series = (TaskSeries) this.data.get(row);
Task task = series.get(columnKey.toString());
if (task != null) {
Task sub = task.getSubtask(subinterval);
if (sub != null) {
result = sub.getEnd();
}
}
return result;
}
デモは次のとおりです。
private static IntervalCategoryDataset createDataset()
{
TaskSeries taskseries = new TaskSeries("Scheduled");
Task task = new Task("task1", 0, 100);// now task can be added from "double" to "double".
task.setPercentComplete(1.0D);
taskseries.add(task);
Task task1 = new Task("task2", 100, 300);
taskseries.add(task1);
Task task2 = new Task("task3",200, 270);
taskseries.add(task2);
TaskSeriesCollection taskseriescollection = new TaskSeriesCollection();
taskseriescollection.add(taskseries);
return taskseriescollection;
}
private static JFreeChart createChart(IntervalCategoryDataset intervalcategorydataset)
{
JFreeChart jfreechart = createGanttChart("Schedule Chart Demo", "Task", "Date", intervalcategorydataset, true, true, false);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setRangePannable(true);
categoryplot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10F);
CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
categoryitemrenderer.setSeriesPaint(0, Color.red);
GanttRenderer ganttrenderer = (GanttRenderer)categoryplot.getRenderer();
ganttrenderer.setShadowVisible(false);
ganttrenderer.setBarPainter(new StandardBarPainter() );
ganttrenderer.setDrawBarOutline(false);
ganttrenderer.setAutoPopulateSeriesFillPaint(true);
return jfreechart;
}
public void createPartControl( Composite parent) {
JFreeChart chart = createChart(createDataset());
ChartComposite frame = new ChartComposite(parent, SWT.FILL, chart,
true);
frame.pack();
}