amCharts的动态指南

时间:2014-08-01 10:22:55

标签: javascript php charts amcharts

我可以通过这种方式向我的amchart添加指南(垂直线):

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);

如何从数据库中的日期列表中动态添加许多这些指南? 我能用PHP生成每一个并将它们包含在我的脚本中吗?

示例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();
?>

HTML文件中的示例JS:

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

1 个答案:

答案 0 :(得分:0)

找到解决方案:

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