请问,如何在JFreeChart画折线的折点上加标注呢?代码如下TimeSeries timeSeries = new TimeSeries("时间", Hour.class);
// 时间曲线数据集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
//构造数据集合
for(int i=0;i<=mm;i++){
timeSeries.add(new Hour(i, today), list.get(i));//list.get(i)获得的是double数据
}
lineDataset.addSeries(timeSeries);
JFreeChart cutomerChart = ChartFactory.createTimeSeriesChart("测试1", "测试2",
"测试3", lineDataset, true, true, true);
// 设置子标题
TextTitle subtitle = new TextTitle("2010",
new Font("黑体", Font.BOLD, 12));
cutomerChart.addSubtitle(subtitle);
// 设置主标题
cutomerChart.setTitle(new TextTitle("\u7528\u7535\u91CF", new Font("黑体", Font.PLAIN, 15)));
cutomerChart.setAntiAlias(true);XYPlot xyplot = (XYPlot)cutomerChart.getPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
for(int i=0;i<=mm;i++){
XYPointerAnnotation xypointerannotation = new XYPointerAnnotation("注标", new Hour(i, today).getHour(), list.get(i).doubleValue(), 10);
renderer.addAnnotation(xypointerannotation);
}
renderer.setShapesVisible(true);像上面那么写的,运行的结果是,折线的折点能出来,可是关于折点的标注不显示,
我是参考下面的DEMO来写的// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 7/18/2005 5:13:38 PM
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3) package cn.wsn.smartgrid.jfreechart.demo;import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.*;
import org.jfree.ui.*;public class AnnotationDemo2 extends ApplicationFrame
{ public AnnotationDemo2(String s)
{
super(s);
setContentPane(createDemoPanel());
} private static XYDataset createDataset1()
{
XYSeries xyseries = new XYSeries("Random Data 1");
xyseries.add(1.0D, 181.80000000000001D);
xyseries.add(2D, 167.30000000000001D);
xyseries.add(3D, 153.80000000000001D);
xyseries.add(4D, 167.59999999999999D);
xyseries.add(5D, 158.80000000000001D);
xyseries.add(6D, 148.30000000000001D);
xyseries.add(7D, 153.90000000000001D);
xyseries.add(8D, 142.69999999999999D);
xyseries.add(9D, 123.2D);
xyseries.add(10D, 131.80000000000001D);
xyseries.add(11D, 139.59999999999999D);
xyseries.add(12D, 142.90000000000001D);
xyseries.add(13D, 138.69999999999999D);
xyseries.add(14D, 137.30000000000001D);
xyseries.add(15D, 143.90000000000001D);
xyseries.add(16D, 139.80000000000001D);
xyseries.add(17D, 137D);
xyseries.add(18D, 132.80000000000001D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
} private static XYDataset createDataset2()
{
XYSeries xyseries = new XYSeries("Random Data 2");
xyseries.add(1.0D, 429.60000000000002D);
xyseries.add(2D, 323.19999999999999D);
xyseries.add(3D, 417.19999999999999D);
xyseries.add(4D, 624.10000000000002D);
xyseries.add(5D, 422.60000000000002D);
xyseries.add(6D, 619.20000000000005D);
xyseries.add(7D, 416.5D);
xyseries.add(8D, 512.70000000000005D);
xyseries.add(9D, 501.5D);
xyseries.add(10D, 306.10000000000002D);
xyseries.add(11D, 410.30000000000001D);
xyseries.add(12D, 511.69999999999999D);
xyseries.add(13D, 611D);
xyseries.add(14D, 709.60000000000002D);
xyseries.add(15D, 613.20000000000005D);
xyseries.add(16D, 711.60000000000002D);
xyseries.add(17D, 708.79999999999995D);
xyseries.add(18D, 501.60000000000002D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
} private static JFreeChart createChart()
{
XYDataset xydataset = createDataset1();
JFreeChart jfreechart = ChartFactory.createXYLineChart("Annotation Demo 2", "Date", "Price Per Unit", xydataset, PlotOrientation.VERTICAL, false, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
numberaxis.setAutoRangeIncludesZero(false);
NumberAxis numberaxis1 = new NumberAxis("Secondary");
numberaxis1.setAutoRangeIncludesZero(false);
xyplot.setRangeAxis(1, numberaxis1);
xyplot.setDataset(1, createDataset2());
xyplot.mapDatasetToRangeAxis(1, 1);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
xylineandshaperenderer.setShapesVisible(true);
xylineandshaperenderer.setShapesFilled(true);
XYPointerAnnotation xypointerannotation = new XYPointerAnnotation("Annotation 1 (2.0, 167.3)", 2D, 167.30000000000001D, -0.78539816339744828D);
xypointerannotation.setTextAnchor(TextAnchor.BOTTOM_LEFT);
xypointerannotation.setPaint(Color.red);
xypointerannotation.setArrowPaint(Color.red);
xylineandshaperenderer.addAnnotation(xypointerannotation);
XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer(true, true);
xylineandshaperenderer1.setSeriesPaint(0, Color.black);
xylineandshaperenderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
XYPointerAnnotation xypointerannotation1 = new XYPointerAnnotation("Annotation 2 (15.0, 613.2)", 15D, 613.20000000000005D, 1.5707963267948966D);
xypointerannotation1.setTextAnchor(TextAnchor.TOP_CENTER);
xylineandshaperenderer1.addAnnotation(xypointerannotation1);
xyplot.setRenderer(1, xylineandshaperenderer1);
LegendTitle legendtitle = new LegendTitle(xylineandshaperenderer);
LegendTitle legendtitle1 = new LegendTitle(xylineandshaperenderer1);
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
blockcontainer.add(legendtitle, RectangleEdge.LEFT);
blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
blockcontainer.add(new EmptyBlock(2000D, 0.0D));
CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
compositetitle.setPosition(RectangleEdge.BOTTOM);
jfreechart.addSubtitle(compositetitle);
return jfreechart;
} public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart();
return new ChartPanel(jfreechart);
} public static void main(String args[])
{
AnnotationDemo2 annotationdemo2 = new AnnotationDemo2("Annotation Demo 2");
annotationdemo2.pack();
RefineryUtilities.centerFrameOnScreen(annotationdemo2);
annotationdemo2.setVisible(true);
}
}是不是XYLineAndShapeRenderer,不能用TimeSeries.add里的值呢?
那该用哪个呢?
在线等,谢谢
// 时间曲线数据集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
//构造数据集合
for(int i=0;i<=mm;i++){
timeSeries.add(new Hour(i, today), list.get(i));//list.get(i)获得的是double数据
}
lineDataset.addSeries(timeSeries);
JFreeChart cutomerChart = ChartFactory.createTimeSeriesChart("测试1", "测试2",
"测试3", lineDataset, true, true, true);
// 设置子标题
TextTitle subtitle = new TextTitle("2010",
new Font("黑体", Font.BOLD, 12));
cutomerChart.addSubtitle(subtitle);
// 设置主标题
cutomerChart.setTitle(new TextTitle("\u7528\u7535\u91CF", new Font("黑体", Font.PLAIN, 15)));
cutomerChart.setAntiAlias(true);XYPlot xyplot = (XYPlot)cutomerChart.getPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
for(int i=0;i<=mm;i++){
XYPointerAnnotation xypointerannotation = new XYPointerAnnotation("注标", new Hour(i, today).getHour(), list.get(i).doubleValue(), 10);
renderer.addAnnotation(xypointerannotation);
}
renderer.setShapesVisible(true);像上面那么写的,运行的结果是,折线的折点能出来,可是关于折点的标注不显示,
我是参考下面的DEMO来写的// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 7/18/2005 5:13:38 PM
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3) package cn.wsn.smartgrid.jfreechart.demo;import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.*;
import org.jfree.ui.*;public class AnnotationDemo2 extends ApplicationFrame
{ public AnnotationDemo2(String s)
{
super(s);
setContentPane(createDemoPanel());
} private static XYDataset createDataset1()
{
XYSeries xyseries = new XYSeries("Random Data 1");
xyseries.add(1.0D, 181.80000000000001D);
xyseries.add(2D, 167.30000000000001D);
xyseries.add(3D, 153.80000000000001D);
xyseries.add(4D, 167.59999999999999D);
xyseries.add(5D, 158.80000000000001D);
xyseries.add(6D, 148.30000000000001D);
xyseries.add(7D, 153.90000000000001D);
xyseries.add(8D, 142.69999999999999D);
xyseries.add(9D, 123.2D);
xyseries.add(10D, 131.80000000000001D);
xyseries.add(11D, 139.59999999999999D);
xyseries.add(12D, 142.90000000000001D);
xyseries.add(13D, 138.69999999999999D);
xyseries.add(14D, 137.30000000000001D);
xyseries.add(15D, 143.90000000000001D);
xyseries.add(16D, 139.80000000000001D);
xyseries.add(17D, 137D);
xyseries.add(18D, 132.80000000000001D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
} private static XYDataset createDataset2()
{
XYSeries xyseries = new XYSeries("Random Data 2");
xyseries.add(1.0D, 429.60000000000002D);
xyseries.add(2D, 323.19999999999999D);
xyseries.add(3D, 417.19999999999999D);
xyseries.add(4D, 624.10000000000002D);
xyseries.add(5D, 422.60000000000002D);
xyseries.add(6D, 619.20000000000005D);
xyseries.add(7D, 416.5D);
xyseries.add(8D, 512.70000000000005D);
xyseries.add(9D, 501.5D);
xyseries.add(10D, 306.10000000000002D);
xyseries.add(11D, 410.30000000000001D);
xyseries.add(12D, 511.69999999999999D);
xyseries.add(13D, 611D);
xyseries.add(14D, 709.60000000000002D);
xyseries.add(15D, 613.20000000000005D);
xyseries.add(16D, 711.60000000000002D);
xyseries.add(17D, 708.79999999999995D);
xyseries.add(18D, 501.60000000000002D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
} private static JFreeChart createChart()
{
XYDataset xydataset = createDataset1();
JFreeChart jfreechart = ChartFactory.createXYLineChart("Annotation Demo 2", "Date", "Price Per Unit", xydataset, PlotOrientation.VERTICAL, false, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
numberaxis.setAutoRangeIncludesZero(false);
NumberAxis numberaxis1 = new NumberAxis("Secondary");
numberaxis1.setAutoRangeIncludesZero(false);
xyplot.setRangeAxis(1, numberaxis1);
xyplot.setDataset(1, createDataset2());
xyplot.mapDatasetToRangeAxis(1, 1);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
xylineandshaperenderer.setShapesVisible(true);
xylineandshaperenderer.setShapesFilled(true);
XYPointerAnnotation xypointerannotation = new XYPointerAnnotation("Annotation 1 (2.0, 167.3)", 2D, 167.30000000000001D, -0.78539816339744828D);
xypointerannotation.setTextAnchor(TextAnchor.BOTTOM_LEFT);
xypointerannotation.setPaint(Color.red);
xypointerannotation.setArrowPaint(Color.red);
xylineandshaperenderer.addAnnotation(xypointerannotation);
XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer(true, true);
xylineandshaperenderer1.setSeriesPaint(0, Color.black);
xylineandshaperenderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
XYPointerAnnotation xypointerannotation1 = new XYPointerAnnotation("Annotation 2 (15.0, 613.2)", 15D, 613.20000000000005D, 1.5707963267948966D);
xypointerannotation1.setTextAnchor(TextAnchor.TOP_CENTER);
xylineandshaperenderer1.addAnnotation(xypointerannotation1);
xyplot.setRenderer(1, xylineandshaperenderer1);
LegendTitle legendtitle = new LegendTitle(xylineandshaperenderer);
LegendTitle legendtitle1 = new LegendTitle(xylineandshaperenderer1);
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
blockcontainer.add(legendtitle, RectangleEdge.LEFT);
blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
blockcontainer.add(new EmptyBlock(2000D, 0.0D));
CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
compositetitle.setPosition(RectangleEdge.BOTTOM);
jfreechart.addSubtitle(compositetitle);
return jfreechart;
} public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart();
return new ChartPanel(jfreechart);
} public static void main(String args[])
{
AnnotationDemo2 annotationdemo2 = new AnnotationDemo2("Annotation Demo 2");
annotationdemo2.pack();
RefineryUtilities.centerFrameOnScreen(annotationdemo2);
annotationdemo2.setVisible(true);
}
}是不是XYLineAndShapeRenderer,不能用TimeSeries.add里的值呢?
那该用哪个呢?
在线等,谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货