StackedXYAreaChart - 始终显示错误的序列号

时间:2015-07-09 01:51:38

标签: java jfreechart

我使用ChartFactory Utility构建了一个stackedXYAreaChart。我正在尝试生成自定义工具提示。我观察到一个非常奇怪的输出。我的系列4行永远不可见,工具提示也从不显示。 我做错了什么?

这是我的代码:

package test;


import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.StackedXYAreaRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimePeriod;
import org.jfree.data.time.TimeTableXYDataset;

public class Test2
{
public static void main(String args[])
{
    TimeTableXYDataset dataset = new TimeTableXYDataset();
    for( int i=0; i< 10; i++)
    {
        TimePeriod period = new Millisecond();
        dataset.add(period, i, "Series 1");
        dataset.add(period, i + 5, "Series 2");
        dataset.add(period, i + 10, "Series 3");
                    dataset.add(period, i + 15 , "Series 4");
    }

    JFreeChart chart = ChartFactory.createStackedXYAreaChart(
            "Sample Chart", "Time", "Size", dataset, PlotOrientation.VERTICAL, true, true, false);
            StackedXYAreaRenderer render = new StackedXYAreaRenderer();
            //render.setSeriesShape(0, new Ellipse2D.Double(-3.0, -3.0, 6.0, 6.0));
            render.setSeriesToolTipGenerator(0, new StandardXYToolTipGenerator());
                            render.setSeriesToolTipGenerator(1, new StandardXYToolTipGenerator());
                            render.setSeriesToolTipGenerator(2, new StandardXYToolTipGenerator());
                            render.setSeriesToolTipGenerator(3, new StandardXYToolTipGenerator());
                            chart.getXYPlot().setRenderer(render);


            ChartPanel panel = new ChartPanel(chart);
                            panel.setMouseWheelEnabled(true);
            JFrame frame = new JFrame();
            frame.add(panel);
                            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
}
}

2 个答案:

答案 0 :(得分:1)

不要创建新的StackedXYAreaRenderer,而是修改工厂方法ChartFactory.createStackedXYAreaChart()创建的StackedXYAreaRenderer2

XYPlot plot = (XYPlot) chart.getPlot();
StackedXYAreaRenderer2 render = (StackedXYAreaRenderer2) plot.getRenderer();

或者,使用source作为创建自定义图表的指南。

image

import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimePeriod;
import org.jfree.data.time.TimeTableXYDataset;

public class Test2 {

    public static void main(String args[]) {
        TimeTableXYDataset dataset = new TimeTableXYDataset();
        for (int i = 0; i < 10; i++) {
            TimePeriod period = new Millisecond();
            dataset.add(period, i, "Series 1");
            dataset.add(period, i + 5, "Series 2");
            dataset.add(period, i + 10, "Series 3");
            dataset.add(period, i + 15, "Series 4");
        }

        JFreeChart chart = ChartFactory.createStackedXYAreaChart(
            "Sample Chart", "Time", "Size", dataset, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        StackedXYAreaRenderer2 render = (StackedXYAreaRenderer2) plot.getRenderer();
        render.setSeriesToolTipGenerator(0, new StandardXYToolTipGenerator());
        render.setSeriesToolTipGenerator(1, new StandardXYToolTipGenerator());
        render.setSeriesToolTipGenerator(2, new StandardXYToolTipGenerator());
        render.setSeriesToolTipGenerator(3, new StandardXYToolTipGenerator());
        chart.getXYPlot().setRenderer(render);

        ChartPanel panel = new ChartPanel(chart);
        panel.setMouseWheelEnabled(true);
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

答案 1 :(得分:0)

StackedXYAreaRenderer支持形状。使用渲染器:

StackedXYAreaRenderer render = new StackedXYAreaRenderer(StackedXYAreaRenderer.AREA_AND_SHAPES);

不幸的是,StackedXYAreaRenderer2不支持形状。