首页 > 解决方案 > Text rotation in go- go chart library

问题描述

I am using go chart library https://github.com/wcharczuk/go-chart to make a bar chart. The problem I am facing is that the label values are of long length and I want to rotate the text with 45 degree to show the complete textenter image description here

The date format I like to show is like this 2018-05-12.

var values []chart.Value
        for k, v := range timeline {

            // leng += 1
            new_data := k[0:10]
            println(new_data)
            val := float64(v)
            values = append(values, chart.Value{Label: new_data, Value: val, Style: chart.Style{FontColor: drawing.Color{R: 255, G: 0, B: 0, A: 255}, FontSize: 4.0}})
        } 

pie := chart.BarChart{
        Title:      "Timeline Chart",
        Height:     350,
        TitleStyle: chart.StyleShow(),
        Background: chart.Style{
            Padding: chart.Box{
                Top: 40,
            },
        },
        BarWidth: 60,
        XAxis:    chart.StyleShow(),
        YAxis: chart.YAxis{
            Style: chart.StyleShow(),
            // TickStyle: chart.Style{
            //  TextRotationDegrees: 45.0,
            // },
            NameStyle: chart.Style{Show: true, TextRotationDegrees: 45.0},
        },
        Bars: values,

}

The chart.Style property is also not working as I want to make font size small and change color of the label text

标签: gochartsbar-chartgo-chart

解决方案


创建时chart.BarChart,只需添加TextRotationDegrees到您的样式XAxis

XAxis: chart.Style{
    Show:                true,
    TextRotationDegrees: 45.0,
},

推荐阅读