Reputation: 3677
I am trying to find out apis in JFreeChart, that would allow me to build up a chart, where a long the X axis I would have categories and the Y axis interval values corresponding to times.
Y axis should represent time. X axis category.
This is similar to a google calendar scenario, where in the X axis such as day of the week. And on the Y axis, you can write what you do along certain regions of time.
The following JFREE chart, represents what I would like to represent. However, my Y axis is holding time as miliseconds and not as a time unit (e.g. seconds).
In the following image, i illustrate categories along the X that we can imagine they are something like an edge on a graph.
And on the Y axis, we have for example something time spend traveling it and then after traveling it time spent saying cultivating it.
The Y axis is compised by double values, and it would be preferable to be able to use an appropriate time axis. This seems to be problem when using category data sets. Alreayd to make the chart bellow, many hacks had to be put in place, to make the stacks not being rendered from 0 - by having invisible data series.
Upvotes: 1
Views: 1070
Reputation: 3677
Althgout it is definitely not obvious how to use JFreeChart to solve the problem posted above, based on the demo7 sample mentinoed in my comments I can now say that it is definitely possible to use the XYBarChart as an engine to render the bars at the right places for your problem, while having an axis that represents time and another axis that represents whatever you want such as categories.
The sample7 example uses a numberic axis and allocates intervals of the numeric axis as steps for rendering with of a bar.
In such a way, the problem can easily be adapted to my question above, and here is the adapted application that pseudo-implements the chart on my question in a proper way.
import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class XYBarChartDemo7VarianRepresentingStackOverFlowQuestion extends ApplicationFrame {
/**
* Constructs the demo application.
*
* @param title
* the frame title.
*/
public XYBarChartDemo7VarianRepresentingStackOverFlowQuestion(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
setContentPane(chartPanel);
}
private static JFreeChart createChart(IntervalXYDataset dataset) {
JFreeChart chart = ChartFactory.createXYBarChart("XYBarChartDemo7", "Date", true, "Y", dataset,
PlotOrientation.HORIZONTAL, true, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
// The Y axis is turned into a date axis
plot.setRangeAxis(new DateAxis("Date"));
// The X axis is turned is turned into a label axis
SymbolAxis xAxis = new SymbolAxis("Series",
new String[] { "R107 => R101", "R101 => R5", "R5 => R15", "R15 => R16" });
xAxis.setGridBandsVisible(false);
plot.setDomainAxis(xAxis);
// Enables using Y interval
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setUseYInterval(true);
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
ChartUtilities.applyCurrentTheme(chart);
return chart;
}
/**
* Creates a sample dataset.
*
* @return A dataset.
*/
private static IntervalXYDataset createDataset() {
// Time points to represnet
// Edge: "R107 => R101"
RegularTimePeriod d0 = new Day(12, 6, 2007);
RegularTimePeriod d1 = new Day(13, 6, 2007);
RegularTimePeriod d2 = new Day(14, 6, 2007);
// "R101 => R5"
RegularTimePeriod d3 = new Day(15, 6, 2007);
RegularTimePeriod d4 = new Day(16, 6, 2007);
RegularTimePeriod d5 = new Day(17, 6, 2007);
// "R5 => R15"
RegularTimePeriod d6 = new Day(18, 6, 2007);
RegularTimePeriod d7 = new Day(19, 6, 2007);
RegularTimePeriod d8 = new Day(20, 6, 2007);
// "R15 => R16"
RegularTimePeriod d9 = new Day(21, 6, 2007);
RegularTimePeriod d10 = new Day(22, 6, 2007);
RegularTimePeriod d11 = new Day(23, 6, 2007);
/// Next edge that is not part of the path
RegularTimePeriod d12 = new Day(24, 6, 2007);
// Create three interval series (each series has a different color)
XYIntervalSeriesCollection dataset = new XYIntervalSeriesCollection();
XYIntervalSeries s1 = new XYIntervalSeries("ProductiveTime");
XYIntervalSeries s2 = new XYIntervalSeries("WaitTime");
// Series 1 and series2 along edge1
addItem(s1, d0, d1, 0);
addItem(s2, d1, d3, 0);
// Series 1 and series2 along edge2
addItem(s1, d3, d4, 1);
addItem(s2, d4, d6, 1);
// Series 1 and series2 along edge3
addItem(s1, d6, d7, 2);
addItem(s2, d7, d9, 2);
// Series 1 and series2 along edge4
addItem(s1, d9, d10, 3);
addItem(s2, d10, d12, 3);
// puts in the data set the data series
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
private static void addItem(XYIntervalSeries s, RegularTimePeriod p0, RegularTimePeriod p1, int index) {
s.add(index,
// xLow x xHigh we see this on the Y because chart is horizontal
index - 0.45, index + 0.45, p0.getFirstMillisecond(),
// yLow yHigh (time interval) - we see this on the X because chart is set as horizontal
// NOTE: - notice how miliseconds are being used to make the xy interval and not
// actually a date object. But then on the date axis we shall have a proper DateAxis for rendering
// these miliseconds values
p0.getFirstMillisecond(), p1.getFirstMillisecond());
}
/**
* Creates a panel for the demo.
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
return new ChartPanel(createChart(createDataset()));
}
/**
* Starting point for the demonstration application.
*
* @param args
* ignored.
*/
public static void main(String[] args) {
XYBarChartDemo7VarianRepresentingStackOverFlowQuestion demo = new XYBarChartDemo7VarianRepresentingStackOverFlowQuestion(
"JFreeChart : XYBarChartDemo7.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
And the image bellow illustrates how stepping away from the BarChart and category data sets, and moving into the XYBarChart and into IntervalXYDataSets, we can render bars where we want them.
Note if we plot the graph vertically, it will look exactly like the first chart at the question. With time axis on the Y and and the cateogories on the X.
So, another satisfied user.
Upvotes: 1