Question

I can add guides (vertical lines) to my amchart this way:

var guide1 = new AmCharts.Guide();

    guide1.date = new Date(2014, 5, 27);
    guide1.lineColor = "#CC0000";
    guide1.lineAlpha = 1;
    guide1.dashLength = 2;
    guide1.inside = true;
    guide1.labelRotation = 90;
    guide1.label = "Guide label";

stockPanel.categoryAxis.addGuide(guide1);

How could I add a number of these guides dynamically from a list of dates in a database? Would I be able to generate each one with PHP and include them in my script?

Sample PHP (echoGuide.php):

<?php
$js = <<<JS
var guide1 = new AmCharts.Guide();
    guide1.date = new Date(2014, 5, 27);
    guide1.lineColor = "#CC0000";
    guide1.lineAlpha = 1;
    guide1.dashLength = 2;
    guide1.inside = true;
    guide1.labelRotation = 90;
    guide1.label = "Guide label";
JS;

header("Content-type: text/javascript");
echo $js;
exit();
?>

Sample JS in HTML file:

var guide = AmCharts.loadJSON('echoGuide.php');
stockPanel.categoryAxis.addGuide(guide);

AmCharts.loadJSON:

AmCharts.loadJSON = function(url) {
  // create the request
  if (window.XMLHttpRequest) {
    // IE7+, Firefox, Chrome, Opera, Safari
    var request = new XMLHttpRequest();
  } else {
    // code for IE6, IE5
    var request = new ActiveXObject('Microsoft.XMLHTTP');
  }

  // load it
  // the last "false" parameter ensures that our code will wait before the
  // data is loaded
  request.open('GET', url, false);
  request.send();

  // parse and return the output
  return eval(request.responseText);
};
Was it helpful?

Solution

Found a solution:

    gDates = AmCharts.loadJSON('db/fetchgDates.php');
    for (var key_2 in gDates) {
            var obj_2 = gDates[key_2];
            var date_2_temp = new Date(obj_2['date']);
                var guide = new AmCharts.Guide();
                guide.date = date_2_temp;
                guide.lineColor = "#CC0000";
                guide.lineAlpha = 1;
                guide.dashLength = 2;
                guide.inside = true;
                guide.labelRotation = 90;
                guide.label = "test";

                stockPanel.categoryAxis.addGuide(guide);
                }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top