1

I'm trying to build c3.js line chart from data grabbed using php from mysql database.

 var chart = c3.generate({
  bindto: '#linear-chart',
  data: {
    json:lineChartData,
    keys:{
      value: ['date', 'temp'],
    }
  },
  zoom: {
    enabled: true
  }
});

lineChartData - json data from mysql and looks like:

[{date: Wed Mar 13 2019, temp: 200},{.......}]

How should be ready json data sent to c3.js?

1 Answer 1

1

You can easily echo your json data into C3.js by doing:

var dataset = <?php echo $lineChartData; ?>; 
data: { 
    json: dataset
    }

I am using the same pipeline you are using for my web project (mySQL-PHP-C3.js). It is important that your json data is exactly in the format that C3 needs for imput. As you can read in the C3.js documentation (https://c3js.org/samples/timeseries.html), data format for time series should be as follows:

data: {
       x: 'x',
       //  xFormat: '%Y%m%d', // 'xFormat' can be used as custom format of 'x'
       columns: [
           ['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
       //  ['x', '20130101', '20130102', '20130103', '20130104', '20130105', '20130106'],
           ['data1', 30, 200, 100, 400, 150, 250],
           ['data2', 130, 340, 200, 500, 250, 350]
         ]
      },
axis: {
       x: {
           type: 'timeseries',
           tick: {
             format: '%Y-%m-%d'
           }
         }
       }

For my data (not time series data), json object looks as follows:

{"data1": [0.23, 0.29, 0.19, 0.14, 0.7], "data2": [0, 0.25, 0.61, 0.4, 0.52]}

If you follow this structure and add your time data (date) as third dimension to it, you should be able to achive what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.