JFreeChart XYLineAndShapeRenderer显示范围开始和结束之外的不需要的值

时间:2018-04-13 13:32:31

标签: jfreechart

我使用XYLineAndShapeRenderer使用jfreechart构建一个系列图,该系列需要显示一个月中每一天的值,然后我需要X轴显示1到30或1到31(取决于当前月份)的值。数据集仅具有1到30/31的X值,但结果图显示ZERO(在1之前)和31/32之后(30/31)。我想在X轴上只显示1到30/31。但我没有成功。 Follow显示构建数据集和图形以及resultante图像的代码。 在这种情况下,如何仅在X轴上显示有效值?

// build dataset
private XYDataset createSampleData()
{
    List<Integer> diario = new ArrayList<>(Arrays.asList(20, 50, 120, 78, 37, 69, 145));
    int dSize = diario.size();
    int dPos = 0;

    XYSeries serieD = new XYSeries("Diário");
    XYSeries serieA = new XYSeries("Acumulado");

    int acumulado = 0;
    for(int i=1; i < 31; i++) {
        int valDay = diario.get(dPos++);
        acumulado += valDay;
        serieD.add(i, valDay);
        serieA.add(i, acumulado);
        if( dPos >= dSize ) {
            dPos = 0;
        }
    }
    XYSeriesCollection result = new XYSeriesCollection(serieD);
    result.addSeries(serieA);
    return result;
}

    private JFreeChart buildSeriesChartBySeriesData(String title, String labelX, String labelY)
{
    NumberAxis xAxis = new NumberAxis(labelX);
    xAxis.setAutoRangeIncludesZero(false);
    xAxis.setRangeType(RangeType.POSITIVE);


    NumberAxis yAxis = new NumberAxis(labelY);
    yAxis.setAutoRangeIncludesZero(false);
    yAxis.setRangeType(RangeType.POSITIVE);

    XYSplineRenderer renderer1 = new XYSplineRenderer();
    XYPlot plot = new XYPlot(this.createSampleData(), xAxis, yAxis, renderer1);
    plot.setDomainGridlinesVisible(true);

    plot.setDomainZeroBaselineVisible(false);
    plot.setDomainPannable(false);

    plot.setBackgroundPaint(new Color(224, 224,235));

    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

    plot.setDomainPannable(false);
    plot.setRangePannable(false);
    plot.setDomainZeroBaselineVisible(false);
    plot.setRangeZeroBaselineVisible(false);

    renderer1.setSeriesPaint(0, Color.blue);
    renderer1.setSeriesPaint(1, Color.yellow);
    plot.setAxisOffset(new RectangleInsets(3, 3, 3, 3));

    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) plot.getRenderer();
    localXYLineAndShapeRenderer.setBaseShapesVisible(true);
    localXYLineAndShapeRenderer.setBaseShapesFilled(true);
    localXYLineAndShapeRenderer.setDrawOutlines(true);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // show item labels:
    final XYItemRenderer renderer = plot.getRenderer();

    ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER);
    renderer.setBasePositiveItemLabelPosition(position);

    final StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator();

    renderer.setSeriesItemLabelGenerator(0, generator);
    renderer.setSeriesItemLabelsVisible(0, true);
    renderer.setSeriesItemLabelGenerator(1, generator);
    renderer.setSeriesItemLabelsVisible(1, true);

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    return chart;
}

enter image description here

在下一个图表中,我使用timeSeries绘制,结果是相同的,显示previos月的最后一天和下个月的第一天,我不想显示这个值。 enter image description here

我需要的结果就像这张图片(来自chartJs的样本): enter image description here

如何从第一张顶部图的结果中隐藏零和31?

很久以后...... ----经过很长一段时间我自己制作了想要的图表----- 此代码生成正确的图表:

    private JFreeChart TimeSeriesChartExample()
{
    XYDataset dataset = createSampleMonthData();

    // Create chart
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Time Series Chart Example",
            "Date",
            "Values",
            dataset, true, true, false);

    // Changes background color
    XYPlot plot = (XYPlot)chart.getPlot();
    plot.setBackgroundPaint(new Color(224, 224,235));

    XYSplineRenderer renderer1 = new XYSplineRenderer();
    renderer1.setSeriesPaint(0, Color.blue);
    renderer1.setSeriesPaint(1, Color.green);

    plot.setRenderer(renderer1);
    plot.setAxisOffset(new RectangleInsets(3, 3, 3, 3));

    // show item labels:
    XYItemRenderer renderer = plot.getRenderer();
    ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER);
    renderer.setBasePositiveItemLabelPosition(position);

    final StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator();
    renderer.setSeriesItemLabelGenerator(0, generator);
    renderer.setSeriesItemLabelsVisible(0, true);
    renderer.setSeriesItemLabelGenerator(1, generator);
    renderer.setSeriesItemLabelsVisible(1, true);

    DateAxis xAxis = (DateAxis)plot.getDomainAxis();
    xAxis.setDateFormatOverride(new SimpleDateFormat("dd/MM/yy"));

    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVerticalTickLabels(true);

    return chart;
}

