peopledoc / django-chartjs

Django Class Based Views to generate Ajax charts js parameters.
Other
407 stars 112 forks source link

Bar chart is not working? What am I doing wrong? #56

Open templargin opened 4 years ago

templargin commented 4 years ago

My view:

class BarChartJSONView(BaseColumnsHighChartsView):
    def get_title(self):
        return "Testing bar charts"

    def get_labels(self):
        return ['Africa', 'America', 'Asia', 'Europe', 'Oceania']

    def get_yUnit(self):
        return "What is this?!"

    def get_providers(self):
        return ["Year 1800", "Year 1900", "Year 2000"]

    def get_data(self):
        return [[107, 31, 635, 203, 2], [133, 156, 947, 408, 6], [1052, 954, 4250, 740, 38]]

My url:

    path('chartJSON', views.BarChartJSONView.as_view(), name='bar_chart_json'),

Template:

    <script type="text/javascript">
        $.get('{% url "accounts:bar_chart_json" %}', function(data) {
            var ctx = $("#myChart").get(0).getContext("2d");
            new Chart(ctx, {
                type: 'bar', data: data
            });
        });
    </script>

Line chart works fine, but just a simple change to implement a bar chart just returns a grid with 1.0 max on Y axis.

Natim commented 4 years ago

What is the template rendering looking like? Can you tell me what you see when you load the url in your browser?

ActionRich commented 4 years ago

I am having the exact same problem, any solution? an example of how to do it properly would be great

image

iragm commented 7 months ago

I'm curious if this works for anyone. It does not appear that there's even a way to set the colors on the bar chart (there's not a get_colors() like there is for BaseLineChartView, and chart.js expects a datasets in the JSON object, but one isn't defined in the context.

I have hacked together something that seems to work, and I can make a PR but I'm not sure this repo is even maintained, or if this would break things for existing users.

Natim commented 7 months ago

Hello @iragm feel free to provide a PR. It would be great not to break to many things for existing users but I believe we can make it work.

iragm commented 7 months ago

That is the code that is working on my system. I am brand new to both chart.js and this project, so feedback is welcome.

Something I can't seem to figure out about this project is why there isn't a generic Chart class in base.py that has functions like get_colors() -- a good chunk of the code in my PR is just copied from lines.py with slight edits and it would make sense to have a base class that has the common functions.