0

I have working with amcharts (line charts). Set data dynamically with ajax and get the data from php code in JSON format.

Problem comes string is not changed to array dynamically.

if i have add this string static in jquery code

code:

[{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}];

Thats working fine.

if i have set this code dynamically. line charts shows undefined.

i have use this function

ajax return code

[{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}]

i have just pass this on this function. like

var ajaxString = [{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}];

and call this funciton

lineChart(ajaxString);

function lineChart(data) { var chart;

    var chartData=data;
    // SERIAL CHART
    chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "country";
    chart.startDuration = 1;
    // AXES
    // category
    var categoryAxis = chart.categoryAxis;
    categoryAxis.labelRotation = 45; // this line makes category values to be rotated
    categoryAxis.gridPosition = "start";
    // value
    var valueAxis = new AmCharts.ValueAxis();
    valueAxis.dashLength = 5;
    //                                    valueAxis.title = "Visitors from country";
    valueAxis.axisAlpha = 0;
    chart.addValueAxis(valueAxis);
    // GRAPH
    var graph = new AmCharts.AmGraph();
    graph.valueField = "visits";
    graph.colorField = "color";
    graph.balloonText = "<b>[[category]]: [[value]] Users</b>";
    graph.type = "column";
    graph.lineAlpha = 0.2;
    graph.fillAlphas = 0.9;
    chart.addGraph(graph);
    // CURSOR
    var chartCursor = new AmCharts.ChartCursor();
    chartCursor.cursorAlpha = 0;
    chartCursor.zoomable = false;
    chartCursor.categoryBalloonEnabled = false;
    chart.addChartCursor(chartCursor);
    chart.creditsPosition = "top-right";
    // WRITE
    chart.write("chartdiv");
}[![undefined message][1]][1]

But thats not working. Thank You in advance

7
  • how are you dynamically setting the string? Commented Feb 18, 2016 at 4:20
  • will get the data from ajax in time of load. Thats i got from ajax return from php code Commented Feb 18, 2016 at 4:20
  • 1
    can you show us the code where you set the string statically? Commented Feb 18, 2016 at 4:21
  • Static String var chartData =[{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}]; i am using this string dynamically. Thats string gots from ajax [{"country":"17 Feb","visits":"4","color":"#EA5759"},{"country":"16 Feb","visits":"1","color":"#EA5759"},{"country":"15 Feb","visits":"3","color":"#EA5759"}] var chartData=dataelement; Commented Feb 18, 2016 at 4:23
  • 1
    you should update this in the question, not in the comments section Commented Feb 18, 2016 at 4:25

3 Answers 3

2

The ajaxString that you are passing to your function is simply an string and not a json object , so what your function lineChart might be expecting is json object so try to convert your ajaxString into valid json object before sending it to your function using

JSON.parse(ajaxString);
Sign up to request clarification or add additional context in comments.

Comments

1

This looks like you are attempting to set a JSON string to chartdata instead of a javascript object array.

In this case, you should try to parse your string gotten through the ajax call like so : JSON.parse(dataelement) before setting it to chartdata.

Comments

-1

You can create the code dynamically in PHP and covert the array in json via json_encode. encoded json can be used later in the jquery.

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.