import java.util.HashMap;
import java.util.Map;
public class MockData {
public static Map<String, Integer> getCarBHP() {
Map<String, Integer> carBHP = new HashMap<>();
carBHP.put("Toyota Camry", 203); // Example BHP values
carBHP.put("Honda Accord", 192);
carBHP.put("Ford Mustang", 450);
carBHP.put("Chevrolet Camaro", 275);
carBHP.put("BMW M3", 473);
return carBHP;
}
}
Installing JFreeChart:
This part took me a long time. I knew I to use JFreeChart library, but it was hard get it working in a Jupyter notebook with Java.
Found the .jar files off the maven repository site. I should try in the future to integrate Maven into all of this. Might be easier to access different libraries.
%jars /home/vishnuaa77/vscode/vishnu/lib/jfreechart-1.5.4.jar
%jars /home/vishnuaa77/vscode/vishnu/lib/jcommon-1.0.24.jar
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
public class ChartExperiment {
public static void main(String[] args) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Map<String, Integer> carBHP = MockData.getCarBHP();
for (String car : carBHP.keySet()) {
dataset.addValue(carBHP.get(car), "BHP", car);
}
JFreeChart barChart = ChartFactory.createBarChart(
"Car Brake Horsepower",
"Car Model",
"BHP",
dataset
);
ChartPanel chartPanel = new ChartPanel(barChart);
try {
BufferedImage chartImage = barChart.createBufferedImage(800, 600);
ImageIO.write(chartImage, "PNG", new File("/home/vishnuaa77/vscode/vishnu/images/chart.png"));
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}
}
}
ChartExperiment.main(null);