How to display jfreechart at specified place in jsp - java

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);

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:

POI Line chart axis position and other issues

I'm dealing with POI to create charts and I've some issues.
First, even if I change the AxisPosition I always have x-axis at bottom and y-axis at left. Second I would like to have 2 y-axis. Here's a part of my code:
Function to create the chart:
private XSSFChart createChart(Sheet sheet,int[] anchors, String xLabel, String yLabel, String title){
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(anchors[0], anchors[1], anchors[2],
anchors[3], anchors[4], anchors[5], anchors[6], anchors[7] );
XSSFChart chart = (XSSFChart) drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.TOP);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
ValueAxis rightAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.RIGHT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
//Function to set axis title took from how-to-set-axis-labels-in-a-line-chart-using-apache-poi
setValueAxisTitle(chart,0,yLabel);
setValueAxisTitle(chart,0,"Second Axis");
setCatAxisTitle(chart,0,xLabel);
chart.setTitle(title);
chart.getAxis();
return chart;
}
Code to plot chart
XSSFChart chart1=createChart(sheet,anchor1,"Temps","Courbes",title );
LineChartData dataM3s = chart1.getChartDataFactory().createLineChartData();
ChartDataSource<Number> yM3s = DataSources.fromNumericCellRange(sheet,
new CellRangeAddress(3, (numOfRows / numOfColumns) + 2, colToWrite, colToWrite));
LineChartSeries chartSeriesM3s = dataM3s.addSeries(xs, yM3s);
chartSeriesM3s.setTitle(metric.getLibelle());
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(1));
//By the way, this works as well:
//chart1.plot(dataM3s, new ChartAxis[] { chart1.getAxis().get(0), chart1.getAxis().get(1)});
When I change
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(1));
by
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(2));
I can't open my xlsx file (and as soon as try to create a second value axis): i got message like "unreadable content". sometimes excel suceed in reparing and tells me it repared the drawing shape in /xl/drawings/drawing1.xml. I got this error: error062800_01.xml but not familiar with XML.
If anyone has some idea

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);

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.

Only bars in a JfreeChart graph

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));

Categories

Resources