Creating a normal distribution graph with JFreeChart - java

I am trying to make a normal distribution graph using the JFreeChart library. I am successful if I try to get one area under the graph. However I did not find a way on how get 2 areas under the graph.
Here is the code for one side :
public GrafHujungKanan() {
Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100,
"Normal");
XYSeries fLine = new XYSeries("fLine");
fLine.add(nilaiKritikal, 0);
fLine.add(4, 0);
((XYSeriesCollection) dataset).addSeries(fLine);
NumberAxis xAxis = new NumberAxis(null);
NumberAxis yAxis = new NumberAxis(null);
XYDifferenceRenderer renderer = new XYDifferenceRenderer();
xAxis.setRange(0, 5);
plot = new XYPlot(dataset, xAxis, yAxis, renderer);
chart = new JFreeChart(plot);
chart.removeLegend();
ChartPanel cp = new ChartPanel(chart);
this.add(cp);
}
Here is how it looks with the above code
And here is how I need it to look :
I already tried flipping the values with positive and negative. But instead the line of the graph turns green.
This is what I tried
public GrafDuaHujung() {
Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100,
"Normal");
// line on right side
XYSeries fLine = new XYSeries("fLine");
fLine.add(2, 0);
fLine.add(4, 0);
((XYSeriesCollection) dataset).addSeries(fLine);
// line on left side
XYSeries dLine = new XYSeries("dLine");
dLine.add(-2, 0);
dLine.add(-4, 0);
((XYSeriesCollection) dataset).addSeries(dLine);
NumberAxis xAxis = new NumberAxis(null);
NumberAxis yAxis = new NumberAxis(null);
XYDifferenceRenderer renderer = new XYDifferenceRenderer();
xAxis.setRange(0, 5);
plot = new XYPlot(dataset, xAxis, yAxis, renderer);
chart = new JFreeChart(plot);
chart.removeLegend();
ChartPanel cp = new ChartPanel(chart);
this.add(cp);
}
Thank you for your answer.

You can use multiple datasets.
public GrafDuaHujung() {
Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, "Normal");
dataset2 = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, "Normal"); //New
// line on right side
XYSeries fLine = new XYSeries("fLine");
fLine.add(2, 0);
fLine.add(4, 0);
((XYSeriesCollection) dataset).addSeries(fLine);
// line on left side
XYSeries dLine = new XYSeries("dLine");
dLine.add(-2, 0);
dLine.add(-4, 0);
((XYSeriesCollection) dataset2).addSeries(dLine); //Changed
XYDifferenceRenderer renderer = new XYDifferenceRenderer();
plot = new XYPlot(); //New
plot.setDataset(0, dataset); //New
plot.setDataset(1, dataset2); //New
plot.setRenderer(renderer); //New
chart = new JFreeChart(plot);
chart.removeLegend();
ChartPanel cp = new ChartPanel(chart);
this.add(cp);
}

Related

How to create a surface chart with Apache POI?

Does anyone know how to create a surface chart with Apache POI?
Unfortunately I could not find an example anywhere.
I tried to use other chart examples but without success.
Since XDDF was introduced with apache poi 4, this should be used. And XDDFChartData has a subclass XDDFSurface3DChartData.
What one needs to know when using:
A surface 3D chart needs an additional series axis. And this series axis needs to be defined for the XDDFSurface3DChartData.
Minimal example which creates a Excel sheet having a surface 3D chart. This is tested and works using the current apache poi 5.2.2.
import java.io.FileOutputStream;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelXDDFSurface3DChart {
public static void main(String[] args) throws Exception {
try (XSSFWorkbook document = new XSSFWorkbook()) {
XSSFSheet sheet = document.createSheet("SurfaceChart");
// create the data
String[] categories = new String[] { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9" };
Double[] values1 = new Double[] { 10d, 20d, 10d, 15d, 12d, 20d, 10d, 18d, 19d };
Double[] values2 = new Double[] { 20d, 10d, 15d, 20d, 11d, 17d, 18d, 20d, 10d };
Double[] values3 = new Double[] { 14.5d, 14d, 13.5d, 13d, 12.5d, 12d, 11.5d, 11d, 10.5d };
int r = 0;
for (String cat : categories) {
sheet.createRow(r).createCell(0).setCellValue(cat);
sheet.getRow(r).createCell(1).setCellValue(values1[r]);
sheet.getRow(r).createCell(2).setCellValue(values2[r]);
sheet.getRow(r).createCell(3).setCellValue(values3[r]);
r++;
}
// create the chart
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 0, 20, 30);
XDDFChart chart = drawing.createChart(anchor);
// create data sources
int numOfPoints = categories.length;
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, numOfPoints-1, 0, 0));
XDDFNumericalDataSource<Double> valuesData1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, numOfPoints-1, 1, 1));
XDDFNumericalDataSource<Double> valuesData2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, numOfPoints-1, 2, 2));
XDDFNumericalDataSource<Double> valuesData3 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, numOfPoints-1, 3, 3));
// surface 3D chart
XDDFCategoryAxis categoryAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis valueAxis = chart.createValueAxis(AxisPosition.LEFT);
valueAxis.setCrosses(AxisCrosses.AUTO_ZERO);
// surface 3D chart needs a series axis
XDDFSeriesAxis seriesAxis = chart.createSeriesAxis(AxisPosition.BOTTOM);
seriesAxis.setCrosses(AxisCrosses.AUTO_ZERO);
seriesAxis.crossAxis(valueAxis);
XDDFChartData data = chart.createData(ChartTypes.SURFACE3D, categoryAxis, valueAxis);
// surface 3D chart needs the series axis applied to the XDDFSurface3DChartData
((XDDFSurface3DChartData)data).defineSeriesAxis(seriesAxis);
XDDFChartData.Series series = data.addSeries(categoriesData, valuesData1);
series.setTitle("Series 1", null);
series = data.addSeries(categoriesData, valuesData2);
series.setTitle("Series 2", null);
series = data.addSeries(categoriesData, valuesData3);
series.setTitle("Series 3", null);
chart.plot(data);
// set legend
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("./CreateExcelXDDFSurface3DChart.xlsx")) {
document.write(fileOut);
}
}
}
}
Btw.: I have never understood where a surface chart will be helpful for data presentation.

