简体   繁体   English

将PHP变量传递到flot条形图中

[英]Passing a PHP variable into flot Bar chart

I am trying to implode my php variable which is an array as one variable into my flot chart. 我正在尝试将我的php变量内插到我的浮动图表中,该数组是一个数组。 I implode my PHP variable with JS chart and it worked for me as you can see in the image : I am trying to get Flot bar data same output with JS Bar chart. 我将我的PHP变量内含JS图表,并且对我有用,如您在图像中所看到的:我正在尝试使JS柱形图的Flot柱形数据输出相同。 Any idea please ? 有什么想法吗?

Thank you 谢谢

在此处输入图片说明

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

That's what I am getting after I used your code. 这就是我使用您的代码后得到的。 在此处输入图片说明

If $studentages is an array of integers, this means that 如果$studentages是整数数组,则意味着

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

This is not the correct format for flot data which expects an array of arrays. 这不是需要数组数组的float数据的正确格式

So, try: 因此,请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM