简体   繁体   English

浮动条形图可点击的条显示错误信息

[英]flot bar graph clickable bars displaying incorrect information

I'm trying to make it so that when a bar on my graph is clicked, the x-axis label of that specific bar is printed out/displayed in a span. 我试图做到这一点,以便在单击图形上的条形图时,该特定条形图的x轴标签被打印/显示为跨度。 My ultimate goal is to make it so that when a bar is clicked, another bar graph is created underneath that displays more detailed information about that specific bar, think broad information on the first graph and when a bar is clicked, more detailed information is displayed in another bar graph underneath. 我的最终目标是做到这一点,以便在单击条形图时在其下创建另一个条形图,该条形图显示有关该特定条形的更详细的信息,在第一个图形上考虑广泛的信息,并在单击条形图时显示更详细的信息。在下面的另一个条形图中。

Test table in my db: 在我的数据库中测试表:

x ------------- y x ------------- y
0 ----------- 1000 0 ----------- 1000
1 ----------- 2000 1 ----------- 2000
2 ----------- 3000 2 ----------- 3000
3 ----------- 4000 3 ----------- 4000
4 ----------- 5000 4 ----------- 5000
5 ----------- 6000 5 ----------- 6000
6 ----------- 7000 6 ----------- 7000
7 ----------- 8000 7 ----------- 8000
8 ----------- 9000 8 ----------- 9000

Query: 查询:

   $sql = "SELECT * FROM flotTest";
   $result = mysqli_query($dbc3, $sql);

    while($row = mysqli_fetch_assoc($result))
    {
        $dataset1[] = array($row['x'],$row['y']);
    }

flot js: 浮点js:

     // BAR CHART
        var percentagehrs = [];
        for (var i = 0; i <= 8; i += 1){
        percentagehrs.push([i, parseInt(Math.random() * 200)]);
}
        var dataset = [{
            label: "Percentage Hours",
            data: percentagehrs,
            color: "#5482FF" }];

        //x-axis label and index pulled from database (testing purposes)
        //I'm thinking this might be where my issue is

        var ticks =  <?php echo json_encode($dataset1); ?>;

        $(document).ready(function () {
            $.plot($("#group-flot-placeholder"), dataset, {
            series: {
                bars: {
                    show: true,
                    clickable: true
                }
            },
            bars: {
                align: "center",
                barWidth: 0.8
            },
            xaxis: {
                axisLabel: "Job Number",
                axisLabelUseCanvas: true,
                axisLabelFontSizePixels: 12,
                axisLabelFontFamily: 'Verdana, Arial',
                axisLabelPadding: 10,
                ticks: ticks
            },
            yaxis: {
                axisLabel: "Percent",
                axisLabelUseCanvas: true,
                axisLabelFontSizePixels: 12,
                axisLabelFontFamily: 'Verdana, Arial',
                axisLabelPadding: 3,
                tickFormatter: function (v, axis) {
                    return v + "%";
                }
            },
            grid: {
                clickable: true,
                hoverable: true,
                borderWidth: 2,
                backgroundColor: { colors: ["#ffffff", "#EDF5FF"] },
                markings: [ { xaxis: { from: -1, to: 12 }, yaxis: { from: 100, to: 300 }, color: "#FFA5A5" }]
            }
        });
            $("#group-flot-placeholder").UseTooltip();

            $("#group-flot-placeholder").bind("plotclick", function (event, pos, item) {
            if (item) {
                $("#clickdata").text(" - click point " + item.dataIndex + " in " + item.series.label);
                plot.highlight(item.series, item.datapoint); //Output here is: - click point 0(0 being the position of the first bar) in Percentage Hours
            }
        });

        });

What am I doing wrong and how can I make it so that when a specific bar is clicked, I can pull data from my database to another bar graph underneath for that specific bar? 我在做什么错,我该怎么做才能在单击特定的条形时将数据从数据库中拉到该特定条形下的另一个条形图中?

You don't say what <?php echo json_encode($dataset1); ?> 您没有说什么<?php echo json_encode($dataset1); ?> <?php echo json_encode($dataset1); ?> resolves to, but it should be in the form of: <?php echo json_encode($dataset1); ?>解析为,但应采用以下形式:

var ticks = [[0,"A"],[1,"B"],[2,"C"],[3,"D"],[4,"E"],[5,"F"],[6,"G"],[7,"H"],[8,"I"]];

So, in your click callback: 因此,在您的click回调中:

$("#group-flot-placeholder").bind("plotclick", function (event, pos, item) {
    if (item) {
        var tickClicked = item.series.xaxis.ticks[item.dataIndex].label;         
        $("#clickdata").text(tickClicked);
    }
});

Here's a fiddle demonstration . 这是一个小提琴表演

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

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