Can someone point me in the right direction...
I have the code to create two charts. They both have the the same
amount of data represented. One chart is a bar chart the other is a
line chart.
Basically it is prices by month for 24 months.
I just want to show both charts together. I want them superimposed.
I am reading all the code about overlaying, and and having a bit of
trouble.
Has anyone done this?
here is the code I use to make the two separate charts....
-------------------------------------------------------------------------------------------
public static byte[] makeGraph(double[] in) {
String[] names={"one","two", "three", "four"};
double[][] vals=new double[1][in.length];
double[][] vals1=new double[in.length][1];
for(int i=0; i<in.length; i++) {
System.out.println("on: "+i);
vals[0]=in;
vals1[0]=in;
}
CategoryDataset data=new DefaultCategoryDataset(vals1);
JFreeChart bar=ChartFactory.createVerticalBarChart(
"Price Trends",// (Indices Re-Based at 100)",
"this is a test this is a test axis label",
"I N D E X",
data,
true
);
data=new DefaultCategoryDataset(vals);
JFreeChart line=ChartFactory.createLineChart(
"Price Trends",// (Indices Re-Based at 100)",
"this is a test this is a test axis label",
"I N D E X",
data,
true
);
System.out.println("made chart");
BufferedImage img=line.createBufferedImage(400, 400);
System.out.println("made image");
PngEncoder enc=new PngEncoder(img);
return enc.pngEncode();
}
overlaid
Re: overlaid
maybe have a look at \com\jrefinery\chart\demo\OverlaidPlotDemo.java. Note: it is only possible to overlay charts that have the same y-axis. Multiple y-axis are not possible (unfortunately).
basicly what you do is (see the demo also):
1)
// make an overlaid plot...
OverlaidXYPlot plot = new OverlaidXYPlot("Date", "Value");
2)
// add subplot 1...
IntervalXYDataset data1 = this.createDataset1();
XYItemRenderer renderer1 = new VerticalXYBarRenderer(0.20);
renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
XYPlot subplot1 = new XYPlot(data1, null, null, renderer1);
plot.add(subplot1);
3)
// add subplot 2...
XYDataset data2 = this.createDataset2();
XYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
XYPlot subplot2 = new XYPlot(data2, null, null, renderer2);
plot.add(subplot2);
in your case you need to define the renderers properly so that subplot1 renders bars and subplot2 renders lines.
basicly what you do is (see the demo also):
1)
// make an overlaid plot...
OverlaidXYPlot plot = new OverlaidXYPlot("Date", "Value");
2)
// add subplot 1...
IntervalXYDataset data1 = this.createDataset1();
XYItemRenderer renderer1 = new VerticalXYBarRenderer(0.20);
renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
XYPlot subplot1 = new XYPlot(data1, null, null, renderer1);
plot.add(subplot1);
3)
// add subplot 2...
XYDataset data2 = this.createDataset2();
XYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
XYPlot subplot2 = new XYPlot(data2, null, null, renderer2);
plot.add(subplot2);
in your case you need to define the renderers properly so that subplot1 renders bars and subplot2 renders lines.
Re: overlaid
There is a demo that relates to category charts:
com.jrefinery.chart.demo.OverlaidCategoryChartDemo
Follow that as an example, and if you have any problems ask some more questions.
Regards,
DG.
com.jrefinery.chart.demo.OverlaidCategoryChartDemo
Follow that as an example, and if you have any problems ask some more questions.
Regards,
DG.