这是结果图: enter image description here

1 个答案:

答案 0 :(得分:1)

解决方案如下,我也提出问题主体:

private JFreeChart TimeSeriesChartExample()
{
    XYDataset dataset = createSampleMonthData();

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Time Series Chart Example",
            "Date",
            "Values",
            dataset, true, true, false);

    // Changes background color
    XYPlot plot = (XYPlot)chart.getPlot();
    plot.setBackgroundPaint(new Color(224, 224,235));

    XYSplineRenderer renderer1 = new XYSplineRenderer();
    renderer1.setSeriesPaint(0, Color.blue);
    renderer1.setSeriesPaint(1, Color.green);

    plot.setRenderer(renderer1);
    plot.setAxisOffset(new RectangleInsets(3, 3, 3, 3));

    XYItemRenderer renderer = plot.getRenderer();
    ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER);
    renderer.setBasePositiveItemLabelPosition(position);

    final StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator();
    renderer.setSeriesItemLabelGenerator(0, generator);
    renderer.setSeriesItemLabelsVisible(0, true);
    renderer.setSeriesItemLabelGenerator(1, generator);
    renderer.setSeriesItemLabelsVisible(1, true);

    DateAxis xAxis = (DateAxis)plot.getDomainAxis();
    xAxis.setDateFormatOverride(new SimpleDateFormat("dd/MM/yy"));

    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVerticalTickLabels(true);

    return chart;
}

结果如下:

enter image description here

经过测试的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;

/**
 * @see https://stackoverflow.com/a/49909020/230513
 */
public class ChartTest {

    private void display() {
        JFrame f = new JFrame("ChartTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(createTimeSeriesChartExample()){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1000, 400);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private XYDataset createSampleMonthData() {
        List<Integer> diario = new ArrayList<>(
            Arrays.asList(20, 50, 120, 78, 37, 69, 145));
        int dSize = diario.size();
        int dPos = 0;

        XYSeries serieD = new XYSeries("Diário");
        XYSeries serieA = new XYSeries("Acumulado");

        int acumulado = 0;
        for (int i = 1; i < 31; i++) {
            int valDay = diario.get(dPos++);
            acumulado += valDay;
            serieD.add(i, valDay);
            serieA.add(i, acumulado);
            if (dPos >= dSize) {
                dPos = 0;
            }
        }
        XYSeriesCollection result = new XYSeriesCollection(serieD);
        result.addSeries(serieA);
        return result;
    }

    private JFreeChart createTimeSeriesChartExample() {
        XYDataset dataset = createSampleMonthData();

        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Time Series Chart Example",
            "Date",
            "Values",
            dataset, true, true, false);

        // Changes background color
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(new Color(224, 224, 235));

        XYSplineRenderer renderer1 = new XYSplineRenderer();
        renderer1.setSeriesPaint(0, Color.blue);
        renderer1.setSeriesPaint(1, Color.green);

        plot.setRenderer(renderer1);
        plot.setAxisOffset(new RectangleInsets(3, 3, 3, 3));

        XYItemRenderer renderer = plot.getRenderer();
        ItemLabelPosition position = new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER);
        renderer.setBasePositiveItemLabelPosition(position);

        final StandardXYItemLabelGenerator generator =
            new StandardXYItemLabelGenerator();
        renderer.setSeriesItemLabelGenerator(0, generator);
        renderer.setSeriesItemLabelsVisible(0, true);
        renderer.setSeriesItemLabelGenerator(1, generator);
        renderer.setSeriesItemLabelsVisible(1, true);

        DateAxis xAxis = (DateAxis) plot.getDomainAxis();
        xAxis.setDateFormatOverride(new SimpleDateFormat("dd/MM/yy"));

        ValueAxis domainAxis = plot.getDomainAxis();
        domainAxis.setVerticalTickLabels(true);

        return chart;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ChartTest()::display);
    }
}