douyouming9180 2013-12-05 19:40
浏览 60
已采纳

flot不画条形图

I tried with my other two scripts(line chart and pie chart) but the flot doesn't draw the bars chart... can you help me with this...I think the error is in the javascript..

The library:

<script src="js/jquery.flot.min.js"></script>
<script src="js/jquery.flot.pie.min.js"></script>
<script src="js/jquery.flot.stack.js"></script>
<script src="js/jquery.flot.resize.min.js"></script>

Here is the printdata db call:

[["junio",390],["julio",125],["agosto",50]]

Here is the script in graficayear.php:

<?php
include 'includes/configs.php';
$sql = $conn->prepare("SELECT DATE_FORMAT(start, '%M') AS mdate, SUM(honorario) AS total_mes 
FROM CITAS WHERE YEAR(current_date) GROUP BY mdate DESC");
    $sql->execute();
    while($row = $sql->fetch(PDO::FETCH_ASSOC)) {

    $datayear[] = array($row['mdate'],(int) $row['total_mes']);
}  
?>

Here is the code in chartyear.php:

<?php include 'graficayear.php'; ?>
<script type='text/javascript' charset='utf-8'> 
$(function () {
$.plot(
    $("#baryear"),
    [{
        data : <?php echo json_encode($datayear);?>,
        color: '#012D4C',
        bars: { show: true, fillColor: '#4682b4', barWidth: (15*24*60*60*1000), align: 'center' }
    }],
    {
        grid: { color: '#012D4C' },
        xaxis: {
            mode: 'time',
            tickDecimals: 0,
            tickSize: [1,'month'],
            autoscaleMargin: 0.001
        }
    }
);
});
</script>

And the DIV with the ID:

<?php include 'chartyear.php'; ?>
<div id="baryear" style="width: 320px; height: 300px;"></div>

here is how my chart look like until now:

enter image description here

And this is the data I need to show inside of the bars chart:

enter image description here

  • 写回答

1条回答 默认 最新

  • douwo3665 2013-12-05 20:32
    关注

    You need to read the documentation on the expected data formats more carefully. Here you've specified an xAxis of type time but then have given it categories. You have to pick one way or the other.

    So, given the format of you json data, here the shortest path to do what you want:

    // given your data
    var datayear = [["junio",390],["julio",125],["agosto",50]];
    
    // split it into a data array and a ticks array
    var data = [], ticks = [];  
    for (var i = 0; i < datayear.length; i++) {
        data.push([i,datayear[i][1]]); // note that the x value is numeric
        ticks.push([i,datayear[i][0]]); // and that the x value is matched to a "category"
    }
    
    $.plot(
        $("#baryear"),
            [{
               data : data,
               color: '#012D4C',
               bars: { show: true, fillColor: '#4682b4', align: 'center' }
            }],
         {
             grid: { color: '#012D4C' },
             xaxis: {
                 ticks: ticks
             }
         });
    

    Fiddle here.

    Produces:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流