[Fixed]-Make the chart display current year in HighChart

1👍

If the year doesn’t need to be dynamic you can just insert the current year in the chart options xAxis title section using built-in JavaScript date object:

xAxis: {
    ...
    title: { 
        text: new Date().getFullYear()
    }
    ...
},

If you need the year to be dynamic then you can pass it along with your existing AJAX call.

views.py

class ChartData(APIView):
    def get(self, request, format=None):
    ...
    data = {
        "xAxisTitle": 2017 # assign year here dynamically using method of choice
        ...
    }

chartViewHigh.html

<script>
    ...
    var xAxisTitle = "";
    ...

    $.ajax({
        ...
        success: function (data) {
            ...
            xAxisTitle = data.xAxisTitle;
            ...
    });

    ...

        xAxis: {
        ...
        title: { 
            text: xAxisTitle
        }
        ...
    },

Leave a comment