How to set uneven (space) tick Units in JFreeChart XYLine - java

I've trying to find a solution to this problem.
I have an XYLineChart and I would like to have uneven spaces between tick units.
This is my code:
//Method (receive the dataset and return the chart)
private JFreeChart createChart(XYDataset dataset){
//--------create XYlineChart
JFreeChart oChart = ChartFactory.createXYLineChart("XYLineChart", "X", "Y", dataset);
XYPlot plot = (XYPlot) oChart.getPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.black);
plot.setRangeGridlinePaint(Color.black);
// render shapes and lines
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true);
plot.setRenderer(renderer);
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setNumberFormatOverride(THOUSANDS); //Override and Use a my NumberFormat
xAxis.setRange(0, 8000);
//I want to use this with my own set of tickUnits, the values(x) that are on the image I'm trying to achieve
//xAxis.setStandardTickUnits();
xAxis.setTickUnit(new NumberTickUnit(500));
yAxis.setRange(0.0, 130.0);
yAxis.setTickUnit(new NumberTickUnit(10));
yAxis.setInverted(true);
return oChart;
}
I dont know how to make my own set of TickUnits or how to space the domainAxis (xAxis) unevenly.
I'll be glad to read any examples of something like that, so i can try to use it or apply it.
I'll really appreciate your help, is the only thing missing in my project.

Related

How to show shapes at plot points with JFreeChart TimeSeries chart?

I'm using JFreeChart v1.0.19 to create the following time series chart:
...using the following code:
final String chartTitle = "";
final XYDataset dataset = createDataset1();
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
chartTitle, "Year End", "Turnover", dataset, true, false, false);
final XYPlot plot = chart.getXYPlot();
final NumberAxis axis2 = new NumberAxis("Cash");
axis2.setAutoRangeIncludesZero(false);
plot.setRangeAxis(1, axis2);
plot.setDataset(1, createDataset2());
plot.mapDatasetToRangeAxis(1, 1);
final StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
renderer1.setSeriesPaint(0, Color.blue);
renderer1.setSeriesShape(0, ShapeUtilities.createDiamond(5));
renderer1.setSeriesShapesFilled(0, true);
plot.setRenderer(0, renderer1);
final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setSeriesPaint(0, Color.black);
renderer2.setSeriesShape(0, ShapeUtilities.createDiamond(5));
renderer2.setSeriesShapesFilled(0, true);
plot.setRenderer(1, renderer2);
chart.getPlot().setBackgroundPaint(Color.WHITE);
chart.getXYPlot().setDomainGridlinePaint(Color.LIGHT_GRAY);
chart.getXYPlot().setRangeGridlinePaint(Color.LIGHT_GRAY);
...but I can't get the data points to show shapes. Is this possible?
It was my renderer that was the problem. Changing from StandardXYItemRenderer to XYLineAndShapeRenderer e.g.
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,true);
...did the trick. The two boolean args in the constructor are for switching on/off lines and shapes, respectively.

Too much data in DomainAxis, another way of setTickUnit

I am creating chart like this:
DefaultCategoryDataset cumulativeResultData = new DefaultCategoryDataset();
DefaultCategoryDataset indexResultData = new DefaultCategoryDataset();
then in for loop im adding data to each Dataset using cumulativeResultData.addValue
CategoryPlot plot = new CategoryPlot();
plot.setDomainAxis(new CategoryAxis("Days"));
plot.setRangeAxis(new NumberAxis("Result"));
plot.setOrientation(PlotOrientation.VERTICAL);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
LineAndShapeRenderer renderer2 = new LineAndShapeRenderer(true, false);
LineAndShapeRenderer renderer = new LineAndShapeRenderer(true, false);
plot.setRenderer(renderer);
plot.setDataset(cumulativeResultData);
ValueAxis rangeAxis2 = new NumberAxis("Index price");
rangeAxis2.setRange(minRange - 1, maxRange + 1);
plot.setRangeAxis(1, rangeAxis2);
plot.setRenderer(1, renderer2);
plot.setDataset(1, indexResultData);
plot.mapDatasetToRangeAxis(1, 1);
JFreeChart chart = new JFreeChart(plot);
But what i get is not what i expected to get, please take a look in the picture bellow.
The domainAxis has to much data and it is blurred. Is any way to repair this? That only every for example 20th element shows on the domainAxis?
http://imgur.com/PkxSZl8
You should only use the CategoryPlot class when your x-axis will show categorical data. It looks like you have time series data, and for that you can use the XYPlot class with a DateAxis for the x-axis. There is also the TimeSeriesCollection class that you can probably use for your dataset.

JFree chart set axis to percentage

I want my Axis in my JFree charts to become % instead of normal numbers does anyone know how I can modify my code so this happens.
It should be said that my table is already in procentage but the graph it self is not!
UPDATE
This is what ive tried so far yet still no changes:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
DecimalFormat pctFormat = new DecimalFormat("#.0%");
rangeAxis.setNumberFormatOverride(pctFormat);
rangeAxis.setRange(0.0, 100.0);
chart.getXYPlot().setRangeAxis(rangeAxis);
title = periode;
chartPanel.setChart(chart);
setTableModel(true,true);
UPDATE2
To set the intervals to 10 you need the following code:
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0.0, 100.0);
rangeAxis.setTickUnit(new NumberTickUnit(10));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
You can use NumberAxis#setNumberFormatOverride() and provie a java.text.NumberFormat
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
DecimalFormat pctFormat = new DecimalFormat("#.0%");
rangeAxis.setNumberFormatOverride(pctFormat);

JFreeChart : Zoom-in for domain axis is not working when CategoryAxis is used

I am using jfreechart library to draw a series chart. I have taken values on y-axis, time on x-axis and 3 categories as series. Everything is fine but I'm not able to zoom-in domain axis although it's working fine for range axis. Is this possible?
Following lines of code may help you to find some scenario of my code:
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
this.add(chartPanel, BorderLayout.CENTER);
and
//set plot specifications
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(0xffffe0));
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);
//CUSTOMIZE DOMAIN AXIS
final CategoryAxis domainAxis = (CategoryAxis) plot.getDomainAxis();
//customize domain label position
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
It looks like you are using a CategoryPlot which does not support zooing on the domain range
To allow zooming on the Domain axis switch over to a XYPlot if you are using the chart factory the code is
JFreeChart chart = ChartFactory.createXYLineChart(...);
If you are able to cast the plot to an CategoryPlot something has gone wrong. I've checked LineChartDemo3 and this code causes an error (java.lang.ClassCastException):
try {
final CategoryPlot cplot = (CategoryPlot) chart.getPlot();
} catch (Exception e) {
e.printStackTrace();
}

How to hide JFreeChart XYSeries yaxis?

Is there a way to hide a JFreeChart XYSeries yaxis?
The yaxis is meaningless on logic analyzer display.
You can use setVisible(), as shown below.
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis range = plot.getRangeAxis();
range.setVisible(false);
hide horizontal and vertical labels
ValueAxis range = xyPlot.getRangeAxis();
range.setVisible(false);
ValueAxis domain = xyPlot.getDomainAxis();
domain.setVisible(false);

Categories