Creating charts in java

Java data visualization: Displaying data with charts and graphs

Data visualization is essential for understanding complex data and extracting valuable insights. Java, being one of the most popular programming languages, provides various libraries and tools to create and customize charts and graphs. In this tutorial, we will go through some of the most popular Java libraries for data visualization and learn how to create different types of charts and graphs using these libraries. So let’s get started!

Table of Contents

  • 1. Java Data Visualization Libraries
  • 2. JFreeChart
  • 3. JavaFX Charts
  • 4. XChart
  • 5. Conclusion

1. Java Data Visualization Libraries

There are several Java libraries available for creating charts and graphs. Some of the popular Java data visualization libraries are:

  • JFreeChart: A widely used open-source library for creating a wide variety of charts, including line charts, bar charts, pie charts, and more.
  • JavaFX Charts: A part of the JavaFX library, providing a rich set of charting components for creating interactive and responsive charts.
  • XChart: A lightweight and easy-to-use library for creating interactive and customizable charts with minimal code.

In this tutorial, we will focus on creating charts and graphs using JFreeChart, JavaFX Charts, and XChart.

2. JFreeChart

JFreeChart is a popular open-source library for creating charts. To get started with JFreeChart, you need to download the library from here and add it to your project.

Читайте также:  Тег BODY

2.1 Creating a Simple Bar Chart

Let’s create a simple bar chart using JFreeChart. First, we need to create a dataset containing the data we want to display in the chart:

 import org.jfree.data.category.DefaultCategoryDataset; public class BarChartExample < public static void main(String[] args) < DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(50, "Category 1", "Series 1"); dataset.addValue(60, "Category 1", "Series 2"); dataset.addValue(40, "Category 2", "Series 1"); dataset.addValue(70, "Category 2", "Series 2"); >> 

Next, we need to create a chart using the dataset and customize its appearance:

 import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; // . public static void main(String[] args) < // . dataset creation JFreeChart barChart = ChartFactory.createBarChart( "Bar Chart Example", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false); >

Finally, we need to display the chart in a window:

 import org.jfree.chart.ChartPanel; import javax.swing.JFrame; // . public static void main(String[] args) < // . dataset creation and chart customization ChartPanel chartPanel = new ChartPanel(barChart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); JFrame frame = new JFrame("Bar Chart"); frame.setContentPane(chartPanel); frame.pack(); frame.setVisible(true); >

Running the above code will display a simple bar chart in a window.

3. JavaFX Charts

JavaFX provides a set of charting components that can be used to create interactive and responsive charts. JavaFX charts are part of the JavaFX library, which is included in Java SE 8 and later.

3.1 Creating a Simple Line Chart

Let’s create a simple line chart using JavaFX. First, we need to create a JavaFX application:

 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.stage.Stage; public class LineChartExample extends Application < @Override public void start(Stage stage) < // . >public static void main(String[] args) < launch(args); >> 

Next, we need to create a line chart and add data to it:

 @Override public void start(Stage stage) < stage.setTitle("Line Chart Example"); final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("X Axis"); yAxis.setLabel("Y Axis"); final LineChartlineChart = new LineChart<>(xAxis, yAxis); lineChart.setTitle("Line Chart Example"); XYChart.Series series = new XYChart.Series(); series.setName("Example Data"); series.getData().add(new XYChart.Data(1, 23)); series.getData().add(new XYChart.Data(2, 14)); series.getData().add(new XYChart.Data(3, 15)); series.getData().add(new XYChart.Data(4, 24)); series.getData().add(new XYChart.Data(5, 34)); series.getData().add(new XYChart.Data(6, 36)); series.getData().add(new XYChart.Data(7, 22)); series.getData().add(new XYChart.Data(8, 45)); series.getData().add(new XYChart.Data(9, 43)); series.getData().add(new XYChart.Data(10, 17)); series.getData().add(new XYChart.Data(11, 29)); series.getData().add(new XYChart.Data(12, 25)); lineChart.getData().add(series); Scene scene = new Scene(lineChart, 800, 600); stage.setScene(scene); stage.show(); > 

Running the above code will display a simple line chart in a window.

4. XChart

XChart is a lightweight and easy-to-use library for creating interactive and customizable charts with minimal code. To get started with XChart, you need to add the library to your project using Maven or Gradle. For example, add the following dependency to your Maven pom.xml file:

  org.knowm.xchart xchart 3.8.0  

4.1 Creating a Simple Pie Chart

