JFreechart(Java) - How to draw lines that is partially dashed lines and partially solid lines?

送分小仙女□ 提交于 2019-12-04 11:19:56

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
    )

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

Override the getItemStroke() method in 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;
            }
       };

You can extend XYLineAndShapeRenderer, and override getItemStroke where you provide a different stroke for different items.

The default implementation simply returns the series stroke.

Maybe you should just define different data sets for the real vs forecast data. It would not only be simpler to change the line style, but the code would probably be clearer too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!