When utilizing amcharts within a div, it requires a fixed size in order to load properly. However, this

Struggling to make these charts work properly and not finding a solution. Attempting to integrate amcharts into a web application, I managed to display the charts but encountered errors. When setting the div width to a percentage, the chart only appears after resizing the browser window or pressing F12. If set to a fixed pixel width, the chart renders initially but with cut off axis labels. A Google search mentioned issues with rendering within a hidden div, although mine is not concealed. Uncertain how to ensure the page has loaded before rendering the chart via JavaScript.

  getSecondChart: function (demoChart3) {


        var chart;
        var chartData = [{
            "year": 2009,
            "income": 23.5

        }, {
            "year": 2010,
            "income": 26.2

        }, {
            "year": 2011,
            "income": 30.1

        }, {
            "year": 2012,
            "income": 29.5

        }, {
            "year": 2013,
            "income": 30.6

        }, {
            "year": 2014,
            "income": 34.1

        }
        ];



        AmCharts.ready(function () {
            // SERIAL CHART
            var chart = new AmCharts.AmSerialChart();
            chart.dataProvider = chartData;
            chart.categoryField = "year";
            chart.startDuration = 2;
            // change balloon text color                
            chart.balloon.color = "#000000";


            // AXES
            // category
            var categoryAxis = chart.categoryAxis;
            categoryAxis.gridPosition = "start";

            // value
            var valueAxis = new AmCharts.ValueAxis();
            valueAxis.axisAlpha = 0;
            chart.addValueAxis(valueAxis);

            // GRAPHS
            // column graph
            var graph1 = new AmCharts.AmGraph();
            graph1.type = "column";
            graph1.title = "Income";
            graph1.lineColor = "#FF6600";
            graph1.valueField = "income";
            graph1.lineAlpha = 1;
            graph1.fillAlphas = 1;
            graph1.dashLengthField = "dashLengthColumn";
            graph1.alphaField = "alpha";
            graph1.balloonText = "<span style='font-size:13px;'>[[title]] in [[category]]:<b>[[value]]</b> [[additional]]</span>";
            chart.addGraph(graph1);

            // WRITE
            chart.write("demoChart3");
        })
    }

html

          <div data-dojo-type="dijit.layout.ContentPane" data-dojo-props="splitter:false">
                   <div id="demoChart3" style="width: 80%; height:342px;"></div>
           </div>

Also utilizing dojo which didn't meet my chart appearance preferences, hence the switch to amcharts. The chart function is called within the initial loading function:

renderChart = function () {
  myhomeModule.getSecondChart("demoChart3");

}

Answer №1

I successfully implemented your example in a fiddle: http://jsfiddle.net/danzoeller/JRuC7/

Utilizing amCharts with twitter bootstrap has proven to be highly adaptable in terms of div layout. The charts have consistently resized according to the browser window dimensions without any issues. It is likely that there may be conflicting CSS rules causing the chart-containing div to truncate. I suggest temporarily removing the stylesheet to verify if the display improves, then gradually reintroduce your styles to identify the problematic area.


function HomeModule() {
    this.getSecondChart = function (demoChart3) {

        var chart;
        var chartData = [{
            "year": 2009,
            "income": 23.5
        }, {
            "year": 2010,
            "income": 26.2
        }, {
            "year": 2011,
            "income": 30.1
        }, {
            "year": 2012,
            "income": 29.5
        }, {
            "year": 2013,
            "income": 30.6
        }, {
            "year": 2014,
            "income": 34.1
        }];

        AmCharts.ready(function () {
            // SERIAL CHART
            var chart = new AmCharts.AmSerialChart();
            chart.dataProvider = chartData;
            chart.categoryField = "year";
            chart.startDuration = 2;
            
            // Customize balloon text color                
            chart.balloon.color = "#000000";

            // AXES
            // category
            var categoryAxis = chart.categoryAxis;
            categoryAxis.gridPosition = "start";

            // value
            var valueAxis = new AmCharts.ValueAxis();
            valueAxis.axisAlpha = 0;
            chart.addValueAxis(valueAxis);

            // GRAPHS
            // column graph
            var graph1 = new AmCharts.AmGraph();
            graph1.type = "column";
            graph1.title = "Income";
            graph1.lineColor = "#FF6600";
            graph1.valueField = "income";
            graph1.lineAlpha = 1;
            graph1.fillAlphas = 1;
            graph1.dashLengthField = "dashLengthColumn";
            graph1.alphaField = "alpha";
            graph1.balloonText = "<span style='font-size:13px;'>[[title]] in [[category]]:<b>[[value]]</b> [[additional]]</span>";
            chart.addGraph(graph1);

            // WRITE
            chart.write("demoChart3");
        })
    }
};

var myHomeModule = new HomeModule();

$(document).ready(function () {
    myHomeModule.getSecondChart();
});

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

"Automatically redirecting after 5 seconds following the submission of an ajax form using

When submitting a form, I use the following code: $(this).parents("form").ajaxForm(options); I'm wondering how can I set a timeout for this process. Specifically, I would like to redirect the page after 5 seconds of form submission. ...

