highcharts-for-python / highcharts-core

Python wrapper for the Highcharts Core JavaScript library
https://www.highcharts.com/integrations/python
Other
58 stars 13 forks source link

Lang when download_chart #167

Closed LukaszWakeapp closed 5 months ago

LukaszWakeapp commented 7 months ago

Hi, I want to change labels language of the X axis. The X axis is a datetime and I would like the format: "day short_month" e.g. 17 Kwi

'xAxis': [{ 'type': 'datetime', 'lineWidth': 1, 'tickLength': 1, 'tickWidth': 1, 'gridLineWidth': 1, 'lineColor': '#EDEEEE', 'tickColor': '#EDEEEE', 'gridLineColor': '#EDEEEE', 'startOnTick':False, 'labels': { 'format': '{value:%d %b}' }, }],

I do not know how to set shortMonths: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru']

I do not display the chart, I download it

chart.download_chart( format = 'png', filename = 'images/chart-visits.png' )

hcpchris commented 5 months ago

Hi @LukaszWakeapp : When you are attempting to export a chart you can apply the lang properties to a set of global options. When you request the export from the export server, you can supply those global options and they will be applied to the chart. The following is a snippet of Python code which generates a simple chart that applies the short month names that you outlined:

from validator_collection import validators
from highcharts_core.chart import Chart
from highcharts_core.global_options.shared_options import SharedOptions
from highcharts_core.global_options.language import Language

# Create the Language options and configure the shraed options for the chart.
language = Language(short_months = ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paz', 'Lis', 'Gru'])
global_options = SharedOptions(language = language)

options = {
    "xAxis": {
        "type": "datetime"
    },
    "series": [
        {
            'name': 'Series 1',
            'type': 'line',
            'data': [{
                'x': validators.date("2024-01-01"),
                "y": 123
            },
            {
                "x": validators.date("2024-02-14"),
                "y": 456
            },
            {
                "x": validators.date("2024-03-14"),
                "y": 789
            },
            {
                "x": validators.date("2024-04-14"),
                "y": 987
            },
            {
                "x": validators.date("2024-05-14"),
                "y": 654
            },
            {
                "x": validators.date("2024-06-14"),
                "y": 321
            }
            ]
        }
    ],
}

# Create a chart object
chart = Chart(options=options)

# Download a PNG
my_png_image = chart.download_chart(format='png', scale=1, filename='download.png', global_options = global_options)

This will produce the following chart as a PNG image saved in the file download.png:

image

Hope this helps and if you run into other questions, please let us know!