I created a line chart that displays time every 10 minutes on the x-axis. The data can be for a month long, so the x-axis labels are blended together (unreadable). Also, the grid lines are too close together. I need to figure out how to hide the x-axis chart labels and grid lines and create custom labels and grid lines to only show every hour (or maybe every month if I need to display the graph for a 6 month period). Currently, the x-axis labels are date time format. If 24 hour period is graphed, I need to only show the time every hour. If more than 1 month is graphed, I need to show the date.
Create custom x-axis labels for line chart jfreechart
jfreechart
Related Solutions
How to create a sample XYPlot with 15 minute intervals (shown as date)
1) Create your data.
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.
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.
4) Use a Tooltip generator to generate tooltips (optional)
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.
One simple solution is to set the Category lables to the backround colour (in this case white).
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelPaint("Category 2", Color.white);
domainAxis.setTickLabelPaint("Category 4", Color.white);
This will produce a chart like this
Best Answer
Note that a month's data is 6 * 24 * 30 = 4320; that's too many values to view with much precision, and 6 moths is worse. As an alternative, you can slide or page the data set. The default axis labels should adjust automatically. If you have problems, please post an sscce.