I am new to JFreeChart. My requirement is to display the X axis (time-axis) as following (time ranges will be configurable as per user input) for a line chart with suppose 3 variables:
3rdAug-8thAug..10thAug-15thAug.. [ and so on ]
Currently my graph's X axis is like this :
1..2..3..4..5 ..
[Unable to attach screenshots]
My demo code is as follows :
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
"Line Chart Demo ", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
// OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, true); //for line visibility
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
// final Axis range = plot.get
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
Can anyone point me in the right direction here?
How do I get only the required values shown on the X axis?
XYPlot differ between domain axes and range axes. In your case the X axis is the domain axis whereas the Y axis is the range axis.
Valuexis domainAxis = plot.getDomainAxis();
You can also set a different domain axis:
ValueAxis dAxis = new ...
plot.setDomainAxis(dAxis);
You want to use JFreeChart's TimeLine interface to limit the shown dates on the DateAxis (which is actually the domain-axis in your case, as already pointed out by #Uli). For your requirement the default implementation SegmentedTimeline should fulfill your requirements. Just configure it and pass it on to your axis:
SegmentedTimeline timeline = new SegmentedTimeline(
86400000l, // segment size = one day in ms (24*3600*1000)
5, // include 5 segments (days)
2); // exclude 2 segments (days)
DateAxis axis = new DateAxis();
axis.setTimeline(timeline);
And don't forget to configure the plot to use the new DateAxis, as XYPlot uses NumberAxis by default:
plot.setDomainAxis(axis);
hth,
- martin
Related
Is there a way to change the position of the Category Plot Labels (title) (censored with black bars in the image) to be on top of the chart and / or aligned vertically to each other?
Here's the classes I use to construct it.
var renderer = new LineAndShapeRenderer();
...
var yAxis = new NumberAxis();
...
var xAxis = new CategoryAxis(title);
...
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
...
var combinedPlot = new CombinedRangeCategoryPlot();
combinedPlot.add(plot, weight);
...
I'm using version 1.0.19 of JFreeChart since newer versions produce some visual artifacts.
There has been a misunderstanding: I meant the labels, "Plot 0" and "Plot1", in your example; and I'm asking for the ability to move them on top of the chart…
Invoke setDomainAxisLocation() on each subplot domain axis to move the axis and its label. Alternatively, obtain the required axis references from the List<CategoryPlot> shown below.
plot0.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
plot1.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
I only need those "Plot 0" and "Plot1" labels on top, not the tick labels…
You can hide the tick labels as desired using setTickLabelsVisible(false), and you can have multiple axes, as shown here.
To adjust the tick label orientation, invoke setCategoryLabelPositions() on each sublot comprising the combined plot. Do this either
When the plot is created.
CategoryAxis axis0 = new CategoryAxis("Plot 0");
axis0.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
CategoryPlot plot0 = new CategoryPlot(…, axis0, …);
…
CategoryAxis axis1 = new CategoryAxis("Plot 1");
axis1.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
CategoryPlot plot1 = new CategoryPlot(…, axis1, …);
After creating the combined plot.
CombinedRangeCategoryPlot combinedplot = new CombinedRangeCategoryPlot(…);
…
List<CategoryPlot> list = combinedplot.getSubplots();
list.get(0).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
list.get(1).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
Vertical labels are illustrated below.
I am trying to hide the y axis line in my stacked bar chart in JFreeChart, and I have already tried below methods:
Setoutlinepaint(null)
Setbackgroundpaint(color.white)
Setoutlinevisible(false)
Categoryaxis.setaxislinevisible(false)
Categoryaxis.setvisible(false)
Many thanks in advance for your help.
Invoking the parent method setAxisLineVisible() appears to produce the expected result for either a CategoryAxis or a ValueAxis. Starting from this example, the following changes in createChart() produce the result shown. I've changed the plot orientation to make the domain axis vertical, and I've invoked setAxisLineVisible(false) on both axes.
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setOrientation(PlotOrientation.HORIZONTAL);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setAxisLineVisible(false);
plot.setDomainAxis(domainAxis);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setAxisLineVisible(false);
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
Axis lines not visible:
Axis lines visible:
Note that the factory methods for both bar chart stacked bar chart use the same axes.
I need to set minimum X-tick interval in the chart. I have a dataset that contains 1000 entries, i.e. <0,12>,<5,22>,<10,23>,...,<1000,20>. In dataset, the interval between X-values is equal to 5. In the chart I want to set X-tick interval equal to 100.
I use this code, but it does not work:
NumberAxis range = (NumberAxis)plot.getRangeAxis();
range.setTickUnit(new NumberTickUnit(100));
Here is my code snippet:
private JFreeChart createChart(CategoryDataset dataset)
{
final JFreeChart chart = ChartFactory.createAreaChart(
this.title,
"Time",
"Net demand",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
Title t = new TextTitle(this.subtitle);
chart.addSubtitle(t);
int transparency = 95;
Color c = new Color(1, 158, 115, transparency);
final CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = new CustomRenderer(c);
renderer.setSeriesOutlinePaint(0, Color.black);
renderer.setSeriesOutlineStroke(0, new BasicStroke(0.5f));
renderer.setSeriesPaint(0,c);
plot.setRenderer(renderer);
NumberAxis range = (NumberAxis)plot.getRangeAxis();
range.setTickUnit(new NumberTickUnit(60));
return chart;
}
You've set the tick unit for the range axis, which is the y-axis in JFreeChart. For the x-axis, you want the domain axis.
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setTickUnit(new NumberTickUnit(60));
The domain and range terms come from a function mapping a set of domain values to a set of range values. With hindsight, using getXAxis() and getYAxis() for the method names would have been clearer for most people...for JFreeChart 2 I'll probably change it.
I have a single series XYAreaChart
final JFreeChart chart = ChartFactory.createXYAreaChart(
"",
"", "Rolling",
dataset,
PlotOrientation.VERTICAL,
false, // legend
true, // tool tips
false // URLs
);
final XYPlot plot = chart.getXYPlot();
I want to set paint color to Red when RangeValue >0 otherwise set it to Green. This is the nearest bit of code I can find BUT I think it is for two series:
plot.setRenderer(new XYDifferenceRenderer(Color.green, Color.red, false));
Which renderer do I need for a single series?
This may be able to help you. Below is an example showing a basic chart from a JFreechart tutorial. I have added the renderer in your example to it, my theory is that when you have 1 series the 'second series' as it were is effectively 0. Therefore when you apply the two series renderer, if the first series dips below 0 it becomes the negative color, and when it is above 0 becomes the positive colour.
When I opened my saved chart it had red on the minus points, and green on the posotive. See if it helps:
public class SO{
public static void main(String[] args) {
// Create a simple XY chart
XYSeries series = new XYSeries("Gradient logs chopped");
series.add(1, -7);
series.add(2, -2);
series.add(3, 4);
series.add(4, 7);
series.add(5, 10);
// Add the series to your data set
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
// Generate the graph
JFreeChart chart = ChartFactory.createXYLineChart(
"Logs chopped", // Title
"Week", // x-axis Label
"Logs", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
//The renderer part
XYPlot plot = chart.getXYPlot();
XYDifferenceRenderer rend = new XYDifferenceRenderer(Color.GREEN, Color.RED, false);
rend.setSeriesFillPaint(0, Color.DARK_GRAY);
rend.setRoundXCoordinates(true);
plot.setRenderer(rend);
//The renderer part
try {
ChartUtilities.saveChartAsJPEG(new File("D:\\Users\\user2777005\\Desktop\\XYchart.jpg"), chart, 500, 300);
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}
}
}
Good luck!
I have updatable OHLCChart.
I need to draw a line over chart.
How to implement it?
If you want to draw a vertical or horizontal line at a given position on an axis, you can use a ValueMarker :
ValueMarker marker = new ValueMarker(position); // position is the value on the axis
marker.setPaint(Color.black);
//marker.setLabel("here"); // see JavaDoc for labels, colors, strokes
XYPlot plot = (XYPlot) chart.getPlot();
plot.addDomainMarker(marker);
Use plot.addRangeMarker() if you want to draw an horizontal line.
Something like this should work if you want to plot a line indicator (like a moving average for example):
XYDataset dataSet = // your line dataset
CombinedDomainXYPlot plot = (CombinedDomainXYPlot) chart.getPlot();
XYPlot plot = (XYPlot) plot.getSubplots().get(0);
int dataSetIndx = plot.getDatasetCount();
plot.setDataset(dataSetIndx, dataSet);
XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, false);
plot.setRenderer(dataSetIndx, lineRenderer);