JFreeCharts Subtitles show up below legend? - java

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)

Related

Java OpenGL rotate text from center point

I am trying to rotate a text using JOGL but it does not rotate from the center point. What can I do with the following code to rotate from the center of the text?
TextRenderer textRenderer = new TextRenderer(new Font("SansSerif", Font.PLAIN, 20));
textRenderer.setColor(Color.WHITE);
textRenderer.beginRendering(viewport.width, viewport.height, false);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glTranslated(screenPoint.x, screenPoint.y, 0);
gl.glRotated(rotation, 0, 0, 1);
gl.glTranslated(-screenPoint.x, -screenPoint.y, 0);
textRenderer.draw("Text", (int) screenPoint.x, (int) screenPoint.y);
textRenderer.endRendering();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPopMatrix();
textRenderer.flush();

How to paint a "region" in a JComponent/JFrame?

what I am trying to do is color in a certain section, is there sort of a tool that will fill in a section/region? Sort of like the Microsoft Paint bucket. Also, is there a way I can resize all objects in my JComponent to be smaller without having to go and change every single coordinate? Like, if I want the image to be twice as small, is there a way to make all lines and arcs and objects smaller with a simple line to them all, instead of manipulating all of the coordinates?
import java.awt.*;
import java.awt.geom.Line2D.Double;
import java.awt.geom.*;
import javax.swing.*;
public class MarioComponent extends JComponent {
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.BLACK);
Color marioM = new Color(153,0,0);
g2.setColor(marioM);
Line2D.Double M1 = new Line2D.Double(309,204,338,154);
Line2D.Double M2 = new Line2D.Double(338,154,355,178);
Line2D.Double M3 = new Line2D.Double(355,178,380,158);
Line2D.Double M4 = new Line2D.Double(380,157,400,217);
Line2D.Double M5 = new Line2D.Double(400,217,380,226);
Line2D.Double M6 = new Line2D.Double(380,226,368,197);
Line2D.Double M7 = new Line2D.Double(368,197,349,213);
Line2D.Double M8 = new Line2D.Double(349,213,338,195);
Line2D.Double M9 = new Line2D.Double(338,195,322,220);
Line2D.Double M10 = new Line2D.Double(322,220,309,205);
g2.draw(M1);
g2.draw(M2);
g2.draw(M3);
g2.draw(M4);
g2.draw(M5);
g2.draw(M6);
g2.draw(M7);
g2.draw(M8);
g2.draw(M9);
g2.draw(M10);
QuadCurve2D.Double hatCurve1 = new QuadCurve2D.Double(263,223,263,86,377,86);
QuadCurve2D.Double hatCurve2 = new QuadCurve2D.Double(377,86,618,86,618,326);
g2.draw(hatCurve1);
g2.draw(hatCurve2);
g2.setColor(Color.WHITE);
QuadCurve2D.Double whiteCurve1 = new QuadCurve2D.Double(298,222,299,133,360,133);
QuadCurve2D.Double whiteCurve2 = new QuadCurve2D.Double(359,133,430,133,414,241);
g2.draw(whiteCurve1);
g2.draw(whiteCurve2);
Color insideHatColor = new Color(221,19,19);
g2.setColor(insideHatColor);
QuadCurve2D.Double redHatCurve1 = new QuadCurve2D.Double(158,262,158,223,326,226);
QuadCurve2D.Double redHatCurve2 = new QuadCurve2D.Double(326,226,545,242,544,337);
g2.draw(redHatCurve1);
g2.draw(redHatCurve2);
g2.setColor(Color.BLACK);
QuadCurve2D.Double hatBlackOutter1 = new QuadCurve2D.Double(542,336,542,346,486,346);
QuadCurve2D.Double hatBlackOutter2 = new QuadCurve2D.Double(486,346,488,255,158,262);
g2.draw(hatBlackOutter1);
g2.draw(hatBlackOutter2);
QuadCurve2D.Double outLine1 = new QuadCurve2D.Double(544,336,573,336,573,376);
QuadCurve2D.Double outLine2 = new QuadCurve2D.Double(533,341,533,397,499,397);
g2.draw(outLine1);
g2.draw(outLine2);
//draws eyes
g2.setColor(Color.BLACK);
g2.fillOval(278,297,35,80);
g2.fillOval(382, 317, 35, 80);
//draws nose, though has to be drawn after eyes because of the left eye being covered by nose
Color skin = new Color(255,204,153);
g2.setColor(skin);
Arc2D.Double nose = new Arc2D.Double(186, 343, 150, 147, 20, 300,Arc2D.OPEN);
g2.draw(nose);
g2.fill(nose);
//now for mustache. *sigh*
Color brown1 = new Color(64,43,21);
g2.setColor(brown1);
QuadCurve2D.Double stache1= new QuadCurve2D.Double(453,414,471,484,441,484);
QuadCurve2D.Double stache2= new QuadCurve2D.Double(441,484,444,541,387,533);
QuadCurve2D.Double stache3= new QuadCurve2D.Double(387,533,352,555,314,532);
QuadCurve2D.Double stache4= new QuadCurve2D.Double(314,532,250,547,249,510);
QuadCurve2D.Double stache5= new QuadCurve2D.Double(249,510,231,515,230,490);
QuadCurve2D.Double stache6= new QuadCurve2D.Double(230,490,348,498,348,453);
QuadCurve2D.Double stache7= new QuadCurve2D.Double(348,453,452,457,452,415);
g2.draw(stache1);
g2.draw(stache2);
g2.draw(stache3);
g2.draw(stache4);
g2.draw(stache5);
g2.draw(stache6);
g2.draw(stache7);
//underneath hat dark area
g2.setColor(Color.BLACK);
QuadCurve2D.Double underHat1= new QuadCurve2D.Double(158,265,158,310,260,310);
Line2D.Double underHat2 = new Line2D.Double(260,310,281,264);
Line2D.Double underHat3 = new Line2D.Double(265,310,263,344);
g2.draw(underHat1);
g2.draw(underHat2);
g2.draw(underHat3);
}
}

