How to put benchmark line on barchart?

陌路散爱 提交于 2019-12-02 10:52:56

问题


We are using the jfreechart with Jasper reports and we are struggling to put the benchmark line on the bar chart.

How can this be achieved using jasper reports?


回答1:


To customize your bar chart in jasper report create a customizer class (ChartCustomizer) extending the JRChartCustomizer.

public void customize(JFreeChart chart, ChartComponent chartComponent)
{
  //get the ploy
  CategoryPlot plot = (CategoryPlot) chart.getPlot();

  //Now add your markers
  ValueMarker vm = new ValueMarker(200); //200 is the position you like it to be
  vm.setPaint(Color.RED);
  vm.setStroke(new BasicStroke(1));
  vm.setLabel("BeanchMark value"); //The label
  vm.setLabelAnchor(RectangleAnchor.TOP);
  vm.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT);
  plot.addRangeMarker(vm);
}

add the class to classpath and in jrxml set the customizerClass attribute

<barChart>
    <chart customizerClass="my.package.ChartCustomizer">
   ....
    </chart>
   ...
</barChart>



回答2:


We have resolved it by using the following code in customise class

ValueMarker marker = new ValueMarker(30);
    marker.setLabel("Average 30%");
    marker.setPaint(Color.black);
    plot.addRangeMarker(marker);

However we need to change the label position, currently it is showing at the start of the line.



来源:https://stackoverflow.com/questions/35649022/how-to-put-benchmark-line-on-barchart

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