vicanso / go-charts

A charts library for Golang
MIT License
236 stars 34 forks source link

Custmize colors for line in line chart #87

Open abhijitpawar12 opened 3 months ago

abhijitpawar12 commented 3 months ago

Hi @vicanso Can we do customisation for line colors in LineChart func GenerateLineChart(values [][]float64, xaxisArray []string, chartName string, id string) { minValue := 0.0 maxValue := 100.0 show := true splitLineShow := true

yAxisOption1 := charts.YAxisOption{
    Min:           &minValue,
    Max:           &maxValue,
    Font:          nil, // Replace with actual *truetype.Font if available
    Data:          []string{"A", "B", "C"},
    FontSize:      6,
    Position:      "left",
    FontColor:     charts.Color{R: 0, G: 0, B: 0, A: 255}, // Black color
    Formatter:     "{value}",
    Color:         charts.Color{R: 255, G: 0, B: 0, A: 255}, // Red color
    Show:          &show,
    DivideCount:   5,
    Unit:          1,
    SplitLineShow: &splitLineShow,
}

fmt.Print(yAxisOption1)

p, err := charts.LineRender(
    values,

    charts.XAxisDataOptionFunc(xaxisArray),

    func(opt *charts.ChartOption) {
        opt.Legend.Padding = charts.Box{
            Top:    20,
            Bottom: 30,
        }

        opt.YAxisOptions = []charts.YAxisOption{
            {
                SplitLineShow: charts.TrueFlag(),
                FontSize:      10,
                FontColor:     charts.Color{R: 0, G: 0, B: 0, A: 255}, // Black color
                Formatter:     "{value} " + "MB",
            },
        }

        opt.XAxis = charts.XAxisOption{
            Data:      xaxisArray,
            FontSize:  10,
            FontColor: charts.Color{R: 0, G: 0, B: 0, A: 255}, // Black color
        }

        opt.SymbolShow = charts.TrueFlag()
        opt.LineStrokeWidth = 4
        opt.Width = 600
        opt.Height = 300

        // Customize line colors for each series using StrokeStyle
        colors := []charts.Color{
            {R: 255, G: 0, B: 0, A: 255}, // Red
            {R: 255, G: 0, B: 0, A: 255}, // Green
            {R: 255, G: 0, B: 0, A: 255}, // Blue
            {R: 255, G: 0, B: 0, A: 255}, // Orange
        }

        for i := range opt.SeriesList {
            opt.SeriesList[i].Style = charts.Style{
                StrokeColor: colors[i%len(colors)],
                FontColor:   colors[i%len(colors)],
                FillColor:   colors[i%len(colors)],
            }

        }
    },

    charts.TitleOptionFunc(charts.TitleOption{
        Text:      "MB",                                      // Set the title with the unit
        FontColor: charts.Color{R: 69, G: 69, B: 69, A: 255}, // Black color
    }),
)
vicanso commented 3 months ago

https://github.com/vicanso/go-charts/blob/main/theme.go#L124

You can add your customize theme.