2
votes

I'm trying to designing a report with bar chart, in which I need to add a threshold. I have tried with multi-axis chart, in which the scale in different axis is always different.

Is there any other solution to add line to bar chart?

My expect output is a chart as below: enter image description here

2
Probably, which chart component are you using show the jrxml or code where you call the chart....Petter Friberg
I draw this chart by excel, I am planning to draw it by jasper report. Plan to draw it by bar chart. But don't know how to dynamically add the threshold line by a given parameter.Raysen Jia
Which component are you planning to use Jia? Need to know the library... the method's are different...Petter Friberg
What do you mean by component? I am panning use dynamic jasper to generate jrxml, then use jasper report to run the report. From dynamic jasper side, use DJBarChartBuilder to build the chart. While seems it can only generate the bar chart. I have googled, suggested to extend the AbstractChartCustomizer created a new chart in jasper report. If go by this solution, I need spend a lot time on dynamic jasper customize. Is there any convenient solution?Raysen Jia
What the library that you like to use jfreechart?Petter Friberg

2 Answers

6
votes

To draw a line on the bar chart you add a ValueMarker to the CategoryPlot.

In jasper report this is done my adding a JRChartCustomizer

public class MyChartCustomizer implements JRChartCustomizer {

    @Override
    public void customize(JFreeChart jfchart, JRChart jrchart) {
        CategoryPlot plot = (CategoryPlot) jfchart.getPlot();
        //Set at what value you like the line, its color and size of stroke
        ValueMarker vm = new ValueMarker(13000,Color.BLUE, new BasicStroke(2.0F));
        //add marker to plot
        plot.addRangeMarker(vm);
    }
}

In jrxml make sure your class is in classpath and set the customizerClass attribute on the chart tag

<barChart>
    <chart customizerClass="MyChartCustomizer">
   ....
    </chart>
   ...
</barChart>

If you are using you can add it directly in code

chart.addCustomizer(new DRIChartCustomizer() {      
    private static final long serialVersionUID = 1L;
    @Override
    public void customize(JFreeChart chart, ReportParameters arg1) {
        CategoryPlot plot = (CategoryPlot) jfchart.getPlot();
        ValueMarker vm = new ValueMarker(13000,Color.BLUE, new BasicStroke(2.0F));
        plot.addRangeMarker(vm);
    }
});

If you are using setCustomizerClass (as in jrxml)

DJBarChartBuilder().setCustomizerClass("MyChartCustomizer");

Example of result

Chart

Note: in example no package name is used, if MyChartCustomizer is in a package full package name needs to be indicated in setCustomizerClass example "my.package.MyChartCustomizer"

0
votes

For one single horizontal line you can use provided chart customizer:

Go to Chart -> Properties -> Chart (tab) -> Chart customizers

Chart properties

There you can add a Range Interval Marker and configure it with start and end values with the desired value (13000 in your example).

This way an horizontal line will be drawn in the 13000 vertical value as you wanted.