0

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.

2
  • There are many ways to do this - you could: 1. Use an AJAX query and populate the data via the returned data. 2. Have the data present inside an element on the page and retrieve the data from that element attribute. (IE: data-chart-data="5,3,4" etc.) That would be my inital approach. I have used chart.js fairly extensively in my case I query a local web service to retrieve the chart information. Commented Jan 6, 2016 at 6:07
  • Then how the source code should be modify? Commented Jan 6, 2016 at 6:10

1 Answer 1

2

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 
  }

})
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.