截断数据点的JFreeChart [英] JFreeChart with truncated data points

查看:52
本文介绍了截断数据点的JFreeChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码创建了一个JFreeChart,但是Y轴标记被截断了.即使数据点在Y轴上重叠,也应如何显示图表? 基本上,我希望从文件中生成Y轴点,并在图表中填充并显示适当的范围.

I have created a JFreeChart with the code below, but the Y-Axis marks are truncated. How should I display the chart even though the data points are overlapped in the Y-Axis? Basically, I want the Y-Axis points to be generated from my file, a proper range is populated and displayed in the chart.

private static JFreeChart buildChart(TimeSeriesCollection dataset,
    String title, boolean endPoints) throws IOException {

// Create the chart

    JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
        title, "Hour", "Count", dataset, true, true, false);

// Setup the appearance of the chart
    chart0.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart0.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

// Display data points or just the lines?

    if (endPoints) {
        XYItemRenderer renderer = plot.getRenderer();
        if (renderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;

            rr.setBaseShapesVisible(true);
            rr.setBaseShapesFilled(true);
            rr.setDrawSeriesLineAsPath(true);
            rr.setSeriesPaint(0, Color.blue.brighter());
            rr.setSeriesVisible(0, true); // default
            rr.setSeriesVisibleInLegend(0, true);  // default

            NumberAxis domainAxis = new NumberAxis();
            domainAxis.setUpperMargin(0.15);
            domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            domainAxis = (NumberAxis) plot.getDomainAxis();
            domainAxis = (NumberAxis) plot.getRangeAxis();
            domainAxis.setAutoRangeIncludesZero(false);
        }
    }

 // Tell the chart how we would like dates to read
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setAutoRange(true);

 //axis.getDefaultAutoRange();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    try {

        ChartUtilities.saveChartAsJPEG(new File("suc.jpg"), 1.0f, chart0, 990, 700);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return chart0;
}

下面是创建的图像,您可以清楚地看到Y轴上有重叠显示.

Below is the image that is created, clearly you can see that the Y-Axis there is an overlap showing.

推荐答案

这是我目前在图表上呈现数据的方法...

This is my current approach on the chart how data is being rendered...

private static JFreeChart buildChart(TimeSeriesCollection dataset,
    String title, boolean endPoints) throws IOException {
// Create the chart
    JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
        title,
        "Hour", "Count",
        dataset,
        true,
        true,
        false);

// Setup the appearance of the chart

    chart0.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart0.getXYPlot();
    plot.getDomainAxis().setAutoRange(true);
    plot.getRangeAxis().setRange(1.0, SucMaxi);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.getAxisOffset();
    plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

// Display data points or just the lines?

    if (endPoints) {
        XYItemRenderer renderer = plot.getRenderer();
        if (renderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
            rr.setBaseShapesVisible(true);
            rr.setBaseShapesFilled(true);
            rr.setDrawSeriesLineAsPath(true);
            rr.setSeriesPaint(0, Color.blue.brighter());
            rr.setSeriesVisible(0, true); // default
            rr.setSeriesVisibleInLegend(0, true);  // default      
        }
    }

// Tell the chart how we would like dates to read

    DateAxis axis = (DateAxis) plot.getDomainAxis();

// Tick the X Axis by unit tick 1 hour
    axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1));
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    try {
        ChartUtilities.saveChartAsJPEG(
            new File("suc.jpg"), 1.0f, chart0, 1000, 700);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return chart0;
}

这篇关于截断数据点的JFreeChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