Category Step Chart Example using JFreeChart

This Example shows you how to create a category step chart using JFreeChart.
Code of the chart shows you run rate at different over in a match.
In the code given below we have extended class ApplicationFrame to create a frame and also pass a string value to the constructor of
ApplicationFrame class by using super keyword that will be name of the created frame.
The method used in this example are described below:
pack(): This method invokes the layout manager.
centerFrameOnScreen(): This method is used for the position of the frame in the middle of the screen.
setVisible(): This method is used for display frame on the screen.
createCategoryDataset(): This method is used for create data set according to the category.
JFreeChart: JFreeChart class object is used to create new chart according CategoryPlot class
object
setBackgroundPaint(): This method is used to set the paint used to fill the chart background.
CategoryStepChart .java
import java.awt.Color;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.CategoryStepRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class CategoryStepChart extends ApplicationFrame {
public CategoryStepChart(String tietel) {
super(tietel);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(
new java.awt.Dimension(600, 350));
setContentPane(chartPanel);
}
private CategoryDataset createDataset() {
double[][] run = new double[][]{
{5, 4, 3, 6, 7, 8, 3, 6, 3, 12},
{9, 2, 2, 6, 8, 4, 0, 8, 9, 11}
};
double[][] runRate = new double[2][10];
float team1run = 0;
float team2run = 0;
for (int i = 0; i < run[0].length; i++) {
team1run += run[0][i];
team2run += run[1][i];
runRate[0][i] = team1run / (i+1);
runRate[1][i] = team2run / (i+1);
}
return DatasetUtilities.createCategoryDataset(
"Team ", "", runRate);
}
private JFreeChart createChart(CategoryDataset dataset) {
final CategoryItemRenderer renderer =
new CategoryStepRenderer(true);
final CategoryAxis domainAxis =
new CategoryAxis("Over");
final ValueAxis rangeAxis = new NumberAxis("Run Rate");
final CategoryPlot plot =
new CategoryPlot(dataset, domainAxis,
rangeAxis, renderer);
final JFreeChart chart =
new JFreeChart("Score Bord", plot);
chart.setBackgroundPaint(new Color(249, 231, 236));
return chart;
}
public static void main(String args[]) {
CategoryStepChart chart =
new CategoryStepChart("Candle Stick Chart");
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
}
}
|
Output:

Download
code