c3.js Error: The value of b is not defined

Struggling to create a graph using a JSON object generated in PHP. Despite following the documentation example, I keep encountering a TypeError: b.value is undefined in the JS log. The data seems to be structured correctly.

for($i = 0; $i < 10; $i++){
  $da=date("Y-m-d", strtotime('-'. $i .' days'));
  $a=mt_rand(150,200);
  $b=mt_rand(100,150);
  $ar["date"][]=$da;
  $ar["Score"][]=$a;
  $ar["ScoreB"][]=$b;

}  
 $all=json_encode($ar);      

 <script>
    var arr=<?php echo $all; ?>;
    var chart = c3.generate({
         bindto: '#scoring',
        data: {
            json: arr,
            type: 'spline',
            keys:{
                x:'date'
            }
        },
        color: {
            pattern: ['red', 'orange']
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    format: '%Y-%m-%d'
                }
            }
        }
    });

The Object:

{"date":"2016-05-09","2016-05-08","2016-05-07","2016-05-06","2016-05-05","2016-05-04","2016-05-03","2016-05-02","2016-05-01","2016-04-30"],"Score":[182,163,196,153,180,154,170,177,191,173],"ScoreB":[121,149,138,113,104,107,111,109,119,132]} 

Tried reformatting the object as follows:

[{"date":"2016-05-09","Score":191,"ScoreB":119},{"date":"2016-05-08","Score":166,"ScoreB":140},{"date":"2016-05-07","Score":172,"ScoreB":103},{"date":"2016-05-06","Score":187,"ScoreB":139},{"date":"2016-05-05","Score":162,"ScoreB":100},{"date":"2016-05-04","Score":197,"ScoreB":121},{"date":"2016-05-03","Score":167,"ScoreB":145},{"date":"2016-05-02","Score":160,"ScoreB":137},{"date":"2016-05-01","Score":175,"ScoreB":100},{"date":"2016-04-30","Score":156,"ScoreB":127}] 

However, the same error persists.

Spent a whole day trying to solve this seemingly simple issue but no luck so far. When inputting the same data in "columns" format, it works fine. Need to get this JSON structure working for future use.

Answer №1

I have found that this code snippet works perfectly when pasted into the page. You can observe the changes in the keys section by adding the value field -

The only modification I made was adjusting the binding ID to match the c3 examples page, and using the JSON data directly instead of generating it via PHP.

var arr = [{"date":"2016-05-09","Score":191,"ScoreB":119},{"date":"2016-05-08","Score":166,"ScoreB":140},{"date":"2016-05-07","Score":172,"ScoreB":103},{"date":"2016-05-06","Score":187,"ScoreB":139},{"date":"2016-05-05","Score":162,"ScoreB":100},{"date":"2016-05-04","Score":197,"ScoreB":121},{"date":"2016-05-03","Score":167,"ScoreB":145},{"date":"2016-05-02","Score":160,"ScoreB":137},{"date":"2016-05-01","Score":175,"ScoreB":100},{"date":"2016-04-30","Score":156,"ScoreB":127}]; ;

var chart = c3.generate({
        bindto: '#chart',
        data: {
            json: arr,
            type: 'spline',
            keys:{
                x:'date',
                value: ['Score', 'ScoreB'], // IMPORTANT
            }
        },
        color: {
            pattern: ['red', 'orange']
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    format: '%Y-%m-%d'
                }
            }
        }
    });

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

What is the best way to parse this JSON using Jackson?

