Data Visualisation In Java

This is a java program where you can visualize the data in form of charts, i have explained everything with code. That’s why this is going to be a long article. So let’s get started.

We will be working with covid-19 csv file, because it’s make things easy to explain.

I have upload all the code on github profile. Which you can check by link in bio. I am going to upload the code here and then the screenshot of code working. I will try to explain how it works.

Github:https://github.com/isauravmanitripathi/covid-19-data-visualization

GUI.java

First thing we need to do is to create a GUI, which the user will first see when he/she runs our program. Below is the code for the GUI. Upon execution this code will show an output like this:

Image by Author

GUI.java Code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.general.PieDataset;
import org.jfree.chart.plot.RingPlot;
import org.jfree.data.category.CategoryDataset;
import javax.swing.ImageIcon;
public final class gui extends JFrame {
public gui() {
assignment_event_handler = new GUIEventHandler();
FILE_CHOOSER = new JFileChooser();

this.setJMenuBar(createMenuBar());
createControlPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setMinimumSize(new Dimension(MIN_FRAME_WIDTH, MIN_FRAME_HEIGHT));assignment_chartmaker = new choosecolour();
assignment_chartmaker.setVisible(false);
}
public void ActivateColour() {
assignment_exportVisualization_Button.setEnabled(true);
assignment_editMenuIteassignment_ChangeColourScheme.setEnabled (true);
assignment_ChangeColourScheme_Button.setEnabled (true);
}
public void DisactivateColour() {

assignment_editMenuIteassignment_ChangeColourScheme.setEnabled (false);
assignment_ChangeColourScheme_Button.setEnabled (false);
}
public void RedrawChartColour() {

JFreeChart chart;
final int NOCHART = -1;
final int BARCHART = 0;
final int LINECHART = 1;
final int RINGCHART = 2;
final int AREACHART = 3;
if (assignment_chart.GetChart() == null) {
System.out.println("Error: No chart object found.");
} else {
chart = assignment_chart.GetChart();

if (assignment_chart.GetChartType() == NOCHART) {
System.out.println("Error: No chart object found.");

} else {
if (assignment_chart.GetChartType() == BARCHART) {
barcoloured renderer = new barcoloured(
gui.assignment_chartmaker.GetActiveMap());
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryDataset barDataset = plot.getDataset();
int i = 1;
while (i <= barDataset.getRowCount()) {
renderer.SetColor(plot, i);
i++;
}
} else {
if (assignment_chart.GetChartType() == LINECHART) {
linecoloured renderer = new linecoloured(
assignment_chartmaker.GetActiveMap());
CategoryPlot plot = (CategoryPlot) chart.getPlot();
renderer.SetColor(plot, 1);
} else {
if (assignment_chart.GetChartType() == RINGCHART) {
RingPlot plot = (RingPlot) chart.getPlot();
PieDataset dataset = plot.getDataset();
ringcoloured renderer = new ringcoloured(
assignment_chartmaker.GetActiveMap());
renderer.SetColor(plot, dataset);
} else {
if (assignment_chart.GetChartType() == AREACHART) {
areacoloured renderer = new areacoloured(
assignment_chartmaker.GetActiveMap());
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setForegroundAlpha(0.5f);
renderer.SetColor(plot, 1);
}
}
}
}
}
}
}
public void SelectChartTab() {

assignment_tabbedPanel.setSelectedIndex(CHART_PANEL);
}
private void generateandexport() {

assignment_drawchartbutton.setEnabled (true);
assignment_exportVisualization_Button.setEnabled (true);
assignment_editMenuIteassignment_DrawChart.setEnabled (true);
assignment_file_menu_Iteassignment_Export.setEnabled (true);
assignment_Forecast_Button.setEnabled(true);
assignment_editMenuIteassignment_Forecast.setEnabled(true);
}
private JPanel createButtonPanel(){ImageIcon openImg = new ImageIcon();
assignment_newFile1_Button = new JButton(openImg);
assignment_newFile1_Button.setOpaque(false);
assignment_newFile1_Button.setContentAreaFilled(false);
assignment_newFile1_Button.setBorderPainted(false);
assignment_newFile1_Button.addActionListener(assignment_event_handler);ImageIcon exportImg = new ImageIcon();
assignment_exportVisualization_Button = new JButton(exportImg);
assignment_exportVisualization_Button.setOpaque(false);
assignment_exportVisualization_Button.setContentAreaFilled(false);
assignment_exportVisualization_Button.setBorderPainted(false);
assignment_exportVisualization_Button.addActionListener(assignment_event_handler);ImageIcon chartImg = new ImageIcon();
assignment_drawchartbutton = new JButton(chartImg);
assignment_drawchartbutton.setOpaque(false);
assignment_drawchartbutton.setContentAreaFilled(false);
assignment_drawchartbutton.setBorderPainted(false);
assignment_drawchartbutton.addActionListener(assignment_event_handler);ImageIcon colourPickerImg = new ImageIcon();
assignment_ChangeColourScheme_Button = new JButton(colourPickerImg);
assignment_ChangeColourScheme_Button.setOpaque(false);
assignment_ChangeColourScheme_Button.setContentAreaFilled(false);
assignment_ChangeColourScheme_Button.setBorderPainted(false);
assignment_ChangeColourScheme_Button.addActionListener(assignment_event_handler);ImageIcon forecastImg = new ImageIcon();
assignment_Forecast_Button = new JButton(forecastImg);
assignment_Forecast_Button.setOpaque(false);
assignment_Forecast_Button.setContentAreaFilled(false);
assignment_Forecast_Button.setBorderPainted(false);
assignment_Forecast_Button.addActionListener(assignment_event_handler);JPanel panel = new JPanel();return panel;
}
private void createControlPanel() {

JPanel assignment_buttonPanel = createButtonPanel();
JTabbedPane tabPanel = newTabPanel();
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1,1));
controlPanel.add(assignment_buttonPanel);
add(controlPanel, BorderLayout.NORTH);
add(tabPanel);
}
private JMenu editingmenu() {assignment_editMenu = new JMenu("Edit");
menubar123.add(assignment_editMenu);
assignment_editMenuIteassignment_DrawChart = new JMenuItem("Draw Chart");assignment_editMenuIteassignment_DrawChart.addActionListener(assignment_event_handler);assignment_editMenuIteassignment_ChangeColourScheme = new JMenuItem(
);
assignment_editMenuIteassignment_ChangeColourScheme.addActionListener(assignment_event_handler);assignment_editMenuIteassignment_Forecast = new JMenuItem("Forecast");assignment_editMenuIteassignment_Forecast.addActionListener(assignment_event_handler);assignment_editMenuIteassignment_DrawChart.setEnabled (false);
assignment_editMenuIteassignment_ChangeColourScheme.setEnabled (false);
assignment_editMenuIteassignment_Forecast.setEnabled(false);
assignment_editMenu.add(assignment_editMenuIteassignment_DrawChart);
assignment_editMenu.add(assignment_editMenuIteassignment_ChangeColourScheme);
assignment_editMenu.add(assignment_editMenuIteassignment_Forecast);

return assignment_editMenu;
}
private JMenu newfilemenu() {assignment_file_menu_ = new JMenu("CSV FILE");
menubar123.add(assignment_file_menu_);
assignment_file_menu_Iteassignment_Open = new JMenuItem("Open");assignment_file_menu_Iteassignment_Open.addActionListener(assignment_event_handler);

assignment_file_menu_Iteassignment_Export = new JMenuItem("Save Chart");
assignment_file_menu_Iteassignment_Export.addActionListener(assignment_event_handler);

assignment_file_menu_Iteassignment_Exit = new JMenuItem("Exit");
assignment_file_menu_Iteassignment_Exit.addActionListener(assignment_event_handler);assignment_file_menu_Iteassignment_Export.setEnabled (false);assignment_file_menu_.add(assignment_file_menu_Iteassignment_Open);
assignment_file_menu_.add(assignment_file_menu_Iteassignment_Export);
assignment_file_menu_.addSeparator();
assignment_file_menu_.add(assignment_file_menu_Iteassignment_Exit);

return assignment_file_menu_;
}
private JMenuBar createMenuBar() {//creates the menu bar at the top

menubar123 = new JMenuBar();
newfilemenu();
editingmenu();
return menubar123;
}
private JTabbedPane newTabPanel(){//creates the tabsassignment_tabbedPanel = new JTabbedPane();assignemnt_gui_panel1 = new JPanel();
assignemnt_gui_panel1.setLayout(new GridLayout(1, 1));
assignment_tabbedPanel.addTab( "CSV File", assignemnt_gui_panel1);
assignment_gui_panel2 = new JPanel();
assignment_gui_panel2.setLayout(new GridLayout(1, 1));
assignment_tabbedPanel.addTab( "Chart", assignment_gui_panel2 );
add(assignment_tabbedPanel, BorderLayout.CENTER);
return assignment_tabbedPanel;
}
private Boolean newFile1(){assignment_dataSetBackup = assignment_dataSet;
//opens file and then outputs it into a table as well as gets the data ready in formal to put into charts
if (openingfilefirst) {
openingfilefirst = false;
FileFilter extensionFilter = new FileNameExtensionFilter(
"CSV Files (.csv)", "csv");
FILE_CHOOSER.addChoosableFileFilter(extensionFilter);
}
int returnVal = FILE_CHOOSER.showOpenDialog(gui.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File assignment_file = FILE_CHOOSER.getSelectedFile();
assignment_dataSet = new chartdata();
if (assignment_dataSet.BuildDataSet(assignment_file)) {
DisactivateColour();
assignemnt_gui_panel1.removeAll();
assignemnt_gui_panel1.add(new multiplefilechooser(assignment_dataSet));
assignemnt_gui_panel1.repaint();
assignemnt_gui_panel1.revalidate();
assignment_tabbedPanel.setSelectedIndex(TABLE_PANEL);
if (loadingfile) {assignment_chart = new chartgenerator(assignment_dataSet);
loadingfile = false;
} else {
assignment_chart.DisposeWindow();
assignment_chart = new chartgenerator(assignment_dataSet);
}
assignment_gui_panel2.removeAll();
assignment_gui_panel2.add(assignment_chart);
assignment_chartmaker.setVisible(false);
} else {
assignment_dataSet = assignment_dataSetBackup;
return false;
}
} else {
return false;
}
return true;
}
private class GUIEventHandler implements ActionListener {@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == assignment_file_menu_Iteassignment_Open
|| event.getSource() == assignment_newFile1_Button) {
try {
if (newFile1()) {
generateandexport();
assignment_exportVisualization_Button.setEnabled(false);
} else {
}
} catch (NullPointerException e) {
}} else {if (event.getSource() != assignment_file_menu_Iteassignment_Exit) {
if(event.getSource()== assignment_Forecast_Button || event.getSource() == assignment_editMenuIteassignment_Forecast){
System.out.println("bruh");
Linear_Regression lr = new Linear_Regression();//starts linear regression. Opens in a new frame
lr.Setup(assignment_dataSet);
}
else if (event.getSource() == assignment_exportVisualization_Button ||
event.getSource() == assignment_file_menu_Iteassignment_Export) {
int chartwidth = assignment_gui_panel2.getSize().width;
int chartheight = assignemnt_gui_panel1.getSize().height;
graphsaver saver = new graphsaver(assignment_chart, chartwidth, chartheight);saver.setJPanel(assignment_gui_panel2);} else {
if (event.getSource() != assignment_drawchartbutton
&& event.getSource() != assignment_editMenuIteassignment_DrawChart) {
if (event.getSource() == assignment_editMenuIteassignment_ChangeColourScheme
|| event.getSource() == assignment_ChangeColourScheme_Button) {
assignment_chartmaker.setVisible(true);
}
} else {
try {
assignment_chart.SetWindowVisible();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
} else {
dispose();}
}
}
} /* end class GUIEventHandler */

public static choosecolour assignment_chartmaker;
private final int FRAME_WIDTH = 600;
private final int FRAME_HEIGHT = 600;
private final int MIN_FRAME_WIDTH = 500;
private final int MIN_FRAME_HEIGHT = 500;
private final int TABLE_PANEL = 0;
private final int CHART_PANEL = 1;
private final JFileChooser FILE_CHOOSER;private Boolean openingfilefirst = true;
private Boolean loadingfile = true;

private GUIEventHandler assignment_event_handler;
private JMenuBar menubar123;
private JMenu assignment_file_menu_;
private JMenu assignment_editMenu;
private JMenuItem assignment_file_menu_Iteassignment_Open;
private JMenuItem assignment_file_menu_Iteassignment_Export;
private JMenuItem assignment_file_menu_Iteassignment_Exit;
private JMenuItem assignment_editMenuIteassignment_DrawChart;
private JMenuItem assignment_editMenuIteassignment_ChangeColourScheme;
private JMenuItem assignment_editMenuIteassignment_Forecast;
private JButton assignment_newFile1_Button;
private JButton assignment_exportVisualization_Button;
private JButton assignment_drawchartbutton;
private JButton assignment_ChangeColourScheme_Button;
private JButton assignment_Forecast_Button;
private JTabbedPane assignment_tabbedPanel;
private JPanel assignemnt_gui_panel1;
private JPanel assignment_gui_panel2;
private chartdata assignment_dataSet;private chartdata assignment_dataSetBackup;
private chartgenerator assignment_chart;
public static void main(String[] args) {
gui dataVisualizer = new gui();
dataVisualizer.setLocationRelativeTo(null);
dataVisualizer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dataVisualizer.setTitle("Data Visualizer");
dataVisualizer.setVisible(true);
}
}

