How to display current time in DynamicTimeSeriesCollection in Jfreechart? - java

In my application i want to display current time in X axis of JFreechart. But it starts with
00:00:00 . But actually i want like 06:15:30PM
I am setting timebase like this,
final DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(1, 2 * 60, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
And setting DomainAxis like this,
final XYPlot plot = Chart.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
My output is,

Date date = new Date();
Dataset.setTimeBase(new Second(date));
By using the above statements I resolved the problem.

Related

JFreeChart hiding (some) x axis values

I'm currently using JFreeChart to create a line chart. This line chart gets updated every second with a new value (currently a random value). This way you can see how your data has changed over a certain time period. However after I've added over ten values they don't fit on the line anymore turning into dots. I would like to only have 5 values shown at a time which are spread across the entire timings. This is how it looks now:
Notice the dots at the bottom of the chart. I would like it to be changed to this:
Note that I want to keep all points which are created between these points in time. So data from 11:31:00, 11:31:01, 11:31:02 etc. should be still there.
This is what I currently have:
LocalDateTime date = LocalDateTime.now();
category = new DefaultCategoryDataset();
category.addValue(new Random().nextInt(10), "Data", date.getHour() + ":" + date.getMinute() + ":" + date.getSecond());
chart = ChartFactory.createLineChart("Values", "Time", "Data", category, PlotOrientation.VERTICAL, false, true, false);
((NumberAxis) ((CategoryPlot) chart.getPlot()).getRangeAxis()).setStandardTickUnits(NumberAxis.createIntegerTickUnits());
I got it by using a TimeSeriesChart. This is what I ended up with:
TimeSeriesCollection collection = new TimeSeriesCollection();
TimeSeries serie = new TimeSeries("Data");
collection.addSeries(serie);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Data", "Time", "Data", collection, false, true, false);

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.

Java JFreeChart Category Step Chart horizontal (image to explane)

i need the following type of chart:
It should be a "steped" line chart with categories on the vertical axis, like this:
I found this example of an Category Step Chart, but it the orientation is not right for my purpose.
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/renderer/category/CategoryStepRenderer.html
All i have done so far is this, but as you can see the red line does not fit to the orientation of the chart(should be horizontal):
The corresponding code to this:
DefaultCategoryDataset ds = new DefaultCategoryDataset();
// create dataset
for (int k = 0; k < ffCount; k++) {
StateSignal ss1 = (StateSignal) this.ffDSet.getFframes().get(k).getSignals().get(i);
ds.setValue((double) k + 1, ss1.getName(), ss1.getStates().get(0).getStatus());
}
CategoryStepRenderer categorysteprenderer = new CategoryStepRenderer(false);
categorysteprenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
CategoryAxis categoryaxis = new CategoryAxis("Category");
NumberAxis numberaxis = new NumberAxis("Value");
CategoryPlot categoryplot = new CategoryPlot(ds, categoryaxis, numberaxis, categorysteprenderer);
categoryplot.setRangePannable(true);
categoryplot.setOrientation(PlotOrientation.HORIZONTAL);
chart = new JFreeChart("test", null, categoryplot, true);
I donĀ“t get it to work. Any ideas?
Thanks in advance!
It looks like you need to use a standard XYLineChart with a XYStepRenderer and a SymbolAxis to replace the default Range Axis rather than a CategoryStepRenderer and a horizontal plot orientation
If you associate Status A and B with a numerical value say 1 and 2 you can create a chart like this:
Using this a XYStepRenderer
XYStepRenderer renderer = new XYStepRenderer();
renderer.setBaseShapesVisible(true);
renderer.setSeriesStroke(0, new BasicStroke(2.0f));
renderer.setSeriesStroke(1, new BasicStroke(2.0f));
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
renderer.setDefaultEntityRadius(6);
plot.setRenderer(renderer);
and a Symbol Axis
String[] grade = new String[3];
grade[0] = "";
grade[1] = "Status A";
grade[2] = "Status B";
SymbolAxis rangeAxis = new SymbolAxis("", grade);
rangeAxis.setTickUnit(new NumberTickUnit(1));
rangeAxis.setRange(0,3);
plot.setRangeAxis(rangeAxis);
In this example the SymbolAxis provides an alternative label for each value in the Axis

Well displaying the Y-axis on jFreechart

Please how can I change the Y-axis to have data displayed with the same number of digits.
This is what I have on my graph
and this is the code that produces that part of the graph
final CategoryPlot plot = (CategoryPlot)result.getPlot ();
plot.setBackgroundPaint(Color.BLACK);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange (1.2, 1.3);
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits ());
rangeAxis.setTickUnit (new NumberTickUnit(0.005));
As you can see, I fix the begin at 1.2 and the end at 1.3 and I define the TickUnit at 0.005. So normaly (I think) I should have something gradualy like :
1.2 1.205 1.210 ... 1.305
This is what I would like to obtain
How can I modify the code to obtain the Y-axis with gradues values ?
Thanks
I think I have found this solution
DecimalFormat df = new DecimalFormat("0.000");
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange (1.200, 1.300);
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits ());
rangeAxis.setTickUnit (new NumberTickUnit(0.005, df, 0));
it seems to work fine.
Thanks
The chart axis will accept a java.text.NumberFormat instance, which it will use to format the labels. To do this, try adding these lines to your code:
DecimalFormat newFormat = new DecimalFormat("0.000");
rangeAxis.setNumberFormatOverride(newFormat);