Creating a normal distribution graph with JFreeChart

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

JfreeChart : changing axis line angle on polar chart

Since a picture is worth a thousand words, I'll show you.
This is what I have:
This is what I need:
Some code maybe useful:
private JFreeChart createChart (XYDataset dataset)
{
ValueAxis radiusAxis = new NumberAxis ();
radiusAxis.setTickLabelsVisible (false);
// Rendering serie ( handeling null values )
DefaultPolarItemRenderer ren = new DefaultPolarItemRenderer ()
{
#Override
public void drawSeries (Graphics2D g2, Rectangle2D dataArea,
PlotRenderingInfo info, PolarPlot plot, XYDataset dataset, int seriesIndex)
{
boolean newPath = true;
GeneralPath polyline = new GeneralPath ();
int numPoints = dataset.getItemCount (seriesIndex);
for (int i = 0 ; i < numPoints - 1 ; i++)
{
double theta = dataset.getXValue (seriesIndex, i);
double radius = dataset.getYValue (seriesIndex, i);
Point p = plot.translateValueThetaRadiusToJava2D (theta, radius,
dataArea);
if (p.x == 0 && p.y == 0)
{
newPath = true;
}
else
{
if (newPath)
{
polyline.moveTo (p.x, p.y);
newPath = false;
}
else
{
polyline.lineTo (p.x, p.y);
}
}
}
g2.setPaint (lookupSeriesPaint (seriesIndex));
g2.setStroke (lookupSeriesStroke (seriesIndex));
g2.draw (polyline);
}
};
// removing fill serie
ren.setSeriesFilled (0, false);
// custimizing angles
PolarPlot plot = new PolarPlot (dataset, radiusAxis, ren)
{
#Override
protected java.util.List refreshAngleTicks ()
{
java.util.List ticks = new ArrayList ();
ticks.add (new NumberTick (0, "12AM", TextAnchor.CENTER, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (30, "2AM", TextAnchor.TOP_LEFT, TextAnchor.TOP_RIGHT, 0));
ticks.add (new NumberTick (60, "4AM", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (90, "6AM", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (120, "8AM", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (150, "10AM", TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (180, "12PM", TextAnchor.CENTER, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (210, "2PM", TextAnchor.TOP_RIGHT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (240, "4PM", TextAnchor.TOP_RIGHT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (270, "6PM", TextAnchor.TOP_RIGHT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (300, "8PM", TextAnchor.TOP_RIGHT, TextAnchor.TOP_LEFT, 0));
ticks.add (new NumberTick (330, "10PM", TextAnchor.TOP_RIGHT, TextAnchor.TOP_LEFT, 0));
return ticks;
}
};
// colors..
plot.setOutlinePaint (new Color (0, 0, 0, 0));
plot.setBackgroundPaint (Color.white);
plot.setRadiusGridlinePaint (Color.gray);
ren.setShapesVisible (true);
plot.setRadiusGridlinesVisible (true);
plot.setAngleGridlinesVisible (true);
plot.setAngleLabelsVisible (true);
plot.setOutlineVisible (true);
plot.setAngleGridlinePaint (Color.BLACK);
plot.setRenderer (ren);
JFreeChart chart = new JFreeChart ("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint (Color.white);
chart.setBorderVisible (false);
chart.setBorderPaint (Color.RED);
// showing Label Axis ( 0 ... 20 ... 30 ...40)
NumberAxis rangeAxis = (NumberAxis) plot.getAxis ();
rangeAxis.setTickLabelsVisible (true);
rangeAxis.setAxisLinePaint (Color.RED);
return chart;
}
As you can see, I want to change Axis Line angle; I don't know how to proceed.
Thank you.
You don't mention the used version of JFreeChart but JFreeChart 1.0.14 got some improvements for polarcharts. You can set an angle-offset via PolarPlot.setAngleOffset() which allows to start with 12am at whatever position you like.
However, the axes themselves cannot be at arbitrary angles but only at north, east, south or west. This can be set using PolarPlot.setAxisLocation().
hth,
- martin

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