Java Fx Scatter Chart more symbols needed for my data

i am currently trying to create an scatter chart with 16 datapoints. My Problem is that if the chart is created there are only 8 different symbols for the other 8 datapoints the symbols and colors just repeate themself. Maybe you guys cold help me out. Here is the code i use :
//Create Scatter Chart to visualize the Data
Stage test2 = new Stage();
test2.setTitle(titel+" optimization Powersag Pupil Ring + FineRes Pupil Ring");
final NumberAxis xAxis = new NumberAxis(50, 100, 75);
final NumberAxis yAxis = new NumberAxis(0, 0.5, 0.25);
final ScatterChart<Number, Number> sc = new ScatterChart<Number, Number>(xAxis, yAxis);
xAxis.setLabel("±0,50 D Group [%]");
yAxis.setLabel("MedAE");
sc.setTitle(titel+" optimization Powersag Pupil Ring + FineRes Pupil Ring");
int tmp2 = 1;
for (int i =8; i <A_const.size();i++) {
XYChart.Series series1 = new XYChart.Series();
series1.setName("Power sag Pupil Ring: " + tmp2 + ".0mm");
series1.getData().add(new XYChart.Data(group_list.get(i), median_list.get(i)));
sc.getData().addAll(series1);
tmp2++;
}
//FineREs
tmp2=0;
for (int i =8; i <A_const.size();i++) {
XYChart.Series series1 = new XYChart.Series();
series1.setName("Fine Res Pupil Ring: " + "2."+tmp2+"mm");
tmp2++;
series1.getData().add(new XYChart.Data(group_list_fineres.get(i), median_list_fineres.get(i)));
sc.getData().addAll(series1);
}
Scene scene = new Scene(sc, 500, 400);
test2.setScene(scene);
test2.show();
i used an css file to define the colors for the series:
.series0.chart-symbol{-fx-background-color: black}
.series1.chart-symbol{-fx-background-color: silver}
.series2.chart-symbol{-fx-background-color: gray}
.series3.chart-symbol{-fx-background-color: white}
.series4.chart-symbol{-fx-background-color: maroon}
.series5.chart-symbol{-fx-background-color: red}
This worked for me !

Customizing Graphs- Apache POI

