0
我是jFreeChart的新手。我正嘗試運行隨下載提供的示例代碼。它工作正常。我想在x軸上顯示整數值。我改變了程序來使用NumberAxis而不是DateAxis。但是,當我生成圖形時,它會顯示以「19:00:00」爲前綴的整數x值。 (應該是5,10,但是顯示的是19:00:00:00,19:00:00:00)。我在這裏做錯了什麼?我的x軸整數值前綴爲「19:00:00」。
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.io.File;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.ChartUtilities;
/**
* An example of a time series chart. For the most part, default settings are
* used, except that the renderer is modified to show filled shapes (as well as
* lines) at each data point.
*/
public class PlotExample3 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
{
// set a theme using the new shadow generator feature available in
// 1.0.14 - for backwards compatibility it is not enabled by default
ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",
true));
}
/**
* A demonstration application showing how to create a simple time series
* chart. This example uses monthly data.
*
* @param title the frame title.
*/
public PlotExample3(String title) {
super(title);
ChartPanel chartPanel = (ChartPanel) createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a chart.
*
* @param dataset a dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", // title
"X", // x-axis label
"Y", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
chart.setBackgroundPaint(Color.white);
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(1, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return chart;
}
/**
* Creates a dataset, consisting of two series of monthly data.
*
* @return The dataset.
*/
private static XYDataset createDataset() {
XYSeries s1 = new XYSeries("Test");
s1.add(1, 181.8);
s1.add(2, 167.3);
s1.add(3, 153.8);
s1.add(4, 167.6);
s1.add(5, 158.8);
s1.add(7, 148.3);
s1.add(8, 153.9);
s1.add(9, 142.7);
s1.add(10,123.2);
s1.add(11,131.8);
s1.add(12,139.6);
// ******************************************************************
// More than 150 demo applications are included with the JFreeChart
// Developer Guide...for more information, see:
//
// > http://www.object-refinery.com/jfreechart/guide.html
//
// ******************************************************************
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
try {
ChartUtilities.saveChartAsPNG(new File("C:/Users/pb/Pictures/MyChart.png"),chart,1000,1000);
} catch(Exception e) {
System.err.println(e.getMessage());
}
panel.setFillZoomRectangle(true);
panel.setMouseWheelEnabled(true);
return panel;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
PlotExample3 demo = new PlotExample3("Time Series Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
我沒有看到你在哪裏改變域軸,只有RangeAxis。 –
嗨安德魯,我想我不知道它是什麼。我認爲將DateAxis改爲NumberAxis就足夠了,因爲我使用了示例程序。由於我有時間限制,我沒有深入JFreeChart的細節。 – balistik
我想我發現它爲什麼這樣做。我在程序中使用createTimeSeriesChart。我還沒有嘗試過,但我想這是造成它。 – balistik