jfreechart histogram with dates - java

I want to display some dates in the X axis of a histogram chart, but i don't understand how i can do it
with this code i can create a simple histogram with couples of x-y values, but they can olny be numbers, not date:
DefaultTableXYDataset dataset = new DefaultTableXYDataset();
XYSeries serie = new XYSeries("Andamento consumi", true, false);
serie.add(30, 8.3);
serie.add(31, 7.1);
serie.add(1, 8.7);
serie.add(2, 6.0);
serie.add(3, 11.9);
dataset.addSeries(serie);
JFreeChart chart = ChartFactory.createHistogram("Grafico di prova", "Giorni", "Consumi", dataset, PlotOrientation.VERTICAL,true,true,true);
ChartFrame frame = new ChartFrame("Titolo finestra", chart);
frame.pack();
frame.setVisible(true);
Is there a way to insert dates instead of numbers?

If you are dealing with dates use a TimeSeriesCollection or TimePeriodValuesCollection dataset instead of DefaultTableXYDataset.

Related

JFreeChart large dataset

I'm creating a bar chart for my database. Everything works except one thing: the data from DB is large, so the bars look tiny. I used scroll pane, but that didn't work. Here is my chart creator:
JFreeChart barChart = ChartFactory.createBarChart(
"du "+du+" a "+a,
"date",
"Score",
categoryDataset,
PlotOrientation.VERTICAL,
true, true, true);
Here is my chart panel:
chartPanel = new ChartPanel( chart );
chartPanel.setMaximumDrawWidth(1000);
chartPanel.setPreferredSize(new Dimension(2000, 1000));
chartPanel.revalidate();
chartPanel.repaint();
chartPanel.setBounds(1, 1, 680, 420);
Here is a picture for clarification:

How show less values in the X-axis in line chart (JFreechart)?

How can I do in order to show less values ​​on the X axis, to get the data organized?
However I want the values ​​are all shown. One idea I had was to use the zoom so you can see all the records, but do not know if it was a good idea, or rather do not even know it.
As you can see in the picture my idea is that data more legible, in order to realize the best of everything.
Does anyone can help me please?
//Code with result from query and chart create
String query="select (CONCAT(`date`, ' ', hour)), temperature from records where idTermomether like 'T1' and date between '2014-06-01' and '2014-06-03'";
JDBCCategoryDataset dataset = new JDBCCategoryDataset (CriaConexao.getConexao(),query);
JFreeChart chart = ChartFactory.createLineChart(" - Temperature", "Date", "Temperature", dataset, PlotOrientation.VERTICAL, false, true, true);
BarRenderer renderer = null;
CategoryPlot plot= chart.getCategoryPlot();
CategoryAxis xAxis=(CategoryAxis)plot.getDomainAxis();
xAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
renderer=new BarRenderer();
ChartFrame frame = new ChartFrame("Temperature", chart);
frame.setVisible(true);
frame.setSize(400,650);

JFreechart XYline connect with database

I created a graph with connection to the database as follows:
String s = jTSensor.getText();
String query="select date, dew_point from records where idSensor like '"+s+"'";
JDBCCategoryDataset dataset = new JDBCCategoryDataset (
CriaConexao.getConexao(),query);
JFreeChart chart = ChartFactory.createLineChart(
"Records", "Date", "Dew Point", dataset,
PlotOrientation.VERTICAL, false, true, true);
BarRenderer renderer = null;
CategoryPlot plot= null;
renderer=new BarRenderer();
ChartFrame frame = new ChartFrame("Records", chart);
frame.setVisible(true);
frame.setSize(400,650);
But only gives a line to show. I wish it was possible to seek other data to the database and show the results with other lines, but I'm not succeeding. Someone can help me please.
Greetings
Look at How to display line graph using JFreeChart in jsp? and build your solution around createXYLineChart. For database access there is already a JDBCXYDataset. XYDataset supports multiple series of data while the CategoryDataset you are useing does not (as far as I know).

How to fixate the domain range of a JFreeChart XY diagram?

I have a JFreeChart chart, which displays measurements from a sensor. The diagram should show how those values change over time.
I create the diagram using following code:
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
"Pulse sensor data", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME 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.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setDrawSeriesLineAsPath(true);
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
There is a problem with this implementation - the domain axis constantly changes, hence, the representation of the signals changes as well. Therefore, it is hard to tell visually whether the signal has changed significantly.
At the start of a measurement session the domain ranges from 0 to approx. 550.
After some time I get more data and now the maximum value is 35000.
If we wait longer, it changes again (actually, with every received measurement a litlle).
How can I change the code such that the diagram shows last X measurements (i. e. the size of the domain axis remains constants) and kind of slides over the time?
Update 1 (01.06.2013 18:06 MSK): I changed the code for adding a new data item to
sensorSeries.add(new Millisecond(new Date()), voltage);
where voltage is the new sensor value.
The code for setting up the chart has also changed:
dataset = new TimeSeriesCollection();
sensorSeries = new TimeSeries("Pulse sensor data");
sensorSeries.setMaximumItemAge(12500);
dataset.addSeries(sensorSeries);
final JFreeChart chart = createChart(dataset);
[...]
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Pulse sensor data", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
final XYPlot plot = chart.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
final ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setAutoRange(true);
return chart;
}
This works fine except that I sometimes get following exception:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1201, Size: 1201
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.jfree.data.time.TimeSeries.getRawDataItem(TimeSeries.java:422)
at org.jfree.data.time.TimeSeries.getTimePeriod(TimeSeries.java:454)
at org.jfree.data.time.TimeSeriesCollection.getXValue(TimeSeriesCollection.java:428)
at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawPrimaryLine(XYLineAndShapeRenderer.java:987)
at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:913)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3828)
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:1672)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
Any ideas how to fix it?
How can I execute adding the data in the event dispatch thread?
As shown here, SwingWorker is ideal for this. Poll or listen to your data source in the worker's doInBackground() method and update the chart's model in process(), which executes on the event dispatch thread.

How to display jfreechart at specified place in jsp

I have a jsp page where i need to display the chart at specified position .But With the following codes chart is getting loaded as png image and all other css design elements of the jsp page is not displaying....
Below is my code.
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("STD", 10);
dataset.setValue("Local", 15);
dataset.setValue("ISD", 47);
dataset.setValue("Inet", 20);
JFreeChart chart = ChartFactory.createPieChart3D(
"Calls by Type", // Title
dataset, // Data
true, // Yes, display the legend
true, // No, don't display tooltips
true // No, no URLs
);
PiePlot3D plot4 = (PiePlot3D) chart.getPlot();
plot4.setForegroundAlpha(0.6f);
plot4.setBackgroundPaint(Color.cyan);
PiePlot piePlot = (PiePlot) chart.getPlot();
StandardPieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0} {1} {2}");
piePlot.setLabelGenerator(labelGenerator);
piePlot.setLegendLabelGenerator(labelGenerator);
response.setContentType("html");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 400, 300);

Categories

Resources