help making simple legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
johnd

help making simple legend

Post by johnd » Wed Jul 24, 2002 6:22 pm

I've been looking through java docs and grepping src but still can't
figure out how to make a very simple legend.

I've got two overlaid graphs. One is a gray bar chart, the other a blue
line chart.

All I need to do is have a legend at the bottom.

a blue dot, square, line, etc then some text of my choice
and the same for the gray.


I can't quite figure out how to do this, any pointer would be greatly
appreciated

David Gilbert

Re: help making simple legend

Post by David Gilbert » Wed Jul 24, 2002 10:16 pm

What type of data (CategoryDataset or XYDataset)? For XYDataset, see the OverlaidXYPlotDemo class, it does what you want. But for CategoryDataset, you end up using OverlaidVerticalCategoryPlot, and it looks like the legend isn't working properly for this class. I'll put it on the to-do list...

Regards,

DG.

johnd

Re: help making simple legend

Post by johnd » Wed Jul 24, 2002 10:44 pm

Here is my code...



import com.keypoint.*;
import com.jrefinery.chart.*;
import com.jrefinery.data.*;
import java.awt.Color;
import java.awt.Font;
import java.text.*;
import java.io.*;
import java.util.*;


public class oTest {

public static byte[] makeGraph(int[] inBar, int[] inLine,
String barLegend, String lineLegend, String endDate) {//yyyymm
double[][] lows=new double[1][inBar.length];
double[][] highs=new double[1][inBar.length];
double[][] lineVals=new double[1][inBar.length];
double min=inBar[0];
double max=inBar[0];

for(int i=0; i<inBar.length; i++) {
System.out.println("on: "+i);
highs[0]=inBar;
lineVals[0]=inLine;
if(inBar < min) min=inBar;
if(inLine < min) min=inLine;
if(inBar > max) max=inBar;
if(inLine[i] > max) max=inLine[i];
}
min=min-min*.05;
max*=1.05;
for(int i=0; i<inBar.length; i++)
lows[0][i]=min;


String[] categories = new String[12];
JFreeChart chart = null;
Calendar cal=Calendar.getInstance() ;

cal.set(Integer.parseInt(endDate.substring(0,4)),
Integer.parseInt(endDate.substring(4,6))-1, 1);
SimpleDateFormat fmt=new SimpleDateFormat("MMM yyyy");
for(int i=11; i>=0; i--) {
System.out.println("--> "+fmt.format(cal.getTime()));
categories[i]=fmt.format(cal.getTime());
System.out.println(categories[i]);
cal.add(Calendar.MONTH, -2);
}

Color[] barColors = new Color[1];
barColors[0] = new Color(127, 127, 127);
Color[] lineColors = new Color[4];
lineColors[0] = Color.red;
lineColors[1] = Color.blue;
lineColors[2] = Color.yellow;
lineColors[3] = Color.gray;
DefaultIntervalCategoryDataset barData =
new DefaultIntervalCategoryDataset(lows, highs);
DefaultCategoryDataset lineData = null;
lineData = new DefaultCategoryDataset(lineVals);
String ctitle = "Price Trends";
String xTitle = "text goes here";
String yTitle = "I N D E X";
HorizontalCategoryAxis xAxis = new HorizontalCategoryAxis(xTitle);
VerticalNumberAxis yAxis = new VerticalNumberAxis(yTitle);
yAxis.setMaximumAxisValue(max);
yAxis.setMinimumAxisValue(min);
xAxis.setVerticalCategoryLabels(true);
NumberFormat formatter = NumberFormat.getInstance();
formatter.setParseIntegerOnly(true);
yAxis.setTickUnit(new NumberTickUnit(4, formatter));

OverlaidVerticalCategoryPlot plot =
new OverlaidVerticalCategoryPlot(xAxis, yAxis, categories);

VerticalIntervalBarRenderer barRenderer = null;
barRenderer = new VerticalIntervalBarRenderer();
VerticalCategoryPlot bars = null;
bars = new VerticalCategoryPlot(barData, null, null, barRenderer);
bars.setSeriesPaint(barColors);
plot.add(bars);
LineAndShapeRenderer lineRenderer = null;
lineRenderer = new LineAndShapeRenderer(
LineAndShapeRenderer.SHAPES_AND_LINES);
VerticalCategoryPlot lines = null;
lines = new VerticalCategoryPlot(lineData, new HorizontalCategoryAxis("heynow"), null, lineRenderer);
//lines = new VerticalCategoryPlot(lineData, null, null, lineRenderer);
lines.setSeriesPaint(lineColors);
lines.setShapeFactory(new SeriesShapeFactory());
plot.add(lines);

chart = new JFreeChart(ctitle, null, plot, true);
List labels=lines.getLegendItemLabels();
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^"+labels.get(0));
labels.set(0,"heynow");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^"+labels.get(0));
Legend leg=chart.getLegend();
chart.setBackgroundPaint(Color.white);
PngEncoder enc=new PngEncoder(chart.createBufferedImage(300, 300),
false, 0, 9);
return enc.pngEncode();
}

static public void main(String[] args) throws Exception {
int[] inBar={100,101,104,108,111,113,111,120,110,103,108,120,
103,100,101,104,108,111,113,111,120,110,103,108};
int[] inLine={105,108,104,108,111,113,111,120,110,103,108,120,
103,100,101,104,108,111,113,111,120,115,120,150};
FileOutputStream out=new FileOutputStream("out.png");
out.write(oTest.makeGraph(inBar, inLine, "Prop Zip Index", "County Index",
"200206"));
System.out.println("done...");
System.exit(0);
}
}

Locked