j2ChartJs allows generating configuration objects for the fantastic Chart.js JavaScript library from your Java code.
<dependency>
<groupId>de.yanwittmann</groupId>
<artifactId>j2chartjs</artifactId>
<version>1.1</version>
</dependency>
Java: Build any of the available chart types and export to json.
BarChartDataset dataset = new BarChartDataset()
.addData(12, 32, 8, 45, 27, 23)
.setLabel("Sample Dataset")
.setBackgroundColor(ChartColors.BACKGROUNDS)
.setBorderColor(ChartColors.BORDERS)
.addBorderWidth(1);
BarChartData data = new BarChartData()
.addDataset(dataset)
.addLabels("Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6");
ChartOptions options = new ChartOptions()
.setTitle(new TitleOption().setText("Sample Chart").setDisplay(true));
BarChart chart = new BarChart()
.setChartOptions(options)
.setChartData(data);
JSONObject jsonChartConfiguration = chart.build();
HTML: Link to a ChartJs script with version >= 3.0 and define a canvas.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<canvas id="canvasId" style="border: gray 2px solid;">
JavaScript: Load the exported JSON configuration together with the canvas element into a chart object.
var ctx = document.getElementById('canvasId');
new Chart(ctx, jsonChartConfiguration);
Quick charts take away a lot of the configuration overhead and let you focus on your data.
Java:
Also export to JSON when the chart is ready.
new QuickBarChart()
.addDataset("Data 1", 10, 20, 30)
.addDataset("Data 2", 34, 22, 14)
.addLabels("Point 1", "Point 2", "Point 3")
.setTitle("Quick chart")
.setGridLinesVisible(false)
.setBeginAtZero(true)
.build();
Supports only ChartJs 3.x
.
Might work for older versions but not all features will be supported there.
Find the JavaDoc on javadoc.io.
j2ChartJs is licensed under the Apache 2.0 license