After installing and configurations jfreechart library now proceed to see the different types of graphics we can create with this program for it will place some examples that have been found in JFreeChart demo library and on the web.
First of all let's create a new project with the NetBeans → then add the libraries that previously had set up our project for this: right-click the Libraries folder → Add Library and add the JFreeChart libraries:
Now we can already begin to create our graphics (this is going to do for each project you want to use the library JFreeChart).
GRAPHIC BARCHART |
- and we create a class that inherits from the Application class frame, so we must import the library: import org.jfree.ui.ApplicationFrame and this should look like: public class BartChart
extends ApplicationFrame
- done this we will import the following libraries :
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
- Let's create our constructor: public
BartChart (String title)
{super (title);
crearDataset CategoryDataset dataset = ();
JFreeChart chart = crearChart (dataset);
ChartPanel panel = new ChartPanel (chart, false);
panel.setPreferredSize (new Dimension (700.500));
this.setContentPane (panel);}
- NetBeans will ask us to believe two methods are crearChart crearDataset and methods own that we create and we will be completing later:
CategoryDataset private static crearDataset () {
........
........
return dataset;}
crearChart private static JFreeChart (CategoryDataset data) {
........
........
return chart;}
- Now let's fill our method crearDataset:
CategoryDataset private static crearDataset () {
/ / Name String serial1
rows = "First"; String
Series2 = "Second" 3 series
String = "Third";
/ / Name String category1
columns = "Category1" String
Category2 = "Category2";
categoria3 String = "Categoria3" ;;
categoria4 String = "Categoria4"
categoria5 String = "Categoria5"
DefaultCategoryDataset
DefaultCategoryDataset dataset = new ();
/ / Here we fill our data
/ / but this data We can pass parameters dataset.addValue
(1.0, serial1, category1);
dataset.addValue (4.0, serial1, Category2);
dataset.addValue (3.0, serial1, categoria3)
dataset.addValue (5.0, serial1, categoria4)
dataset.addValue (5.0, serial1, categoria5)
dataset.addValue (5.0, Series2, category1);
dataset.addValue (7.0, Series2, Category2);
dataset.addValue (6.0, Series2, categoria3 )
dataset.addValue (8.0, Series2, categoria4)
dataset.addValue (4.0, Series2, categoria5)
dataset.addValue (4.0, 3 series, category1);
dataset.addValue (3.0, 3 series, Category2);
dataset.addValue (2.0, 3 series, categoria3)
dataset.addValue (3.0, 3 series, categoria4)
dataset.addValue (6.0, 3 series, categoria5)
dataset.addValue (1.0, serial1, category1);
dataset.addValue (4.0, serial1, Category2);
dataset.addValue (3.0, serial1, categoria3)
dataset.addValue (5.0, serial1, categoria4)
dataset. AddValue (5.0, serial1, categoria5)
return dataset;}
- We will fill the following method, I hope that the comments are clear (if you want to change the direction of the bars we put PlotOrientation.VERTICAL laying in the example):
crearChart private static JFreeChart (CategoryDataset data)
{JFreeChart chart = ChartFactory
. createBarChart (
"BartChart Demo", / / \u200b\u200bName of the graph
"Category", / / \u200b\u200bName
Horizontal axis "values", / / \u200b\u200bName
vertical axis data, / / \u200b\u200bData
PlotOrientation.HORIZONTAL, / / \u200b\u200bOrientation
true, / / \u200b\u200bInclude
legend true, / / \u200b\u200binformation when mouse
true) / / URls
/ / Change the color of the bottom panel chart.setBackgroundPaint
(Color.ORANGE)
/ / Change the color of each category
CategoryPlot plot = (CategoryPlot) chart.getPlot ();
plot.setBackgroundPaint (Color.Blue)
/ / background color graphic plot.setBackgroundPaint
(Color.white);
/ / dividing lines
plot.setDomainGridlinesVisible (true);
plot.setRangeGridlinePaint (Color.Black);
/ / Calculation of the x-axis final
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis ();
rangeAxis.setStandardTickUnits (NumberAxis.createIntegerTickUnits ());
Sweepers renderer = (sweepers) plot.getRenderer ();
renderer.setDrawBarOutline (false);
/ / Change the color of each bar
GradientPaint GP0 = new GradientPaint (0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color (0,0,64));
GradientPaint gp1 = new GradientPaint (0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color (0,64,0));
GradientPaint gp2 = new GradientPaint (0.0f, 0.0f, Color.Red, 0.0f, 0.0f, new Color (64,0,0));
renderer.setSeriesPaint (0, GP0)
renderer.setSeriesPaint (1, gp1);
renderer.setSeriesPaint (2, gp2);
plot.getDomainAxis
CategoryAxis domainAxis = ();
domainAxis.setCategoryLabelPositions (
CategoryLabelPositions.createUpRotationLabelPositions (Math.PI/6.0));
return chart;}
- Finally, in our main class write the following: import
org.jfree.ui.RefineryUtilities;
public class Main {
public static void main (String [] args) {
BartChart
demo1 = new BartChart (BarChartDemo01 ");
demo1.pack ();
RefineryUtilities.centerFrameOnScreen (demo1)
demo1.setVisible (true);}
}
- This is the final result:
- The example can be downloaded from here:
done this
0 comments:
Post a Comment