0% found this document useful (0 votes)
4 views

Visualization_code

The document is a Java program that creates visualizations for biostatistical data using the XChart library. It includes methods to parse a sample dataset, create bar charts, histograms, and line charts based on the data. The visualizations display average weight by gender, weight distribution, and the relationship between age and height.

Uploaded by

seckinalpkargi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Visualization_code

The document is a Java program that creates visualizations for biostatistical data using the XChart library. It includes methods to parse a sample dataset, create bar charts, histograms, and line charts based on the data. The visualizations display average weight by gender, weight distribution, and the relationship between age and height.

Uploaded by

seckinalpkargi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import org.knowm.xchart.

*;
import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

public class BiostatVisualizations {

public static void main(String[] args) {


// Sample dataset (you would read from the CSV file in practice)
String[][] data = {
{"Alex", "M", "41", "74", "170"},
{"Bert", "M", "42", "68", "166"},
{"Carl", "M", "32", "70", "155"},
{"Dave", "M", "39", "72", "167"},
{"Elly", "F", "30", "66", "124"},
{"Faye", "F", "35", "62", "115"},
{"Gina", "F", "29", "64", "130"},
{"Hank", "M", "43", "71", "158"},
{"Ivy", "F", "34", "65", "140"}
};

// Parse the dataset


List<Map<String, String>> records = parseData(data);

// Create and display charts


SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Biostat Visualizations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(),
BoxLayout.Y_AXIS));
JPanel chartGrid = createChartGrid(records);
frame.add(chartGrid);

frame.pack();
frame.setVisible(true);
});
}

private static List<Map<String, String>> parseData(String[][] data) {


List<Map<String, String>> records = new ArrayList<>();
for (String[] row : data) {
Map<String, String> record = new HashMap<>();
record.put("Name", row[0]);
record.put("Sex", row[1]);
record.put("Age", row[2]);
record.put("Height", row[3]);
record.put("Weight", row[4]);
records.add(record);
}
return records;
}

private static JPanel createChartGrid(List<Map<String, String>> records) {


JPanel gridPanel = new JPanel();
gridPanel.setLayout(new BoxLayout(gridPanel, BoxLayout.Y_AXIS));

gridPanel.add(createBarChart(records));
gridPanel.add(createHistogram(records));
gridPanel.add(createLineChart(records));
return gridPanel;
}

private static JPanel createBarChart(List<Map<String, String>> records) {


Map<String, Double> averageWeightByGender = records.stream()
.collect(Collectors.groupingBy(
record -> record.get("Sex"),
Collectors.averagingDouble(record ->
Double.parseDouble(record.get("Weight")))));

CategoryChart chart = new CategoryChartBuilder()


.width(800)
.height(600)
.title("Average Weight by Gender")
.xAxisTitle("Gender")
.yAxisTitle("Average Weight (lbs)")
.build();

chart.addSeries("Average Weight", new


ArrayList<>(averageWeightByGender.keySet()), new
ArrayList<>(averageWeightByGender.values()));

return new XChartPanel<>(chart);


}

private static JPanel createHistogram(List<Map<String, String>> records) {


List<Double> weights = records.stream()
.map(record -> Double.parseDouble(record.get("Weight")))
.collect(Collectors.toList());
Histogram histogram = new Histogram(weights, 5);

CategoryChart chart = new CategoryChartBuilder()


.width(800)
.height(600)
.title("Weight Distribution")
.xAxisTitle("Weight Bins")
.yAxisTitle("Frequency")
.build();

chart.addSeries("Weight Histogram", histogram.getxAxisData(),


histogram.getyAxisData());

return new XChartPanel<>(chart);


}

private static JPanel createLineChart(List<Map<String, String>> records) {


List<Double> ages = new ArrayList<>();
List<Double> heights = new ArrayList<>();

for (Map<String, String> record : records) {


double age = Double.parseDouble(record.get("Age"));
double height = Double.parseDouble(record.get("Height"));

ages.add(age);
heights.add(height);
}

XYChart chart = new XYChartBuilder()


.width(800)
.height(600)
.title("Age vs Height Line Chart")
.xAxisTitle("Age (years)")
.yAxisTitle("Height (inches)")
.build();

chart.getStyler().setDefaultSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Line);

chart.addSeries("Line Chart", ages, heights);

return new XChartPanel<>(chart);


}
}

You might also like