JFreeChart proper zoom behaviour between passed java points - java

Assume that I have a JFreeChart 's line chart with points (0,0) and (3,3). So the line from (0,0) to (3,3) is shown. However when I zoom X-Axis so that to see the data between x=1 and x=2 no data (no line) is shown.
The problem occurs for me in two cases:
JFreeChart chart = org.jfree.chart.ChartFactory.createTimeSeriesChart("Example",
"", "test", null, false, true, false);
and for:
JFreeChart chart = org.jfree.chart.ChartFactory.createXYStepChart
("Example", "", "test", null,PlotOrientation.VERTICAL, true, true, false);
However in the second additionally I am getting the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Requires xLow < xHigh.
at org.jfree.chart.renderer.RendererUtilities.findLiveItemsLowerBound(RendererUtilities.java:76)
at org.jfree.chart.renderer.RendererUtilities.findLiveItems(RendererUtilities.java:261)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3845)
at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3389)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1237)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1677)
The main question is:
How can I set the renderer up to see the data (line) even if I zoom between 'passed data points'?

Related

Category Axis Label Expression Alignment In jasper Report through Java code

Is there any way, to change Category Axis Label Expression's Alignment of a Chart in Jasper Report through Java Code.
I want Left alignment of Category Axis Label Expression. As shown the picture below, I want "hello" to be left aligned.
Starting with BarChartDemo1, included in the distribution, the changes below create a bar chart with an axis label having its location set to the LOW_END. For PlotOrientation.VERTICAL, that means aligned on the left.
JFreeChart chart = ChartFactory.createBarChart(
"Performance: JFreeSVG vs Batik",
"$P{hello}" /* x-axis label*/,
"Milliseconds" /* y-axis label */,
dataset, PlotOrientation.VERTICAL, false, false, false);
…
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelLocation(AxisLabelLocation.LOW_END);
Try doing the same for the range axis to see the effect:
rangeAxis.setLabelLocation(AxisLabelLocation.LOW_END);

Resize an axis fitting another with JFreechart

