Chart.js2 Radar, how to configure the label paddin

2019-04-24 01:46发布

问题:

I have the following Radar chart using Chart.js v2.

My configuration:

legend: false,
scale: {
    pointLabels :{
        fontSize: 16,
        fontStyle: "bold",
    }
}

The problem here is the "Communication" label has 0 padding between the label and the number 100. How can I configure this padding and/or fix this issue?

回答1:

Spent an hour and still can't find the proper label padding options.

My workaround is padding the labels with newlines and spaces:

['行業競爭情況', ''],
['擁有專利', ''],
'     成本控制',
'     現金流',
['', '回本期'],
['', '營運能力'],
['', '行業潛力'],
'行業網絡     ',
'團隊經驗     ',
['計劃的完整性', ''],

The outcome is acceptable:

Make it auto if you wish:

scale: {
  pointLabels: {
    callback: function (label, i, labels) {}...


回答2:

I have the same problem as described in the question and also was unable to find a solution using known chart options.

However, here is another workaround to achieve a behaviour similar to the desired padding (although not exactly):

ticks: {
    display: false,
    max: 11, // notice how this is +1 more than what you actually want
},

gridLines: {
    display: true,
    color: [ 
        "#dddddd", "#dddddd", "#dddddd", "#dddddd", "#dddddd",
        "#dddddd", "#dddddd", "#dddddd", "#dddddd", "#dddddd",
        "transparent" ], // notice how the last (additional) line is set to transparent
},

angleLines: {
    display: true,
    color: "#dddddd",
},

The idea is to add one additional grid line with a transparent color. While this does not cause any padding between the pointLabels and the angleLines, it does cause there to be one gridLine worth of space between the label and the next gridLine. To me, this at least looks a little better.

Note that this is only feasible if you do not need to display ticks (or if you are ok with your scale showing one additional tick value that you don't actually use).



回答3:

I use chart.js 2.6.0. I suffered from the same problem as you. I use only the radar type chart and amended as follows.

// chart.js v2.6.0
function adjustPointPositionForLabelHeight(angle, textSize, position) {
		console.log(position.y);
		if (angle === 90 || angle === 270) {
			position.y -= (textSize.h / 2);
		} else if (angle > 270 || angle < 90) {
			position.y -= textSize.h;
		position.y -= 7;	//add source 
		}
	}



回答4:

Whenever my PR will be merged, pointLabels.padding option will be added ;)



回答5:

Usually problem occurs with the first pointLabel when it is single liner you can add the callback in options as follows

pointLabels: {
      callback: function (label, index) {
        /* Hack to add spacing between first pointLabel item and radar graph */
        return index == 0 && label.length == 1? [''].concat(label): label;
      }

Making pointLabel multi line text solves the problem.

EDIT: Current version of chartjs is 2.7.3. Upcoming version will probably solves this problem.