Create image using JFreeChart [reprint]

xiaoxiao2021-03-06  91

Create image using JFreeChart [reprint]

One: JFreechart introduction

JFreeChart is a Java tool for creating a picture. You can create the following graphics:

Pie chart (Pie Charts)

Graph (Line Charts)

Horizontal / Vertical Bar Charts

Gantt Charts

XY Plots and Scatter Plots

Time Series, High / Low / Open / Close Charts and Candle Stick Charts

Combination Charts

Pareto Charts

Bubble Charts

Wind Plots, Meter Charts and Symbol Charts

From the following address, you can see the type type of JFreeChart you can create:

http://www.jfree.org/jfreechart/samples.html

SourceForge has a JFreeChart-based project CEWOLF can be easily created in the JSP / Servlet. JFreeChart (2003-05-08) version is 0.98 I hope to get detailed information or download JFreeChart, please visit the following sites:

http://www.jfree.org/jfreechart/

2: Special explanation:

JFreeChart is an open source project, but the documentation requires 40 US dollars to buy. There is also a very important issue. If JFreeChart uses Chinese, the Chinese who uses the default font display will be very vague, you may need to modify the source code. Let me take a few simple examples to show how to use JFreeChart creation pictures that may import the following classes in development.

Import com.jrefinery.Chart.ChartFactory;

Import com.jrefinery.Chart.Chartutilities;

Import com.jrefinery.Chart.jfreechart;

Import com.jrefinery.Chart.TextTitle;

Import com.jrefinery.chart.axis.numbers;

Import com.jrefinery.Chart.plot.categoryPlot;

Import com.jrefinery.Chart.Plot.pieplot;

Import com.jrefinery.data.day;

Import com.jrefinery.data.defaultcategoryDataSet;

Import com.jrefinery.data.defaultpiedataset;

Import com.jrefinery.data.timeseries;

Import com.jrefinery.data.timeseriescollection;

Import com.jrefinery.data.timeseriesdatapair;

After 0.98, the package is changed from com.jrefinery. * ORG.JFREE

Three: Create a pie chart

// Picture title

String Title = "Air Conditioning 2002 Market Shape";

// Set the data source

DEFAULTPIEDATASET PIEDATA = New DefaultPiedata ();

// The first parameter is name, the second parameter is Double number

Piedata.SetValue ("Lenovo", 27.3);

Piedata.SetValue ("Great Wall", 12.2);

Piedata.SetValue ("Haier", 5.5);

Piedata.SetValue ("Beautiful", 17.1);

Piedata.SetValue ("Panasonic", 9.0);

Piedata.SetValue ("科龙", 19.0); // Create JFreeChart, use ChartFactory to create JFreechart, very standard factory design mode

JFreeChart chart =

ChartFactory.createPiechart (Title, Piedata, True, True, True);

// Set the picture title

Chart.SetTitle (New TextTitle (Title, New Font ("Lishu", Font.ITAlic, 15));

///CHART.Addsubtitle (New TEXTTILE ("2002 FYRATES", New Font ("Lishu", font.italic, 12))))

// Set the background

Chart.SetBackgroundPaint (color.white);

//CHART.S

// Pie, use a PIEPLOT

PIEPLOT PIE = (PIEPLOT) Chart.getPlot ();

//pie.setsectionLabelType (PIEPLOT.NAME_AND_PERCENT_LABELS);

PIE.SETSECTIONLABELTYPE (PIEPLOT.NAME_AND_VALUE_LABELS);

/ / Set the display format (name plus percentage or value)

PIE.SETPERCENTFORMATSTRING ("#, ### 0.0 #%);

// Set a percentage display format

Pie.setBackgroundPaint (Color.White);

PIE.SETSECTIONLABELFONT (New Font ("Black Body", Font.TrueType_Font, 12));

/ / Set the background transparency (between 0-1.0)

Pie.setBackgroundAlpha (0.6F);

/ / Set the foreground transparency (between 0-1.0)

Pie.setForegroundAlpha (0.90F);

// Output file to the specified directory

String rfname = Mathutil.GetroundCode (12) ".jpeg";

String filename = "d: / test /" rfname;

Try {

// You can save the file as JPG or PNG format.

ChartUTILITIES.SAVECHARTASJPEG (New File (Filename), 100, Chart, 600, 600);

// The first parameter is the file name

// Second parameter quality

// Which Chart of the third parameter creates a picture

// Fourth width

// Fifth height

} catch (ioexception exz) {

System.out.print (".... cant't create image file";

}

In fact, using JFreeChart creation pictures is simple, different picture type differences is to set the data set

4: Creating a graph

// Create a Default Chart Based on Some Sample Data ...

// Curve header

String Title = "Trend Analysis";

/ / Curve X-axis prompt

String Domain = "Month Trend";

// Curve Y axis prompt

String Range = "Results";

// Curve from the title

String subtitlestr = "2003 FYRATA";

// Create time data source

/ / Each TimeSeries is a curve on the graph

TimeSeries CA = New TimeSeries ("UF");

For (int I = 1999; i <2005; i ) {? for (int mon = 0; MON <12; MON ) {

??? //ca.add(new Month (MON 1, I), New Double (500 math.random () * 100));

???? // TimeSeriesDataPair is a time point numerical reflection

??? ca.add (New TimeSeriesDataPair (New Day (1, MON 1, I), New Double (500 Math.random ()));

?

}

TimeSeries IBM = New TimeSeries; "Gold Disc";

For (int i = 1999; i <2005; i ) {

? for (int mon = 0; MON <12; MON ) {

??? / / (1, i), New Double (400-math.random () * 100));

??? IBM.Add (New TimeSeriesDataPair (New Day (1, MON 1, I), New Double (400 - math.random () *

100)))))));

??}

}