Choosing File

The following code allows you to choose the file which you want to visualise.

multiplefilechooser.java

import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import java.io.File; /* Used in testing */



public class
multiplefilechooser extends JPanel {


public multiplefilechooser(chartdata ds) {

super(new GridLayout(1, 0));

final JTable table = new JTable(ds.GetData(), ds.GetAttributes()) {
private static final long serialVersionUID = 1L;

public boolean getScrollableTracksViewportWidth(){
return getPreferredSize().width < getParent().getWidth();
}
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disable cell editing
}
};

table.setFillsViewportHeight(true);


table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);


DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );

for(int i=0 ; i < ds.GetNoOfAttributes(); i++){
table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
}


JScrollPane scrollPane = new JScrollPane(table);

this.add(scrollPane);
this.setOpaque(true);
}


public static void main(String args[]) {
File inputfile = new File("test.csv");
chartdata data = new chartdata();
data.BuildDataSet(inputfile);
System.out.println("TableView:: TableView(testdata)");
multiplefilechooser table = new multiplefilechooser(data);
table.show();
System.out.println("TableView:: TableView(testdata) - Test Passed");
}


} /* end TableView class */

Upon execution this will output the following:

Here you can select/find the csv file which you want to visualise. In particular i will using the csv file provided by the british government on coronavirus. Which you download by click this:

When you open your file, this is what you will see:

When you click CSV file, there are few options available for you:

You can either open a csv file, you can save your chart(this will only happen when there is a chart availble. I will talk about this further) and you can exit the program basically terminate the java program.

After this we need a visualiser, that will help us visualise a chart from the data provided.

Visualising the Data

Upon execution this file will ask for columns which it will use to plot a chart. As this generate a 2D graph, so we are working with only two axis. One axis will only take input as a string and the other will only take input as an integer. The reason for this is that when i was trying to plot a the chart using data as a column it was showing an error, the error was caused because the date column contained a slash (/) between them. (example: 29/01/2020). So i decide to change this axis as a string rather than integer. And then made further modification to add the integers. Now it works fine.

Here is a demo screenshot of how it is supposed to look:

Image by Author:

You can also change the name of file here, and then simply generate the file. You can also choose which type of chart you want. You have different options such as bar chart, line chart, area chart, ring chart, bubble chart, scatter chart.

import java.io.File;


public class visualizer {


public int firstattribute() {
return assignment_attribute1;
}


public int secondattribute() {
return assignment_attribute2;
}


public chartdata getDataset() {
return assignment_dataset;
}


public String getHeader() {
return assignment_header;
}


public String dataforxaxis() {
return assignment_xAxis;
}


public String dataforyaxis() {
return assignment_yAxis;
}


public boolean setAttribute1 (int attribute1) {
assignment_attribute1 = attribute1;
return true;
}


public boolean setAttribute2 (int attribute2) {
assignment_attribute2 = attribute2;
return true;
}


public boolean setDataset (chartdata dataset) {
assignment_dataset = dataset;
return true;
}


public boolean setHeader (String header) {
assignment_header = header;
return true;
}


public boolean setxAxis (String xAxis) {
assignment_xAxis = xAxis;
return true;
}


public boolean setyAxis (String yAxis) {
assignment_yAxis = yAxis;
return true;
}

private chartdata assignment_dataset;
private int assignment_attribute1;
private int assignment_attribute2;
private String assignment_header;
private String assignment_xAxis;
private String assignment_yAxis;

public static void main(String args[]){

final int TEST_DATA = 100;


System.out.println("Visualization:: Visualization()");
visualizer testVisualization=new visualizer();
System.out.println("Visualization:: Visualization() - Test Passed");


chartdata testdataset = new chartdata();
File inputfile = new File("test.csv");
testdataset.BuildDataSet(inputfile);


System.out.println("Visualization:: setDataset()");
testVisualization.setDataset(testdataset);
System.out.println("Visualization:: setDataset() - Test Passed");


System.out.println("Visualization:: setAttribute1()");
boolean testSetAttribute1=testVisualization.setAttribute1(TEST_DATA);
System.out.println("Visualization:: setAttribute1() - Test Passed");


System.out.println("Visualization:: setAttribute2()");
boolean testSetAttribute2=testVisualization.setAttribute2(TEST_DATA);
System.out.println("Visualization:: setAttribute2() - Test Passed");


System.out.println("Visualization:: setHeader()");
boolean testSetHeader=testVisualization.setHeader("Visulization");
System.out.println("Visualization:: setHeader() - Test Passed");


System.out.println("Visualization:: setxAxis()");
boolean testSetxAxis=testVisualization.setxAxis("xAxis");
System.out.println("Visualization:: setxAxis() - Test Passed");


System.out.println("Visualization:: setyAxis()");
boolean testSetyAxis=testVisualization.setyAxis("yAxis");
System.out.println("Visualization:: setyAxis() - Test Passed");


System.out.println("Visualization:: getDataset()");
testVisualization.getDataset();
System.out.println("Visualization:: getDataset() - Test Passed");


System.out.println("Visualization:: firstattribute()");
int testfirstattribute=testVisualization.firstattribute();
System.out.println("Visualization:: firstattribute() - Test Passed");


System.out.println("Visualization:: secondattribute()");
int testsecondattribute=testVisualization.secondattribute();
System.out.println("Visualization:: secondattribute() - Test Passed");


System.out.println("Visualization:: getHeader()");
String testGetHeader=testVisualization.getHeader();
System.out.println("Visualization:: getHeader() - Test Passed");


System.out.println("Visualization:: dataforxaxis()");
String dataforxaxis=testVisualization.dataforxaxis();
System.out.println("Visualization:: dataforxaxis - Test Passed");


System.out.println("Visualization:: dataforyaxis()");
String testdataforyaxis=testVisualization.dataforyaxis();
System.out.println("Visualization:: dataforyaxis() - Test Passed");
}

} /* End of Visualization Class */

So now you have decided which file to open and what kind of chart you want to draw, so now lets talk about all the charts available.

ChartData.java

This will help us to visualize the chart by feeding data into it. Here we also find the columns names and and we will be able to divide data into integer and strings.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



