I have this line chart and want to make it a bar chart, but I'm new to Java and am not knowing if it possible since the x-axis is TimeSeries
. This is the code I have that visualizes the line chart:
public class Time {
private static Time INSTANCE;
public static boolean isInitialized = false;
private Marker marker;
private Long markerStart;
private Long markerEnd;
private XYPlot plot;
long last_lowerBound;
long last_upperBound;
@Inject
public Time() {
}
Composite comp;
TimeSeriesCollection dataset;
ChartPanel panel;
JFreeChart chart;
protected Point endPoint;
@PostConstruct
public void postConstruct(Composite parent) {
comp = new Composite(parent, SWT.NONE | SWT.EMBEDDED);
Frame frame = SWT_AWT.new_Frame(comp);
JApplet rootContainer = new JApplet();
TimeSeries series = new TimeSeries("Timeline");
dataset = new TimeSeriesCollection();
String plotTitle = "";
String xaxis = "Time";
String yaxis = "Docs";
PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean show = false;
boolean toolTips = true;
boolean urls = false;
chart = ChartFactory.createTimeSeriesChart(plotTitle, xaxis, yaxis, dataset, show, toolTips, urls );
// get a reference to the plot for further customisation...
plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.gray);
plot.setRangeGridlinePaint(Color.gray);
plot.setOutlinePaint(Color.white);
plot.getRangeAxis().setLabel("");
plot.getDomainAxis().setLabel("");
ValueAxis y_axis = plot.getRangeAxis(); // Y
ValueAxis x_axis = plot.getDomainAxis(); // X
Font font = new Font("Veranda", Font.PLAIN, 12);
y_axis.setTickLabelFont(font);
x_axis.setTickLabelFont(font);
x_axis.setTickLabelPaint(Color.black);
y_axis.setTickLabelPaint(Color.black);
plot.getDomainAxis().setAxisLineVisible(false);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
// renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(0, false);
plot.setRenderer(renderer);
Should I only update this line: chart = ChartFactory.createTimeSeriesChart(plotTitle, xaxis, yaxis, dataset, show, toolTips, urls );
or I should change it completely?
I tried changing this part like this but it doesn't show anything:
dataset = new TimeSeriesCollection();
String plotTitle = "";
String xaxis = "Time";
String yaxis = "Docs";
PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean show = false;
boolean toolTips = true;
boolean urls = false;
chart = ChartFactory.createBarChart(plotTitle, xaxis, yaxis, (CategoryDataset) dataset, orientation, show, toolTips, urls);
chart.setBackgroundPaint(null);
chart.setBackgroundImageAlpha(0.0f);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
plot.setOutlineVisible(false);
plot.setRangeZeroBaselineVisible(false);
plot.setDomainGridlinesVisible(false);
plot.setBackgroundPaint(COLOR_INVISIBLE);
plot.setBackgroundImageAlpha(0.0f);
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setSeriesPaint(0, AttributeGuiTools.getColorForValueType(Ontology.NOMINAL));
renderer.setBarPainter(new StandardBarPainter());
renderer.setDrawBarOutline(true);
renderer.setShadowVisible(false); } }
Any help would be highly appreciated!
Given a suitable
XYDataset
, such asIntervalXYDataset
, you can replaceChartFactory.createTimeSeriesChart()
withChartFactory.createXYBarChart()
, which provides for an optionalDateAxis
. The example below uses the same dataset to create both charts.