SO Charts
A wrapper around the "echarts" Javascript library for creating charts.
SO Charts
This add-on is a wrapper around the echarts Javascript library for using it with Vaadin Flow. The charts can be used just like any other Vaadin component.
"echarts" is quite a rich library and bringing all the available functionalities into this add-on will require quite some effort. However, my plan is to keep integrating more and more features into this add-on as and when I have some free time.
Architecture of the Add-on
The client-side part of the add-on is a very thin LitElement wrapper around the "echarts" library. The SOChart class is the one that wraps that into the Java class to make it appear as a Vaadin Flow Component. Communication between them are through a couple of property change messages and a monolithic update call that carries all the chart data (including the configuration information) as a string parameter that can be JSONified. At the client-side, the string parameter received is used to create the structure for setting the options of the chart.
Just before sending the string containing the chart data and the configuration to the client-side, it is passed through a method - customizeJSON(String)
- so that if someone wants to debug or customize it, it is very well possible. (Starting from 2.2.0, data can be transmitted separately and that can be intercepted via a new method - customizeDataJSON(String)
).
Most classes in this add-on are either "chart components" or "chart component parts". All "chart components" can be added to the SOChart instance (the only Vaadin Flow Component). So, one instance of SOChart class (represents a "chart display") can be used for displaying any number of Charts (instances of Chart class - it is also a "chart component") and other "chart components".
Starting from version 2.1.0, various shapes, including texts and images, can be defined and added to the chart display.
Starting from version 2.2.0, a new method is added to transmit data separately whenever required.
Starting from version 2.3.0, a new method is added to transmit data separately for a specific chart whenever required.
Starting from version 3.2.5, a couple of new methods are added to append custom JSON code to the chart. This way, one can add echarts features that are not currently supported via the APIs of this component.
Note: The online demo contains chart examples and other proprietary examples. You may use the keyword "chart" to filter out chart examples.
Notes to those who use Vaadin Spring Configuration:
Please make sure that you update your application.properties
if needed. It has been reported that this add-on may not work if the add-on package is not white-listed there. (Thanks to Marcus Merten for pointing this out). See here
Configuration example of application.properties (Thanks to Ryan Neuharth):
vaadin.whitelisted-packages = com.vaadin,org.vaadin,com.storedobject
Credits to other contributors:
(1) Christian Asnel Ngoulla Sob
(2) Stefano Bossi
(3) Lazy Math Student
Sample code
// Creating a chart display area. SOChart soChart = new SOChart(); soChart.setSize("800px", "500px"); // Let us define some inline data. CategoryData labels = new CategoryData("Banana", "Apple", "Orange", "Grapes"); Data data = new Data(25, 40, 20, 30); // We are going to create a couple of charts. So, each chart should be positioned // appropriately. // Create a self-positioning chart. NightingaleRoseChart nc = new NightingaleRoseChart(labels, data); Position p = new Position(); p.setTop(Size.percentage(50)); nc.setPosition(p); // Position it leaving 50% space at the top // Second chart to add. BarChart bc = new BarChart(labels, data); RectangularCoordinate rc; rc = new RectangularCoordinate(new XAxis(DataType.CATEGORY), new YAxis(DataType.NUMBER)); p = new Position(); p.setBottom(Size.percentage(55)); rc.setPosition(p); // Position it leaving 55% space at the bottom bc.plotOn(rc); // Bar chart needs to be plotted on a coordinate system // Just to demonstrate it, we are creating a "Download" and a "Zoom" toolbox button. Toolbox toolbox = new Toolbox(); toolbox.addButton(new Toolbox.Download(), new Toolbox.Zoom()); // Let's add some titles. Title title = new Title("My First Chart"); title.setSubtext("2nd Line of the Title"); // Add the chart components to the chart display area. soChart.add(nc, bc, toolbox, title); // Now, add the chart display (which is a Vaadin Component) to your layout. myLayout.add(soChart);
// Creating a chart display area SOChart soChart = new SOChart(); soChart.setSize("800px", "500px"); // Generating some random values for a LineChart Random random = new Random(); Data xValues = new Data(), yValues = new Data(); for(int x = 0; x < 40; x++) { xValues.add(x); yValues.add(random.nextDouble()); } xValues.setName("X Values"); yValues.setName("Random Values"); // Line chart is initialized with the generated XY values LineChart lineChart = new LineChart(xValues, yValues); lineChart.setName("40 Random Values"); // Line chart needs a coordinate system to plot on // We need Number-type for both X and Y axes in this case XAxis xAxis = new XAxis(DataType.NUMBER); YAxis yAxis = new YAxis(DataType.NUMBER); RectangularCoordinate rc = new RectangularCoordinate(xAxis, yAxis); lineChart.plotOn(rc); // Add to the chart display area with a simple title soChart.add(lineChart, new Title("Sample Line Chart")); // Add to my layout myLayout.add(soChart);
// Creating a chart display area SOChart soChart = new SOChart(); soChart.setSize("600px", "650px"); // Generating 10 set of values for 10 LineCharts for the equation: // y = a + a * x / (a - 11) where a = 1 to 10, x and y are positive LineChart[] lineCharts = new LineChart[10]; Data[] xValues = new Data[lineCharts.length]; Data[] yValues = new Data[lineCharts.length]; int i; for(i = 0; i < lineCharts.length; i++) { xValues[i] = new Data(); xValues[i].setName("X (a = " + (i + 1) + ")"); yValues[i] = new Data(); yValues[i].setName("Y (a = " + (i + 1) + ")"); } // For each line chart, we need only 2 end-points (because they are straight lines). int a; for(i = 0; i < lineCharts.length; i++) { a = i + 1; xValues[i].add(0); yValues[i].add(a); xValues[i].add(11 - a); yValues[i].add(0); } // Line charts are initialized here for(i = 0; i < lineCharts.length; i++) { lineCharts[i] = new LineChart(xValues[i], yValues[i]); lineCharts[i].setName("a = " + (i + 1)); } // Line charts need a coordinate system to plot on // We need Number-type for both X and Y axes in this case XAxis xAxis = new XAxis(DataType.NUMBER); YAxis yAxis = new YAxis(DataType.NUMBER); RectangularCoordinate rc = new RectangularCoordinate(xAxis, yAxis); for(i = 0; i < lineCharts.length; i++) { lineCharts[i].plotOn(rc); soChart.add(lineCharts[i]); // Add the chart to the display area } // Add a simple title too soChart.add(new Title("Equation: y = a + a * x / (a - 11) where a = 1 to 10, x and y are positive")); // We don't want any legends soChart.disableDefaultLegend(); // Add it to my layout myLayout.add(soChart);
// Creating a chart display area SOChart soChart = new SOChart(); soChart.setSize("800px", "500px"); // Tree chart // (By default it assumes circular shape. Otherwise, we can set orientation) // All values are randomly generated TreeChart tc = new TreeChart(); TreeData td = new TreeData("Root", 1000); tc.setTreeData(td); Random r = new Random(); for(int i = 1; i < 21; i++) { td.add(new TreeData("Node " + i, r.nextInt(500))); } TreeData td1 = td.get(13); td = td.get(9); for(int i = 50; i < 56; i++) { td.add(new TreeData("Node " + i, r.nextInt(500))); } for(int i = 30; i < 34; i++) { td1.add(new TreeData("Node " + i, r.nextInt(500))); } // Add to the chart display area with a simple title soChart.add(tc, new Title("A Circular Tree Chart")); // Finally, add it to my layout myLayout.add(tc);
// Creating a chart display area SOChart soChart = new SOChart(); soChart.setSize("800px", "500px"); // To hold multiple charts List<Chart> charts = new ArrayList<>(); // Create multiple charts createCharts(charts); // Add the chart component(s) to the chart display area charts.forEach(soChart::add); // Add to my layout myLayout.add(soChart); private void createCharts(List<Chart> charts) { // Define a data matrix to hold production data. DataMatrix dataMatrix = new DataMatrix("Production in Million Tons"); // Columns contain products dataMatrix.setColumnNames("Matcha Latte", "Milk Tea", "Cheese Cocoa"); dataMatrix.setColumnDataName("Products"); // Rows contain years of production dataMatrix.setRowNames("2012", "2013", "2014", "2015"); dataMatrix.setRowDataName("Years"); // Add row values dataMatrix.addRow(41.1, 86.5, 24.1); dataMatrix.addRow(30.4, 92.1, 24.1); dataMatrix.addRow(31.9, 85.7, 67.2); dataMatrix.addRow(53.3, 85.1, 86.4); // Define axes XAxis xAxisProduct = new XAxis(DataType.CATEGORY); xAxisProduct.setName(dataMatrix.getColumnDataName()); XAxis xAxisYear = new XAxis(DataType.CATEGORY); xAxisYear.setName(dataMatrix.getRowDataName()); YAxis yAxis = new YAxis(DataType.NUMBER); yAxis.setName(dataMatrix.getName()); // First rectangular coordinate RectangularCoordinate rc1 = new RectangularCoordinate(); rc1.addAxis(xAxisProduct, yAxis); rc1.getPosition(true) .setBottom(Size.percentage(55)); // Position it leaving 55% space at the bottom // Second rectangular coordinate RectangularCoordinate rc2 = new RectangularCoordinate(); rc2.addAxis(xAxisYear, yAxis); // Same Y-axis is re-used here rc2.getPosition(true).setTop(Size.percentage(55)); // Position it leaving 55% space at the top // Bar chart variable BarChart bc; // Crate a bar chart for each data row for (int i = 0; i < dataMatrix.getRowCount(); i++) { bc = new BarChart(dataMatrix.getColumnNames(), dataMatrix.getRow(i)); bc.setName(dataMatrix.getRowName(i)); bc.plotOn(rc1); charts.add(bc); } // Crate a bar chart for each data column for (int i = 0; i < dataMatrix.getColumnCount(); i++) { bc = new BarChart(dataMatrix.getRowNames(), dataMatrix.getColumn(i)); bc.setName(dataMatrix.getColumnName(i)); bc.plotOn(rc2); charts.add(bc); } }
import com.storedobject.chart.*; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.router.Route; @Route("") public class ChartTest extends HorizontalLayout { public ChartTest() { // Creating a chart display area SOChart soChart = new SOChart(); soChart.setSize("900px", "500px"); // Let us define some inline data CategoryData labels = new CategoryData("April Fool's Day", "Marriage Day", "Election Day", "Any Other Day"); Data data = new Data(5, 20, 100, 2); // Axes XAxis xAxis; YAxis yAxis; // Bar chart BarChart bc1 = new BarChart(labels, data); // First bar chart xAxis = new XAxis(labels); xAxis.getLabel(true).setRotation(45); yAxis = new YAxis(data); RectangularCoordinate coordinate = new RectangularCoordinate(xAxis, yAxis); bc1.plotOn(coordinate); // Bar chart needs to be plotted on a coordinate system coordinate.getPosition(true).setRight(Size.percentage(60)); // Leave space on the right side BarChart bc2 = new BarChart(data, labels); // Second bar chart xAxis = new XAxis(data); yAxis = new YAxis(labels); coordinate = new RectangularCoordinate(xAxis, yAxis); bc2.plotOn(coordinate); // Bar chart needs to be plotted on a coordinate system coordinate.getPosition(true).setLeft(Size.percentage(60)); // Leave space on the left side // Just to demonstrate it, we are creating a "Download" and a "Zoom" toolbox button Toolbox toolbox = new Toolbox(); toolbox.addButton(new Toolbox.Download(), new Toolbox.Zoom()); // Switching off the default legend soChart.disableDefaultLegend(); // Let's add some titles Title title = new Title("Probability of Getting Fooled"); title.setSubtext("Truth is always simple but mostly hidden - Syam"); // Add the chart components to the chart display area soChart.add(bc1, bc2, toolbox, title); // Add to the view add(soChart); } }
// Examples of shapes that can be added to SOChart ShapeGroup shapes = new ShapeGroup(); // Optional grouping shapes.setZ(100); shapes.getPosition(true).center(); Ring ring = new Ring(100, 20); ring.setDraggable(true); var style = ring.getStyle(true); style.setStrokeColor(new Color("red")); Text text = new Text("Hello World!\nHow are you?"); text.getStyle(true).setStrokeColor(new Color("red")); Font font = new Font(Font.Family.fantasy(), Font.Size.x_large()); font.setStyle(Font.Style.OBLIQUE); text.setFont(font); Sector arc = new Sector(80, 0, 45); arc.getStyle(true).setFillColor(new Color("yellow")); Rectangle rectangle = new Rectangle(40, 30, 20); rectangle.getStyle(true).setStrokeColor(new Color("red")); BezierCurve bc = new BezierCurve(new Shape.Point(0, 0), new Shape.Point(40, 40), new Shape.Point(30, 20)); Polygon polygon = new Polygon(new Shape.Point(0, 0), new Shape.Point(-20, -30), new Shape.Point(10, -10)); polygon.useBezierSmoothening(0.6, true); // Grouped together. Also, can be added individually. shapes.add(ring, text, bc, rectangle, arc, polygon); // Add to SOChart along with others like chart, legend, title etc. soChart.add(shapes, others...);
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Compiled with Vaadin 17.0.8 and Java 11
Initial versions. Minor API changes expected.
Fixed bug in DataZoom's base class.
- Released
- 2020-10-31
- Maturity
- TESTED
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 17+
- Vaadin 23+ in 3.2.6
- Vaadin 18+ in 0.2.1
- Vaadin 14+ in 2.4.1
- Browser
- Firefox
- Google Chrome
SO Charts - Vaadin Add-on Directory
A wrapper around the "echarts" Javascript library for creating charts.Online Demo
View on GitHub
SO Charts version 0.0.3
Vaadin 16.0.0.rc1+
Requires Java 11
Initial versions. API changes expected.
SO Charts version 0.0.4
Vaadin 16.0.0+
Requires Java 11+
Initial versions. API changes expected.
SO Charts version 0.0.5
Vaadin 16.0.0+
Requires Java 11+
Initial versions. API changes expected.
Breaking change: Axis classes are no more typed. Instead, now it has DataType as the parameter.
SO Charts version 0.0.6
Vaadin 17.0.3+
Requires Java 11+
Initial versions. Minor API changes expected.
SO Charts version 0.0.7
Compiled with Vaadin 17.0.6 and Java 11
LitComponent now extends Vaadin's LitTemplate
Initial versions. Minor API changes expected.
SO Charts version 0.0.8
Compiled with Vaadin 17.0.8 and Java 11
Initial versions. Minor API changes expected.
Fixed an issue in Axis class that was not rendering some features.
SO Charts version 0.0.9
Compiled with Vaadin 17.0.8 and Java 11
Initial versions. Minor API changes expected.
Added new methods in Chart and LineChart.
SO Charts version 0.1.0
Compiled with Vaadin 17.0.8 and Java 11
Initial versions. Minor API changes expected.
Fixed bug in DataZoom.
Added new methods in LineChart.
Class PointSymbol renamed to PointSymbolType.
New class PointSymbol.
SO Charts version 0.1.1
Compiled with Vaadin 17.0.8 and Java 11
Initial versions. Minor API changes expected.
Fixed bug in DataZoom's base class.
SO Charts version 0.2.0
Compiled with Vaadin 18.0.2 and Java 11
Initial versions. Minor API changes expected.
SO Charts version 0.2.1
Complied with Vaadin version 18.0.3 and Java 11
Minor API changes expected.
Removed unwanted files from the jar.
SO Charts version 1.0.0
Complied with Vaadin version 14.4.6 and Java 11.
Minor API changes expected.
Removed dependency on LitTemplate and hence this should now work with Vaadin 14.x.
SO Charts version 1.1.0
Compiled with Vaadin 14.5.1 and Java 11.
Compatible with Vaadin 14.5.x and 18.x upwards.
echarts updated to 5.0.2
SO Charts version 1.1.1
Compiled with Java version 11+ and Vaadin 14.5.2+.
Compatible with Vaadin 14.5.x and 18.x upwards.
Additional constructors in Axis and more relaxed constructors/methods in BarChart.
SO Charts version 1.1.2
Compiled with Java version 11+ and Vaadin 14.5.2+.
Compatible with Vaadin 14.5.x and 18.x upwards.
Added formatting capabilities for axis labels.
SO Charts version 2.0.0
Compiled with Java version 11+ and Vaadin 14.5.2+.
Compatible with Vaadin 14.5.x and 18.x upwards.
API additions and additional classes for new features.
Some of the existing code require recompilation.
SO Charts version 2.0.1
Compiled with Java version 11+ and Vaadin 14.5.3+.
Compatible with Vaadin 14.5.x and 18.x upwards.
Methods to get default Tooltip and Legend from the chart display.
Methods in Legend to hide/show selected chart.
SO Charts version 2.0.2
Compiled with Java version 11 and Vaadin 14.5.3.
Compatible with Vaadin 14.5.x and 19.x upwards.
New DataChannel class to dynamically push data to the front-end.
SO Charts version 2.0.3
Compiled with Java version 11 and Vaadin 14.5.3.
Compatible with Vaadin 14.5.x and 19.x upwards.
Added reset() method to DataChannel class.
SO Charts version 2.1.0
Compiled with Java version 11 and Vaadin 14.5.3.
Compatible with Vaadin 14.5.x and 19.x upwards.
New: Shape classes. Shape instances can be added to SOChart.
SO Charts version 2.1.1
Compiled with Java version 11 and Vaadin 14.6.0.
Compatible with Vaadin 14.5.x and 19.x upwards.
A runtime exception is raised if any chart exception occurs while attaching the chart to the front-end.
SO Charts version 2.1.2
Compiled with Java version 11 and Vaadin 14.6.7.
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
Rich text styles added to axis labels and chart labels.
SO Charts version 2.2.0
Compiled with Java version 11 and Vaadin 14.6.7.
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
There are some changes in the API and architecture. So, a re-compilation of your existing code is recommended.
New chart added: HeatmapChart.
SO Charts version 2.3.0
Compiled with Java version 11 and Vaadin 14.6.8.
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
There are some additions to the API and refactoring of the code. So, a re-compilation of your existing code is recommended.
New chart type: BubbleChart.
Initial support for "mark areas" in charts.
SO Charts version 2.3.3
Compiled with Java version 17 and Vaadin 14.8.1
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
echarts updated to version 5.2.2
New chart: GraphChart (Thanks to Christian Asnel Ngoulla Sob).
SO Charts version 2.3.4
Compiled with Java version 17 and Vaadin 14.8.3
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
echarts updated to version 5.3.0
SO Charts version 2.3.5
Compiled with Java version 17 and Vaadin 14.8.4
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
Added a new method in Axis.Label to set a Javascript Function as the label formatter.
SO Charts version 2.3.7
Compiled with Java version 17 and Vaadin 14.8.4
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
New class: FunctionData to generate data via maths functions.
Added methods for disabling emphasis and animation effects on the chart elements.
Bug fixes.
SO Charts version 2.4.1
Compiled with Java version 17 and Vaadin 14.8.6
Compatible with Vaadin 14.5.x upwards and 19.x upwards.
Caution: Code restructured with minor API changes.
New charts: GanttChart, ActivityChart, XRangeChart.
New classes: ComponentGroup, AbstractProject, Project, ActivityList, AbstractTask.
SO Charts version 3.0.0
Compiled with Java version 17 and Vaadin 23.0.9
Compatible with Vaadin 23.0.9 upwards
Dependency update (Lit & echarts upgraded)
Bug fixed in axis rendering (Implementation of offset and opposite were buggy)
SO Charts version 3.1.0
Compiled with Java version 17 and Vaadin 23.3.1
Compatible with Vaadin 23.1.2 upwards.
New chart: CandlestickChart.
Chart is now responsive to browser window size changes (Thanks to [this fellow](https://github.com/lazymathstudent)).
Certain methods were made public to create charts that are not yet available in this add-on.
Dependency update (echarts upgraded to 5.4.1).
SO Charts version 3.1.1
Compiled with Java version 17 and Vaadin 23.2.15
Compatible with Vaadin 23.1.2 upwards. However, there are issues with Vaadin 23.3.x versions.
Bug fixed in GanttChart and ActivityChart.
SO Charts version 3.1.2
Compiled with Java version 17 and Vaadin 23.2.15
Compatible with Vaadin 23.1.2 upwards.
Javascript error fixed (Semicolon missing in import statements).
SO Charts version 3.1.3
Compiled with Java version 17 and Vaadin 23.2.15
Added new methods in Toolbox.Zoom class to set applicable X/Y axes.
SO Charts version 3.2.1
Compiled with Java version 17 and Vaadin 23.2.15
Added new methods in Toolbox.Zoom class to set applicable X/Y axes.
New chart: SankeyChart
Code refactoring.
Note: A recompilation of the existing code may be required.
SO Charts version 3.2.2
Compiled with Java version 17 and Vaadin 23.2.15
New class: Language
New Method: SOChart.setLanguage
SO Charts version 3.2.3
Compiled with Java version 17 and Vaadin 23.2.15
Bug fixes: Rendering of default fonts, colors, and grid-lines.
SO Charts version 3.2.4
Compiled with Java version 17 and Vaadin 23.2.15
Added methods to set font-sizes in AbstractProject and AbstractTask classes.
SO Charts version 3.2.5
Compiled with Java version 17 and Vaadin 23.2.16
Added new methods Data, DataMatrix. GaugeChart, NightingaleRoseChart.
New methods in SOChart to add custom code to component parts or at root-level.
Bug fixed in Tooltip. It was not working when data provider was used in Tooltip and the data was not explicitly added to the SOChart.
SVG rendering support added.
echarts version updated to 5.5.0
SO Charts version 3.2.6
Adjusted the Data and DataMatrix methods to accept double arrays as parameters instead of variable arguments. This enhancement simplifies the calls to these methods by ensuring the data passed aligns with the data structure. Also incremented the version to 3.2.6.