Chart.js2 Radar, how to configure the label paddin

2019-04-24 00:51发布

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

radar chart

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?

5条回答
神经病院院长
2楼-- · 2019-04-24 01:34

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.

查看更多
三岁会撩人
3楼-- · 2019-04-24 01:37

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: [ 
        "#ffffdffffd", "#ffffdffffd", "#ffffdffffd", "#ffffdffffd", "#ffffdffffd",
        "#ffffdffffd", "#ffffdffffd", "#ffffdffffd", "#ffffdffffd", "#ffffdffffd",
        "transparent" ], // notice how the last (additional) line is set to transparent
},

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

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).

查看更多
做自己的国王
4楼-- · 2019-04-24 01:48

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:enter image description here

Make it auto if you wish:

scale: {
  pointLabels: {
    callback: function (label, i, labels) {}...
查看更多
Emotional °昔
5楼-- · 2019-04-24 01:49

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 
		}
	}

查看更多
Explosion°爆炸
6楼-- · 2019-04-24 01:49

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

查看更多
登录 后发表回答