I want to create a chart using mysql database with Jfreechart, this is the code i used :
try {
String query =
"Select id,Prop_menages_Urbains_Proprietaires from fes";
JDBCCategoryDataset dataset =
new JDBCCategoryDataset(
DBConnection.DBConnection(),
query
);
JFreeChart chart =
ChartFactory.createBarChart(
"test",
"id",
"Prop_menages_Urbains_Proprietaires",
dataset,
PlotOrientation.VERTICAL,
false,
true,
true
);
BarRenderer render = null;
CategoryPlot plot = null;
render = new BarRenderer();
ChartFrame fram =
new ChartFrame("test", chart);
fram.setVisible(true);
fram.setSize(600,650);
}
catch(Exception ex) {
System.out.println("Erreur: " + ex);
}
after the execution the barchart doesn't show up although there are no errors in the code syntax
any help please ?
UPDATE: i tried the same code but this time i used other columns from the database and it worked , what's the issues with the other one knowing that they're all numbers
The JDBCCategoryDataset API specifies:
The SQL query must return at least two columns. The first column will be the category name and remaining columns values (each column represents a series).
This complete example, which you have successfully adapted, illustrates the effect. The question then becomes,
What's the issues with the other one knowing that they're all numbers?
The executeQuery() source shows how each of the java.sql.Types is mapped to a numeric value: a numeric type becomes a Number directly, a date type becomes a Number representing milliseconds from the Java epoch, and a character type is converted using Double.valueOf(); the default is null.
In this case, verify that Prop_menages_Urbains_Proprietaires has a SQL type that is suitable for conversion to a numeric value.
Related
I have a JTable bound with my database using Netbeans Wizard. Everything works fine but when I try to change the query and results based on filtering the table stops displaying the new results.
private static void updateResults() {
if (complaintList != null) {
LOG.log(Level.FINE, getQuery());
complaintList.clear();
complaintQuery = entityManager.createQuery(getQuery())
.setMaxResults(1000);
complaintList = complaintQuery.getResultList();
LOG.log(Level.INFO, "Result size: {0}", complaintList.size());
complaints.firePropertyChange(null, true, false);
}
}
Where:
complaintList is the list containing the results which is bound to the table.
complaintQuery is the bound Query.
I verified that the result has a size > 0. The contents of the table only update when I click/move one of the scroll bars.
Might not be pretty, but made it work by selecting the last row on the table after the change.
if (!complaintList.isEmpty()) {
complaints.changeSelection(complaintList.size() - 1, 0,
false, false);
}
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);
I am learning java and I am trying to build an app. I'm stuck with this one last part of the application and was hoping some of you may be able to help me. The application stores values in a database and, upon the users request, it will retrieve the data and plot this data on a line graph. The application is producing a line graph, but my issue is that it is producing one line graph for each piece of data that it retrieves from the database. So if the query returns 15 results, the application produces 15 graphs with one plot each. I want all of the data retrieved and plotted onto one graph. Below is my code. Can someone point me in the right direction?
try
{
Connection con = new DataConnection().connect();
ResultSet rs;
PreparedStatement retrieve = con.prepareStatement("SELECT row FROM table");
rs = retrieve.executeQuery();
while (rs.next())
{
String string = rs.getString(1);
double double = Double.parseDouble(string);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(double, "Chart", "Data");
JFreeChart chart = ChartFactory.createLineChart("Graph", "Data", "Data", dataset, PlotOrientation.VERTICAL, true, false, false);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.black);
ChartFrame frame = new ChartFrame("Line Chart", chart);
frame.setVisible(true);
frame.setSize(450, 350);
}
}
So if the query returns 15 results, the application produces 15 graphs
with one plot each. I want all of the data retrieved and plotted onto
one graph.
Your code creates a new JFreeChart instance for each row in your database query result set and that's why you get too many frames with charts opened. You have to create just a single JFreeChart instance and add the data to its model as a series.
See this related Q&A: Multiple graphs in multiple figures using jFreeChart. There are also plenty of examples under jfreechart tag.
I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer. I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation. My code is as follows:
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
try {
Object table = xNamedTables.getByName(tblName);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
if(flag){
XColumnRowRange xCollumnAndRowRange =(XColumnRowRange)
UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
XTableRows rows = xCollumnAndRowRange.getRows();
System.out.println("Testing if this works");
rows.insertByIndex(4, size-4);
}
I am not sure if I am missing something here or if I should be using a different function.
As Lyrl suggested, this works:
XTableRows rows = xTable.getRows();
Apparently XColumnRowRange is only used for spreadsheets.
Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface. The code would simply be:
table = tables.getByName(tblName)
rows = table.getRows()
I am creating a multiple column JMesa table. I want to pre-sort the table on a specified column. How can I achieve this?
You have to call addSort() on SortSet, then associate the SortSet with the TableModel. Sample code:
SortSet mySortSet = new SortSet() {};
mySortSet.addSort("your column property here", Order.ASC);
Limit limit = TableModelUtils.getLimit("table model id here", request, itemsList);
limit.setSortSet(mySortSet);
model.setLimit(limit);