My JSON data is structured like this: { "summary":{ "somefield1":"somevalue1", "Twilio":{ "field1":"value1", "field2":"value2" }, "Tropo":{ "field1":"value1", "field2":"va ...

Does Vuex dispatch from within a component include a particular type of behavior known as a "promise"?

Currently, I am diving into vuex and facing an issue. During the created() lifecycle hook, my goal is to fetch data from an API. Once this action is complete, I need to call a getter from the component and assign the retrieved cards to the component's ...

Retrieve the information from a website and display it on the current webpage using an ajax request

Is there a way to insert parsed HTML content into my webpage using just a link from another page? I'm attempting to use an AJAX call, but I keep receiving an error. Below is the code I've written, and the browser doesn't seem to be the issue ...

Exploring Python's requests module: encoding JSON data in URLs

I'm currently developing an API wrapper. The specification I am trying to implement includes the following request: curl -H "Content-type:application/json" -X POST -d data='{"name":"Partner13", "email":"<a href="/cdn-cgi/l/email-protection" c ...

Tips for extracting values from an array that was fetched using cURL and stored as JSON with .JQ

{ "accounts": [ { "accountId": "account001", "tokens": [ "tokens1", "tokens2", "tokens3", "tokens4", ] } ] } the ...

Close specific child python processes as needed, triggered by a jQuery event

Having trouble managing child processes independently without affecting the main parent process in a web client using jQuery? Look no further. Here's my scenario: I've got a Flask server hosting a basic webpage with a button. Clicking the button ...

"The Vue component's data is successfully updating within a method using setInterval(), however, the changes are not reflected in the DOM

I have a function in my methods that gets triggered with a @click in the view. The function starts a timer, and although it seems to work fine in the DevTools, the timer only updates in the DevTools when I refresh the page. Moreover, in the view, the time ...

Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this: <input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">, and display the result in another textbox ...

What is the best way to flatten a nested list within a JSON object?

I have extracted sleep data from my Fitbit and obtained a json file containing nested variables + dict. I am looking to convert this json file into a csv file that will showcase all the "regular" variables, such as "dateOfSleep", as well as the nested vari ...

Unable to locate the name 'Cheerio' in the @types/enzyme/index.d.t file

When I try to run my Node application, I encounter the following error: C:/Me/MyApp/node_modules/@types/enzyme/index.d.ts (351,15): Cannot find name 'Cheerio'. I found a suggestion in a forum that recommends using cheerio instead of Cheerio. H ...

The Bootstrap 4 card component is a versatile and stylish

Currently working on a display layout using Bootstrap 4, specifically utilizing cards. The issue I'm facing is that the text exceeds the limit of the card, causing it to overflow. Is there a solution to make the text automatically wrap to the bottom ...

The issue arises in RAD Server Delphi when attempting to use savetostream and loadfromstream due to distorted vowels following Json conversion, causing the functionality

I am attempting to transfer data between a RadServer IIS Package and Delphi Client using EMSEndpoint. The process seems straightforward, but I am encountering difficulties in achieving it. Within the package, there is a TFDConnection that points to an MSS ...

Is the JSON Data Structure provided categorized correctly?

I have successfully converted my PHP result array to JSON. However, I am uncertain if it is in the correct JSON format. Here is the code I used: echo json_encode($row); I need to integrate this with a jQuery autocomplete plugin... {"0":"1","id":"1","1": ...

In search of a simple solution for parsing JSON efficiently

I'm currently working on parsing JSON data using Java language: { "student_id": "123456789", "student_name": "Bart Simpson", "student_absences": 1} Can someone suggest a more efficient method to achieve this? I have attempted the code below but feel ...

Unexpected behavior: getElementById returning URL instead of element

I created a function that accepts a thumbnail path as an argument, waits for the bootstrap modal to open, and then assigns the correct path to the thumbnail href attribute within the modal. However, when I use console.log with the element(el), it displays ...

Changing the website address | Node.js | Express

Is there a way to redirect incoming requests to different endpoints depending on a query parameter in the request? For instance, http://localhost:9000/?category=xyz&param1=...&param2=... The category value can be any of [abc, ijk, pqr, xyz]. Gi ...

How can I display lowercase am/pm instead of uppercase AM/PM with angularjs date filtering?

Hi there, I'm a newcomer to AngularJS and I have a specific requirement. The server is sending me two dates: start_date and end_date. In the scenario where both dates are in 'pm', such as Sun 29 Jan 5.00 pm to Sun 29 Jan 6.00 pm, I would li ...

Triggering an event when the cursor enters a specific div/span/a element and again when the cursor exits the element

Here's the scenario - Imagine a contenteditable element with text inside. I'm working on creating a tagging feature similar to Twitter's mention tagging when someone types '@'. As the user types, a popover appears with suggestion ...

methods for transferring JSON data from JavaScript to PHP

I am trying to figure out how to parse JSON data from JavaScript to PHP. Here is my JavaScript code: var Dataconvert; var asetid = new Array(); $("#simpanmodifikasi").click(function(){ var table = $('#tableasal tbody' ...

Can values from a dgrid store be saved in a variable or displayed in the console?

Currently, I am utilizing the dgrid store to display a grid (dgrid 0.4) on my website. Below is the code snippet that I have implemented: require([ 'dojo/_base/declare', 'dojo/Deferred', 'dstore/RequestMemory', ...