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

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

Open
templargin opened this issue Jul 31, 2020 · 5 comments
Open

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

templargin opened this issue Jul 31, 2020 · 5 comments

Comments

@templargin
Copy link

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
Copy link
Collaborator

Natim commented Aug 3, 2020

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

@ActionRich
Copy link

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

image

@iragm
Copy link

iragm commented Apr 11, 2024

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
Copy link
Collaborator

Natim commented Apr 11, 2024

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 pushed a commit to iragm/django-chartjs that referenced this issue Apr 11, 2024
@iragm
Copy link

iragm commented Apr 11, 2024

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.

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

4 participants