Let’s create a simple pie chart using XChart. First, we need to create a chart and add data to it:

 import org.knowm.xchart.PieChart; import org.knowm.xchart.PieChartBuilder; import org.knowm.xchart.SwingWrapper; public class PieChartExample < public static void main(String[] args) < PieChart chart = new PieChartBuilder() .width(800) .height(600) .title("Pie Chart Example") .build(); chart.addSeries("A", 30); chart.addSeries("B", 20); chart.addSeries("C", 40); chart.addSeries("D", 10); >> 

Next, we need to display the chart in a window:

 public static void main(String[] args) < // . chart creation and data addition new SwingWrapper<>(chart).displayChart(); > 

Running the above code will display a simple pie chart in a window.

5. Conclusion

In this tutorial, we have learned how to create various charts and graphs using popular Java data visualization libraries such as JFreeChart, JavaFX Charts, and XChart. These libraries make it easy to create visually appealing and interactive charts, allowing you to understand better and present your data.

If you’re looking for dedicated Java developers proficient in these libraries, Reintech can help you find the right remote Java development team for your project.

Are you an engineering manager tired of recruiting?

Checkout out how we can help you to focus on delivering technical excellence and growing your product by hiring remote developers and creating high-performing teams.

Источник

Working with JavaFX Charts

This tutorial describes the graphical charts available in the javafx.scene.chart package of the JavaFX SDK and contains the following chapters:

  • Pie Chart Describes a chart that represents data in a form of circle divided into triangular wedges called slices.
  • Line Chart Describes the line chart, a type of two-axis chart that presents data as a series of points connected by straight lines
  • Area Chart Describes the area chart that presents data as an area between a series of points connected by straight lines and the axis.
  • Bubble Chart Describes the bubble chart, a two-axis chart that plots bubbles for the data points in a series.
  • Scatter Chart Describes the scatter chart, a two-axis chart that presents its data as a set of points.
  • Bar Chart Describes the bar chart, a two-axis chart that presents discrete data with rectangular bars.

Each chapter provides code samples and applications to illustrate how to use a particular chart. You can find the source files of the applications and the corresponding NetBeans projects in the Chart Samples appendix.

Close Window

Table of Contents

