JFreechart(Java) - 如何绘制部分虚线和部分实线的线条? [英] JFreechart(Java) - How to draw lines that is partially dashed lines and partially solid lines?

查看:180
本文介绍了JFreechart(Java) - 如何绘制部分虚线和部分实线的线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将绘制一条折线图,该折线图将从实线变为虚线,以指示实际数据和预测数据。我不确定是否需要扩展一些类,如XYLineAndShapeRenderer或其他类似的东西,或者可能有一些方便的方法来实现这一点?

I'm going to plot a line chart that will change from solid line to dashed line to to indicate real data and forecasting data. I'm not sure if i need to extend some of the classes like XYLineAndShapeRenderer or something else, or maybe there is some convenient way to achieve this?

这是一个演示使用Excel绘制图表。

Here is a demostration graph i plotted using Excel.

我说的是图中的灰线。这就是我想要的。
我不知道是否有渲染器允许我指示哪个范围是虚线还是实线

I am talking about the gray lines in the graph. That is what i want. I don't know if there is a renderer that allow me to indicate which range dashed or solid

推荐答案

那里这是我能想到的两种方式。它们都不优雅,但这可能是你问题的现实。

There are two ways I can think of for doing this. Neither of them are elegant, but that may be the reality of your problem.

一个是你可以定义两个恰好相互连接的系列。这可能是你最好的选择,但你也必须修复你的传奇。

One is that you could define two series that happen to connect to each other. This is probably your best option, but you will also have to fix your legend.

这会使你的第二个系列显示为虚线:

This would make your second series show as a dashed line:

plot.getRenderer().setSeriesStroke(
    1, 
    new BasicStroke(
        2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
        1.0f, new float[] {6.0f, 6.0f}, 0.0f
    )

编辑:我原来的第二个选项错了。这是一个更好的方法:

My original "second option" was wrong. Here is a better way to do it:

覆盖 getItemStroke() 中的方法AbstractRenderer

     final BasicStroke dashedStroke = new BasicStroke(
                  2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                  1.0f, new float[] {6.0f, 6.0f}, 0.0f);
     XYLineAndShapeRenderer render = new XYLineAndShapeRenderer() {
        @Override
        public Stroke getItemStroke(int row, int column) {
            if (column < dashedThreshold) {
                return lookupSeriesStroke(row);
            } else {
               return dashedStroke;
            }
       };

这篇关于JFreechart(Java) - 如何绘制部分虚线和部分实线的线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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