i'm trying to make an overlaid plot,
And my problem is that i can't bring the second plot to fit the second one.
Here is my first plot :
And here the second one :
And when i try to fit both, here is what i get :
So basically, i would like to fit the whole second plot between 0 and 30, how can i do this without losing any data?
First I tried using plot.mapDatasetToRangeAxis()
Then i tried with :
domain.setRange(0.00, 30.0);
domain.setTickUnit(new NumberTickUnit(1));
But i couldn't bring neither the first, nor the second one to work as i wish.
Do you have any other ideas? (except buying this - which i can't afford right now as a student).
Any help will be greatly appreciated :)
Oh and by the way the x-axis is a speed (forgot to draw it on the plot).
So here a very ugly photomontage of the kind of result i wish to have (with fitting units on the x and y axis) :
Sorry for my Gimp skills, which are beyond bad.
Here is what i did :
private JFreeChart createOverlaidChart()
{
final NumberAxis domainAxis = new NumberAxis("Speed (m / s)");
final ValueAxis rangeAxis = new NumberAxis("Power (kw)");
// create plot ...
final IntervalXYDataset data0 = createDataset0();
final XYItemRenderer renderer0 = new XYBarRenderer(0.20);
// change "new XYBarRenderer(0.20)" to "StandardXYItemRenderer()" if you want to change type of graph
final XYPlot plot = new XYPlot(data0, domainAxis, rangeAxis, renderer0);
// add a second dataset and renderer...
final IntervalXYDataset data1 = createDataset1();
final XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer(false, true);
// arguments of new XYLineAndShapeRenderer are to activate or deactivate the display of points or line. Set first argument to true if you want to draw lines between the points for e.g.
plot.setDataset(1, data1);
plot.setRenderer(1, renderer1);
// add a third dataset and renderer...
final IntervalXYDataset data2 = createDataset2();
final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, true);
// arguments of new XYLineAndShapeRenderer are to activate or deactivate the display of points or line. Set first argument to true if you want to draw lines between the points for e.g.
plot.setDataset(2, data2);
plot.setRenderer(2, renderer2);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
// return a new chart containing the overlaid plot...
return new JFreeChart("Test", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}
The Range Axis is the Vertical/Y Axis, you need to add a second Domain Axis (Horizontal/X Axis) to your chart.
I did something similar -- although using same X axis vales but differing Y axis. For this, I use single Domain (you'd want single Range). I convert for you (there may be typos due to my edits):
priceXYPlot.setRangeAxis( new new NumberAxis( "Y" ) );
priceXYPlot.setDomainAxis( 0, new NumberAxis( "X1" ) );
priceXYPlot.setDomainAxis( 1, new NumberAxis("X2") );
and mapDatasetToRangeAxis so that you had two diff X axis along top & bottom, something like:
priceXYPlot.setDataset( 0, data0);
priceXYPlot.mapDatasetToDomainAxis( 0, 0 ); //1st dataset to 1st x-axis
priceXYPlot.setDataset( 1, data1 );
priceXYPlot.mapDatasetToDomainAxis( 1, 1 ); //2nd dataset to 2nd x-axis

Accessing Legend Item text

I am creating a Bar Chart:
final JFreeChart result = ChartFactory.createBarChart(TITLE, // chart title
X_TITLE, // domain axis label
Y_TITLE, // range axis label
dataset, // data
PlotOrientation.HORIZONTAL, // the plot orientation
true, // legend
true, // tooltips
false // urls
);
I then added a ChartMouseListener as described here.
When I click on a legend item, "LegendItemEntity: seriesKey=null, dataset=null" is displayed.
How do I access the legend text for the corresponding item?
Starting from org.jfree.chart.demo.BarChartDemo1 and adding the same ChartMouseListener to the source yields results like this:
LegendItemEntity: seriesKey=First, dataset=org.jfree.data.category.DefaultCategoryDataset#d9510071
LegendItemEntity: seriesKey=Second, dataset=org.jfree.data.category.DefaultCategoryDataset#d9510071
LegendItemEntity: seriesKey=Third, dataset=org.jfree.data.category.DefaultCategoryDataset#d9510071
If you're geting a different result, an sscce may help.

jFreeChart: How to draw the y-axis in a line chart

With Java, is there any way of drawing the y-axis in a line chart with JFreeChart?
I need to show both the x-axis and the y-axis, and I already draw the x-axis with the flowing code:
LineFunction2D x_axis = new LineFunction2D(0, 0);
XYDataset xdataset = DatasetUtilities.sampleFunction2D(
x_axis, min_x-min_x/15, max_x+max_x/15, 100, "X-Axis");
xyplot.setDataset(1, xdataset);
I think it's impossible to draw the line x = 0 with LineFunction2D, which take the value of a and b of the equation y = ax + b.
Maybe there is a function to call to show the axis because I have seen a demo showing this.
There is a method in the XYPlot class:
public void setDomainZeroBaselineVisible(boolean visible);
...that will show a line at the zero value on the domain (x) axis.
Y=aX+b can be displayed using ChartFactory.createLineChart. This way, you get your series, the X and Y axis in one go.

CrossHair Tracing In JFreeChart

I was able to implement real-time mouse tracing as follow :
The source code is as follow :
http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/charting/CrossHairUI.java?revision=1.5&view=markup
http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/gui/ChartJDialog.java?revision=1.9&view=markup
However, I unable to obtained the correct y screen coordinate, when an subplot being added.
(broken image)
I suspect I didn't get the correct screen data area.
When there is only one plot in the screen, I get a screen data area with height 300++
When a sub plot added to the bottom, I expect screen data area height will be reduced, due to the height occupied by the newly added subplot.
However, I have no idea how to obtain the correct screen data area for the first plot.
final XYPlot plot = (XYPlot) cplot.getSubplots().get(0);
// Shall I get _plotArea represents screen for getSubplots().get(0)?
// How?
// I try
//
// chartPanel.getScreenDataArea(0, 0);
// chartPanel.getScreenDataArea(0, 1);
// chartPanel.getScreenDataArea(1, 0);
// chartPanel.getScreenDataArea(1, 1);
//
// All returned null
// OK. I suspect this is causing me unable to get the correct screen y coordinate
// I always get the same _plotArea, although a new sub plot had been added.
final Rectangle2D _plotArea = chartPanel.getScreenDataArea();
final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
final double yJava2D = rangeAxis.valueToJava2D(yValue, _plotArea, rangeAxisEdge);
Here is the code snippet to obtained the correct rectangle after dive into JFreeChart source code.
/* Try to get correct main chart area. */
final Rectangle2D _plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0).getDataArea();

Categories