A Java client for the QuickChart.io chart API.
Add QuickChart to your project (Java 6+). The io.quickchart package is available on Maven Central
Maven:
<dependency>
<groupId>io.quickchart</groupId>
<artifactId>QuickChart</artifactId>
</dependency>Gradle:
dependencies {
implementation 'io.quickchart:Quickchart:VERSION'
}This library provides a QuickChart namespace containing a Chart class. Import and instantiate it. Then set properties on it and specify a Chart.js config:
import io.quickchart.QuickChart;
// ...
QuickChart chart = new QuickChart();
chart.setWidth(500);
chart.setHeight(300);
chart.setConfig("{"
+ " type: 'bar',"
+ " data: {"
+ " labels: ['Q1', 'Q2', 'Q3', 'Q4'],"
+ " datasets: [{"
+ " label: 'Users',"
+ " data: [50, 60, 70, 180]"
+ " }]"
+ " }"
+ "}"
);For more details on configuring your chart, reference the QuickChart documentation.
Use getUrl() on your QuickChart object to get the encoded URL that renders your chart:
System.out.println(qc.getUrl());
// https://quickchart.io/chart?c=%7B%22chart%22%3A+%7B%22type%22%3A+%22bar%22%2C+%22data%22%3A+%7B%22labels%22%3A+%5B%22Hello+world%22%2C+%22Test%22%5D%2C+%22datasets%22%3A+%5B%7B%22label%22%3A+%22Foo%22%2C+%22data%22%3A+%5B1%2C+2%5D%7D%5D%7D%7D%7D&w=600&h=300&bkg=%23ffffff&devicePixelRatio=2.0&f=pngIf you want to provide your Chart.js config in a neat manner instead of doing all the string formatting, you can use getUrlV2()
Example:
// Instanciate ChartConfig object
ChartConfig config = new ChartConfig();
// Set the char type. e.g bar, line etc
config.setType("bar");
//Instantiate ChartData
ChartData data = new ChartData();
// Set the labels and chart Data
data.setLabels(List.of("Q1", "Q2", "Q3", "Q4")); //labels
//Dataset 1
Dataset dataset = new Dataset();
dataset.setLabel("Users");
dataset.setData(List.of("50", "60", "70", "180"));
//Dataset 2
Dataset dataset2 = new Dataset();
dataset2.setLabel("Revenue");
dataset2.setData(List.of("100", "200", "300", "400"));
//Set the required Data
data.setDatasets(List.of(dataset, dataset2));
config.setData(data);
chart.setChartConfig(config);
//get the URL
chart.getUrlV2()If you have a long or complicated chart, use getShortUrl() to get a fixed-length URL using the quickchart.io web service (note that these URLs only persist for a short time unless you have a subscription):
System.out.println(qc.getShortUrl());
// https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401The URLs will render an image of a chart:
See io.quickchart.examples to see other example usage.
You can set the following properties:
The Chart.js chart configuration.
The Chart.js config in terms of Java Object
Width of the chart image in pixels. Defaults to 500
Height of the chart image in pixels. Defaults to 300
The background color of the chart. Any valid HTML color works. Defaults to #ffffff (white). Also takes rgb, rgba, and hsl values.
The device pixel ratio of the chart. This will multiply the number of pixels by the value. This is usually used for retina displays. Defaults to 1.0.
The Chart.js version of the chart. See QuickChart documentation for default.
There are a few ways to get a URL for your chart object.
Returns a URL that will display the chart image when loaded.
Uses the quickchart.io web service to create a fixed-length chart URL that displays the chart image. Returns a URL such as https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401.
Note that short URLs expire after a few days for users of the free service. You can subscribe to keep them around longer.
Write your chart to file.
Returns an array of bytes representing your image.
Returns a string suitable for display as a base 64 url beginning with data:image/png;base64,
Check out the io.quickchart.examples to see other usage.