The initial size of C3 charts is unnecessarily large upon loading

I am utilizing the C3 JavaScript library to showcase graph data on my webpage. Initially, when the page loads, the graphs remain hidden until a specific "tab" is selected. However, I am encountering an issue where the first graph does not fit within its designated div tag upon initial load. Changing the date range by clicking a button corrects this sizing problem.

Below is the HTML code snippet that includes the AngularJS framework for rendering the graphs:

<div class="chartgrouping">

<div ng-show="showGraphSection" class="graphA">
    <h2 class="section-main-heading">Data A</h2>
    <div id="dataAChart" class="chart c3"></div>
</div>
<div ng-show="showGraphSection" class="graphB">
    <h2 class="section-main-heading">Data B</h2>
    <div class="stats">
        <div class="highest-value">Data Breakdown<span>{{percentageOfSubstance}}%</span></div>
        <div class="goals">GOAL: 20%</div>
    </div>
    <div id="dataBChart" class="c3" style="max-height: 320px; position: relative;"></div>
</div>

</div>

Initially, the display is incorrect with the first graph stretching across completely. After loading new data and adjusting the display, the graphs resize appropriately. Is there a recommended solution for solving this issue?

Answer №1

The solution I discovered is related to how the full width of an element is determined when it is hidden, which is often the case when using bootstrap tabs or other hidden elements.

To work around this issue, you must trigger the resize event on the window so that d3, the underlying graphing library, can calculate the correct sizing.

jQuery(window).trigger('resize');

You can achieve this in bootstrap by using something like

jQuery('a[data-toggle=tab]').on('shown.bs.tab', function() {
jQuery(window).trigger('resize');
});

Answer №2

Although @Flanamacca's suggestion was helpful, I found that only part of it worked for me. Implementing a setTimeout function with a delay of 0 did the trick in resolving the issue.

Answer №3

After exploring various options, I finally uncovered a resolution. This particular fix is specific to Angular, so those encountering the same issue without using Angular will need to make appropriate adjustments.

The workaround I implemented involved removing an ng-show from the parent div and instead replicating it exactly on each individual sub-tag (the h2 and its siblings). It may seem like a simple solution now, but it was quite a challenge to arrive at this conclusion.

Answer №4

I had no luck with the

jQuery(window).trigger('resize');
method. However, utilizing the C3 library's resize() function worked perfectly:

var chart = c3.generate({
bindto: '#chart',
data: {
    columns: [
        ['data1', 300, 350, 300, 0, 0, 0],
        ['data2', 130, 100, 140, 200, 150, 50]
    ],
    types: {
        data1: 'area',
        data2: 'area-spline'
    }
}
});


$('.collapse').on('shown.bs.collapse', function() {//Your event listener
    chart.resize();

});

Answer №5

In the case of utilizing Foundation with several tabs, the solution that proved effective for me was:

$('#myPanelId' ).on('toggled', function(){
        jQuery(window).trigger('resize');
 })

Ensure to substitute myPanelId with the actual panel id housing the charts.

Answer №6

Although I tried @Flanamacca's solution and it didn't work for me initially, I found that using the jQuery selector they recommended helped solve my bootstrap tab-related issue.

What ended up working for me was utilizing the c3 flush() method, which triggers a chart redraw (check out ).

$('a[data-toggle=tab]').on('shown.bs.tab', function() {
  my_c3_chart.flush();
});

Answer №7

By adjusting the CSS max-width property of the container div, I was able to successfully display the chart.

Answer №8

Although our circumstances may vary, there is a possibility that someone with a deeper understanding of html rendering can draw insights from this situation.

In my case, the problem arose when I was concealing the graph until my data was fetched and the graph was created using c3.generate. The issue stemmed from hiding the graph using css "display: none;". Once I made the adjustment to use "opacity: 0," the problem was resolved.

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

Issue with display of animation on MUI TextField

I'm facing an issue with my TextField component. I have a placeholder and value set up, but when I insert an icon using the InputProps prop, the animation that normally changes from placeholder to value doesn't appear. However, if I remove InputP ...

Is it possible for me to bring in/need the Kik.js document instead of using the Kik Card?

Currently working on a mobile web app designed for Kik. Their documentation advises including the Kik.js file directly in your HTML like this: <!-- simply include this script in your webpage --> <script src="http://cdn.kik.com/kik/2.3.6/kik.js"&g ...

How to connect an external module to NuxtJS using Vue.js

