如何在图表中绘制更多两个网格线?

时间:2015-10-08 08:23:27

标签: charts jfreechart

我需要在下面的图表中绘制更多网格线。 enter image description here

看起来应该是这样的,顶部有一条线,底部有一条线。 enter image description here

如何使用JFreeChart执行此操作?

到目前为止,这是我的代码:

public JFreeChart draw(TimeSeriesCollection dataSet, StyleData styleData, Locale locale) {
    ResourceBundle resourceBundle =   Internationalization.getPropertyResourceBundle("", locale)
    JFreeChart chart = ChartFactory.createTimeSeriesChart("", // Title
            "", // Time Axis Label
            "", // Value Axis Label
            dataSet, // XYDataSet
            false, // Legend
            false, // Tooltips
            false); // URLS

    Map<String, Color> colorMap = styleData.getColorMap();
    Map<String, Stroke> strokeMap = styleData.getStrokeMap();

    chart.setBackgroundPaint(new Color(0, 0, 0, 0));
    chart.setBorderVisible(false);
    chart.setPadding(new RectangleInsets(0, 0, 0, 10));

    XYPlot plot = chart.getXYPlot();
    TS_General_Plot_Defaults.applyDefault(plot);

    plot.getRenderer().setSeriesPaint(0, colorMap.get("performance"));
    plot.getRenderer().setSeriesStroke(0, strokeMap.get("series"));
    plot.setBackgroundPaint(colorMap.get("Background"));
    plot.setRangeGridlinePaint(colorMap.get("range-axis-grid"));
    plot.setRangeGridlineStroke(strokeMap.get("stroke"));
    plot.setDomainGridlinesVisible(false);

    // Change Color of the Numbers Legends to Grey
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

    rangeAxis.setTickLabelPaint(BLACK);
    rangeAxis.setTickLabelFont(styleData.setupFontMap().get("Font"));
    rangeAxis.setAxisLineVisible(false);
    rangeAxis.setTickMarksVisible(false);
    rangeAxis.setTickUnit(getTickUnit(locale, rangeAxis.getRange().getLength()));

    DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
    domainAxis.setTickLabelPaint(BLACK);
    domainAxis.setTickLabelFont(styleData.setupFontMap().get("Font"));
    domainAxis.setAxisLineVisible(false);

    domainAxis.setTickUnit(getDateTickUnit(dataSet));
    DateFormat formatter = new SimpleDateFormat(resourceBundle.getString("chart_date_pattern"));
    domainAxis.setDateFormatOverride(formatter);

    return chart;
}

修改

@DonCorleone,我无法在getUpperBound()方法之上和getLowerBound()方法之下画一条线。见下图。知道怎么做吗?

chart-3

这是我的代码使用您的建议:

        ValueMarker marker = new ValueMarker(rangeAxis.getLowerBound());  // position is the value on the Y axis
        marker.setStroke(new BasicStroke(0.5f));
        marker.setPaint(Color.red);

        //add  to the plot
        plot.addRangeMarker(marker);

        marker = new ValueMarker(rangeAxis.getUpperBound());  // position is the value on the Y axis
        marker.setStroke(new BasicStroke(0.5f));
        marker.setPaint(Color.red);

        //add  to the plot
        plot.addRangeMarker(marker);

1 个答案:

答案 0 :(得分:1)

您可以使用标记

                ValueMarker marker = new ValueMarker(position);  // position is the value on the Y axis

                marker.setStroke(new BasicStroke(1.0f));
                marker.setPaint(Color.black);

                XYPlot plot = (XYPlot) chart.getPlot();

                //add  to the plot
                plot.addRangeMarker(marker);