XYSeriesCollection dataset = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYLineChart(
null, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, false);
//add Series
XYSeries series1= new XYSeries("Series 1");
Random rn = new Random();
for (int i = 0; i <=50; i=i+5) {
int r = rn.nextInt();
series1.add((double) i,(double) r);
}
dataset.addSeries(series1)
XYPlot xyPlot = chart.getXYPlot();
//Set plot style
XYSplineRenderer xySplineRenderer = new XYSplineRenderer();
xyPlot.setRenderer(xySplineRenderer);
//get xAxis
NumberAxis xAxis = (NumberAxis) xyPlot.getDomainAxis();
//set xAxis range from 0-xMax
xAxis.setRange(0,xMax);
//set xAxis unit
//format number of axis
//NumberFormat numberFormat = NumberFormat.getInstance(Locale.GERMANY);
xAxis.setTickUnit(new NumberTickUnit(xUnit));
xAxis.setNumberFormatOverride(new DecimalFormat("###.0"));
//get yAxis
NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();
//set yAxis range from 0-yMax
yAxis.setRange(0,yMax);
//set yAxis unit
yAxis.setTickUnit(new NumberTickUnit(yUnit));
yAxis.setNumberFormatOverride(new DecimalFormat("###.0"));
//Plot Style Sheet
//set background background
xyPlot.setBackgroundPaint(Color.white);
//Create grid style
xyPlot.setDomainGridlinePaint(Color.gray);
xyPlot.setRangeGridlinePaint(Color.gray);
//Set legend Position
LegendTitle legend = chart.getLegend();
legend.setPosition(RectangleEdge.BOTTOM);
//Set series style (color, shape point)
//Change shape point into circle
Shape circle = new Ellipse2D.Double(-2,-2,5,5);
xySplineRenderer.setSeriesPaint(0,Color.green);
xySplineRenderer.setSeriesShape(0,circle);
xySplineRenderer.setSeriesPaint(1,Color.red);
xySplineRenderer.setSeriesShape(1,circle);
0 comments:
Post a Comment