A step-by-step guide on incorporating a dynamic data series into a line chart using the powerful tool

The data retrieved from a webservice is processed using the BindTrend method and then converted to a JSON object before being applied to the chart. The code for this process is as follows:

 var plot;
    var itemdata = [];
    var chart;
    var data = [];
 $(document).ready(function () {

            $.ajax({
            type: "POST",
            url: "ChartBinder.asmx/BindTrend",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {           
               var newJ = $.parseJSON(msg.d);               
                DrawTrend(newJ);
            },
            error: function (msg) {
                alert("Error");
            }
        });    
    })   

    function DrawTrend(plot) {
       for (i = 0; i < plot.length; i++) {
        var x = {}
        x.id=plot[i].Name;
        x.name = plot[i].Name;
        x.data = [plot[i].Value];
        itemdata.push(x);
        }
              chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 130,
                marginBottom: 25
            },
            series: itemdata
        });
    }

I have noticed that when I manually input values using 'x.data=[1,2,3....]', the chart appears properly. However, in my current implementation, it does not work. Any assistance would be greatly appreciated.

Answer №1

In relation to your comment, it seems that the variable `plot[i].Value` is an array of strings instead of numbers or floats. To convert it to the correct data type using JavaScript, you can follow this code snippet. Additionally, you don't need to explicitly add brackets ([ and ]) around the value since a JavaScript array already includes them by default.

for (i = 0; i < plot.length; i++) {
    var x = {};
    x.id=plot[i].Name;
    x.name = plot[i].Name;

    //plot[i].Value = ["3121", "21211", "3121", "21211", "21000", "9872", "83402", "83402", "28302", "109523", "2832", "9523"];

    var stringArr = plot[i].Value;
    var floatArr = [];
    for(var j = 0; j < stringArr.length; j++){
       doubleArr.push(parseFloat(stringArr[j]));
    }
    x.data = floatArr;

    itemdata.push(x);
}

If you want more information about this topic, I recommend reading the discussion on Highcharts returning error 14.

Answer №2