I've generated a graph using Apache POI. I got the graphs correctly but in the border line, the corners are round. I need to change them to normal sharp corners. how can I do that?
my code is as follows. (I've posted only the code for border line since the code is too long. If anybody needs the full code, please comment)
XSSFSheet sheet4 = wb.createSheet("Graph");
Drawing<?> drawing = sheet4.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 17, 27);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.RIGHT);
LineChartData data = chart.getChartDataFactory().createLineChartData();
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 0, 0));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 1, 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 3, 3));
ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 4, 4));
ChartDataSource<Number> ys4 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(2, m-1, 8, 8));
LineChartSeries series1 = data.addSeries(xs, ys1);
series1.setTitle("VAL1");
LineChartSeries series2 = data.addSeries(xs, ys2);
series2.setTitle("VAL2");
LineChartSeries series3 = data.addSeries(xs, ys3);
series3.setTitle("VAL3");
LineChartSeries series4 = data.addSeries(xs, ys4);
series4.setTitle("VAL4");
chart.plot(data, bottomAxis, leftAxis);
if (chart instanceof XSSFChart) {
XSSFChart xchart = (XSSFChart) chart;
CTChart ctChart = xchart.getCTChart();
CTTitle title = ctChart.addNewTitle();
CTTx tx = title.addNewTx();
CTTextBody rich = tx.addNewRich();
rich.addNewBodyPr();
CTTextParagraph para = rich.addNewP();
CTRegularTextRun r = para.addNewR();
r.setT("TITLE");
}
if (chart instanceof XSSFChart) {
XSSFChart xssfChart = (XSSFChart)chart;
//Plot area background and border line
if (xssfChart.getCTChartSpace().getSpPr() == null) xssfChart.getCTChartSpace().addNewSpPr();
if (xssfChart.getCTChartSpace().getSpPr().getSolidFill() == null)
xssfChart.getCTChartSpace().getSpPr().addNewSolidFill();
if (xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr() == null)
xssfChart.getCTChartSpace().getSpPr().getSolidFill().addNewSrgbClr();
xssfChart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)255,(byte)255,(byte)255});
if (xssfChart.getCTChartSpace().getSpPr().getLn() == null) xssfChart.getCTChartSpace().getSpPr().addNewLn();
xssfChart.getCTChartSpace().getSpPr().getLn().setW(Units.pixelToEMU(0));
if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill() == null)
xssfChart.getCTChartSpace().getSpPr().getLn().addNewSolidFill();
if (xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr() == null)
xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().addNewSrgbClr();
xssfChart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr().setVal(new byte[]{(byte)0,(byte)0,(byte)0});
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
plotArea.getLineChartArray()[0].getSmooth();
CTBoolean ctBool = CTBoolean.Factory.newInstance();
ctBool.setVal(false);
plotArea.getLineChartArray()[0].setSmooth(ctBool);
for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
ser.setSmooth(ctBool);
}

JFreeCharts Subtitles show up below legend?

