go-chart icon indicating copy to clipboard operation
go-chart copied to clipboard

Can't show long label at XAxis in BarChart

Open panicake opened this issue 7 years ago • 2 comments

When the label is too long to show the label at one line, the label is dispeared, and I puzzled! I finally find the problem, in BarChart's function drawXAxis, there is calling of function Draw.TextWithin, this function has a bug;

in draw.go line 23 lines := Text.WrapFit(r, text, box.Width(), style) lines array will contains empty string, like ["", "test"], empty string will cause lineBox := r.MeasureText(line), lineBox.Width() and lineBox.Height() get invalid value.

I fixed this by rewriting the code in text.go WrapFitWord function, line 101

// old

	output = append(output, t.Trim(line))
			
// new
if len(line) != 0 {
	output = append(output, t.Trim(line))
}

panicake avatar Nov 30 '17 05:11 panicake

当BarChat的横坐标的标签过长时,会导致标签不显示。 原因是 在BarChart调用drawXAxis,Draw.TextWithin, 在一个框里画标签时,首先会将标签分成多行lines := Text.WrapFit(r, text, box.Width(), style),然而分成多行后,lines数组里总是有空字符串,空字符串会导致在计算box时无法获取box的高和宽,导致文字的坐标偏离十万八千里。 我通过改源码修复,在in text.go WrapFitWord function, 101行:

// old

	output = append(output, t.Trim(line))
// new
if len(line) != 0 {
	output = append(output, t.Trim(line))
}

panicake avatar Nov 30 '17 06:11 panicake

Any update on this?

Raggaer avatar Apr 03 '18 11:04 Raggaer