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

Error message from sails.js: `req.target` is not defined

Experiencing a problem where req.target sometimes returns undefined, causing issues with other functionalities dependent on req.target. Seeking assistance to resolve this issue. Appreciate any help! ...

Postpone the execution of the page controller until all necessary initial data has been collected

My Single Page Application (SPA) is in need of some initial data fetched from the server, such as user login status and permissions. However, I want to defer loading the page until this vital information is retrieved to avoid any issues with incomplete dat ...

Working with scrollIntoView in useEffect (Encountering an error trying to access 'scrollIntoView' property of null)

Starting from scratch, I'm working on setting up my webpage and crafting some code. function BasicDetails() { React.useEffect(()=>{ scrollView(); }, []); function scrollView() { document.getElementById('main-root& ...

AngularStrap's bs-modal is failing to display content

I have a question, here is a Plunker with the code: http://plnkr.co/edit/SNFy2XcOBefUavG1QCqD?p=preview <button class="btn btn-default" data-template="customer.tpl.html" bs-modal="modal">New Customer </button> < ...

What is the best way to invoke a TypeScript function within a jQuery function?

Is it possible to invoke a TypeScript function within a jQuery function? If so, what is the correct approach? Here is an example of my component.ts file: getCalendar(){ calendarOptions:Object = { height: 'parent', fixedWeekCount : ...

Converting HTML to a Text String (not for display) using Angular

When it comes to displaying HTML in Angular, there are numerous approaches available. For example, using $sce.trustAsHtml(myHtmlVariable) is one way to achieve this. However, I am interested in creating something like the following: myStringVariable = s ...

I am experiencing issues with the search feature in angular and node.js that is not functioning properly

Need assistance with debugging this code. I am currently working on adding search functionality to my Angular web page. However, when testing the code in Postman, I keep receiving the message "NO USER FOUND WITH USERNAME: undefined". Additionally, on the w ...

Sign in with Google / External authentication service

Currently working on an AngularJS application that includes Google login functionality. To implement this feature, I found a useful guide at the following link: . Additionally, there is a script that is called when the page loads: <script type="text/j ...

How can I deactivate a Material UI button after it has been clicked once?

Looking to make a button disabled after one click in my React project that utilizes the MUI CSS framework. How can I achieve this functionality? <Button variant="contained" onClick={()=>handleAdd(course)} disabled={isDisabled} > ...

Unable to view the refreshed DOM within the specifications after it has been altered

For my current project, I am working on writing a functional spec that involves using Mocha/JSDOM and making assertions with 'chai'. The specific use case I am tackling is related to the function called updateContent: When this function is exec ...

"Redirecting to an HTML page from the POST method is not supported in the Flask backend and Vanilla JS frontend setup

Utilizing the selected dropdown value from the frontend to perform calculations on the backend, resulting in an HTML page being returned. It's worth noting that no response is needed from the POST method, such as using return jsonify. Currently, I am ...

Tips on safeguarding your templates while working with SailsJS and AngularJS

I am currently working on a web application using SailsJS and AngularJS. My project requires session management to track user login status. I came across this helpful tutorial and implemented an index.ejs view located in the /view folder. Within the index. ...

Using filters to target children within the scope (AngularJS)

As a newcomer to AngularJS, I am currently working on a basic proof-of-concept project for my supervisor. The project involves creating listings for car hire, with results displayed in the main section of the view using external JSON data, and filters on t ...

ng-tourguide component for navigating through multiple screens

Currently, I am utilizing Angular JS to develop my project. One requirement I have is to implement a tour guide that navigates through multiple pages and displays the next step upon clicking the next button. I have successfully incorporated ng-walkthrough ...

assigning the browser's width to a variable within an scss file

Is there a way to dynamically set the browser's width as a variable? I am familiar with @media queries, but I need to calculate the browser's width for a specific purpose. I attempted using jQuery to achieve this, and it works well with a fixed ...

How to Merge Items within an Array of Objects Using Typescript?

I'm currently facing a challenge in combining objects from an array of Objects in typescript. The structure of the array is as follows: 0: {type: 'FeatureCollection', features: Array(134)} 1: {type: 'FeatureCollection', features: ...

Successful execution of asynchronous method in Node.js without any errors

When encountering duplicates in the config, I use the code snippet below: require('./ut/valid').validateFile() }); If a duplicate is found during validation, an error is sent like so: module.exports = { validateFile: function (req) { ... ...

Instructions on creating an input field and a slider simultaneously

Currently, I am exploring the world of CSS 3D transforms and I am particularly interested in creating sliders to manage these transforms. I recently stumbled upon the Three.js editor and was impressed by how it handles the positioning, rotation, and scalin ...

Issue with JavaScript function not triggering in Internet Explorer

Here is the code snippet I am working with: <body onLoad="subcatSelection();"> The function that should run on body load is located in a .js file that is included using this code: <script type="text/javascript" src="bincgi/search_lists.js"& ...

What is the best way to adjust the decimal values in my API calls?

Is there a way to adjust the numerical data returned by the API to display only two decimal places instead of three? For example, if the number is 140.444, I want it to show as 140.44 What changes do I need to make? function fetchData() { fetch(" ...