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).
Related
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:
I have a table that contains some data. I know that I can access of my first row like this,
Object nameOfFile = Main_Menu.jTable3.getValueAt(0, 0);
String nameOfFileToString = nameOfFile.toString();
My table is dynamic. Sometimes my row of the table is 100, sometimes is 200. How can I get the last row of my table even my table is dynamically change.
P.S : my table is connected with a jfreechart, so I need this for my update Y axis in jfreechart like this
Object nameOfFile = Main_Menu.jTable3.getValueAt(0, 0);
String nameOfFileToString = nameOfFile.toString();
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
"Persentastion Of Similarity", // chart title
"", // domain axis label
nameOfFileToString, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
for the help, thanks
You can use JTable.getModel().getRowCount() to get the number of rows; the rest is straightforward:
Object nameOfFile = Main_Menu.jTable3.getValueAt(jTable3.getModel().getRowCount()-1, 0);
// go on with your code
Thank you so much for all of you.
This code is rock
Object nameOfFile = Main_Menu.jTable3.getValueAt(jTable3.getModel().getRowCount()-1, 0);
Now, my application is looking fine. I haven't try getrowSorter but soon I will try.
Once again, thanks...
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);
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.
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.