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

Possible to extend horizontal bar controller? #2726

Closed
daviddevore11 opened this issue Jun 7, 2016 · 4 comments
Closed

Possible to extend horizontal bar controller? #2726

daviddevore11 opened this issue Jun 7, 2016 · 4 comments

Comments

@daviddevore11
Copy link

daviddevore11 commented Jun 7, 2016

In the documentation I see that you can extend the controllers for .line, .bar, .radar, .doughnut, .polarArea, and .bubble. Can you also extend the horizontal bar controller? I am attempting to add target lines to each bar in the graph and I successfully did that by overriding the original controller. I would also like to create other horizontal graphs without the target line but since I overridden the horizontal bar controller it will always draw a target line. Here is my code for the target line with overriding the controller:

            var originalHorBarController = Chart.controllers.horizontalBar;
            Chart.controllers.horizontalBar = Chart.controllers.horizontalBar.extend({
                draw: function(){
                    var self = this;
                    var base;
                    var x;
                    var y;
                    var width;

                    console.log(arguments);
                    originalHorBarController.prototype.draw.call(this, arguments[0]);
                    console.log(this.chart.data.labels[0]);
                    console.log("x:" + this.calculateBarX(1, 0) + " y:" + this.calculateBarY(1, 0) + " width:" + this.calculateBarBase(0, 0));
                    console.log(this.getRuler(0).barHeight);
                    console.log(this.chart.config.data.labels);
                    console.log(this.chart.config.data.datasets[0].data);

                    for(var i = 0; i < this.chart.data.datasets[0].data.length; i++){
                        base = this.calculateBarBase(0, i);
                        x = this.calculateBarX(i, 0);
                        y = this.calculateBarY(i, 0);
                        width = this.getRuler(i).barHeight;

                        this.chart.chart.ctx.beginPath();
                        this.chart.chart.ctx.lineWidth = "5";
                        this.chart.chart.ctx.strokeStyle ="black";
                        this.chart.chart.ctx.moveTo(x - 10, y - (width / 2));
                        this.chart.chart.ctx.lineTo(x - 10, y + (width / 2));
                        this.chart.chart.ctx.stroke();
                    }
                }
            });

              new Chart(canvas, {
                type: 'horizontalBar',
                data: data
              });
@etimberg
Copy link
Member

etimberg commented Jun 7, 2016

@daviddevore11 the problem that you're running into is that you are replacing the original horizontalBar controller.

I would create a new type

Chart.controllers.newHorizontalBar= Chart.controllers.horizontalBar.extend({});

new Chart(canvas, {
  type: 'newHorizontalBar',
  data: data
});

@daviddevore11
Copy link
Author

@etimberg When I try to do this the graph switches my labels around to make it seem like a vertical bar chart and the graph is empty

@daviddevore11
Copy link
Author

daviddevore11 commented Jun 7, 2016

@etimberg here is a link to a fiddle that I just created
https://jsfiddle.net/ntcdfs7q/1/

@etimberg
Copy link
Member

etimberg commented Jun 7, 2016

@daviddevore11 that's because there is no default config for the new chart type.

I updated your fiddle to use the same default config as the horizontal bar https://jsfiddle.net/ntcdfs7q/2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants