ShanWave007
ShanWave007

Reputation: 361

How to feed dynamic data to static jquery function?

My source code is here below. The chart it creates made by using chartist.js .

<html>
<head>

    <link rel="stylesheet" href="chartist.min.css">

</head>
<body>
<h3>Chartist Test</h3>
<br>

<div id="ct-chart5" class="ct-perfect-fourth"></div>

<script src="chartist.min.js"></script>
<script src="jquery-2.1.4.min.js"></script>
<script>
    $(document).ready(function(){

        var data = {
            series: [5, 3, 4]
        };

        var sum = function(a, b) { return a + b };

        new Chartist.Pie('#ct-chart5', data, {
            labelInterpolationFnc: function(value) {
                return Math.round(value / data.series.reduce(sum) * 100) + '%';
            }
        });
    });
</script>

</body>
</html>

There is a var called data inside scripts tags below. But the thing is it has predefined values and chat is creating by those static values. How i can post some dynamic data into that variable? lets say we have some data retrieving from mysql db. i want to know how to feed those retrieving data into the predefined variable dynamically.

Upvotes: 0

Views: 617

Answers (1)

Siddharth
Siddharth

Reputation: 869

I think you are talking about ajax calls, well you declare that variable globally make and ajax call, get dynamic data and update the variable.

var data;
//some code

$.ajax({
  url: some_URL,
  data: some_data,
  dataType: some_datatype
  //other options,
  success:function(jqXHR){
    data=jqXHR;//assuming jqXHR is the dynamic data 
  }

})

Upvotes: 2

Related Questions