public class chartdata {


public String GetAttributeName(int columnNo){

if (columnNo < assignment_attributeCount) {
return assignment_attributes[columnNo];
} else {
JPanel frame = new JPanel();
JOptionPane.showMessageDialog(frame,
"Data Out Of Bound",
"Error",
JOptionPane.ERROR_MESSAGE);
}
return null;
}


public String[] GetAttributes() {

return assignment_attributes;
}


public Object[] GetColumnData(int columnNo){

if (columnNo < assignment_attributeCount) {
assignment_columnData = new Object[assignment_entryCount];
for(int i = 0; i<assignment_entryCount; i++){
assignment_columnData[i] = assignment_data[i][columnNo];
}
return assignment_columnData;
} else {
JPanel frame = new JPanel();
JOptionPane.showMessageDialog(frame,
"Column Index out of bounds",
"File error",
JOptionPane.ERROR_MESSAGE);
}
return null;
}


public Object [][] GetData() {
return assignment_data;
}


public int GetNoOfAttributes(){
return assignment_attributeCount;
}


public int GetNoOfEntrys(){
return assignment_entryCount;

}


public Boolean BuildDataSet (File dataFile) {

boolean test = false;

if(!checkForValidFile(dataFile)){
return false;
}

try {
assignment_fileScan = new Scanner(dataFile);
} catch (FileNotFoundException e) {
}

assignment_attributes = new String[assignment_attributeCount];
assignment_lineScanner = new Scanner(assignment_fileScan.nextLine());
assignment_lineScanner.useDelimiter(assignment_delimiter);

{
int i=0;
while (i<assignment_attributeCount) {
assignment_attributes[i]=assignment_lineScanner.next();
i++;
}
}

assignment_data = new Object[assignment_entryCount][assignment_attributeCount];

int i=0;
while (i<assignment_entryCount) {
assignment_lineScanner = new Scanner(assignment_fileScan.nextLine());
assignment_lineScanner.useDelimiter(assignment_delimiter);

int n=0;
while (n<assignment_attributeCount) {
if(!(assignment_lineScanner.hasNext())){
i--;
break;
}

assignment_dataEntry=assignment_lineScanner.next();
switch (assignment_dataEntry) {
case "" -> n--;
default -> assignment_data[i][n] = assignment_dataEntry;
}
n++;
}
i++;
}
if (test) {
System.out.println("Data Set Built");
}
return true;
}


private boolean checkForAttributes(File dataFile) {

try {
assignment_fileScan = new Scanner(dataFile);
} catch (FileNotFoundException e) {
return false;
}
assignment_delimiter=",";
assignment_lineScanner= new Scanner(assignment_fileScan.nextLine());
assignment_lineScanner.useDelimiter(assignment_delimiter);

while(assignment_lineScanner.hasNext()){
try {
Double.parseDouble(assignment_lineScanner.next());
} catch(NumberFormatException nfe) {
return true;
}
}
return false;
}


private Boolean checkForCommas (File dataFile) {

assignment_delimiter="";

try {
assignment_fileScan = new Scanner(dataFile);
} catch (FileNotFoundException e) {
return false;
}
assignment_fileScan.useDelimiter(assignment_delimiter);

if (!assignment_fileScan.hasNext()) {
return false;
}
do {
String assignment_charCheck = assignment_fileScan.next();
if (!assignment_charCheck.equals(",")) {
} else {
return true;
}
} while (assignment_fileScan.hasNext());
return false;
}


private boolean checkForConsistentData(File dataFile) {

try {
assignment_fileScan = new Scanner(dataFile);
} catch (FileNotFoundException e) {
}

assignment_entryCount=0;
assignment_attributeCount=countAttributes(dataFile);
assignment_delimiter=",";

if (assignment_fileScan.hasNextLine()) {
do {
assignment_lineScanner = new Scanner(assignment_fileScan.nextLine());
assignment_lineScanner.useDelimiter(assignment_delimiter);
assignment_lineDataCount = 0;
assignment_emptyEntryCount = 0;

if (assignment_lineScanner.hasNext()) {
do {
assignment_dataEntry = assignment_lineScanner.next();
if (assignment_dataEntry.equals("")) {
assignment_emptyEntryCount++;
}
assignment_lineDataCount++;
} while (assignment_lineScanner.hasNext());
}

if (assignment_lineDataCount - assignment_emptyEntryCount == assignment_attributeCount) {
} else {
if (!(assignment_lineDataCount - assignment_emptyEntryCount == 0)) {
return false;
} else {
assignment_entryCount--;
}
}
assignment_entryCount++;
} while (assignment_fileScan.hasNextLine());
}
return true;
}


private Boolean checkForValidFile(File dataFile){

final JPanel frame = new JPanel();
if (checkForCommas(dataFile)) {
if (!checkForAttributes(dataFile)) {
JOptionPane.showMessageDialog(frame,
"File Corrupted",
"Error",
JOptionPane.ERROR_MESSAGE);
return false;
}

if (!checkForConsistentData(dataFile)) {
JOptionPane.showMessageDialog(frame,
"Missing Values In Some Cells",
"Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
} else {
JOptionPane.showMessageDialog(frame,
"There are no Commas In File",
"Error",
JOptionPane.ERROR_MESSAGE);
return false;
}

}


private int countAttributes(File dataFile) {

assignment_numberOfAttributes = 0;
assignment_delimiter=",";

try {
assignment_fileScan = new Scanner(dataFile);
} catch (FileNotFoundException e) {
}
assignment_lineScanner = new Scanner(assignment_fileScan.nextLine());
assignment_lineScanner.useDelimiter(assignment_delimiter);
while(assignment_lineScanner.hasNext()){
assignment_lineScanner.next();
assignment_numberOfAttributes++;
}
return assignment_numberOfAttributes;
}


private Scanner assignment_fileScan;


private String assignment_delimiter;


private Scanner assignment_lineScanner;


private int assignment_attributeCount;


private int assignment_entryCount;


private int assignment_lineDataCount;


private String[] assignment_attributes;


private Object[][] assignment_data;


private String assignment_dataEntry;


private int assignment_emptyEntryCount;


private Object[] assignment_columnData;


private int assignment_numberOfAttributes;

public static void main(String[] args) {
//Test Class
chartdata testDataSet = new chartdata();
//Test One
System.out.println("DataSet:: GetData()");
Object[][] test1Object = testDataSet.GetData();
System.out.println("DataSet:: GetData() - Test Passed");
//Test Two
System.out.println("DataSet:: GetAttirbutes()");
String[] test2Attributes = testDataSet.GetAttributes();
System.out.println("DataSet:: GetAttributes() - Test Passed");
//Test Three
System.out.println("DataSet:: GetColumnData(2) - No File");
Object[] test3Object = testDataSet.GetColumnData(2);
//Getting random information from a testDataSet
//No file has been given, so no data should exist.
//Expecting null.
if
(test3Object == null) {
System.out.println("DataSet:: GetColumnData(2) - Test Passed");
} else {
System.out.println("DataSet:: GetColumnData(2) - Unexpected Error");
System.out.println(test3Object);
}
//Test Four
System.out.println("DataSet:: GetAttributeName(2)");
String test4AttributeName = testDataSet.GetAttributeName(2);
//Getting random information from a testDataSet
//No File - Expecting null
if
(test4AttributeName == null) {
System.out.println("DataSet:: GetAttributeName(2) - Test Passed");
} else {
System.out.println("DataSet:: GetAttributeName(2) - Test Failed");
System.out.println(test4AttributeName);
}
//Test Five
System.out.println("DataSet:: GetNoOfEntrys()");
int test5entries = testDataSet.GetNoOfEntrys();
System.out.println("DataSet:: GetNoOfEntrys() - Test Passed");
//Test Six
System.out.println("DataSet:: GetNoOfAttributes()");
int test6entries = testDataSet.GetNoOfAttributes();
System.out.println("DataSet:: GetNoOfAttributes() - Test Passed");

System.out.println("DataSet:: BuildDataSet(Typical File)");
File inputfile = new File("test.csv");

//Test Seven
boolean
test7 = testDataSet.BuildDataSet(inputfile);
if (test7) {
System.out.println("DataSet:: BuildDataSet - Test Passed");
} else {
System.out.println("DataSet:: BuildDataSet - Unexpected Error");
}
//Test Eight
System.out.println("DataSet:: GetNoOfEntrys()");
int test8entries = testDataSet.GetNoOfEntrys();
final int ENTRIES = 11;
if (test8entries == ENTRIES) {
System.out.println("DataSet:: GetNoOfEntrys() - Test Passed");
} else {
System.out.println("DataSet:: GetNoOfEntrys() - Test Failed");
System.out.println(test8entries);
}
//Test Nine
System.out.println("DataSet:: GetNoOfAttributes()");
int test9entries = testDataSet.GetNoOfAttributes();
final int ATTRIBUTES = 2;
if (test9entries == ATTRIBUTES) {
System.out.println("DataSet:: GetNoOfAttributes() - Test Passed");
} else {
System.out.println("DataSet:: GetNoOfAttributes() - Test Failed");
System.out.println(test9entries);
}
}
}

I have also added some fail safe method here:

  1. If the file is corrupt it will show an error message, and it will not open the file.
  2. If there are some missing values in the file it is going to show you an error. Saying that there are some missing values.
  3. If you are trying to open a pdf or word file or anything apart from a csv file it is going to show you an error, saying there are no commas in file.

ChartGenerator.java

This is the chart generator, it will ask you what kind of chart you want to visualize and you will be able to select from different kinds of charts.

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.title.DateTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.PieDataset;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;


public class chartgenerator extends JPanel {


final private int B_ROW = 4;
final private int B_COL = 2;
final private int H_GAP = 5;
final private int V_GAP = 5;
final private int WIDTH = 650;
final private int HEIGHT = 230;
final private int NOCHART = -1;
final private int BARCHART = 0;
final private int LINECHART = 2;
final private int RINGCHART = 3;
final private int AREACHART = 4;
final private int NUMOFCHARTS = 7;
private final int DAY = 24;
private final int MILLISECOND = 1000;
private final int MINUTE = 60;
private final int HOUR = 3600;
private final int FORMATTING = 2;
private JComboBox assingmnet_attributeDropdownBar1;
private JComboBox assingmnet_attributeDropdownBar2;

private JTextField menubar123Header;
private JTextField assingmnet_pieHeader;
private JButton assingmnet_generateBarButton;
private JButton assingmnet_generateLineButton;
private JButton assingmnet_generateRingButton;
private JButton assingmnet_generateAreaButton;
private chartdata assignment_dataset;
private ChartPanel assingmnet_frame;
private JFrame assingmnet_window;
private int assingmnet_chartType;
private JFreeChart assingmnet_chart;
private final chartdata assingmnet_data;
public chartgenerator(chartdata data) {

super(new GridLayout(1, 0));

assingmnet_data = data;
assingmnet_chartType = -1;
assingmnet_chart = null;

}

public static void main(String[] args) {

chartdata testdataset = new chartdata();
File inputfile = new File("test.csv");
testdataset.BuildDataSet(inputfile);
System.out.println("Chart:: Chart()");
chartgenerator testChart = new chartgenerator(testdataset);
System.out.println("Chart:: Chart() - Test Passed");
System.out.println("Chart:: SetWindowVisible()");
testChart.SetWindowVisible();
System.out.println("Chart:: SetWindowVisible() - Test Passed");
System.out.println("Chart:: DisposeWindow()");
testChart.DisposeWindow();
System.out.println("Chart:: DisposeWindow() - Test Passed");
}

public int GetChartType() {
return (assingmnet_chartType);
}

public boolean SetChartType(int type) {

if ((type >= 0) && (type < NUMOFCHARTS)) {
assingmnet_chartType = type;
return (true);
} else {
return (false);
}
}

public JFreeChart GetChart() {
return (assingmnet_chart);
}

public void SetChart(JFreeChart chart) {
assingmnet_chart = chart;
}

public String ConvertTime(long timeMillis) {
long elapsedTime = timeMillis;
String format = String.format("%%0%dd", FORMATTING);
elapsedTime = elapsedTime / MILLISECOND;
String seconds = String.format(format, elapsedTime % MINUTE);
String minutes = String.format(format, (elapsedTime % HOUR) / MINUTE);

int hoursTime = (int) (elapsedTime / HOUR);

while (hoursTime > DAY) {
hoursTime = hoursTime - DAY;
}
hoursTime++;

String hours = String.format(format, hoursTime);
String time = hours + ":" + minutes;// + ":" + seconds;
return
time;
}

public void DisposeWindow() {
assingmnet_window.dispose();
}

public void SetWindowVisible() {
Object[] possibleValues = new Object[NUMOFCHARTS];
possibleValues[BARCHART] = "Bar Chart";
possibleValues[LINECHART] = "Line Chart";
possibleValues[RINGCHART] = "Ring Chart";
possibleValues[AREACHART] = "Area Chart";


Object selectedValue = JOptionPane.showInputDialog(null,
"Type Of Chart", "Covid-19 visualizer",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[BARCHART]);

if (selectedValue == null) {

} else if (selectedValue == possibleValues[BARCHART]) {
SetUpBarChart(assingmnet_data);
} else if (selectedValue == possibleValues[LINECHART]) {
SetUpLineChart(assingmnet_data);
} else if (selectedValue == possibleValues[RINGCHART]) {
SetUpRingChart(assingmnet_data);
} else if (selectedValue == possibleValues[AREACHART]) {
SetUpAreaChart(assingmnet_data);
}

if (selectedValue != null) {
assingmnet_window.setVisible(true);
assingmnet_window.setLocationRelativeTo(null);
}
}

public boolean SetUpAreaChart(chartdata data) {
assignment_dataset = data;
assingmnet_chartType = NOCHART;
assingmnet_chart = null;
assingmnet_window = new JFrame("Select Data");
assingmnet_window.setSize(WIDTH, HEIGHT);
assingmnet_window.setResizable(false);
assingmnet_window.setLayout(new FlowLayout());

JPanel areaPanel = new JPanel(new GridLayout(B_ROW, B_COL, H_GAP, V_GAP));
assingmnet_window.add(areaPanel);



Border areaPanelBorder = BorderFactory
.createTitledBorder("Area Chart");
areaPanel.setBorder(areaPanelBorder);

String[] assingmnet_modelString = new String[data.GetNoOfAttributes()];

for (int i = 0; i < data.GetNoOfAttributes(); i++) {
assingmnet_modelString[i] = data.GetAttributeName(i);
}

JLabel label = new JLabel("X: Axis (Date Only)");
areaPanel.add(label);
assingmnet_attributeDropdownBar1 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar1.setMaximumRowCount(data.GetNoOfAttributes());
areaPanel.add(assingmnet_attributeDropdownBar1);
label = new JLabel("Y:Axis (Integer Value Only)");
areaPanel.add(label);
assingmnet_attributeDropdownBar2 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar2.setMaximumRowCount(data.GetNoOfAttributes());
areaPanel.add(assingmnet_attributeDropdownBar2);

menubar123Header = new JTextField("Covid-19 Area Chart");
areaPanel.add(menubar123Header);



assingmnet_generateAreaButton = new JButton("Generate");

areaPanel.add(assingmnet_generateAreaButton);

GUIEventHandler assingmnet_eventHandeler = new GUIEventHandler();
assingmnet_generateAreaButton.addActionListener(assingmnet_eventHandeler);

return true;
}

public boolean SetUpBarChart(chartdata data) {

assignment_dataset = data;
assingmnet_chartType = NOCHART;
assingmnet_chart = null;
assingmnet_window = new JFrame("Select Data ");
assingmnet_window.setSize(WIDTH, HEIGHT);
assingmnet_window.setResizable(false);
assingmnet_window.setLayout(new FlowLayout());

JPanel barPanel = new JPanel(new GridLayout(B_ROW, B_COL, H_GAP, V_GAP));
assingmnet_window.add(barPanel);



Border barPanelBorder = BorderFactory
.createTitledBorder("Bar Chart");
barPanel.setBorder(barPanelBorder);

String[] assingmnet_modelString = new String[data.GetNoOfAttributes()];

for (int i = 0; i < data.GetNoOfAttributes(); i++) {
assingmnet_modelString[i] = data.GetAttributeName(i);
}

JLabel label = new JLabel("X:Axis (Date Only)");
barPanel.add(label);
assingmnet_attributeDropdownBar1 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar1.setMaximumRowCount(data.GetNoOfAttributes());
barPanel.add(assingmnet_attributeDropdownBar1);
label = new JLabel("Y:Axis (Integer Value Only)");
barPanel.add(label);
assingmnet_attributeDropdownBar2 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar2.setMaximumRowCount(data.GetNoOfAttributes());
barPanel.add(assingmnet_attributeDropdownBar2);
menubar123Header = new JTextField("Covid-19 Bar Chart");
barPanel.add(menubar123Header);


assingmnet_generateBarButton = new JButton("Generate");

barPanel.add(assingmnet_generateBarButton);

GUIEventHandler assingmnet_eventHandeler = new GUIEventHandler();
assingmnet_generateBarButton.addActionListener(assingmnet_eventHandeler);

return true;
}



public boolean SetUpLineChart(chartdata data) {
assignment_dataset = data;
assingmnet_chartType = NOCHART;
assingmnet_chart = null;
assingmnet_window = new JFrame("Select Data");
assingmnet_window.setSize(WIDTH, HEIGHT);
assingmnet_window.setResizable(false);
assingmnet_window.setLayout(new FlowLayout());

JPanel linePanel = new JPanel(new GridLayout(B_ROW, B_COL, H_GAP, V_GAP));
assingmnet_window.add(linePanel);


Border linePanelBorder = BorderFactory
.createTitledBorder("Line Chart");
linePanel.setBorder(linePanelBorder);

String[] assingmnet_modelString = new String[data.GetNoOfAttributes()];

for (int i = 0; i < data.GetNoOfAttributes(); i++) {
assingmnet_modelString[i] = data.GetAttributeName(i);
}

JLabel label = new JLabel("X:Axis (Date Only)");
linePanel.add(label);
assingmnet_attributeDropdownBar1 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar1.setMaximumRowCount(data.GetNoOfAttributes());
linePanel.add(assingmnet_attributeDropdownBar1);
label = new JLabel("Y:Axis (Integer Value Only)");
linePanel.add(label);
assingmnet_attributeDropdownBar2 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar2.setMaximumRowCount(data.GetNoOfAttributes());
linePanel.add(assingmnet_attributeDropdownBar2);

menubar123Header = new JTextField("Covid_19 Line Chart");
linePanel.add(menubar123Header);



assingmnet_generateLineButton = new JButton("Generate");

linePanel.add(assingmnet_generateLineButton);

GUIEventHandler assingmnet_eventHandeler = new GUIEventHandler();
assingmnet_generateLineButton.addActionListener(assingmnet_eventHandeler);

return true;
}



public boolean SetUpRingChart(chartdata data) {
assignment_dataset = data;
assingmnet_chartType = NOCHART;
assingmnet_chart = null;
assingmnet_window = new JFrame("Select Data");
assingmnet_window.setSize(WIDTH, HEIGHT);
assingmnet_window.setResizable(false);
assingmnet_window.setLayout(new FlowLayout());

JPanel ringPanel = new JPanel(new GridLayout(B_ROW, B_COL, H_GAP, V_GAP));
assingmnet_window.add(ringPanel);



Border ringPanelBorder = BorderFactory
.createTitledBorder("Ring Chart");
ringPanel.setBorder(ringPanelBorder);

String[] assingmnet_modelString = new String[data.GetNoOfAttributes()];

for (int i = 0; i < data.GetNoOfAttributes(); i++) {
assingmnet_modelString[i] = data.GetAttributeName(i);
}

JLabel label = new JLabel("X:Axis (Date Only)");
ringPanel.add(label);
assingmnet_attributeDropdownBar1 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar1.setMaximumRowCount(data.GetNoOfAttributes());
ringPanel.add(assingmnet_attributeDropdownBar1);
label = new JLabel("Y:Axis (Integer Value)");
ringPanel.add(label);
assingmnet_attributeDropdownBar2 = new JComboBox(assingmnet_modelString);
assingmnet_attributeDropdownBar2.setMaximumRowCount(data.GetNoOfAttributes());
ringPanel.add(assingmnet_attributeDropdownBar2);
menubar123Header = new JTextField("Covid_19 Ring Chart");
ringPanel.add(menubar123Header);


assingmnet_generateRingButton = new JButton("Generate");

ringPanel.add(assingmnet_generateRingButton);

GUIEventHandler assingmnet_eventHandeler = new GUIEventHandler();
assingmnet_generateRingButton.addActionListener(assingmnet_eventHandeler);

return true;
}



private class GUIEventHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == assingmnet_generateBarButton) {
generateBarChart();
} else if (event.getSource() == assingmnet_generateLineButton) {
generateLineChart();
} else if (event.getSource() == assingmnet_generateRingButton) {
generateRingChart();
} else if (event.getSource() == assingmnet_generateAreaButton) {
generateAreaChart();
}
}


public boolean generateAreaChart() {
int A1 = assingmnet_attributeDropdownBar1.getSelectedIndex();
int A2 = assingmnet_attributeDropdownBar2.getSelectedIndex();
String header = menubar123Header.getText();

areachart areaChart = new areachart(assignment_dataset);
areaChart.setDataset(assignment_dataset);
areaChart.setAttribute1(A1);
areaChart.setAttribute2(A2);
areaChart.setHeader(header);
areaChart.setxAxis(assignment_dataset.GetAttributeName(A1));
areaChart.setyAxis(assignment_dataset.GetAttributeName(A2));
JFreeChart chart = areaChart.generatechart();



areacoloured renderer = new areacoloured(
gui.assignment_chartmaker.GetActiveMap());
CategoryPlot plot = (CategoryPlot) chart.getPlot();
renderer.SetColor(plot, 1);
plot.setForegroundAlpha(0.5f);
SetChartType(AREACHART);
SetChart(chart);
assingmnet_frame = new ChartPanel(chart);
removeAll();
add(assingmnet_frame);
repaint();
revalidate();
GroupJVTApplication.dataVisualizer.ActivateColour();
GroupJVTApplication.dataVisualizer.SelectChartTab();
assingmnet_window.setVisible(false);
return true;
}


public boolean generateBarChart() {
int A1 = assingmnet_attributeDropdownBar1.getSelectedIndex();
int A2 = assingmnet_attributeDropdownBar2.getSelectedIndex();
String header = menubar123Header.getText();


barchart barChart = new barchart(assignment_dataset);
barChart.setDataset(assignment_dataset);
barChart.setAttribute1(A1);
barChart.setAttribute2(A2);
barChart.setHeader(header);
barChart.setxAxis(assignment_dataset.GetAttributeName(A1));
barChart.setyAxis(assignment_dataset.GetAttributeName(A2));
JFreeChart chart = barChart.generatechart();
DateTitle dateTitle = new DateTitle();
Date now = new Date();
String dateAndTime = dateTitle.getText();
dateAndTime = dateAndTime + "\n" + ConvertTime(now.getTime());
dateTitle.setText(dateAndTime);
chart.addSubtitle(dateTitle);



barcoloured renderer = new barcoloured(
gui.assignment_chartmaker.GetActiveMap());
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryDataset barDataset = plot.getDataset();
for (int i = 1; i <= barDataset.getRowCount(); i++) {
renderer.SetColor(plot, i);
}


assingmnet_frame = new ChartPanel(chart);
SetChartType(BARCHART);
SetChart(chart);
removeAll();
add(assingmnet_frame);
repaint();
revalidate();
GroupJVTApplication.dataVisualizer.ActivateColour();
GroupJVTApplication.dataVisualizer.SelectChartTab();
assingmnet_window.setVisible(false);
return true;
}




public boolean generateLineChart() {
int A1 = assingmnet_attributeDropdownBar1.getSelectedIndex();
int A2 = assingmnet_attributeDropdownBar2.getSelectedIndex();
String header = menubar123Header.getText();

linechart lineChart = new linechart(assignment_dataset);
lineChart.setDataset(assignment_dataset);
lineChart.setAttribute1(A1);
lineChart.setAttribute2(A2);
lineChart.setHeader(header);
lineChart.setxAxis(assignment_dataset.GetAttributeName(A1));
lineChart.setyAxis(assignment_dataset.GetAttributeName(A2));
JFreeChart chart = lineChart.generatechart();


linecoloured renderer = new linecoloured(
gui.assignment_chartmaker.GetActiveMap());
CategoryPlot plot = (CategoryPlot) chart.getPlot();
renderer.SetColor(plot, 1);
SetChartType(LINECHART);
SetChart(chart);
assingmnet_frame = new ChartPanel(chart);
removeAll();
add(assingmnet_frame);
repaint();
revalidate();
GroupJVTApplication.dataVisualizer.ActivateColour();
GroupJVTApplication.dataVisualizer.SelectChartTab();
assingmnet_window.setVisible(false);
return true;
}





public boolean generateRingChart() {
int A1 = assingmnet_attributeDropdownBar1.getSelectedIndex();
int A2 = assingmnet_attributeDropdownBar2.getSelectedIndex();
String header = menubar123Header.getText();

ringchart ring = new ringchart(assignment_dataset);
ring.setAttribute1(A1);
ring.setAttribute2(A2);
ring.setHeader(header);
JFreeChart chart = ring.generatechart();


RingPlot plot = (RingPlot) chart.getPlot();
PieDataset dataset = plot.getDataset();

ringcoloured renderer = new ringcoloured(
gui.assignment_chartmaker.GetActiveMap());
renderer.SetColor(plot, dataset);
SetChartType(RINGCHART);
SetChart(chart);
assingmnet_frame = new ChartPanel(chart);
removeAll();
add(assingmnet_frame);
repaint();
revalidate();
GroupJVTApplication.dataVisualizer.ActivateColour();
GroupJVTApplication.dataVisualizer.SelectChartTab();
assingmnet_window.setVisible(false);
return true;
}


}

} /* end Chart class */