TimeSeries King = New TimeSeries ("East Soft");

For (int i = 1999; i <2005; i ) {

? for (int mon = 0; MON <12; MON ) {

??? / / (1, i), New Double (400-math.random () * 100));

??? King.Add (

??? New TimeSeriesDataPair (New Day (1, MON 1, I), New Double (300 - math.random () * 100))))

?

}

// Time curve data collection

TimeSeriescollection DataSet = New TimeSeriesCollection ();

DataSet.AddSeries (CA);

DataSet.Addseries (IBM);

DataSet.AddSeries (king);

//dataset.addseries(jpy);

//Dataset.addseries(mav);

// Time curve element

JFreeChart chart =

ChartFactory.createtimeSerieschart (Title, Domain, Range, Dataset, True, True, False)

// Then Customise It a little ...

TextTitle Subtitle = New TextTitle (Subtitlestr, New Font ("Black Body", Font.Bold, 12);

Chart.addsubtitle (SUBTITE);

Chart.SetTitle (New TextTitle (Title, New Font ("Lishu", Font.ITAlic, 15));

//pie.setserieslabelfont(new font ("black body", font.bold, 15));

Chart.SetBackground (New GradientPaint (0, 0, Color.White, 0, 1000, Color.Blue);

// sysout

// Output file to the specified directory

String rfname = Mathutil.GetroundCode (22) ".jpeg";

String filename = "d: / test /" rfname;

Try {

? // for

? //System.out.println ();

CHARTUTILITIES.SAVECHARTASJPEG (New File (Filename), 100, Chart, 600, 600);

? // log.info (".... CREATE Image File:" filename);

} catch (ioexception exz) {

? System.out.print (".... cant't create image file");

}

Five: Create a histogram

String title = "Column Test";

String Domain = "Unit Comparison";

String Range = "Numerical";

// categoryDataSet Data = DemodatasetFactory.createcategoryDataSet ();

DefaultcategoryDataSet Data = New Default ();

For (int R = 0; R <5; R ) {

? String Rowkey = "Unit [" (R 1) "]";

? // First layer loop: analysis object

? for (int C = 0; C <6; C ) {

??? // Second layer loop: analysis of the data on the time point

??? String columnkey = "2001" (C 1) "Month";

??? Data.addValue (New Double (R * C 5), RowKey, Columnkey);

?

}

JFreeChart Chart = ChartFactory.createVerticalBarchart (Title, Domain, Range, Data, True, True, False)

// Then Customise It a little ...

Chart.SetBackground (New GradientPaint (0, 0, Color.White, 1000, 0, Color.red);

Chart.SetTitle (New TextTitle (Title, New Font ("Lishu", Font.ITAlic, 15));

CategoryPlot Plot = (categoryplot) chart.getplot ();

Plot.setForegroundAlpha (0.9F);

Plot.setValueLabelfont (New Font ("Black Body", Font.TrueType_Font, 12));

//plot.setsectionLabelfont (New Font ("Black Body", Font.TrueType_Font, 12));

/ / Note the following code

Numberaxis verticais = (numberaxis) Plot.GetRangeaxis ();

VerticaXis.setStandardTickUnits (Numberaxis.createIntegertickUnits ()); // Output file to the specified directory

String rfname = mathutil.getroundcode (22) "b.jpeg";

String filename = "d: / test /" rfname;

Try {

CHARTUTILITIES.SAVECHARTASJPEG (New File (Filename), 100, Chart, 600, 600);

? // log.info (".... CREATE Image File:" filename);

} catch (ioexception exz) {

? System.out.print (".... cant't create image file");

}

Six: Conclusion

Personally, JFreeChart can meet the needs of most picture creation, and the beauty is not enough: It is not good enough to set the font, especially when using Chinese, the font is very unclear. Because this reason is recommended to modify his source code, it is best to use the Properties file to set the font. There is a document to be done, so take more time to see the source code. Or on the community. Because time, I only introduce the creation of three pictures, other types of pictures can refer to the example provided by JFreeChart.

?

转载请注明原文地址:https://www.9cbs.com/read-123368.html

New Post(0)