greetings,
I have a pet project to show charts.js - spider chart and I try to reduce spaces between legends and charts.
Based on this stackoverflow and youtube
this is my project layout
/my-app
/frontend
/themes
/spiderchart
spiderchart.js
/src/main/java
/SpiderChartView.java
spiderchart.js
window.initSpiderChart = function (dataJson) {
var data = JSON.parse(dataJson);
console.log(data); // For debugging purposes
// Check if data is an array
if (!Array.isArray(data)) {
console.error('Data is not an array:', data);
return;
}
var labels = data.map(item => item.label);
var values = data.map(item => item.value);
var container = document.getElementById('spider-chart-container');
var canvas = document.createElement('canvas');
canvas.id = 'spider-chart';
container.appendChild(canvas);
var ctx = canvas.getContext('2d');
var myChart = new Chart(ctx, {
type: 'radar',
data: {
labels: labels, datasets: [{
label: 'Scores',
data: values,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
},
plugins: [{
beforeInit: function (chart, options) {
chart.legend.afterFit = function () {
this.height = this.height + 10;
};
}
}],
});
};
and my SpiderChartView.java
package ...;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.JavaScript;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
@Route("spider-chart")
@Tag("div")
@JavaScript("https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js")
@JsModule("./themes/spidercharts/spiderchart.js")
@CssImport("./themes/spidercharts/spiderchart.css")
public class SpiderChartView extends Div {
public SpiderChartView() {}
@Override
protected void onAttach(AttachEvent attachEvent) {
VerticalLayout leftSection = new VerticalLayout();
leftSection.setWidth("50%");
leftSection.add(new Text("Name: John Doe"), new Text("Address: 123 Main St"));
VerticalLayout rightSection = new VerticalLayout();
rightSection.setWidth("50%");
rightSection.add(); // Create right section for the spider chart
Div rightSectionDiv = new Div();
rightSectionDiv.setId("spider-chart-container"); // Set ID for the spider chart container
rightSection.add(rightSectionDiv);
String jsonData = getData();
getUI().ifPresent(ui -> ui.getPage().executeJs("window.initSpiderChart($0)", jsonData));
// Arrange left and right sections horizontally
HorizontalLayout layout = new HorizontalLayout(leftSection, rightSectionDiv);
layout.setWidth("100%");
add(layout);
}
private String getData() {
return "[{\"label\": \"Quality\", \"value\": 4}, " +
"{\"label\": \"Performance\", \"value\": 3}, " +
"{\"label\": \"Price\", \"value\": 5}, " +
"{\"label\": \"Design\", \"value\": 4}, " +
"{\"label\": \"Customer Support\", \"value\": 5}]";
}
}
It seems that plugins:beforeInit() is not executed.
Someone has an idea how to solve this problem? or maybe once faced the same isssue like this?
many thanks.