AreaChart.java

To plot area chart i will using the JFreeChart library, it easier to work with and it have many options available. And it also gives you further flexibility than others. You can download the jFreeChart library here: click here!

arechart.java

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.io.File;


public class areachart extends visualizer {


private final Double TRUE = 1.0;
private final Double FALSE = 0.0;
private final int MAX_ERROR_LENGTH = 5;

public areachart(chartdata inputdataset) {
super.setDataset(inputdataset);
}

public static void main(String[] args) {
chartdata testdataset = new chartdata();
File inputfile = new File("test.csv");
testdataset.BuildDataSet(inputfile);

System.out.println("AreaChart:: AreaChart()");
areachart testAreaChart = new areachart(testdataset);
System.out.println("AreaChart::BarChart()- Test Passed");

System.out.println("AreaChart:: generatechart()");
testAreaChart.generatechart();
System.out.println("AreaChart::generatechart()- Test Passed");
}

public JFreeChart generatechart() {

DefaultCategoryDataset areaDataset = new DefaultCategoryDataset();

Object[] column1Data = super.getDataset().GetColumnData(
super.firstattribute());
Object[] column2Data = super.getDataset().GetColumnData(
super.secondattribute());

String column2Header = super.getDataset().GetAttributeName(
super.secondattribute());

boolean addDataset = true;
String errors = "";
int errorCounter = 0;

int i = 0;
while (i < super.getDataset().GetNoOfEntrys()) {

Comparable<Object> nextValue1;
//First value can have be any comparable object.

nextValue1 = (Comparable<Object>) column1Data[i];
Double nextValue2 = null;
boolean addThis = true;
//Second value can only be integer, double or boolean.

try
{
int intNextValue2 = Integer.parseInt(column2Data[i].toString());
nextValue2 = (Double) ((double) intNextValue2);
} catch (NumberFormatException nfe) {
try {
double doubleNextValue2 = Double.parseDouble(
column2Data[i].toString());
nextValue2 = doubleNextValue2;
} catch (NumberFormatException nfe2) {
String strNextValue2 = column2Data[i].toString();
if (!strNextValue2.equalsIgnoreCase("True")) {
if (strNextValue2.equalsIgnoreCase("False")) {
nextValue2 = FALSE;
} else {
addThis = false;
}
} else {
nextValue2 = TRUE;
}
}
} catch (Exception e) {
addThis = false;
}

if (addThis == true) {
areaDataset.addValue(nextValue2, column2Header, nextValue1);
} else {
addDataset = false;
if (errorCounter >= MAX_ERROR_LENGTH) {
} else {
errors = errors + "\n"
+ column2Data[i].toString();
errorCounter++;
}
}
i++;
}

if (addDataset != false) {
} else {
areaDataset = new DefaultCategoryDataset(); //Reset
JOptionPane.showMessageDialog(null,
"Y-axis is a string, please select an integer value" + "\n"
+ errors);
}

JFreeChart chart = ChartFactory.createAreaChart(
super.getHeader(),
super.dataforxaxis(),
super.dataforyaxis(),
areaDataset,
PlotOrientation.VERTICAL,
true, //include legend
true
,
false);
return chart;
}

}

