JFreeChart - SPLessons

JFreeChart Line Chart

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JFreeChart Line Chart

JFreeChart Line Chart

shape Description

A line chart is a kind of diagram which indicates information as a movement of data centers called markers related by straight line segments. It is a fundamental sort of diagram customary in many fields. It resembles a disperse plot beside that the estimation centers are asked for and joined with straight line segments. A line chart is as often as possible used to imagine an example in data over breaks of time. The following is an another definition of the line chart. A line layout is a sensible representation of data that is plotted using a movement of lines. Line charts indicate lines going over the diagram on a level plane, with the qualities center point being appeared on the left 50% of the layout. In the photograph underneath, is an instance of a line chart showing outstanding visitors to Computer Hope.

shape Example

The following is an example. JavaJFreeChartLineChartExample.java [java]package com.splessons; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.HorizontalAlignment; import org.jfree.ui.RectangleEdge; import org.jfree.ui.RefineryUtilities; public class JavaJFreeChartLineChartExample extends ApplicationFrame { public JavaJFreeChartLineChartExample(String title) { super(title); CategoryDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(chartPanel); } private static CategoryDataset createDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(212, "Classes", "JDK 1.0"); dataset.addValue(504, "Classes", "JDK 1.1"); dataset.addValue(1520, "Classes", "SDK 1.2"); dataset.addValue(1842, "Classes", "SDK 1.3"); dataset.addValue(2991, "Classes", "SDK 1.4"); return dataset; } private static JFreeChart createChart(CategoryDataset dataset) { // create the chart... JFreeChart chart = ChartFactory.createLineChart("Java Standard Class Library", // chart // // // title "Release", // domain axis label "Class Count", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation false, // include legend true, // tooltips false // urls ); chart.addSubtitle(new TextTitle("Chat Subtitle")); TextTitle source = new TextTitle(" Text Title Example"); source.setFont(new Font("SansSerif", Font.PLAIN, 10)); source.setPosition(RectangleEdge.BOTTOM); source.setHorizontalAlignment(HorizontalAlignment.RIGHT); chart.addSubtitle(source); chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setRangeGridlinePaint(Color.white); // customise the range axis... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // customise the renderer... LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setShapesVisible(true); renderer.setDrawOutlines(true); renderer.setUseFillPaint(true); renderer.setFillPaint(Color.white); return chart; } public static JPanel createDemoPanel() { JFreeChart chart = createChart(createDataset()); return new ChartPanel(chart); } public static void main(String[] args) { JavaJFreeChartLineChartExample barChart = new JavaJFreeChartLineChartExample("SPLESSONS - Line Chart Demo"); barChart.pack(); RefineryUtilities.centerFrameOnScreen(barChart); barChart.setVisible(true); } } [/java] In the above example the class JavaJFreeChartLineChartExample is extending ApplicationFrame that means all the properties of ApplicationFrame will be used by the class. [java]public class JavaJFreeChartLineChartExample extends ApplicationFrame[/java] All AWT parts will be repeated in Swing moreover with an extra functionalities as an effect of the Swing sections are lightweight fragments while AWT portions are overpowering weight sections. Swing controls are accepting huge part in swing applications, every application will have a couple portions and their relating event gathering of people. Output: The result will be as follows.

Summary

shape Key Points

  • UI Elements, Layouts and Behavior of elements are the main requirements in AWT Controls.
  • The JPanel class is a generic lightweight container.
  • Controls are components that communicate with the users.