java 如何使用 Jfreechart 绘制每日图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/424603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot day-wise chart using Jfreechart
提问by user48094
I have data for every 15 minutes. I want to plot a graph to display values from 'date1' to 'date2'. The plot should show every 15 minutes value. But display on X-axis should show only dates.
我每 15 分钟就有一次数据。我想绘制一个图表来显示从“date1”到“date2”的值。该图应每 15 分钟显示一次值。但是 X 轴上的显示应该只显示日期。
回答by futureelite7
How to create a sample XYPlot with 15 minute intervals (shown as date)
如何创建一个以 15 分钟为间隔的示例 XYPlot(显示为日期)
1) Create your data.
1) 创建数据。
XYSeries dataSeries = new XYSeries("SMS Sent");
2) Add your axes. If you want the x-axis to show dates, use a DateAxis as the x-axis. Input your date data as a long (in milliseconds). jfreecharts will take care of the formatting for you.
2)添加你的轴。如果您希望 x 轴显示日期,请使用 DateAxis 作为 x 轴。输入您的日期数据作为一个长(以毫秒为单位)。jfreecharts 将为您处理格式。
DateAxis dateAxis = new DateAxis(timeAxisTitle);
DateTickUnit unit = null;
unit = new DateTickUnit(DateTickUnit.MINUTE,15);
DateFormat chartFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
dateAxis.setDateFormatOverride(chartFormatter);
dateAxis.setTickUnit(unit);
NumberAxis valueAxis = new NumberAxis(valueAxisTitle);
3) Use a DateTickUnit object to set the tick size (e.g. 15 mins.) This will plot a point every 15 mins.
3) 使用 DateTickUnit 对象设置刻度大小(例如 15 分钟)。这将每 15 分钟绘制一个点。
4) Use a Tooltip generator to generate tooltips (optional)
4)使用工具提示生成器生成工具提示(可选)
XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
"{0}: {2}", chartFormatter, NumberFormat.getInstance());
StandardXYItemRenderer renderer = new StandardXYItemRenderer(
StandardXYItemRenderer.SHAPES_AND_LINES, ttg, null);
renderer.setShapesFilled(true);
XYPlot plot = new XYPlot(xyDataset, dateAxis, valueAxis, renderer);
JFreeChart chart = new JFreeChart(chartTitle, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(java.awt.Color.WHITE);
5) create the chart by instantiating a new JFreeChart object. You can then save it or display it on screen. Refer to Java documentation on how to do this.
5) 通过实例化一个新的 JFreeChart 对象来创建图表。然后您可以保存它或在屏幕上显示它。有关如何执行此操作,请参阅 Java 文档。