Are you declaring x within the 'for' loop?

 let obj = {}
 function GenerateTrend(plot) {
       for (let i = 0; i < plot.length; i++) {

        obj.id=plot[i].Name;
        obj.name = plot[i].Name;
        obj.data = [plot[i].Value];
        dataArr.push(obj);
        }

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

Customizing X-axis labels for server-side chart rendering with PhantomJS and JSON data

Currently, I am utilizing phantomjs to generate png images of a highchart using a json file in a commandline script. Since the data is provided in JSON format, I am unable to apply xAxis.labels.formatter as I would in a web client version. However, since t ...

There is an error message in Vue nextTick: "TypeError: this.method is not a function." The error only appears in my console, but the functionality seems to be working correctly in the browser

I'm experiencing an issue where I encounter an error when accessing a certain component for the second time in my browser. The strange thing is that everything appears to be working fine initially, but the error occurs when I try to perform actions li ...

Error: The JSON data type is not recognized - Asp.Net MVC 4

I've been struggling to send a complex JSON object to my action with no success. Below is my javascript code: $.ajax({ url: "http://localhost:52593/" + urlAction.Controller + "/" + urlAction.Action, type: type, dataType: dataType, da ...

Can we switch back to a partial view and return it as JSON?

I encountered a problem with my views, specifically the "index" and "_Track". I am trying to return my datalist to "_Track", which is a partial view. Can someone please assist me with this issue? The error message I received states: "for each statement can ...

The necessary parameters are not provided for [Route: sender] with the [URI: {name}/{code}]. The missing parameters are name and code

I have encountered a problem while using AJAX and I am struggling to find a solution. Routes: web.php Route::get('/{name}/{code}', 'TestController@counter'); Route::post('/{name}/{code}', 'TestController@sender')-&g ...

Tips for removing borders from the AJAX Loading icon in jQuery Mobile

Is there a way to remove the borders from the default jQuery Mobile loading icon? I am attempting to call the ajax-loading.gif icon before any page creation. $(document).on('pagebeforecreate', '[data-role="page"]', function ( ...

Error Encountered: Django - AJAX Request Not Found

I'm currently working on building an ajax function that, when triggered, should display information in a newly created modal. I seem to be encountering an error that says "not found" every time I attempt to access the specified URL. Below is a screens ...

ASP.Net Core Razor: Troubleshooting Null JSON Value in Ajax Requests

I am currently using .Net Core Razor for my application and encountering an issue with passing values to the controller when calling the action from Ajax. Below is the script I am using: var table = $('#dataTable').DataTable(); var data = table. ...

Struggling with JavaScript's getElementById function

If anyone has any suggestions or alternative methods, please kindly assist me. I currently have: 1 Textbox 1 Label 1 LinkButton Upon clicking the lnk_NameEdit button, the txtUserName textbox should become visible while the lblusername label should becom ...

Analyzing a JSON Structure Containing Numerous Sub-Objects

<script type="text/javascript" src="http://static.stackmob.com/js/2.5.3-crypto-sha1-hmac.js"></script> <script type="text/javascript"> $j.ajax({ dataType : 'json', url : "/api/core/v2/groups/", //or any other res ...

Determining the height of the first element in jQuery

I am dealing with multiple elements that share the same class but have different heights. The class 'xyz' is only for styling borders, as shown below: <div class='xyz'></div> //1st element height=10px <div class='xy ...

Using multi-dimensional JSON arrays for posting requests through Ajax

Is it possible to serialize HTML fields in a multi-dimensional array format for AJAX post transmission? I tried using serializeArray but it only formats the first level of the array. The data I need to serialize includes name/value pairs like: name="cus ...

Bootstrap tab toggle feature

I'm currently facing an issue with Bootstrap's tab component. I need help figuring out how to hide a lorem ipsum section and show a hidden div when a specific tab is clicked, and then revert the changes when a different tab is selected. $(func ...

Is there a way to enable data-add-back-btn using jQuery script?

Currently, I am utilizing jQuery 1.9.1 and jQuery Mobile 1.3.1 to define multiple pages within my project setup: <div id="q1" data-role="page" data-add-back-btn="false" data-back-btn-text="Home"> <div data-role="header" data-position="fixed" ...

What is the best approach to storing and retrieving special characters ('+<>$") from a textfield into a database using PHP?

I have a form where users can enter a personal message with a subject. The data entered in the textarea is passed to a Javascript/jQuery function, which then sends it to a PHP file for storage in a database. However, I am encountering issues when special c ...

Learn the process of transferring information from a dynamically generated table to a database using PHP

After creating a table using PHP dynamically, I am facing an issue with updating some cell values based on user input. I have provided my code below. I tried using [] in the names attribute to make names an array as suggested on Stack Overflow, but it didn ...

Is it possible to deselect a checkbox if a radio button is selected, and vice versa?

I'm presenting these two options: <div class="pricing-details pricing-details-downloads"> <h4>Single purchase (60 lessons)</h4> <h4>Bulk Purchase: Lesson</h4> <div class="pricing-details-separator">&l ...

Generating a JQuery mobile page on the fly using JSON data

Currently, I am working on developing a Jquery mobile app within cordova. My goal is to dynamically construct the entire page based on Remote Restful JSON results. Since the content of this page is not fixed and will change when the JSON data on the server ...

Unable to communicate with MediaWiki API using jQuery

My attempt to retrieve content from Wikipedia in JSON format has been unsuccessful: $.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles="+title+"&format=json", function(data) { doSomethingWith ...

The AngularJS framework is not effectively generating the desired table structure

EDIT 1 - Here is a Plnkr example that demonstrates the issue - http://plnkr.co/edit/qQn2K3JtSNoPXs9vI7CK?p=preview ---------------- ORIGINAL CODE AND PROBLEM ---------------- I've been using the angular datatable library (angular-datatables) to g ...