/*end AreaChart class */

This program is pretty self explanatory, i don’t need to dive into it, if you have basics idea about data visualisation in java or python then you can understand it.

Upon execution this is what you will see:

As i have already a file open in my gui, i will now select the input data. (One more thing i forgot to mention, when you open the file the program automatically reads for all the columns.)

After this i am going to click on generate and walla, we have a chart.

As this is an example file i working with a small csv file, but you can as large as you want to, the csv file contains this data:

After this you have the ability to save the chart, copy chart, change properties, print chart, zoom in and if you zoom in then you have option to zoom out, the option become available when you right click on the chart: (the code for this is further down!)

When you select properties: (the code for properties is further down!)

You have the ability to change the text, the color, background color, chart color, the font used and everything. When you want to change the colour of the chart i have made a rgb option available, so you can choose anything. For fonts you can choose any fonts, the fonts library which i am using the same library used by Microsoft word.

Font Selection
color selection

Choose colour

This gives us ability to choose/change colours according to our needs, the above image which you see the colour selector, this helps us do that.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;


public class choosecolour extends JFrame {


public choosecolour() {
integermaps();
InitGUI();
this.setLocationRelativeTo(null);
setSize(new Dimension(Cassingmnet_WIDTH, Cassingmnet_HEIGHT));
setTitle("Colour Options");
}


public void CloseFrame() {
setVisible(false);
}


public newcolours GetActiveMap() {
if (assingmnet_activeMap == CUSTOM) {
return assingmnet_custom;
} else {
return(assingmnet_colourMaps[assingmnet_activeMap]);
}
}


public barcoloured GetBarRenderer() {
return(menubar123Renderer);
}






public newcolours colourmap(int mapIndex) {
return(assingmnet_colourMaps[mapIndex]);
}





public linecoloured GetLineRenderer() {
return(assingmnet_lineRenderer);
}


public ringcoloured GetRingRenderer() {
return(assingmnet_ringRenderer);
}









public JPanel DrawSwatches(int mapIndex) {

JPanel swatchPanel = new JPanel();
Border swatchPanelBorder = BorderFactory.createTitledBorder("Preview");
swatchPanel.setBorder(swatchPanelBorder);

Dimension swatchSize = new Dimension(S_WIDTH, S_HEIGHT);
int borderWidth = 1;
Color borderColour = Color.black;

Border swatchOutline = BorderFactory.createMatteBorder(
borderWidth,
borderWidth,
borderWidth,
borderWidth,
borderColour );

Color colour;

if (mapIndex == CUSTOM) {

for (int colourIndex=0; colourIndex<MAPCOUNT; colourIndex++) {
if (colourIndex == 0 || colourIndex == 2) {
JPanel colour1 = new JPanel();
colour1.setSize(swatchSize);
colour1.setBorder(swatchOutline);
colour = assingmnet_custom.choosecolour(colourIndex)
.choosecolour();
colour1.setBackground(colour);
swatchPanel.add(colour1);
} else if (colourIndex == 1) {
JPanel colour2 = new JPanel();
colour2.setSize(swatchSize);
colour2.setBorder(swatchOutline);
colour = assingmnet_custom.choosecolour(colourIndex)
.choosecolour();
colour2.setBackground(colour);
swatchPanel.add(colour2);
} else if (colourIndex == 3) {
JPanel colour4 = new JPanel();
colour4.setSize(swatchSize);
colour4.setBorder(swatchOutline);
colour = assingmnet_custom.choosecolour(colourIndex)
.choosecolour();
colour4.setBackground(colour);
swatchPanel.add(colour4);
} else if (colourIndex == 4) {
JPanel colour5 = new JPanel();
colour5.setSize(swatchSize);
colour5.setBorder(swatchOutline);
colour = assingmnet_custom.choosecolour(colourIndex)
.choosecolour();
colour5.setBackground(colour);
swatchPanel.add(colour5);
}
}

} else {

newcolours map = colourmap(mapIndex);

for (int colourIndex=0; colourIndex<MAPCOUNT; colourIndex++) {
if (colourIndex == 0) {
JPanel colour1 = new JPanel();
colour1.setSize(swatchSize);
colour1.setBorder(swatchOutline);
colour = map.choosecolour(colourIndex)
.choosecolour();
colour1.setBackground(colour);
swatchPanel.add(colour1);
} else if (colourIndex == 1) {
JPanel colour2 = new JPanel();
colour2.setSize(swatchSize);
colour2.setBorder(swatchOutline);
colour = map.choosecolour(colourIndex)
.choosecolour();
colour2.setBackground(colour);
swatchPanel.add(colour2);
} else if (colourIndex == 2) {
JPanel colour3 = new JPanel();
colour3.setSize(swatchSize);
colour3.setBorder(swatchOutline);
colour = map.choosecolour(colourIndex)
.choosecolour();
colour3.setBackground(colour);
swatchPanel.add(colour3);
} else if (colourIndex == 3) {
JPanel colour4 = new JPanel();
colour4.setSize(swatchSize);
colour4.setBorder(swatchOutline);
colour = map.choosecolour(colourIndex)
.choosecolour();
colour4.setBackground(colour);
swatchPanel.add(colour4);
} else if (colourIndex == 4) {
JPanel colour5 = new JPanel();
colour5.setSize(swatchSize);
colour5.setBorder(swatchOutline);
colour = map.choosecolour(colourIndex)
.choosecolour();
colour5.setBackground(colour);
swatchPanel.add(colour5);
}
}

}
return(swatchPanel);
}


public void InitGUI() {

cmActionListener listener = new cmActionListener();

Container assingmnet_container = getContentPane();
assingmnet_container.setLayout(new FlowLayout());

JPanel optionsPanel = new JPanel(new GridLayout(UI_ROW,UI_COL));
assingmnet_container.add(optionsPanel);

JLabel dropboxText = new JLabel("Select colour scheme: ");
optionsPanel.add(dropboxText);

String[] colourMaps = new String[MAPCOUNT];
for (int i=0; i<MAPCOUNT; i++) {
colourMaps[i] = colourmap(i).colourmapName();
}

assingmnet_mapList = new JComboBox(colourMaps);
assingmnet_mapList.setEnabled(true);
assingmnet_mapList.setPreferredSize(new Dimension(UI_WIDTH, UI_HEIGHT));
assingmnet_mapList.addActionListener(listener);
optionsPanel.add(assingmnet_mapList);

JLabel blankLabel1 = new JLabel("");
optionsPanel.add(blankLabel1);

assingmnet_previewContainer = new JPanel();
optionsPanel.add(assingmnet_previewContainer);

assingmnet_previewPanel = DrawSwatches(assingmnet_mapList.getSelectedIndex());
assingmnet_previewContainer.add(assingmnet_previewPanel);

JPanel spacerPanel1 = new JPanel();
spacerPanel1.setSize(new Dimension(CP_WIDTH, CP_HEIGHT));
optionsPanel.add(spacerPanel1);



JPanel spacerPanel2 = new JPanel();
spacerPanel2.setSize(new Dimension(CP_WIDTH, CP_HEIGHT));
optionsPanel.add(spacerPanel2);

assingmnet_CustomButton = new JButton("Custom Colours");
assingmnet_CustomButton.addActionListener(listener);
spacerPanel2.add(assingmnet_CustomButton);

JLabel blankLabel2 = new JLabel("");
optionsPanel.add(blankLabel2);



JPanel buttonPanel = new JPanel(new FlowLayout());
optionsPanel.add(buttonPanel);

assingmnet_saveButton = new JButton("OK");
assingmnet_saveButton.addActionListener(listener);
buttonPanel.add(assingmnet_saveButton);

assingmnet_closeButton = new JButton("Cancel");
assingmnet_closeButton.addActionListener(listener);
buttonPanel.add(assingmnet_closeButton);

pack();
setResizable(false);
setLocationRelativeTo(null);

if (assignemnt_chartmakerFirstTimeOpen) {
setVisible(false);
assignemnt_chartmakerFirstTimeOpen = false;
} else {
setVisible(true);
}
}


public void integermaps() {
colourmaker[] colours = new colourmaker[MAPCOUNT];

if (0 == 0) {
colourmaker black = new colourmaker("#000000");
colours[0] = black;
} else if (0 == 1) {
colourmaker darkgrey = new colourmaker("#222222");
colours[0] = darkgrey;
} else if (0 == 2) {
colourmaker grey = new colourmaker("#555555");
colours[0] = grey;
} else if (0 == 3) {
colourmaker lightgrey = new colourmaker("#BBBBBB");
colours[0] = lightgrey;
} else if (0 == 4) {
colourmaker white = new colourmaker("#EEEEEE");
colours[0] = white;
}
if (1 == 0) {
colourmaker black = new colourmaker("#000000");
colours[1] = black;
} else if (1 == 1) {
colourmaker darkgrey = new colourmaker("#222222");
colours[1] = darkgrey;
} else if (1 == 2) {
colourmaker grey = new colourmaker("#555555");
colours[1] = grey;
} else if (1 == 3) {
colourmaker lightgrey = new colourmaker("#BBBBBB");
colours[1] = lightgrey;
} else if (1 == 4) {
colourmaker white = new colourmaker("#EEEEEE");
colours[1] = white;
}
if (2 == 0) {
colourmaker black = new colourmaker("#000000");
colours[2] = black;
} else if (2 == 1) {
colourmaker darkgrey = new colourmaker("#222222");
colours[2] = darkgrey;
} else if (2 == 2) {
colourmaker grey = new colourmaker("#555555");
colours[2] = grey;
} else if (2 == 3) {
colourmaker lightgrey = new colourmaker("#BBBBBB");
colours[2] = lightgrey;
} else if (2 == 4) {
colourmaker white = new colourmaker("#EEEEEE");
colours[2] = white;
}
if (3 == 0) {
colourmaker black = new colourmaker("#000000");
colours[3] = black;
} else if (3 == 1) {
colourmaker darkgrey = new colourmaker("#222222");
colours[3] = darkgrey;
} else if (3 == 2) {
colourmaker grey = new colourmaker("#555555");
colours[3] = grey;
} else if (3 == 3) {
colourmaker lightgrey = new colourmaker("#BBBBBB");
colours[3] = lightgrey;
} else if (3 == 4) {
colourmaker white = new colourmaker("#EEEEEE");
colours[3] = white;
}
if (4 == 0) {
colourmaker black = new colourmaker("#000000");
colours[4] = black;
} else if (4 == 1) {
colourmaker darkgrey = new colourmaker("#222222");
colours[4] = darkgrey;
} else if (4 == 2) {
colourmaker grey = new colourmaker("#555555");
colours[4] = grey;
} else if (4 == 3) {
colourmaker lightgrey = new colourmaker("#BBBBBB");
colours[4] = lightgrey;
} else if (4 == 4) {
colourmaker white = new colourmaker("#EEEEEE");
colours[4] = white;
}
newcolours greyscale = new newcolours(colours, "Greyscale");
SetMap(GREY, greyscale);


switch (0) {
case 0:
colourmaker colour1 = new colourmaker("#FF0099");
colours[0] = colour1;
break;
case 1:
colourmaker colour2 = new colourmaker("#F3F315");
colours[0] = colour2;
break;
case 2:
colourmaker colour3 = new colourmaker("#83F52C");
colours[0] = colour3;
break;
case 3:
colourmaker colour4 = new colourmaker("#FF6600");
colours[0] = colour4;
break;
case 4:
colourmaker colour5 = new colourmaker("#6E0DD0");
colours[0] = colour5;
break;
}
switch (1) {
case 0:
colourmaker colour1 = new colourmaker("#FF0099");
colours[1] = colour1;
break;
case 1:
colourmaker colour2 = new colourmaker("#F3F315");
colours[1] = colour2;
break;
case 2:
colourmaker colour3 = new colourmaker("#83F52C");
colours[1] = colour3;
break;
case 3:
colourmaker colour4 = new colourmaker("#FF6600");
colours[1] = colour4;
break;
case 4:
colourmaker colour5 = new colourmaker("#6E0DD0");
colours[1] = colour5;
break;
}
switch (2) {
case 0:
colourmaker colour1 = new colourmaker("#FF0099");
colours[2] = colour1;
break;
case 1:
colourmaker colour2 = new colourmaker("#F3F315");
colours[2] = colour2;
break;
case 2:
colourmaker colour3 = new colourmaker("#83F52C");
colours[2] = colour3;
break;
case 3:
colourmaker colour4 = new colourmaker("#FF6600");
colours[2] = colour4;
break;
case 4:
colourmaker colour5 = new colourmaker("#6E0DD0");
colours[2] = colour5;
break;
}
switch (3) {
case 0:
colourmaker colour1 = new colourmaker("#FF0099");
colours[3] = colour1;
break;
case 1:
colourmaker colour2 = new colourmaker("#F3F315");
colours[3] = colour2;
break;
case 2:
colourmaker colour3 = new colourmaker("#83F52C");
colours[3] = colour3;
break;
case 3:
colourmaker colour4 = new colourmaker("#FF6600");
colours[3] = colour4;
break;
case 4:
colourmaker colour5 = new colourmaker("#6E0DD0");
colours[3] = colour5;
break;
}
switch (4) {
case 0:
colourmaker colour1 = new colourmaker("#FF0099");
colours[4] = colour1;
break;
case 1:
colourmaker colour2 = new colourmaker("#F3F315");
colours[4] = colour2;
break;
case 2:
colourmaker colour3 = new colourmaker("#83F52C");
colours[4] = colour3;
break;
case 3:
colourmaker colour4 = new colourmaker("#FF6600");
colours[4] = colour4;
break;
case 4:
colourmaker colour5 = new colourmaker("#6E0DD0");
colours[4] = colour5;
break;
}
newcolours neon = new newcolours(colours, "Neon");
SetMap(NEON, neon);


for (int i=0; i<MAPCOUNT; i++) {
if (i == 0) {
colourmaker colour1 = new colourmaker("#FFFF00");
colours[i] = colour1;
} else if (i == 1) {
colourmaker colour2 = new colourmaker("#FFCC00");
colours[i] = colour2;
} else if (i == 2) {
colourmaker colour3 = new colourmaker("#FF9900");
colours[i] = colour3;
} else if (i == 3) {
colourmaker colour4 = new colourmaker("#FF6600");
colours[i] = colour4;
} else if (i == 4) {
colourmaker colour5 = new colourmaker("#FF3300");
colours[i] = colour5;
}
}
newcolours warm = new newcolours(colours, "Warm");
SetMap(WARM, warm);


if (0 == 0) {
colourmaker colour1 = new colourmaker("#33FFFF");
colours[0] = colour1;
} else if (0 == 1) {
colourmaker colour2 = new colourmaker("#3399FF");
colours[0] = colour2;
} else if (0 == 2) {
colourmaker colour3 = new colourmaker("#3333FF");
colours[0] = colour3;
} else if (0 == 3) {
colourmaker colour4 = new colourmaker("#9933FF");
colours[0] = colour4;
} else if (0 == 4) {
colourmaker colour5 = new colourmaker("#5C00B8");
colours[0] = colour5;
}
if (1 == 0) {
colourmaker colour1 = new colourmaker("#33FFFF");
colours[1] = colour1;
} else if (1 == 1) {
colourmaker colour2 = new colourmaker("#3399FF");
colours[1] = colour2;
} else if (1 == 2) {
colourmaker colour3 = new colourmaker("#3333FF");
colours[1] = colour3;
} else if (1 == 3) {
colourmaker colour4 = new colourmaker("#9933FF");
colours[1] = colour4;
} else if (1 == 4) {
colourmaker colour5 = new colourmaker("#5C00B8");
colours[1] = colour5;
}
if (2 == 0) {
colourmaker colour1 = new colourmaker("#33FFFF");
colours[2] = colour1;
} else if (2 == 1) {
colourmaker colour2 = new colourmaker("#3399FF");
colours[2] = colour2;
} else if (2 == 2) {
colourmaker colour3 = new colourmaker("#3333FF");
colours[2] = colour3;
} else if (2 == 3) {
colourmaker colour4 = new colourmaker("#9933FF");
colours[2] = colour4;
} else if (2 == 4) {
colourmaker colour5 = new colourmaker("#5C00B8");
colours[2] = colour5;
}
if (3 == 0) {
colourmaker colour1 = new colourmaker("#33FFFF");
colours[3] = colour1;
} else if (3 == 1) {
colourmaker colour2 = new colourmaker("#3399FF");
colours[3] = colour2;
} else if (3 == 2) {
colourmaker colour3 = new colourmaker("#3333FF");
colours[3] = colour3;
} else if (3 == 3) {
colourmaker colour4 = new colourmaker("#9933FF");
colours[3] = colour4;
} else if (3 == 4) {
colourmaker colour5 = new colourmaker("#5C00B8");
colours[3] = colour5;
}
if (4 == 0) {
colourmaker colour1 = new colourmaker("#33FFFF");
colours[4] = colour1;
} else if (4 == 1) {
colourmaker colour2 = new colourmaker("#3399FF");
colours[4] = colour2;
} else if (4 == 2) {
colourmaker colour3 = new colourmaker("#3333FF");
colours[4] = colour3;
} else if (4 == 3) {
colourmaker colour4 = new colourmaker("#9933FF");
colours[4] = colour4;
} else if (4 == 4) {
colourmaker colour5 = new colourmaker("#5C00B8");
colours[4] = colour5;
}
newcolours cold = new newcolours(colours, "Cold");
SetMap(COLD, cold);


if (0 == 0) {
colourmaker colour1 = new colourmaker("#00E000");
colours[0] = colour1;
} else if (0 == 1) {
colourmaker colour2 = new colourmaker("#64D88B");
colours[0] = colour2;
} else if (0 == 2) {
colourmaker colour3 = new colourmaker("#99CC66");
colours[0] = colour3;
} else if (0 == 3) {
colourmaker colour4 = new colourmaker("#66FF99");
colours[0] = colour4;
} else if (0 == 4) {
colourmaker colour5 = new colourmaker("#CAFF7A");
colours[0] = colour5;
}
if (1 == 0) {
colourmaker colour1 = new colourmaker("#00E000");
colours[1] = colour1;
} else if (1 == 1) {
colourmaker colour2 = new colourmaker("#64D88B");
colours[1] = colour2;
} else if (1 == 2) {
colourmaker colour3 = new colourmaker("#99CC66");
colours[1] = colour3;
} else if (1 == 3) {
colourmaker colour4 = new colourmaker("#66FF99");
colours[1] = colour4;
} else if (1 == 4) {
colourmaker colour5 = new colourmaker("#CAFF7A");
colours[1] = colour5;
}
if (2 == 0) {
colourmaker colour1 = new colourmaker("#00E000");
colours[2] = colour1;
} else if (2 == 1) {
colourmaker colour2 = new colourmaker("#64D88B");
colours[2] = colour2;
} else if (2 == 2) {
colourmaker colour3 = new colourmaker("#99CC66");
colours[2] = colour3;
} else if (2 == 3) {
colourmaker colour4 = new colourmaker("#66FF99");
colours[2] = colour4;
} else if (2 == 4) {
colourmaker colour5 = new colourmaker("#CAFF7A");
colours[2] = colour5;
}
if (3 == 0) {
colourmaker colour1 = new colourmaker("#00E000");
colours[3] = colour1;
} else if (3 == 1) {
colourmaker colour2 = new colourmaker("#64D88B");
colours[3] = colour2;
} else if (3 == 2) {
colourmaker colour3 = new colourmaker("#99CC66");
colours[3] = colour3;
} else if (3 == 3) {
colourmaker colour4 = new colourmaker("#66FF99");
colours[3] = colour4;
} else if (3 == 4) {
colourmaker colour5 = new colourmaker("#CAFF7A");
colours[3] = colour5;
}
if (4 == 0) {
colourmaker colour1 = new colourmaker("#00E000");
colours[4] = colour1;
} else if (4 == 1) {
colourmaker colour2 = new colourmaker("#64D88B");
colours[4] = colour2;
} else if (4 == 2) {
colourmaker colour3 = new colourmaker("#99CC66");
colours[4] = colour3;
} else if (4 == 3) {
colourmaker colour4 = new colourmaker("#66FF99");
colours[4] = colour4;
} else if (4 == 4) {
colourmaker colour5 = new colourmaker("#CAFF7A");
colours[4] = colour5;
}
newcolours meadow = new newcolours(colours, "Meadow");
SetMap(MEADOW, meadow);


SetActiveMap(GREY);
SetRenderers();
}


public void SaveAndClose() {

if (assingmnet_mapList.isEnabled()) {
SetActiveMap(assingmnet_mapList.getSelectedIndex());
} else {
SetActiveMap(CUSTOM);
}

GroupJVTApplication.dataVisualizer.RedrawChartColour();
assingmnet_mapList.setEnabled(true);
setVisible(false);
}


public boolean SetActiveMap(int mapIndex) {
assingmnet_activeMap = mapIndex;
SetRenderers();
return(true);
}


public boolean SetMap(int mapIndex, newcolours map) {
assingmnet_colourMaps[mapIndex] = map;
SetRenderers();
return(true);
}


public boolean SetRenderers() {
menubar123Renderer = new barcoloured(GetActiveMap());
assingmnet_lineRenderer = new linecoloured(GetActiveMap());
assingmnet_areaRenderer = new areacoloured(GetActiveMap());
return(true);
}


private class cmActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
/* When combo box changed, redraw preview panel. */
if
(e.getSource()==assingmnet_mapList) {

assingmnet_previewContainer.removeAll();
assingmnet_previewPanel = DrawSwatches(assingmnet_mapList.getSelectedIndex());
assingmnet_previewContainer.add(assingmnet_previewPanel);
assingmnet_previewPanel.repaint();
assingmnet_previewPanel.revalidate();
}

if (e.getSource()==assingmnet_closeButton) {
assingmnet_mapList.setEnabled(true);
CloseFrame();
}

if (e.getSource()==assingmnet_saveButton) {
GroupJVTApplication.dataVisualizer.SelectChartTab(); //line added
SaveAndClose();
}

if (e.getSource() == assingmnet_CustomButton) {

findcolour select = new findcolour();
assingmnet_custom = select.InitMap();
if (assingmnet_custom == null) {

} else {
assingmnet_mapList.setEnabled(false);
assingmnet_previewContainer.removeAll();
assingmnet_previewPanel = DrawSwatches(CUSTOM);
assingmnet_previewContainer.add(assingmnet_previewPanel);
assingmnet_previewPanel.repaint();
assingmnet_previewPanel.revalidate();
}

}
}
}