Attempting to incorporate a widget/plugin/extension system into my existing web UI built with NuxtJS. Within the pages/view.vue single-file component, I aim to establish the extension system by dynamically loading components indicated through query paramet ...

How to monitor multiple items using the mouseleave event in jQuery

I am currently trying to achieve a specific effect by keeping the green border visible for the active menu item when hovering over the dropdown. The issue I am encountering is that the border disappears when moving the cursor over the dropdown, which is no ...

Displaying data on the user interface in Angular by populating it with information from the form inputs

I am working on a project where I need to display data on the screen based on user input received via radio buttons, and apply specific conditions. Additionally, I require assistance in retrieving the id of an object when the name attribute is chosen from ...

Enhance your web forms with the Jquery selectBox plugin, allowing you to effortlessly pass multiple values to

Is there anyone available to assist me with the jQuery selectbox plugin? I have successfully implemented sending data from a select box onChange through Ajax, but I am facing an issue with using multiple select boxes. I am unsure how to pass values from a ...

Show two columns horizontally using JavaScript

Hey, I'm working on a project where I need to display data from an array in an HTML output table. However, I'm facing an issue with displaying the table on the same page after clicking the submit button. Using document.write redirects me to anoth ...

Using regular expressions to extract the value of a specific key from a JavaScript object

After scraping a webpage with BeautifulSoup and requests, I came across this snippet of code within the HTML content: $create(Web.Scheduler, { "model": '{"apt":[0]}', "timeZone": "UTC", "_uniqueI ...

Unable to establish the origin for an element using JavaScript

Currently, I am developing a simple game in JavaScript that includes a grid and various cells. Here is the current look of the game which functions perfectly. However, I am facing an issue with setting the margin to 0 and wanting to center the canvas inste ...

Debugging Slideshows Using JavaScript

I am currently working on creating a slideshow and I'm facing some challenges with the JavaScript functionality. Everything seems to be running smoothly except for one issue - when I click right once, it transitions correctly, but if I then click left ...

What steps are required to start running Karma once it has been installed?

I'm experimenting with using karma for various watch processes. To start, I globally installed karma by running: npm i -g karma After that, I executed karma start karma.conf.js and it ran successfully. Now I want to install karma locally within th ...

Is there a way to automatically select all checkboxes when I select contacts in my case using Ionic 2?

initializeSelection() { for (var i = 0; i < this.groupedContacts.length; i++) { for(var j = 0; j < this.groupedContacts[i].length; j++) this.groupedContacts[i][j].selected = this.selectedAll; } } evaluateSelectionStatus() { ...

Building an Express API using ES6 script: A Step-by-Step Guide

After creating a no-view REST API using the express generator, I decided to convert everything to ES6 script and compile it with Babel. However, upon entering localhost after the changes, an error message appeared: No default engine was specified and no ...

Display the DIV element once a specific number of radio buttons have been selected

I need help figuring out how to display a DIV after a specific number of radio buttons are selected. Currently, I have it set up to appear only when all the radio buttons are selected thanks to some guidance from others who had similar questions. However, ...

Hover over parts of an image to bring attention to them

I am interested in developing a webpage featuring a black and white image of 5 individuals. When hovering over each person, I would like them to illuminate and display relevant information in a dialog box next to them. Do you have any tips on how I can ac ...

Trouble with Bootstrap columns displaying on smaller devices

I designed a grid with 3 columns that exhibits issues, specifically that the columns shrink too much on smaller screens and distort the content. Is there a way to introduce a break-point at around 1000px so that all content shifts into a single column? An ...

After a postback, the Javascript function removes the displayed div

I am facing an issue with a page containing a button that uses JavaScript to display the content of a div. I also have an ASP.net validator control that triggers a postback. When I debug the JavaScript in Firebug, the div remains visible; however, if I all ...

Exploring a property within a JavaScript object

Within my code, I am working with an array of objects called user. When I try to log the first object using console.log(user[0]);, I receive this output: Object {activityId: "2", id: "1", activityDt: "01/15/2016"} Afterwards, I attempt to store user[0] in ...

Trying to bring in components from directories above

I'm currently facing an issue with importing components from a parent directory into my project. My goal is to be able to use these components across multiple projects, which seems like the most straightforward approach. However, when I attempt this, ...

What is the best way to eliminate a specific element from a JavaScript array?

What is the best way to eliminate a particular value from an array? For example: array.exclude(value); Limitations: I must solely rely on pure JavaScript without any frameworks. ...