How to construct and use TimeSeriesCollections - java

I want to display some dates in the X axis of a chart, and here it is said that i have to use a TimeSeriesCollections object
It seems that i have to add a TimeSeries to the TimeSeriesCollections, and that the TimeSeries has to be constructed using a RegularTimePeriod...
I am a bit confused...
Can you please explain me what i have to do?
If possible can you provide some example code?
Thanks

TimeSeriesCollections are made up of TimeSeries objects
Use this method to add series to the dataset: addSeries(TimeSeries series)
When creating TimeSeries objects. Fill them with the time and values. Here is a rough example:
TimeSeries ts= new TimeSeries("Name of Series");
ts.addOrUpdate(new Year(2008), 42);
ts.addOrUpdate(new Year(2009), 51);
ts.addOrUpdate(new Year(2010), 97);
ts.addOrUpdate(new Year(2011), 45);
For getting the Axis to display the dates nicely, you will have to do something like this:
XYPlot plot = chart.getXYPlot();
DateAxis axis = new DateAxis();
plot.setDomainAxis(axis);
axis.setDateFormatOverride(new SimpleDateFormat("yyyy"));

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());
}

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

Getting Assigned Paint for TimeSeries

I am obviously not understanding the documentation for the getSeriesPaint method. I have the TimeSeries object and I want to get the color being used to render it. However, it seems like I am in a catch-22. I need to know the series index (getIndex), but to find that I need to know the series time period. However, to find the series time period, I need to know the index. I'm looking to do something like this:
Color color=(Color) r1.getSeriesPaint(arg0);
where r1 is the XYLineAndShapeRenderer. What do I use for arg0 given the TimeSeries object?
Because XYLineAndShapeRenderer is an XYItemRenderer, it invokes the AbstractRenderer method getItemPaint(), which "Returns the paint used to color data items as they are drawn." Note that "The default implementation passes control to the lookupSeriesPaint() method." Starting from this example, the following fragment obtains the dataset and renderer from the chart. It then enumerates the series paints—shades of red and blue seen in the image:
JFreeChart chart = chartPanel.getChart();
XYPlot plot = (XYPlot) chart.getPlot();
TimeSeriesCollection tsc = (TimeSeriesCollection) plot.getDataset();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
for (int i = 0; i < tsc.getSeriesCount(); i++) {
System.out.println(renderer.lookupSeriesPaint(i));
}
Console:
java.awt.Color[r=255,g=85,b=85]
java.awt.Color[r=85,g=85,b=255]
Alternatively, consider a custom DrawingSupplier, mentioned here.

JFreeChart relative scaling

I want to create a Linegraph with JFreechart. My Data is like :
Number1,Number2,Timestamp
So I want two Lines Where the Y-Axis is Number1 and Number2 and the X-Axis it the Timestamp.
I save the Chart as a png Image and the X-Axis looks just to long I think it is because of the timestamp beeing long apart.
I Searched google and Stackoverflow but answers like this dont work for me. How can i Fix this?
Here is my code:
TimeSeries ts = new TimeSeries("Systole");
ts.add(new Minute(new Date(1356999660000L)), 141);
ts.add(new Minute(new Date(1356999840000L)), 129);
ts.add(new Minute(new Date(1433074800000L)), 146);
ts.add(new Minute(new Date(1433075700000L)), 136);
TimeSeries diast = new TimeSeries("Diastole");
diast.add(new Minute(new Date(1356999660000L)), 95);
diast.add(new Minute(new Date(1356999840000L)), 86);
diast.add(new Minute(new Date(1433074800000L)), 98);
diast.add(new Minute(new Date(1433075700000L)), 91);
final XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(systole);
dataset.addSeries(diastole);
XYDataset data = dataset;
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(ts);
dataset.addSeries(diast);
XYDataset data = dataset;
//String title, String timeAxisLabel, String valueAxisLabel, XYDataset dataset
JFreeChart chart = ChartFactory.createTimeSeriesChart("Blutdruck", "", "", data);
SaveImage(chart.createBufferedImage(500,500),"Test1.png");
The "vertical lines" effect happens because you are plotting a series that fluctuates on very different scales (minutes vs. years). Obviously, the variations at the scale of a minute are completely invisible when plotting them over a period of two years. It is like trying to draw the streets of a city on the map of a country.
There is no out-of-the-box solution for that problem, but you could find inspiration from the typical share price plots (from Google or Yahoo), where they let the user choose the scale they want to use.
Variation over five days:
Variation over three months:
Note that the variation over three months is smoother than the variation over five days. The reason is that they do not try to plot the value at every minute. Instead, they "subsample" their data.
In other words, they take the average over one day and plot only the values for each day.

JFreeChart Margin

I am using JasperReports to create a line chart for my webapps.
I have successfully passed the dataset to the compiled report (created in iReport) and can see the data correctly.
However, I want to do some customization on the margin.
The value shown on the line chart is trimming for the highest value as there is no margin.
The X-Axis label is coming after few empty space from Y-Axis 0 value. I want to remove that margin and start the X-Axis from very close to the meeting point of X & Y.
Please see the picture:
I am using customized class which is defined in my webspps. I am able to change the font size and rotation of the label but don't know how to adjust margin.
public class LineChartCustomizer implements JRChartCustomizer {
#Override
public void customize(JFreeChart jFreeChart, JRChart jrChart) {
CategoryPlot plot = jFreeChart.getCategoryPlot();
DecimalFormat dfKey = new DecimalFormat("###,###");
StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator("{2}", dfKey);
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelGenerator(labelGenerator);
renderer.setBaseItemLabelFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 4));
renderer.setSeriesShape(0, ShapeUtilities.createDiamond(1F));
plot.setRenderer(renderer);
}
}
I think* you're looking for ValueAxis#setUpperMargin(double) and CategoryAxis#setLowerMargin(double). You can get the CategoryAxis and ValueAxis from plot.getDomainAxis() and plot.getRangeAxis(). Note that the margins are a percentage of the axis length and not pixel values.
* I'm not familiar with JasperReports, but it seems a little strange that you have a CategoryPlot in hand as opposed to an XYPlot. I would have expected the chart in your picture to have used an xy time series. I have only ever tested this with an XYPlot, so I'm not entirely sure how it will behave with a CategoryPlot.

Categories

Resources