Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseLineOptionsChartView: Need an option callback for y-axes ticks to line chart #50

Closed
gauravndabhade opened this issue Apr 11, 2020 · 1 comment

Comments

@gauravndabhade
Copy link

gauravndabhade commented Apr 11, 2020

I am trying to configure line chart options, for formatting y-axes tick values.

class LineChartJSONView(BaseLineOptionsChartView):
    
    ...
    ## I have implemented `get_labels` & `get_data` functions
    ...

    def get_options(self):
        return {
            "responsive": True,
            "scales": {
                "xAxes": [{
                    "scaleLabel": {
                        "display": True,
                        "labelString": "Daily"
                    }
                }],
                "yAxes": [{
                    "scaleLabel": {
                        "display": True,
                        "labelString": 'Count'
                    },
                    "ticks": {
                        "beginAtZero": True,

                         ## Need to pass function from `dict`
                        "callback": function (value) { if (value % 1 === 0) { return value; } }
                    }
                }],
            },
        }

line_chart_json = LineChartJSONView.as_view()

I have called line_chart_json in template.

...
<html>
    <canvas id="myChart" width="500" height="400"></canvas>
    ...
<script type="text/javascript">
    $.get('{% url "line_chart_json" %}', function (response) {
        var ctx = $("#myChart").get(0).getContext("2d");
        new Chart(ctx, {
            type: 'line',
            data: response.data,
            options: response.options
        });
    });

</script>
</html>
@Natim
Copy link
Collaborator

Natim commented May 6, 2020

I don't think we should support writting JavaScript in a Python string and then evaluate it in JS.

I guess one should define the javascript callback in javascript and then add it like that:

    $.get('{% url "line_chart_json" %}', function (response) {
        var ctx = $("#myChart").get(0).getContext("2d");
        var options = response.options;
        options.scales.yAxes[0].ticks.callback = function (value) { if (value % 1 === 0) { return value; } };
        new Chart(ctx, {
            type: 'line',
            data: response.data,
            options: options
        });
    });

@Natim Natim closed this as completed May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants