question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Somebody give a Chart.controllers.bar.extend example?

See original GitHub issue

I’m stuck in extend bar chart. I use codes below but not working…

    Chart.controllers.bar.extend({
        draw: function() {
            Chart.controllers.bar.prototype.draw.call(this, arguments);

            /* your own drawing code here */
            console.log(this); // but no any console log appear
        }
    });
    var sleepToday = new Chart(ctx, {
         type: 'bar',
         data: data,
         options: options
    });

Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
snakecommented, Aug 29, 2016

Bump. Is there an issue with extension that is being looked at currently? Looking at the docs, it isn’t 100% clear as to the best approach to extending an existing chart type. Whilst the code posted by Rafael does achieve the extension (Thanks Rafael for posting that!), surely this isn’t the preferred means to extend? Like Iain said, it would be really nice to have a clear example in the docs.

For anyone who may stumble across this, here is how I created a new chart type based on the line chart. Credit to Rafael for pointing out that Chart.defaults needed to be set.

var baseController = Chart.controllers.line;
Chart.defaults.mytype = Chart.defaults.line;

Chart.controllers.mytype = Chart.controllers.line.extend({
    draw: function (ease) {
        baseController.prototype.draw.apply(this, arguments);

        // Draw other stuff here.
    }
});
2reactions
etimbergcommented, Aug 29, 2016

Rafael’s code is close to the recommended way to extend a chart type. I think there is a bug here because we shouldn’t cause a problem if the defaults are not defined. I’ve added comments for each line of the

// Sets the default config for 'myType' to be the same as the bubble defaults. 
// We look for the defaults by doing Chart.defaults[chartType]
// It looks like a bug exists when the defaults don't exist
Chart.defaults.myType = Chart.defaults.bubble;

// I think the recommend using Chart.controllers.bubble.extend({ extensions here });
var custom = Chart.DatasetController.extend(Chart.controllers.bubble.prototype);

// Overwrites the `linkScales` method. I don't think this is necessary and could cause problems
custom.linkScales = Chart.helpers.noop;

// Stores the controller so that the chart initialization routine can look it up with
// Chart.controllers[type]
Chart.controllers.myType = custom; 

I have an example of a chart derived from one of the existing types. I wrote a Smith Chart which derives from a line chart. I think you can use that as a partial reference for getting started.

If you don’t want to create a new chart, you would simply extend Chart.DatasetController to your liking. The API that needs to be implemented is documented at http://www.chartjs.org/docs/#advanced-usage-writing-new-chart-types

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extend bar chart on Chart JS 2 into a new type of Chart
I finally suceeded in creating the new type that extend the bar type and add a line if the value is provided. //...
Read more >
Extend bar chart on Chart JS 2 into a new type of Chart-Chart.js
I finally suceeded in creating the new type that extend the bar type and add a line if the value is provided. //...
Read more >
Layout - Foundations - Human Interface Guidelines - Design
Extend visual content to fill the screen. Make sure backgrounds extend to the edges of the display, and that vertically scrollable layouts, like...
Read more >
Chapter 2: Aeronautical Decision-Making - FAA
Controllers work to make flights as safe as possible. time to use a particular resource and the impact its use will have upon...
Read more >
Apache NiFi User Guide
Controller Service: Controller Services are extension points that, ... This Template is given a name and can then be dragged onto the canvas ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found