private final int MAPCOUNT = 5;


private newcolours[] assingmnet_colourMaps = new newcolours[MAPCOUNT];


private int assingmnet_activeMap;


private final int CUSTOM = MAPCOUNT + 1;


private newcolours assingmnet_custom;


private barcoloured menubar123Renderer;



private linecoloured assingmnet_lineRenderer;


private ringcoloured assingmnet_ringRenderer;




private areacoloured assingmnet_areaRenderer;



private JComboBox assingmnet_mapList;


private JPanel assingmnet_previewPanel;


private JPanel assingmnet_previewContainer;


private JButton assingmnet_saveButton;


private JButton assingmnet_closeButton;


private JButton assingmnet_CustomButton;


private Boolean assignemnt_chartmakerFirstTimeOpen = true;
private final int GREY = 0;
private final int NEON = 1;
private final int WARM = 2;
private final int COLD = 3;
private final int MEADOW = 4;
private int Cassingmnet_WIDTH = 350;
private int Cassingmnet_HEIGHT = 250;
private int UI_WIDTH = 150;
private int UI_HEIGHT = 10;
private int CP_WIDTH = 10;
private int CP_HEIGHT = 400;
private int S_WIDTH = 20;
private int S_HEIGHT = 20;
private int UI_ROW = 4;
private int UI_COL = 2;