Date Chart in aChartEngine

I have edited my question...
Here is the code I am using, I am adding weight of an animal with time.
else if (str.equals("Weight"))
{
mDbHelper.open();
wtArray = mDbHelper.getWeight();
mDbHelper.close();
TimeSeries diaSeries = new TimeSeries("Weight");
for ( int i =0; i <wtArray.size(); i++)
{
Weight wt = wtArray.get(i);
diaSeries.add(wt.date, wt.weight);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(diaSeries);
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
renderer.setFillPoints(true);
mrenderer.addSeriesRenderer(renderer);
graphLayout.addView(ChartFactory.getTimeChartView(this, dataset, mrenderer, "MM/dd/yyyy"));
}
It never shows any value on x axis, i-e time. I am using LinearLayout. Kindly tell me what is wrong. Also no Graph line is showing.
Secondly, What If I want to show data from specific date to another date?
You have two options here:
You keep using a LineChart and add custom labels while disabling the regular ones:
renderer.setXLabels(0);
renderer.addXTextLabel(date.getTime(), "Label");
You use a TimeChart which is a LineChart displaying time values using a format provided by you, instead of the LineChart.
umar.
Check my following code. I use a TimeChart here.
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
// mDateFrom and mDateTo are Date Objects
// Set the date span here
setChartSettings(renderer, "title", "x_tiele", "y_title",
mDateFrom.getTime(),
mDateTo.getTime(), 0, 1, Color.GRAY, Color.LTGRAY);
XYMultipleSeriesDataset dataset = buildDateDataset(titles, mHashMapGroupedEntites);
return ChartFactory.getTimeChartView(context, dataset,
renderer, "yyyy/MM/dd HH"+":00:00");
Try this.
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(diaSeries);
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
mrenderer.setXAxisMin(YOUR_DATE_MIN);
mrenderer.setXAxisMax(YOUR_DATE_MAX);
// XYSeriesRenderer renderer = new XYSeriesRenderer();
mrenderer.setColor(Color.RED);
mrenderer.setPointStyle(PointStyle.DIAMOND);
mrenderer.setFillPoints(true);
// mrenderer.addSeriesRenderer(renderer);
graphLayout.addView(ChartFactory.getTimeChartView(this, dataset, mrenderer, "MM/dd/yyyy"));

Categories