public class Example {
public static void main(String args[]){
/*creating timeSeriesCollection*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries timeSeries1 = new TimeSeries("Sample 1");
timeSeries1.add(new Day(8,4, 2012), 7.0);
timeSeries1.add(new Day(19,4, 2012), 5.0);
dataset.addSeries(timeSeries1);
/*Creating the chart*/
JFreeChart chart = ChartFactory.createTimeSeriesChart("Population","Date","Population",dataset,true,true,false);
/*Altering the graph */
XYPlot plot = (XYPlot) chart.getPlot();
plot.setAxisOffset(new RectangleInsets(5.0, 10.0, 10.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));
axis.setAutoTickUnitSelection(false);
axis.setVerticalTickLabels(true);
/* Displaying the chart*/
ChartFrame frame = new ChartFrame("Test", chart);
frame.pack();
frame.setVisible(true);
}
}
I have written the above code which works perfectly fine but when rendering the graph it displays date which does not have values. It displays dates like 9/4/12,10/4/2012 to 18/4/2012 in y-axis which does not have a value. How can I remove the date which does not have value. And why it is behaving so?
Can any one help me please?
Try this:
public class Example1 {
public static void main(String args[]){
DefaultKeyedValues data = new DefaultKeyedValues();
data.addValue("8/4/2012" ,7.0);
data.addValue("19/04/2012",5.0);
CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data);
JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false);
ChartFrame frame = new ChartFrame("Test", chart);
//Switch from a Bar Rendered to a LineAndShapeRenderer so the chart looks like an XYChart
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseLinesVisible(false); //TUrn of the lines
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRenderer(0, renderer);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
frame.pack();
frame.setVisible(true);
}
}
Rather than using a TimeSeriesChart use a CategoryDataSet as #Dirk sugested and then switch to a LineAndShapeRenderer.
The TimeSeries is displayed continuously, which means that the dates are equally distributed along the axis, even if there is no actual value for this date. If you want only certain dates displayed, you should use some kind of CategoryDataSet.
Related
I am trying to create a score/percent/status bar using jfreechart.
The image show what it's suppose to look like.Good ScoreChart
It looks like one bar from a stack chart, so I try to adjust from there. (Do let me know if there is a better way to do this other than the stack chart)
I am seeing multiple issues doing the bar this way. I can't make the start line and end line longer. I can't put text on top of both start/end line.(I think they get chopped off) I cannot put center text in the middle of the bar either...
Here is what I got now. Bad ScoreChart
Here is the code for the bad Chart.
public JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createStackedBarChart(null, null, null, dataset,
PlotOrientation.HORIZONTAL, false, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
plot.getRenderer().setSeriesPaint(0, DONUT_CHART_ORANGE);
plot.getRenderer().setSeriesPaint(1, DONUT_CHART_BLUE);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setVisible(false);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(false);
plot.setRangeGridlinesVisible(false);
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlineVisible(false);
CategoryItemRenderer renderer = ((CategoryPlot) chart.getPlot()).getRenderer();
//renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelGenerator(new CustomItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER);
renderer.setBasePositiveItemLabelPosition(position);
Marker startMarker = new ValueMarker(0);
startMarker.setPaint(Color.black);
startMarker.setLabel("000000000");
startMarker.setStroke(new BasicStroke(1.0f));
startMarker.setLabelOffset( new RectangleInsets(UnitType.ABSOLUTE, 10,0,0,0));
startMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT) ;
startMarker.setLabelTextAnchor(TextAnchor.BASELINE_CENTER);
plot.addRangeMarker(startMarker);
Marker endMarker = new ValueMarker(1000);
endMarker.setPaint(Color.black);
endMarker.setLabel("1000");
endMarker.setStroke(new BasicStroke(1.0f));
endMarker.setLabelAnchor(RectangleAnchor.TOP);
endMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
plot.addRangeMarker(endMarker);
RectangleInsets chartRectangle = new RectangleInsets(35, 0, 35, 0);
chart.setPadding(chartRectangle);
return chart;
}
public CategoryDataset createDataset() {
double inValue = 775;
double[][] data = new double[][] {
{ inValue },
{ 1000- inValue },
};
return DatasetUtilities.createCategoryDataset("A", "B", data);
}
Any Idea how to move the text so they show up on top of the lines and at the center of the bar?
Can we adjust the line width at the beginning and the end?
Thanks for any idea/info.
I've created a BoxAndWhiskerRenderer plot using JFreeChart and it seems that it is automatically creating a sort of legend (see attached picutre).
Is there a way to remove the outside border of this legend and customize the font of the labels in the legend items?
Here is an example of my code:
//Get the desired BoxAndWhiskerCategoryDataset from a LinkedHashMap
BoxAndWhiskerCategoryDataset dataset = values.get(b);
//Create X axis
CategoryAxis xAxis = new CategoryAxis();
xAxis.setAxisLineVisible(false);
//Create Y axis
NumberAxis yAxis = new NumberAxis(b.getLabel());
yAxis.setAxisLineVisible(false);
yAxis.setTickLabelFont(FDFont.getFont(12f));
yAxis.setLabelFont(FDFont.getFont());
yAxis.setLabelPaint(FDPalette.secondaryText);
yAxis.setTickLabelPaint(FDPalette.secondaryText);
yAxis.setAutoRangeIncludesZero(false);
//Create renderer
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
int count = 0;
for(Map.Entry<Integer,Color> map : clusterColor.entrySet()){
//Set color for the series (I have a previously created map which links colors and series)
renderer.setSeriesPaint(count,map.getValue());
count++;
}
renderer.setFillBox(true);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart(plot);
chart.setBackgroundPaint(white);
chart.setBorderVisible(false);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(300, 270));
As shown here, the simplest JFreeChart constructor adds a legend by default. The code for doing so is seen here; simply substitute your desired frame, color and position.
Starting from this example, the following changes produce the chart shown below:
private void createChartPanel() {
…
JFreeChart chart = new JFreeChart("BoxAndWhiskerDemo", plot);
LegendTitle legend = chart.getLegend();
legend.setFrame(new LineBorder(Color.white, new BasicStroke(1.0f),
new RectangleInsets(1.0, 1.0, 1.0, 1.0)));
legend.setItemFont(legend.getItemFont().deriveFont(16f));
chartPanel = new ChartPanel(chart);
}
Compare to this default legend:
I am using JFreeChart to plot a line graph. The app reads in sensory data every 100 milliseconds so for a few minutes of capture it's a a lot of data. I don't plot the graph dynamically, it is static. I am using a Category plot since the axis can sometimes be decimal values, other times it can be strings, other times it can be boolean. My issue is the X axis (time) has so much data I can't make out the text:
Anyone know what I can do here to? Any tips or tricks to deal with this will be great!
private CategoryDataset createDataset() {
String series1 = "First";
String series2 = "Second";
String category1 = "Category 1";
String category2 = "Category 2";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < time.size(); i++) {
dataset.addValue(Math.random(), series1, time.get(i));
}
return dataset;
}
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
"Line Chart Demo 6", // chart title
"Time", // x axis label
"RPM", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
plot.setRenderer(renderer);
return chart;
}
public void setLists(ArrayList<String> time) {
this.time = time;
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
You can turn off the Lables and TickMarks by adding:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis cx = new CategoryAxis();
cx.setTickLabelsVisible(false);
cx.setTickMarksVisible(false);
plot.setDomainAxis(cx);
If you wantto display a subset of the labels (every nth value) then you will need to subclass CategoryAxis so you can overide CategoryAxis#drawCategoryLabels() andCategoryAxis#drawTickMarks()
i have managed to plot an xy chart with a few points using jfreechart.
what am trying to do is to be able to click anywhere on the line that has been drawn and get its x or y axis value.
Can anybody help me out ?
its my first time using j freechart and i feel somewhat lost.
i created the dataset and generated the chart so far.
TimeSeries s = new TimeSeries("security", Day.class);
while (rate_i.hasNext()) {
rate r = (rate) rate_i.next();
Calendar cal = Calendar.getInstance();
cal.setTime(r.d);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DATE);
int year = cal.get(Calendar.YEAR);
s.add(new Day(day, month, year), r.rate);
}
TimeSeriesCollection ds = new TimeSeriesCollection();
ds.addSeries(s);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Security Performance over time.", // title
"Date", // x-axis label
"Value", // y-axis label
ds, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot xyplot = (XYPlot) chart.getPlot();
xyplot.setDomainPannable(true);
xyplot.setRangePannable(false);
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot
.getRenderer();
if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer xylineandshaperenderer =
(XYLineAndShapeRenderer) xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(false);
}
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setDateFormatOverride(
new SimpleDateFormat("EEE, MMM d, ''yy"));
ChartFrame frame = new ChartFrame("Chart", chart);
frame.setVisible(true);
frame.setSize(700, 900);
Add a ChartMouseListener to your enclosing ChartPanel; examples are seen here and here. The ChartEntity will contain details about the mouse target.
I a have problem while using getTime(). The method is passing from another class and using System.currentTimeMillis(). Can someone help me? I really appreciate your help. Thank you.
panelGraph = new JPanel();
panelGraph.setLayout(new BorderLayout());
aesDataset = XYDataset(caes.getTimeTotal());
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Graph: Encrypted Result", "SetFile", "Time", aesDataset, true, true, false);
chart.setBackgroundPaint(Color.green);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
XYItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, Color.red);
NumberAxis yAxis1 = (NumberAxis) plot.getRangeAxis();
yAxis1.setTickLabelPaint(Color.red);
ChartPanel frame1 = new ChartPanel(chart);
frame1.setSize(400, 300);
panelGraph.add(frame1);
Use TimeSeriesCollection dataset = new TimeSeriesCollection(); instead of XYDataset.
Then add to the dataset TimeSeries as dataset.addSeries(timeSeries); where timeSeries is an object with list of TimeSeriesDataItem.