public static void main(String[] args) {
choosecolour test = new choosecolour();
int grey = 0;
int neon = 1;
int warm = 2;
int cold = 3;
int meadow = 4;

test.SetActiveMap(grey);
System.out.println("ColourManager:: GetActiveMap()");
if (test.GetActiveMap() == test.colourmap(grey)) {
System.out.println("ColourManager:: GetActiveMap() - Test Passed");
} else {
System.out.println("ColourManager:: GetActiveMap() - Test Failed");
}

test.setVisible(true);
}

}

This is not the complete colour code, it have few more java classes connected to this, you can see all of the classes here:

https://github.com/isauravmanitripathi/covid-19-data-visualization

When you click the above link, you will also find the code for the rest of charts available. I won’t be adding those to my medium article, because it will make it more lengthy than it already is.

Now let’s talk about saving the graph. The code below help us save the charts we have visualised.

import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import javax.swing.border.Border;
import javax.swing.BorderFactory;


public class graphsaver {


public graphsaver(chartgenerator chart, int defaultchartwidth,
int defaultchartheight) {
assignment_chart = chart;
assignment_Frame = new JFrame("Save Chart");
assignment_Frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
assignment_Frame.setResizable(false);
assignment_Frame.setLocationRelativeTo(null);
assignment_Frame.setLayout(new FlowLayout());

assignment_panel = new JPanel(new GridLayout(ROWS,COLUMNS));

Border border = BorderFactory
.createTitledBorder("Save Settings");
assignment_panel.setBorder(border);

assignment_Frame.add(assignment_panel);

JLabel label = new JLabel("File Title");
assignment_panel.add(label);
assignment_fileTitle = new JTextField("Untitled");
assignment_panel.add(assignment_fileTitle);

label = new JLabel("Chart Width");
assignment_panel.add(label);
assignment_chartWidth = new JTextField(Integer.toString(
defaultchartwidth));
assignment_panel.add(assignment_chartWidth);

label = new JLabel("Chart Height ");
assignment_panel.add(label);
assignment_chartHeight = new JTextField(Integer.toString(
defaultchartheight));
assignment_panel.add(assignment_chartHeight);

JLabel blankLabel1 = new JLabel("");
assignment_panel.add(blankLabel1);
JLabel blankLabel2 = new JLabel("");
assignment_panel.add(blankLabel2);
JLabel blankLabel3 = new JLabel("");
assignment_panel.add(blankLabel3);

assignment_Save = new JButton("Save");
assignment_panel.add(assignment_Save);

SaveEventHandler eventHandeler = new SaveEventHandler();
assignment_Save.addActionListener(eventHandeler);

assignment_Frame.setVisible(true);

}


public JPanel getJPanel() {
return assignment_ChartPanel;
}


public boolean setJPanel(JPanel panel) {
assignment_ChartPanel = panel;
return true;
}



private class SaveEventHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource()== assignment_Save){
Save();
}
}

