开发者

JFreeChart + JRuby - fill area between two points on domain axis

I'm using JRuby to access JFreeChart. But I can't seem to set a domain marker on the date axis... Can anyone tell me why this is not working?

def create_plot
    rangeaxis = NumberAxis.new
    rangeaxis.setAutoRangeIncludesZero(true)

    daxis = DateAxis.new
    daxis.setRange( Time.at(@dataset['date_start'].to_i) , Time.at(@dataset['date_end'].to_i) )  

    @plot = XYPlot.new(@datasets.first, daxis, rangeaxis, @base_renderer)
    @plot.setDatasetRenderingOrder(DatasetRenderingOrder::FORWARD)
    @plot.setBackgroundPaint(java.awt.Color.white)

    lol = IntervalMarker.new( 0, 99999999999, java.awt.Color.gray, BasicStroke.new(2.0), java.awt.Color.gray, nil, 1.0 )
    lol.setLabel开发者_运维技巧("ARGH")
    @plot.addDomainMarker(lol)
  end

Even though the marker is supposed to cover January 1970 to November 5138 with a gray area, it's not showing. If I replace the call with addRangeMarker, it works but I want it on the other axis.

Thanks for any replies!


I found a better example. The first one I gave was for a "CategoryMarker", when what you want is a more generic marker. The domain in this example is time, so the code sets up a time interval for the marker to range over, and then sets the label parameters to display. This is from example MarkerDemo2, which uses createXYLineChart. This should be the relevant code:


        Marker threshold = new ValueMarker(80.0);
        Hour hour1 = new Hour(18, 30, 6, 2005);
        Hour hour2 = new Hour(20, 30, 6, 2005);
        double millis1 = hour1.getFirstMillisecond();
        double millis2 = hour2.getFirstMillisecond();
        Marker cooling = new IntervalMarker(millis1, millis2);
        cooling.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        cooling.setPaint(new Color(150, 150, 255));
        cooling.setLabel("Automatic Cooling");
        cooling.setLabelFont(new Font("SansSerif", Font.PLAIN, 11));
        cooling.setLabelPaint(Color.blue);
        cooling.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        cooling.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        plot.addDomainMarker(cooling, Layer.BACKGROUND);
So the crucial thing is to set up the IntervalMarker, then attach that using addDomainMarker. You definitely want a RectangleAnchor


BarChartDemo3 in the demo package shows how to paint the background for a particular domain value. You can use a rectangle to color the whole width for that value. If you do that for adjacent values, you should get the effect you want. Here are some of the key methods (I assume you can look up the documentation for details.)


        CategoryMarker marker = new CategoryMarker("Category 3");
        marker.setPaint(new Color(0xDD, 0xFF, 0xDD, 0x80));
        marker.setAlpha(0.5f);
        plot.addDomainMarker(marker, Layer.BACKGROUND);

Paying extra for the documentation was well worth, it in my opinion.

added: This is just before the code above. It seems to be positioning the marker.

        renderer.setItemLabelsVisible(true);
        ItemLabelPosition p = new ItemLabelPosition(
            ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0
        );
        renderer.setPositiveItemLabelPosition(p);
        plot.setRenderer(renderer);
and then there's this code which appears just before the call to addDomainMarker:

        marker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
        marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