Ranges in bar graph JFreeChart - java

I have created a graph with JFreeChart and I've been trying to change the ranges on the Y-axis.
I need it to go from 1 to 10, but with intervals of 1 unit. (1,2,3,...,9,10)
I haven't been able to find a way for that, can anyone help me?

Try this it will work
CategoryPlot p = chart.getCategoryPlot();
final NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis();
rangeAxis.setTickUnit(new NumberTickUnit(10));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

Related

JFreeChart Candlestick zoom into last n candles on creation

I have an app and I somehow successfully managed to get a candlestick chart visible. However, once the chart is displayed, it shows all the candles (can be several hundred) which causes the chart to be very tiny and zoomed out. What I actually want is, the chart to be zoomed into the range of the last N candles so it looks more visible to the eye.
Here is how my charts look once they get created
I tried working with setRange, the Range on the Date Axis returns values in the format of [09.02.2023, 23:21:02 --> 10.02.2023, 08:29:56]
I dont seem to get how to make the chart like zoom in to a range of for example range.length-100 -> range.length. Whatever I tried, I always end up somewhere randomly without any data really shown.
Is there a way to define the last n entries on the range and set the dateAxis to start showing this range?
What I actually want to see after creation is something like this
private void displayChart(String title, String quoteMarket, BarSeries series) {
OHLCDataset dataSet = createOHLCDataset(title,series);
JFreeChart chart = ChartFactory.createCandlestickChart(
title,
"Time",
quoteMarket,
dataSet,
false);
chart.setBackgroundPaint(new Color(28, 39, 57));
chart.setBorderVisible(false);
chart.getTitle().setPaint(Color.WHITE);
// Candlestick rendering
CandlestickRenderer renderer = new CandlestickRenderer();
renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_INTERVALDATA);
renderer.setUpPaint(new Color(0, 214, 110));
renderer.setDownPaint(new Color(196, 19, 30));
renderer.setSeriesPaint(0,new Color(211, 211, 211));
renderer.setCandleWidth(3.0);
XYPlot plot = chart.getXYPlot();
plot.setRenderer(renderer);
plot.setRangeGridlinePaint(Color.lightGray);
plot.setBackgroundPaint(new Color(28, 39, 57));
plot.setDomainGridlinesVisible( false );
plot.setRangeGridlinesVisible( false );
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
numberAxis.setAutoRangeIncludesZero(false);
numberAxis.setLabelPaint(Color.WHITE);
numberAxis.setTickLabelPaint(Color.WHITE);
numberAxis.setAutoRangeIncludesZero(false);
DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
dateAxis.setAutoRange(true);
dateAxis.setLabelPaint(Color.WHITE);
dateAxis.setTickLabelPaint(Color.WHITE);
dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
panel.setMouseZoomable(true);
panel.setRangeZoomable(true);
panel.setDomainZoomable(true);
this.add(panel);
System.out.println(dateAxis.getRange());
}

Unable to hide y axis line in a stacked bar graph

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.

doing a line graph with jfreechart in excel file

I am doing a lineal graph in java with JFreeChart, i was checking in internet and this is that i have at the moment:
DefaultKeyedValues data = new DefaultKeyedValues();
data.addValue("01/04/2012",7);
data.addValue("01/05/2012",5);
data.addValue("01/06/2012",6);
data.addValue("01/07/2012",2);
CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data);
JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false);
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseLinesVisible(true);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRenderer(0, renderer);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
CategoryAxis axis = plot.getDomainAxis();
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
The problem comes when I try to insert more categories in the linear graph, I don't know how to do it. I need to do 3 categories more.
actual graph
I need use DefaultKeyedValues or some series collection that let me use String param with double/int param. I want to make a generalized method that passes different formats of dates, and the method always draw a graph. In the photo of the example I am passing a date range 'dd-MM-yyyy' but later I will pass a format 'MM-yyyy'.
Edit: Okey i find this example in internet that solves my problem.
enter link description here

JFREECHART line chart : Want X axis to be collection of date ranges

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

JFreeChart: set minimum X-tick interval in the chart

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.

Categories

Resources