public boolean Save() {
String filename = assignment_fileTitle.getText();

//Stop saving if no filename is given.
if
(filename.isEmpty()) {
JOptionPane.showMessageDialog(null,
"No filename");
return false;
}

int width;
int height;
String str_width = assignment_chartWidth.getText().toString();
String str_height = assignment_chartHeight.getText().toString();
try {
width = Integer.valueOf(str_width);
height = Integer.valueOf(str_height);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Width and Height entered not numbers");
return false;
}

try {

BufferedImage bi = new BufferedImage(width,
height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
assignment_ChartPanel.paint(g); //this == JComponent
g.dispose();

ImageIO.write(bi,"png",new File(filename + ".png"));
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Save unsuccessful");
return false;
}

JOptionPane.showMessageDialog(null, "Save successful");
assignment_Frame.setVisible(false);
return true;
}



}

final private int DEFAULT_WIDTH = 200;

final private int DEFAULT_HEIGHT = 200;

final private int ROWS = 5;

final private int COLUMNS = 2;

private JPanel assignment_ChartPanel;

private JPanel assignment_panel;

private JButton assignment_Save;

private chartgenerator assignment_chart;

private JFrame assignment_Frame;

private JTextField assignment_fileTitle;

private JTextField assignment_chartWidth;

private JTextField assignment_chartHeight;


public static void main(String args[]){
chartdata testdataset = new chartdata();
File inputfile = new File("test.csv");
testdataset.BuildDataSet(inputfile);
chartgenerator testChart = new chartgenerator(testdataset);
int width = 400;
int height = 400;
System.out.println("GraphSaver:: GraphSaver()");
graphsaver testSave = new graphsaver(testChart, width, height);
System.out.println("GraphSaver:: GraphSaver() - Test Passed");
}
}

Upon execution you will be able to save the chart in the folder of the program, but you can change the location to anywhere you want, the code folder is the default location.

So that all for today. Please check my github page where you will find all the code.

https://github.com/isauravmanitripathi/covid-19-data-visualization

And also connect with me on linkedin:

https://www.linkedin.com/in/saurav-mani-tripathi-30859b20b/

--

--

--

Computer science and here is a link to my linkedin https://www.linkedin.com/in/saurav-mani-tripathi-30859b20b/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saurav Tripathi

Saurav Tripathi

Computer science and here is a link to my linkedin https://www.linkedin.com/in/saurav-mani-tripathi-30859b20b/

More from Medium

Introduction to Parallel Programming in Java

10 Things I’ve Learnt In My First Year As A Java Developer

Global’s Technology & Digital Early Careers Programme

Debugging RAM: Java Garbage Collection — Java Heap Deep Dive, Part 1

Java — A Quick Tour