|
| 1 | +package com.baeldung.commons.math3; |
| 2 | + |
| 3 | +import org.apache.commons.math3.stat.Frequency; |
| 4 | +import org.knowm.xchart.CategoryChart; |
| 5 | +import org.knowm.xchart.CategoryChartBuilder; |
| 6 | +import org.knowm.xchart.SwingWrapper; |
| 7 | +import org.knowm.xchart.style.Styler; |
| 8 | + |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +public class Histogram { |
| 12 | + |
| 13 | + public Histogram() { |
| 14 | + |
| 15 | + Map distributionMap = processRawData(); |
| 16 | + List yData = new ArrayList(); |
| 17 | + yData.addAll(distributionMap.values()); |
| 18 | + List xData = Arrays.asList(distributionMap.keySet().toArray()); |
| 19 | + |
| 20 | + CategoryChart chart = buildChart(xData, yData); |
| 21 | + new SwingWrapper<>(chart).displayChart(); |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + private CategoryChart buildChart(List xData, List yData) { |
| 26 | + |
| 27 | + // Create Chart |
| 28 | + CategoryChart chart = new CategoryChartBuilder().width(800).height(600) |
| 29 | + .title("Age Distribution") |
| 30 | + .xAxisTitle("Age Group") |
| 31 | + .yAxisTitle("Frequency") |
| 32 | + .build(); |
| 33 | + |
| 34 | + chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNW); |
| 35 | + chart.getStyler().setAvailableSpaceFill(0.99); |
| 36 | + chart.getStyler().setOverlapped(true); |
| 37 | + |
| 38 | + chart.addSeries("age group", xData, yData); |
| 39 | + |
| 40 | + return chart; |
| 41 | + } |
| 42 | + |
| 43 | + private Map processRawData() { |
| 44 | + |
| 45 | + List<Integer> datasetList = Arrays.asList(36, 25, 38, 46, 55, 68, 72, 55, 36, 38, 67, 45, 22, 48, 91, 46, 52, 61, 58, 55); |
| 46 | + Frequency frequency = new Frequency(); |
| 47 | + datasetList.forEach(d -> frequency.addValue(Double.parseDouble(d.toString()))); |
| 48 | + |
| 49 | + int classWidth = 10; |
| 50 | + |
| 51 | + Map distributionMap = new TreeMap(); |
| 52 | + List processed = new ArrayList(); |
| 53 | + datasetList.forEach(d -> { |
| 54 | + |
| 55 | + double observation = Double.parseDouble(d.toString()); |
| 56 | + |
| 57 | + if(processed.contains(observation)) |
| 58 | + return; |
| 59 | + |
| 60 | + long observationFrequency = frequency.getCount(observation); |
| 61 | + int upperBoundary = (observation > classWidth) ? Math.multiplyExact( (int) Math.ceil(observation / classWidth), classWidth) : classWidth; |
| 62 | + int lowerBoundary = (upperBoundary > classWidth) ? Math.subtractExact(upperBoundary, classWidth) : 0; |
| 63 | + String bin = lowerBoundary + "-" + upperBoundary; |
| 64 | + |
| 65 | + int prevUpperBoundary = lowerBoundary; |
| 66 | + int prevLowerBoundary = (lowerBoundary > classWidth) ? lowerBoundary - classWidth : 0; |
| 67 | + String prevBin = prevLowerBoundary + "-" + prevUpperBoundary; |
| 68 | + if(!distributionMap.containsKey(prevBin)) |
| 69 | + distributionMap.put(prevBin, 0); |
| 70 | + |
| 71 | + if(!distributionMap.containsKey(bin)) { |
| 72 | + distributionMap.put(bin, observationFrequency); |
| 73 | + } |
| 74 | + else { |
| 75 | + long oldFrequency = Long.parseLong(distributionMap.get(bin).toString()); |
| 76 | + distributionMap.replace(bin, oldFrequency + observationFrequency); |
| 77 | + } |
| 78 | + |
| 79 | + processed.add(observation); |
| 80 | + |
| 81 | + }); |
| 82 | + |
| 83 | + return distributionMap; |
| 84 | + } |
| 85 | + |
| 86 | + public static void main(String[] args) { |
| 87 | + new Histogram(); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments