gpt4 book ai didi

java - 将甘特图 x 轴更改为秒,间隔 5 秒 - jFreeChart

转载 作者:行者123 更新时间:2023-12-01 04:59:49 44 4
gpt4 key购买 nike

我想将此图表的 x 轴值更改为秒,间隔为 5 秒。这里常用的 x 轴以小时为单位。我想把它改成秒。我想将此图表用于不同的场景。我尝试过“new Second()”对象但很困惑。谁能帮我解决这个问题吗?

public class XYTaskDatasetDemo2 extends ApplicationFrame {

/**
* Constructs the demo application.
*
* @param title the frame title.
*/
public XYTaskDatasetDemo2(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
// chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
setContentPane(chartPanel);
}

/**
* Creates a subplot.
*
* @param dataset the dataset.
*
* @return A subplot.
*/
private static XYPlot createSubplot1(XYDataset dataset) {
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
renderer.setBaseShape(new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));
renderer.setAutoPopulateSeriesShape(false);
NumberAxis yAxis = new NumberAxis("Y");
yAxis.setLowerMargin(0.1);
yAxis.setUpperMargin(0.1);
XYPlot plot = new XYPlot(dataset, new DateAxis("Time"), yAxis,
renderer);
return plot;
}

/**
* Creates a subplot.
*
* @param dataset the dataset.
*
* @return A subplot.
*/
private static XYPlot createSubplot2(IntervalXYDataset dataset) {
DateAxis xAxis = new DateAxis("Date/Time");
SymbolAxis yAxis = new SymbolAxis("Resources", new String[] {"Team A",
"Team B", "Team C", "Team D", "Team E"});
yAxis.setGridBandsVisible(false);
XYBarRenderer renderer = new XYBarRenderer();
renderer.setUseYInterval(true);
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
return plot;
}

/**
* Creates a demo chart.
*
* @return A demo chart.
*/
private static JFreeChart createChart() {
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
new DateAxis("Date/Time"));
plot.setDomainPannable(true);
plot.add(createSubplot1(createDataset1()));
plot.add(createSubplot2(createDataset2()));
JFreeChart chart = new JFreeChart("XYTaskDatasetDemo2", plot);
chart.setBackgroundPaint(Color.white);
ChartUtilities.applyCurrentTheme(chart);
return chart;
}

/**
* Creates a panel for the demo.
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
return new ChartPanel(createChart());
}

/**
* Creates a dataset for the demo. Normally a dataset wouldn't be hard
* coded like this - it would be read from a file or a database or some
* other source.
*
* @return A dataset.
*/
private static XYDataset createDataset1() {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries s1 = new TimeSeries("Time Series 1");
s1.add(new Hour(0, new Day()), 20214.5);
s1.add(new Hour(4, new Day()), 73346.5);
s1.add(new Hour(8, new Day()), 54643.6);
s1.add(new Hour(12, new Day()), 92683.8);
s1.add(new Hour(16, new Day()), 110235.4);
s1.add(new Hour(20, new Day()), 120742.5);
s1.add(new Hour(24, new Day()), 90654.5);
dataset.addSeries(s1);
return dataset;
}

/**
* Creates a dataset for the demo. Normally a dataset wouldn't be hard
* coded like this - it would be read from a file or a database or some
* other source.
*
* @return A dataset.
*/
private static IntervalXYDataset createDataset2() {
XYTaskDataset dataset = new XYTaskDataset(createTasks());
dataset.setTransposed(true);
dataset.setSeriesWidth(0.6);
return dataset;
}

/**
* Creates a task series collection.
*
* @return A task series collection.
*/
private static TaskSeriesCollection createTasks() {
TaskSeriesCollection dataset = new TaskSeriesCollection();
TaskSeries s1 = new TaskSeries("Team A");
s1.add(new Task("T1a", new Hour(11, new Day())));
s1.add(new Task("T1b", new Hour(14, new Day())));
s1.add(new Task("T1c", new Hour(16, new Day())));
TaskSeries s2 = new TaskSeries("Team B");
s2.add(new Task("T2a", new Hour(13, new Day())));
s2.add(new Task("T2b", new Hour(19, new Day())));
s2.add(new Task("T2c", new Hour(21, new Day())));
TaskSeries s3 = new TaskSeries("Team C");
s3.add(new Task("T3a", new Hour(13, new Day())));
s3.add(new Task("T3b", new Hour(19, new Day())));
s3.add(new Task("T3c", new Hour(21, new Day())));
TaskSeries s4 = new TaskSeries("Team D");
s4.add(new Task("T4a", new Day()));
TaskSeries s5 = new TaskSeries("Team E");
s5.add(new Task("T5a", new Day()));
dataset.add(s1);
dataset.add(s2);
dataset.add(s3);
dataset.add(s4);
dataset.add(s5);
return dataset;
}

/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
XYTaskDatasetDemo2 demo = new XYTaskDatasetDemo2(
"JFreeChart : XYTaskDatasetDemo2.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}

}

最佳答案

您可以通过以下方式更改 createChart 方法来更改水平轴:

private static JFreeChart createChart() {
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Date/Time"));
plot.setDomainPannable(true);
plot.add(createSubplot1(createDataset1()));
plot.add(createSubplot2(createDataset2()));

// Get the horizontal Axis for the plot
DateAxis axis = (DateAxis) plot.getDomainAxis();
// Change the unit tick to 5 hours
axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 5));
// Change the date format used to display tick values
axis.setDateFormatOverride(new SimpleDateFormat("hh:mm"));

JFreeChart chart = new JFreeChart("XYTaskDatasetDemo2", plot);

chart.setBackgroundPaint(Color.white);
ChartUtilities.applyCurrentTheme(chart);
return chart;
}

在此示例中,我在水平轴上定义了 5 小时刻度,因为它适合您的示例数据。如果您想要 5 秒的间隔,请使用:

   axis.setTickUnit(new DateTickUnit(DateTickUnitType.SECOND, 5));

关于java - 将甘特图 x 轴更改为秒,间隔 5 秒 - jFreeChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13538031/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com