I am using JFreeCharts and I have both the legend and subtitles positioned at the top of the chart. However, my desire is to have the title, then the subtitle in the second line, and then the legend underneath. Instead, it has the title, then legend, and then subtitle. This is the current layout:
As you can see, the legend is above the subtitles, whereas it should be the other way around. All of this, the title, legend, and subtitles, should be above the chart.
My current code to make the chart and customize the titles, subtitles, and legend are:
public JFreeChart createStackedChart(final CategoryDataset categorydataset, String Title) throws DocumentException, IOException {
final JFreeChart chart = ChartFactory.createStackedBarChart(
Title, // chart title
"", // domain axis label
"", // range axis label
categorydataset, // data
PlotOrientation.VERTICAL, // the plot orientation
true, // legend
true, // tooltips
false // urls
);
chart.setTitle(
new org.jfree.chart.title.TextTitle(Title,
new java.awt.Font("Calibri", java.awt.Font.PLAIN, 12)
));
chart.getTitle().setPaint(Color.GRAY);
Color subExc = new Color(237,125,49);
chart.addSubtitle(new TextTitle("Title",
new Font("Calibri", Font.PLAIN, 12), subExc,
RectangleEdge.TOP, HorizontalAlignment.CENTER,
VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS));
chart.addSubtitle(new TextTitle("Title2",
new Font("Calibri", Font.PLAIN, 12), Color.GRAY,
RectangleEdge.TOP, HorizontalAlignment.CENTER,
VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS));
LegendTitle legend = chart.getLegend();
legend.setPosition(RectangleEdge.TOP);
chart.getLegend().setFrame(BlockBorder.NONE);
legend.setItemPaint(Color.GRAY);
Font labelFont = new Font("Calibri", Font.PLAIN, 8);
legend.setItemFont(labelFont);
int columnCount = categorydataset.getColumnCount();
chart.setBackgroundPaint(Color.white);
chart.getTitle().setPadding(10, 2, 0, 2);
chart.setBorderVisible(true);
chart.setBorderPaint(Color.lightGray);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);
plot.setDomainGridlineStroke(new BasicStroke(0.5f));
plot.setRangeGridlineStroke(new BasicStroke(0.5f));
plot.setOutlineVisible(true);
plot.setOutlinePaint(Color.lightGray);
plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0));
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickLabelFont(new Font("Calibri", Font.PLAIN, 9));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeStickyZero(false);
rangeAxis.setLowerMargin(0.0);
rangeAxis.setTickLabelPaint(Color.GRAY);
rangeAxis.setTickMarkPaint(Color.lightGray);
rangeAxis.setTickMarkStroke(new BasicStroke(0.3f));
rangeAxis.setAxisLineStroke(new BasicStroke(0.3f));
rangeAxis.setLowerBound(0.0f);
rangeAxis.setAxisLinePaint(Color.lightGray);
GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
renderer.setDrawBarOutline(false);
renderer.setBarPainter(new StandardBarPainter());
Paint p1 = new GradientPaint(
0.0f, 0.0f, new Color(0x22, 0x22, 0xFF), 0.0f, 0.0f, new Color(0x88, 0x88, 0xFF)
);
renderer.setSeriesPaint(0, p1);
renderer.setSeriesPaint(4, p1);
renderer.setSeriesPaint(8, p1);
Paint p2 = new GradientPaint(
0.0f, 0.0f, new Color(0x22, 0xFF, 0x22), 0.0f, 0.0f, new Color(0x88, 0xFF, 0x88)
);
renderer.setSeriesPaint(1, p2);
renderer.setSeriesPaint(5, p2);
renderer.setSeriesPaint(9, p2);
Paint p3 = new GradientPaint(
0.0f, 0.0f, new Color(0xFF, 0x22, 0x22), 0.0f, 0.0f, new Color(0xFF, 0x88, 0x88)
);
renderer.setSeriesPaint(2, p3);
renderer.setSeriesPaint(6, p3);
renderer.setSeriesPaint(10, p3);
Paint p4 = new GradientPaint(
0.0f, 0.0f, new Color(0xFF, 0xFF, 0x22), 0.0f, 0.0f, new Color(0xFF, 0xFF, 0x88)
);
renderer.setSeriesPaint(3, p4);
renderer.setSeriesPaint(7, p4);
renderer.setSeriesPaint(11, p4);
renderer.setGradientPaintTransformer(
new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL)
);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelPaint(Color.GRAY);
domainAxis.setTickLabelFont(new Font("Calibri", Font.PLAIN, 9));
Color transparent = new Color(0, 0, 0, 0);
domainAxis.setAxisLinePaint(transparent);
domainAxis.setTickMarkPaint(Color.lightGray);
domainAxis.setTickMarkStroke(new BasicStroke(0.3f));
domainAxis.setMaximumCategoryLabelWidthRatio(4f);
if (columnCount == 2) {
domainAxis.setCategoryMargin(.6);
domainAxis.setLowerMargin(0.015);
domainAxis.setUpperMargin(0.015);
} else if (columnCount == 3) {
domainAxis.setCategoryMargin(.35);
domainAxis.setLowerMargin(0.15);
domainAxis.setUpperMargin(0.15);
} else {
domainAxis.setCategoryMargin(.55);
domainAxis.setLowerMargin(0.015);
domainAxis.setUpperMargin(0.015);
}
if (columnCount >= 5) {
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
chart.setPadding(new RectangleInsets(0, 5, 0, 5));
} else {
domainAxis.setCategoryLabelPositions(
STANDARD);
}
plot.setDomainAxis(domainAxis);
return chart;
}
What should I do besides RectangleEdge TOP to be able to determine the stacking order of the legend and subtitles? Thanks!
Actually, I have found a solution. Just in case this helps others out there. The source of inspiration for the answer was found in:
http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=23187
I had to change my code to incorporate this. I wrote
chart.addSubtitle(0,new TextTitle("Title",
new Font("Calibri", Font.PLAIN, 12), subExc,
RectangleEdge.TOP, HorizontalAlignment.CENTER,
VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS));
chart.addSubtitle(1,new TextTitle("Title2",
new Font("Calibri", Font.PLAIN, 12), Color.GRAY,
RectangleEdge.TOP, HorizontalAlignment.CENTER,
VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS));
Adding the 0 and 1 made these subtitles get written BEFORE the legend.
Here is a work-around that might do the trick for you:
Do not plot the legend in your chart: chart.removeLegend();
Wrap your ChartPanel inside a JPanel (with BorderLayout)
At the southern position of the panel, add your manually-generated legend:
Should look like:
public static JPanel wrapChart(ChartPanel chartPanel) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(chartPanel, BorderLayout.CENTER);
panel.add(createLegend(chartPanel.getChart().getPlot()), BorderLayout.SOUTH);
return panel;
}
private static JPanel createLegend(Plot plot) {
JPanel panel = new JPanel(); // Using FlowLayout here
Iterator iterator = plot.getLegendItems().iterator();
while (iterator.hasNext()) {
LegendItem item = (LegendItem) iterator.next();
JLabel label = new JLabel(item.getLabel());
label.setIcon(new ColorIcon(8, item.getFillPaint()));
panel.add(label);
}
return panel;
}
(createLegend() method borrowed from this answer)

