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:
Related
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 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).
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 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);
This might sound silly, but I'd only want to display bars in a JfreeChart and a transparent background. All examples show how to create a PNG with transparent background but this is not what I want, I just want to display it, no need to create it.
Besides, I find no way to "disable" text in horizontal and vertical axis. I just want the lines for the axis and the bars, simple as that. Here's the code:
private static JFreeChart createActivityBarGraph(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
null, // chart title
null, // domain axis label
null, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart to be transparent
chart.setBackgroundPaint( new Color(255,255,255,0) );
chart.setBorderVisible(false);
CategoryPlot cPlot = chart.getCategoryPlot();
cPlot.setBackgroundPaint( new Color(255,255,255,0) );
cPlot.setBackgroundAlpha(0.0f);
cPlot.setDomainGridlinePaint(Color.white);
cPlot.setDomainGridlinesVisible(false);
cPlot.setRangeGridlinePaint(Color.white);
cPlot.setOutlineVisible(false);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) cPlot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
BarRenderer renderer = (BarRenderer) cPlot.getRenderer();
renderer.setDrawBarOutline(true);
// set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64)
);
renderer.setSeriesPaint(0, gp0);
CategoryAxis domainAxis = cPlot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
That said, any help is appreciated.
You can use setTickLabelsVisible(false) and setTickMarksVisible(false) to hide the text on the axes.
try this
chart.getCategoryPlot().setBackgroundPaint(new Color(0,0,0,0));