将PHP变量传递到flot条形图

时间:2014-11-18 14:33:50

标签: javascript php jquery charts flot

我试图将我的php变量内爆,这是一个数组作为我的flot图表中的一个变量。 我用JS图表破坏了我的PHP变量,它对我有用,你可以在图像中看到: 我试图用JS条形图获得Flot bar数据相同的输出。好吗?

谢谢

enter image description here

var data = [ 0, <?php echo '['.implode(", ", $studentages).']'?>];
var dataset = [
   { label: "Exams By Student Age", data: data, color: "#5482FF"  }
        ];
  var ticks = [ [0, "0-2"]
  ];

 var options = {
   series: {
    bars: {
        show: true
    }
  },
 bars: {
    align: "center",
    barWidth: 0.6 ,
    vertical: true ,
    show:true
 },
  xaxis: {
    axisLabel: "Exams By Student Ages",
    axisLabelUseCanvas: true,
    axisLabelFontSizePixels: 12,
    axisLabelFontFamily: 'Verdana, Arial',
    axisLabelPadding: 10,
    tickLength:0,

    ticks: ticks

},
yaxis: {
    axisLabel: "Number of Exams",
    axisLabelUseCanvas: true,
    axisLabelFontSizePixels: 12,
    axisLabelFontFamily: 'Verdana, Arial',
    axisLabelPadding: 3,

    max:20, tickSize:1,
    tickFormatter: function (v, axis) {
        return v;
    }
},
legend: {
    noColumns: 0,
    labelBoxBorderColor: "#000000",
    position: "nw"
},
grid: {
    clickable: true,
    borderWidth: 1,      

    backgroundColor: { colors: ["#ffffff", "#EDF5FF"] }
}
};

$(document).ready(function () {
$.plot($("#flot-placeholder"), dataset, options);    
$("#flot-placeholder").UseTooltip();
 });
function gd(year, month, day) {
return new Date(year, month, day).getTime();
}
var previousPoint = null, previousLabel = null;
$.fn.UseTooltip = function () {
$(this).bind("plotclick", function (event, pos, item) {
var links = [ '../../Chart/StudentTests/result.php']; 
    if (item) 
      {
           //alert("clicked");
         //  window.location = (links[item.dataIndex]); 
             window.open(links[item.dataIndex], '_blank');
             console.log(item);              
        }
     else {
        $("#tooltip").remove();
        previousPoint = null;
    }
    });
 };

   function showTooltip(x, y, color, contents) {
   $('<div id="tooltip">' + contents + '</div>').css({
    position: 'absolute',
    display: 'none',
    top: y - 40,
    left: x - 120,
    border: '2px solid ' + color,
    padding: '3px',
    'font-size': '9px',
    'border-radius': '5px',
    'background-color': '#fff',
    'font-family': 'Verdana, Arial, Helvetica, Tahoma, sans-serif',
    opacity: 10
    }).appendTo("body").fadeIn(200);
   }

这是我使用你的代码后得到的。 enter image description here

1 个答案:

答案 0 :(得分:1)

如果$studentages是整数数组,则表示

 var data = [ 0, [1, 2, 3, 4, 5]];

这不是需要数组数组的flot数据的correct format

所以,试试:

var data = $.map(<?php echo '['.implode(", ", $studentages).']'?>, function(val, idx){
    return [[idx, val]];
});
var dataset = [
    { label: "Exams By Student Age", data: data, color: "#5482FF"  }
];
var ticks = [ [0, "0-2"] ]; // expand this for each idx in data