How to dynamically color candlesticks and generate a PDF of charts using Angular or Go?

Discussão em 'Angular' iniciado por krskara, Dezembro 28, 2024.

  1. krskara

    krskara Guest

    I'm building an application to analyze and visualize stock data, and I need to generate a PDF containing candlestick charts for selected days (between 1 to 1000+). Each candlestick should be dynamically colored based on rule-based. The charts themselves don't need to be rendered in the application, only the PDF output is required.

    • Backend: Go
    • Frontend: Angular

    I can prepare the data with candle colors in backend or frontend.

    The following is a sample image of what I want to do.

    [​IMG]

    I've tried go-echarts but it supports only up candle and down candle colors; individual candle coloring is not possible. Here's the code -

    func plotCandlestickChart(candles []candle.Candle) {
    // Prepare data for the KLine chart
    var xValues []string
    var kLineData []opts.KlineData

    for _, candle := range candles {
    // Format the timestamp for x-axis
    xValues = append(xValues, candle.TimeStamp.Format("15:04"))

    // Create KlineData for each candle
    kLineData = append(kLineData, opts.KlineData{
    Value: []interface{}{
    candle.Open,
    candle.Close,
    candle.Low,
    candle.High,
    },
    })

    }

    // Create a new KLine chart
    kLine := charts.NewKLine()

    // Add the KLine data
    kLine.SetXAxis(xValues).
    AddSeries("KLine", kLineData).
    SetSeriesOptions(
    charts.WithMarkPointNameTypeItemOpts(opts.MarkPointNameTypeItem{
    Name: "highest value",
    Type: "max",
    ValueDim: "highest",
    }),
    charts.WithMarkPointNameTypeItemOpts(opts.MarkPointNameTypeItem{
    Name: "lowest value",
    Type: "min",
    ValueDim: "lowest",
    }),
    charts.WithMarkPointStyleOpts(opts.MarkPointStyle{
    Label: &opts.Label{
    Show: opts.Bool(true),
    },
    }),
    charts.WithItemStyleOpts(opts.ItemStyle{
    Color: "#00da3c",
    Color0: "#ec0000",
    BorderColor: "#008F28",
    BorderColor0: "#8A0000",
    }),
    )

    // Set global chart options
    kLine.SetGlobalOptions(
    charts.WithTitleOpts(opts.Title{Title: "Candlestick Chart"}),
    charts.WithXAxisOpts(opts.XAxis{Data: xValues}),
    charts.WithYAxisOpts(opts.YAxis{Scale: opts.Bool(true),}),
    charts.WithInitializationOpts(opts.Initialization{Width: "1200px", Height: "800px"}), // Set width and height in pixels
    )

    // Render the chart to a file
    f, err := os.Create("candlestick_chart.html")
    if err != nil {
    log.Info().Msgf("[plotCandlestickChart] Error creating file: %v", err)
    return
    }
    defer f.Close()

    // Render the chart as HTML
    if err := kLine.Render(f); err != nil {
    log.Info().Msgf("[plotCandlestickChart] Error rendering chart: %v", err)
    }
    }


    WithItemStyleOpts has Color for up candles and Color0 for down candles and these are applied globally to the series.

    If it’s not possible in go, which angular charting library can be used for this?

    Continue reading...

Compartilhe esta Página