JavaFX: Working with JavaFX UI Components

  • Title and Copyright Information
  • Preface
    • Audience
    • Documentation Accessibility
    • Related Documents
    • Conventions
    • 1 What Is New
    • 2 Label
      • Creating a Label
      • Setting a Font
      • Wrapping Text
      • Applying Effects
      • Creating a Button
      • Assigning an Action
      • Applying Effects
      • Styling a Button
      • Creating a Radio Button
      • Adding Radio Buttons to Groups
      • Processing Events for Radio Buttons
      • Requesting Focus for a Radio Button
      • Creating a Toggle Button
      • Adding Toggle Buttons to a Group
      • Setting the Behavior
      • Styling Toggle Buttons
      • Creating Checkboxes
      • Defining a State
      • Setting the Behavior
      • Creating a Choice Box
      • Setting the Behavior for a Choice Box
      • Applying a Tooltip
      • Creating a Text Field
      • Building the UI with Text Fields
      • Processing Text Field Data
      • Creating a Password Field
      • Evaluating the Password
      • Creating a Scroll Bar
      • Using a Scroll Bar in Your Application
      • Creating a Scroll Pane
      • Setting the Scroll Bar Policy for a Scroll Pane
      • Resizing Components in the Scroll Pane
      • Sample Application with a Scroll Pane
      • Creating a List View
      • Populating a List View with Data
      • Customizing the Content of a List View
      • Processing the List Item Selection
      • Creating a Table
      • Defining the Data Model
      • Adding New Rows
      • Sorting Data in Columns
      • Editing Data in the Table
      • Adding Maps of Data to the Table
      • Creating Tree Views
      • Implementing Cell Factories
      • Adding New Tree Items on Demand
      • Using Tree Cell Editors
      • Creating a TreeTableView control
      • Adding Several Columns
      • Altering Visual Appearance
      • Managing Selection Mode
      • Creating Combo Boxes
      • Editable Combo Boxes
      • Applying Cell Factories to Combo Boxes
      • Creating a Separator
      • Adding Separators to the UI of Your Application
      • Styling Separators
      • Creating a Slider
      • Using Sliders in Graphical Applications
      • Creating Progress Controls
      • Indicating Progress in Your User Interface
      • Creating a Hyperlink
      • Linking the Local Content
      • Linking the Remote Content
      • Adding an HTML Editor
      • Using an HTML Editor to Build the User Interface
      • Obtaining HTML Content
      • Creating a Tooltip
      • Presenting Application Data in Tooltips
      • Creating Titled Panes
      • Adding Titled Panes to an Accordion
      • Processing Events for an Accordion with Titled Panes
      • Building Menus in JavaFX Applications
      • Creating a Menu Bar
      • Adding Menu Items
      • Creating Submenus
      • Adding Context Menus
      • Design Overview
        • Color Chooser
        • Color Palette
        • Custom Color Dialog Window
        • Working with Time Data and Date Formats
        • Date Picker Design Overview
        • Adding a Date Picker to an Application UI
        • Customizing the Date Picker
        • Altering the Calendar System
        • Creating a Pagination Control
        • Implementing Page Factories
        • Styling a Pagination Control
        • Opening Files
        • Configuring a File Chooser
        • Setting Extension Filters
        • Saving Files
        • Applying CSS
        • Altering Default Behavior
        • Implementing Cell Factories
        • Embedded Runtime Features
          • Support for Touch-Enabled Devices
          • Virtual Keyboard
          • Scrolling Controls
          • Text Input Controls
          • Context Menus
          • 31 Pie Chart
            • Creating a Pie Chart
            • Setting a Pie Chart and a Legend
            • Processing Events for a Pie Chart
            • Chart Settings
            • Chart Data
            • Creating a Line Chart
            • Creating Categories for a Line Chart
            • Adding Series to the Line Chart
            • Creating an Area Chart
            • Creating a Stacked Area Chart
            • Setting Axis and Tick Properties
            • Adding Negative Values
            • Styling Area Charts
            • Creating a Bubble Chart
            • Using the Extra Value Property
            • Changing the Appearance Visual Setting of the Plot and Tick Marks
            • Creating a Scatter Chart
            • Managing Chart Data
            • Adding Effects to Charts
            • Changing the Chart Symbol
            • Creating a Bar Chart
            • Horizontal Bar Chart
            • Creating a Stacked Bar Chart
            • Animating Data in Charts
            • 37 Styling UI Controls with CSS
              • Default Style Sheet
              • Creating Style Sheets
              • Defining Styles
                • Selectors
                • Rules and Properties
                • Overriding Default Styles
                • Creating Class Styles
                • Creating ID Styles
                • Setting Styles in the Code
                • Modifying Basic Chart Elements
                • Altering Colors of the Chart Plot
                • Setting the Axes
                • Setting Chart Colors
                • Changing Chart Symbols
                • 39 Using Text in JavaFX
                  • Introduction
                  • Adding Text
                    • Setting Text Font and Color
                    • Making Text Bold or Italic
                    • Using Custom Fonts
                    • Setting LCD Text Support
                    • Rich Text and Bidirectional Support
                    • Perspective Effect
                    • Blur Effect
                    • Drop Shadow Effect
                    • Inner Shadow Effect
                    • Reflection
                    • Combining Several Effects
                    • Application Files
                    • A UI Control Samples
                      • LabelSample.java
                      • ButtonSample.java
                      • RadioButtonSample.java
                      • ToggleButtonSample.java
                      • CheckboxSample.java
                      • ChoiceBoxSample.java
                      • TextFieldSample.java
                      • PasswordField.java
                      • ScrollBarSample.java
                      • ScrollPaneSample.java
                      • ListViewSample.java
                      • TableViewSample.java
                      • TreeViewSample.java
                      • TreeTableViewSample.java
                      • ComboBoxSample.java
                      • SeparatorSample.java
                      • SliderSample.java
                      • ProgressSample.java
                      • HyperlinkSample.java
                      • HyperlinkWebViewSample.java
                      • HTMLEditorSample.java
                      • TooltipSample.java
                      • TitledPaneSample.java
                      • MenuSample.java
                      • ColorPickerSample.java
                      • DatePickerSample.java
                      • PaginationSample.java
                      • FileChooserSample.java
                      • PieChartSample.java
                      • LineChartSample.java
                      • AreaChartSample.java
                      • BubbleChartSample.java
                      • ScatterChartSample.java
                      • BarChartSample.java
                      • DownloadButton.java
                      • DownloadButtonStyle1.css
                      • DownloadButtonStyle2.css
                      • StyleStage.java
                      • UIControlCSS.java
                      • controlStyle1.css
                      • controlStyle2.css
                      • TextEffects.java
                      • NeonSign.java

                      Источник

Оцените статью