Creating a dependency between two checkboxes using jQueryFeedback: Looks good!

Looking for help with jQuery code to make two checkboxes dependent. When checkbox1 is checked, checkbox2 should automatically turn on. Here's my current code: //php $cus_centre = $this->createElement('checkbox', 'checkbox1'); ...

Tips for eliminating the "Your connection is not secure" page in Firefox when using "Selenium with JavaScript"

My current project involves automating a secure URL using Selenium, WebdriverIO, and JavaScript. However, the page keeps redirecting to a "Your connection is not secure" message. I have attempted to address this issue by setting various preferences in the ...

Why did the Mongoose fail to cast to ObjectID for this value?

I'm facing an issue where I know what the problem is, but can't wrap my head around why it's happening. In my simple recipe app that utilizes express and mongoose, users input recipe information through a form which is then saved to the data ...

Is there a way to transfer JavaScript data to PHP?

<div> <p>This is a sample HTML code with JavaScript for tallying radio button values and passing them to PHP via email.</p> </div> If you need help converting JavaScript data to PHP and sending it via email, there are v ...

Transform the decimal numbers within an array into percentages before feeding all the values into a graph

I'm currently working on converting an array of decimals to percentages to effectively display the data as percentages in my chart created with chart.js. Below is the array that I am dealing with. https://i.sstatic.net/HVXLi.png During my data mappi ...

jQuery Validation for Radio Buttons and Optional Fields

I've been pondering over this issue for a couple of hours now. Currently, my focus is on validating a form using a jQuery plugin. Here's the link to the documentation: http://docs.jquery.com/Plugins/Validation Here's the code I have so fa ...

Is it possible to both break down a function parameter and maintain a named reference to it at the same time?

When working with stateless functional components in React, it is common to destructure the props object right away. Like this: export function MyCompoment({ title, foo, bar }) { return <div> title: {title}, ...</div> } Now ...

The importance of dependencies in functions and testing with Jasmine

In troubleshooting my AngularJS Service and Jasmine test, I encountered an issue. I am using service dependency within another service, and when attempting to perform a Unit Test, an error is thrown: TypeError: undefined is not an object (evaluating sso ...

Trigger .gif on hover using ng-repeat in AngularJS

Many solutions to this problem involve using jQuery, such as the following examples: Stop a gif animation onload, on mouseover start the activation and Animating a gif on hover. However, I'm interested in achieving the same functionality using Angular ...

Achieving the validation with a bold red border

Hi, I'm currently learning React and I've been using regular JavaScript to validate my form. Here's a snippet of how I'm doing it: <TextField label="Title" variant="outlined" si ...

"I am trying to figure out how to set a link to an image using JavaScript. Can someone help me

I need help figuring out how to insert an image or gif file within two inverted commas '' in this line of code: _("status").innerHTML = ''; (line number 13 in the actual code) Your assistance with this question would be greatly appreci ...

Monitor the change in FileReader().readyState using AngularJS

Below is the AngularJS code I have written to read a local file. var files = document.getElementById("file"); files.addEventListener("change", handleFile, false); function handleFile(event) { SpinnerService.upload(); // Display loading spinner ...

Ways to verify if a string is a number without using isNaN and with specified criteria

I am trying to determine if a string represents a valid number without relying on the isNaN function. The reason for this is that I need to be able to accept the ',' character, which isNaN ignores. Additionally, I do not want to allow negative nu ...

What is the technique for accessing dynamic object properties in Javascript?

I am dealing with a dynamic object where the property values change based on different data sets. For example: MyObj = { country:"Ind", Place:"Pune"} Now, I have a data value that determines which property value I need to retrieve. var MyArr = this.Filt ...

After completing a sequence, it does not reset

I'm working on creating a versatile "slideshow" feature that can be used for various elements. Currently, I am testing it on a div that contains paragraph text taken from my HTML document and represented as container1, container2, and container3. The ...

Bypassing the CORS restrictions to communicate with your backend API

I've recently begun teaching myself web development and have started sending XMLHttpRequests from a client-facing page to an Express.js API backend. The main purpose of this is to transmit form data and store it in a Mongo Database. However, I'm ...

Using Google Maps: How can I trigger an InfoWindow to open by hovering over a link?

Trying to figure out how to make the "info window" on my map pop up when hovering over a corresponding link in the list of points. Currently, you have to click on a point to see the information. Any suggestions on how to achieve this while ensuring that al ...

Stacked column tooltip displaying an array of 3 values

I am working with a json code that looks like this: [[1385420403000,9.86,6.91],[1385506802000,11.89,6.57],[1385593203000,14.11,10.58],[1385679602000,9.1,8.9],[1385766003000,13.59,7.53],[1385852402000,10.68,6.69],[1385938803000,11.03,10.52],[1386025202000, ...

Dynamic SVG circles with timer and progress animation

Is there a way to modify the following: var el = document.getElementById('graph'); // get canvas var options = { percent: el.getAttribute('data-percent') || 25, size: el.getAttribute('data-size') || 220, lineW ...