django-chartjs icon indicating copy to clipboard operation
django-chartjs copied to clipboard

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

Open templargin opened this issue 5 years ago • 5 comments
trafficstars

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.

templargin avatar Jul 31 '20 20:07 templargin

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

Natim avatar Aug 03 '20 07:08 Natim

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

image

ActionRich avatar Oct 08 '20 15:10 ActionRich

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.

iragm avatar Apr 11 '24 01:04 iragm

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.

Natim avatar Apr 11 '24 07:04 Natim

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.

iragm avatar Apr 11 '24 11:04 iragm