Can I change default color for DomainCrosshair in XYPlot: jFreeChart

I am referring to :
Changing the shapes of points in scatter plot
But Am not able to see the shape of my x,y data points:
public void plotHysteresis()
{
s1= new double[6];
s2= new double[6];
s3= new double[6];
s4= new double[6];
int i=0;
for(i=0;i<6;i++)
{
s1[i]=hyst[i];
System.out.println("s1[" +i +"] : " +s1[i] +"\n");
}
for(i=0;i<6;i++)
{
s2[i]=hyst[10-i];
System.out.println("s2[" +i +"] : " +s2[i] +"\n");
}
for(i=0;i<6;i++)
{
s3[i]=hyst[11+i];
System.out.println("s3[" +i +"] : " +s3[i] +"\n");
}
for(i=0;i<6;i++)
{
s4[i]=hyst[21-i];
System.out.println("s4[" +i +"] : " +s4[i] +"\n");
}
// DefaultCategoryDataset dataset = new DefaultCategoryDataset();
XYSeries series3 = new XYSeries("III");
int x=-25;
System.out.println(x +"***\n");
for(i=5;i>=0;i--)
{
series3.add(x,s3[i]);
x=x+5;
}
System.out.println(x +"***\n");
XYSeries series4 = new XYSeries("IV");
for(i=0;i<6;i++)
{
x=x-5;
series4.add(x,s4[i] );
}
System.out.println(x +"###\n");
XYSeries series1 = new XYSeries("I");
x=0;
for(i=0;i<6;i++)
{
series1.add(x,s1[i] );
x=x+5;
}
// x=x-5;
System.out.println(x +"***\n");
XYSeries series2 = new XYSeries("II");
for(i=5;i>=0;i--)
{
x=x-5;
series2.add(x,s2[i] );
}
System.out.println(x +"***\n");
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series3);
dataset.addSeries(series4);
dataset.addSeries(series1);
dataset.addSeries(series2);
JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title
"Pounds(lb)", // domain axis label
"Movement(inch)", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
//chart.setBackgroundPaint(new Color(249, 231, 236));
/*CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
plot.getRenderer().setSeriesPaint(0, Color.BLUE);
plot.getRenderer().setSeriesPaint(1, Color.BLUE);
plot.getRenderer().setSeriesPaint(2, Color.BLUE);
plot.getRenderer().setSeriesPaint(3, Color.BLUE);*/
Shape cross = ShapeUtilities.createRegularCross(4, 3);
XYPlot plot = (XYPlot) chart.getPlot();
XYItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesPaint(1, Color.RED);
renderer.setSeriesPaint(2, Color.RED);
renderer.setSeriesPaint(3, Color.RED);
renderer.setSeriesShape(0, cross);
renderer.setSeriesShape(1, cross);
renderer.setSeriesShape(2, cross);
renderer.setSeriesShape(3, cross);
renderer.setSeriesVisible(0,true);
renderer.setSeriesVisible(1,true);
renderer.setSeriesVisible(2,true);
renderer.setSeriesVisible(3,true);
plot.setDomainCrosshairVisible(true);//Sets Y axis visible blue
plot.setRangeCrosshairVisible(true);//Sets X axis visible blue
/*LineAndShapeRenderer rend
= (LineAndShapeRenderer) plot.getRenderer();
rend.setShapesVisible(true);*/
//renderer.setDrawOutlines(true);
//renderer.setUseFillPaint(true);
//renderer.setFillPaint(Color.white);
ChartPanel frame = new ChartPanel(chart);
frame.setVisible(true);
frame.setSize(plotPanel.getWidth(),plotPanel.getHeight());
plotPanel.add(frame);
plotPanel.repaint();
}
The above code gives me output:
Earlier I was able to see the shape with CategoryPlot and LineChart as shown in commented part of the code, but it is not working for XYLineChart.
Please help
Thanks
Got the solution at:
How to get diamond shape for points in JFreechart
Can I change the color of
plot.setDomainCrosshairVisible(true);//Sets Y axis visible blue
plot.setRangeCrosshairVisible(true);//Sets X axis visible blue
want to make it black instead of it appearing as blue.
Ok got the answer:
How to get diamond shape for points in JFreechart
XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
r.setSeriesShapesVisible(0, true);
Was not adding XYLineAndShapeRenderer in my code

Categories