Angular directive for resizing C3 charts

I've recently implemented a basic donut chart using C3, which I've enclosed in a directive and everything is functioning smoothly.

Below is the code for my directive:

angular.module('dashboardApp').directive('donutChart', function() {       

    function link(scope, element, attr) {      


        scope.$watch(function() {

            return scope.data;
         },
            function() {                    
                if(scope.data !== undefined){                       

                    var chart = c3.generate({
                        bindto: 'donut-chart',
                        data:{
                            columns: scope.data,                           
                            type: 'donut'
                            },
                        donut:{
                            label : {
                                format: function(value,ratio,id){
                                    return value;
                                },

                            }
                        },
                        tooltip:{
                                position: function(data,width,height,element){
                                    return {top:-180,left:300}
                                }
                            },                      
                    }); 
                }       
            }
        )};         

    return {
        restrict: 'EA',         
        scope: {
            data: '='       

        },
        link: link
    };
});

In my HTML file, you can find the following structure:

<div class="row">
    <div class="donut-route" >
        <donut-chart  data='routes'></donut-chart>
    </div>
</div>

My objective is to dynamically resize the donut chart within its current div container.

I'm familiar with C3's resizing capabilities where values can be passed through. One approach could involve incorporating window sizes into the $scope.watch function to trigger a resize each time. However, I'm unsure if this would impose too much overhead. It seems that C3 graphs should resize automatically, but due to it being inside a directive, there might be limitations.

If anyone has any insights or guidance on this matter, I'd greatly appreciate it.

Answer №1

I placed your code into a functional Plunker to analyze the issue at hand.

Since the chart generated is an SVG object, resizing it automatically using CSS can be complex. However, I recommend exploring the following resources:

https://css-tricks.com/scale-svg/

or reference this query on Stackoverflow

In my opinion, the most efficient approach (as you mentioned) would be to utilize the size option during generation:

   var chart = c3.generate({
        size: {
          height: 200,
          width: 200
        }...

and then use the resize method of C3 API to adjust the size when needed:

  chart.resize({height:100, width:300})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to incorporate a computed value into a table prop while working with Element UI?

In my frontend development, I am utilizing vuejs along with element ui. I am faced with the task of rendering a table that includes dates in unix format. To make these dates more user-friendly, I have incorporated moment.js to convert them into a readable ...

iOS hover menu behavior: Close dropdown when the same element is tapped again on iPads

The navigation bar features categories such as: politics, economy, education Upon clicking on these categories, a dropdown menu appears with locations like: asia, europe, North America What I am looking for is this: If the user clicks (tabs) or hovers (o ...

Pass data from controller using Ajax in CodeIgniter

Here is my JavaScript code: $(document).ready(function(){ $("input[type='checkbox']").change(function(){ var menu_id = this.value; if(this.checked) var statusvalue = "On"; else var statusvalue = "Off"; $.ajax( ...

tips on retrieving the Vue element using a method in Vue.js

How can I dynamically add an element to the array by clicking a button in vue.js? Is it possible to call the method aggiungiViaggio on click, and how do I determine which viaggio I am currently in? Below is an example of the HTML: <div class=" ...

Jade, res.render, and res.write are essential tools for rendering

I am currently working on creating a simple FTP client in Node.js. Everything seems to be running smoothly, but I am facing difficulty in displaying the "singleFile.name" in my Jade template. app.post('/ftp/login', function(req, res){ ftp.ls(" ...

Add the scss file to the vuejs component npm package only if certain conditions specified in the project are met

Creating css/scss themes for my Vue Components Npm package has been a focus of mine lately. This particular package is local and currently being tested using npm link. Both the Package and Project are utilizing webpack. index.js of Package import "./src ...

Angular's $cookies are limited to functioning solely on the 'localhost' domain

Prepare yourself, an unusual Angular / IIS issue is on the horizon: I have developed an Angular 1.4.0 application that utilizes $cookies to store the oauth access token once a user logs in. Everything runs smoothly on localhost but encounters issues when ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

Updating the class of a div based on a checkbox being checked or unchecked

I am attempting to apply the "isChecked" class to the parent div when a checkbox is checked, using the ngClass directive. Here is the HTML: <div ng-class="{isChecked: (isChecked_1 == 'true')}"> <input type="checkbox" id="check_ ...

Maximizing React Performance: Strategies for Avoiding Unnecessary Renderings on State Updates

I am facing a major performance issue due to constant re-rendering of my child components on every state change. I need a solution to prevent this from happening and maintain the stability of my application. Any suggestions or guidance would be highly ap ...

The surprising twist of hasOwnProperty's behavior

I am currently working on a function that is designed to check whether an object contains keys such as 'id' or 'serif:id'. However, I have encountered some issues with its functionality. function returnIdPreferSerifId(object) { if ...

Can you explain the significance of setting scope:true in an AngularJS directive?

When scope:false is used, it indicates that the directive will not have its own scope. If scope:{something}, an isolated scope will be created for the directive. But what does scope:true mean? And more importantly, why is it useful? Thank you! ...

How can I customize the ngSrc directive in Angular to include a request header?

Is there a way to attach an authentication token to the URL request in angular js when using the ngSrc directive? If so, how can this be done? ...

Combine multiple values into a single input in the number field

I have a number type input field... What I am looking for is: Age-Height-Weight 20-180-80 Is there a way to ensure that users input data in this exact format and then have the final result inserted into the input field with type="number" and submitted? ...

The TypeScript error occurs when attempting to assign a type of 'Promise<void | Object>' to a type of 'Promise<Object>' within a Promise.then() function

I'm currently working on a service to cache documents in base64 format. The idea is to first check sessionStorage for the document, and if it's not there, fetch it from IRequestService and then store it in sessionStorage. However, I've encou ...

What is the best way to split up the information and place it into separate text boxes?

I am currently working on a page that allows users to create and edit email structures. To facilitate editing, I added two columns to the page. One of the columns displays information from all the email structures along with two buttons - one for editing ...

Currently dealing with a script that utilizes AJAX GET to incorporate JSON data into my table

Greetings and thank you for taking the time to read this! Within my HTML, I have implemented a form element that allows inputting data into 5 different fields. This data is then transmitted to a database using my unique API key. Once stored in the database ...

What steps do I need to take to include React in my background.js script for a chrome

I'm currently facing an issue with my React code that I need to include in my background.js file. However, I encountered the following error message: SyntaxError: Cannot use import statement outside a module The specific import causing this error is: ...

A guide on utilizing the React Suite range slider in rsuite

Hello there, I recently started working with the UI framework rsuite (React suite) and everything was going smoothly until I tried to use the Range slider component's API. Unfortunately, I am facing some issues with the labels and tooltips not display ...

Troubleshooting problem with Ajax responseText

Can an ajax responseText be received without replacing the existing content? For instance: <div id="content"> <p>Original content</p> </div> Typically, after running an ajax request with a responseText that targets id="conten ...