Add threshold line in a line chart | Line Shapes | Line Customizations | jfreechart

How to add a constant horizontal line or a control line.

Following is a simple line chart

Image

The query for the above chart is as follows

select calendar_month_name, sum(quantity_sold)-3500 as qty_sold
from [detail_data$]
where fiscal_year = 2001
and country_region like ‘Americas’
group by calendar_month_name

Now if we need to add a Target line which would be a constant horizontal line then we can use the following technique

We will simply change the above query and add a new column with the target value representing the target sales

select calendar_month_name, sum(quantity_sold)-3500 as qty_sold, 11500 as Target
from [detail_data$]
where fiscal_year = 2001
and country_region like ‘Americas’
group by calendar_month_name

We get the below result

Image

To customize the lines, for example to make them thicker follow this tutorial or simply add the following code in the Dynamic java script section

//import the necessary classes

import org.jfree.*;

import org.jfree.chart.axis.CategoryAxis;

//import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

//import org.jfree.chart.labels.CategoryItemLabelGenerator;

//import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.renderer.category.LineAndShapeRenderer;

// CategoryPlot plot= chart.getCategoryPlot();< /FONT>

LineAndShapeRenderer lrenderer = (LineAndShapeRenderer) plot.getRenderer();< /FONT >

lrenderer.setStroke(

new BasicStroke(4f, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL)

);

Image

In order to change the line style please use the below script

Image

//import the necessary classes

import org.jfree.*;

import org.jfree.chart.axis.CategoryAxis;

//import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

//import org.jfree.chart.labels.CategoryItemLabelGenerator;

//import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.renderer.category.LineAndShapeRenderer;

// CategoryPlot plot= chart.getCategoryPlot();< /FONT>

LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();< /FONT >

/*renderer.setStroke(

new BasicStroke(4f, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL)

);
*/

renderer.setSeriesStroke(
0, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {10.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
1, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
2, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {2.0f, 6.0f}, 0.0f
));

Usually the target will not be a straight line, for example if you have forecasted the sales for each month, then the seasonal activity will differ from month to month and hence the target values. You could join to a budget or forecast table and get the target values for each month or period and display them along with the sales quantity. This gives more flexibility rather than hard coding the target value.

 

Custom Shapes on the Line Chart

Image

 

 

 

 

 

 

 

 

 

 

To create the shapes on the line chart use the following javascript

//import the necessary classes

import org.jfree.*;

import org.jfree.chart.axis.CategoryAxis;

//import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

//import org.jfree.chart.labels.CategoryItemLabelGenerator;

//import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;

import org.jfree.chart.plot.CategoryPlot;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.DrawingSupplier;

// CategoryPlot plot= chart.getCategoryPlot();

LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();

//Enable shapes on the line chart
renderer.setShapesVisible(true);
/* renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);

*/
/*
renderer.setStroke(

new BasicStroke(4f, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL)

);
*/

//Create Dotted or Dashed lines
renderer.setSeriesStroke(
0, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {10.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
1, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
2, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {2.0f, 6.0f}, 0.0f
)

Add custom colors

Image

 

 

 

 

 

 

 

 

 

 

//import the necessary classes

import org.jfree.*;

import org.jfree.chart.axis.CategoryAxis;

//import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

//import org.jfree.chart.labels.CategoryItemLabelGenerator;

//import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;

import org.jfree.chart.plot.CategoryPlot;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.DrawingSupplier;

// CategoryPlot plot= chart.getCategoryPlot();

LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();

//Enable shapes on the line chart
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.yellow);

/*
renderer.setStroke(

new BasicStroke(4f, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL)

);
*/

//Create Dotted or Dashed lines
renderer.setSeriesStroke(
0, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {10.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
1, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
2, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {2.0f, 6.0f}, 0.0f
)

Excel Dashboard           OR           Access Dashboard

  1. 1 Trackback(s)

  2. Dec 2, 2008: Add threshold line in a line chart | Line Shapes | Line Customizations | jfreechart | Dashboard Examples - Samples - Tutorials

Post a Comment