HighCharts上的X轴标签

时间:2017-01-17 21:57:23

标签: php highcharts

此代码集...生成一个图表,该图表应计算每周每天喝的啤酒数量。我的问题是在X轴上显示星期标签。在图片中,您可以看到阵列的两个列(Beers,Day)出现,但我无法得到几天出现。我不想将一周中的几天加载到一个单独的数组中,因为我有其他图表,其中X轴会有所不同。

enter image description here

有关如何获取xAxis:{categores}或系列:[{data:}]的任何建议,只显示图表中数组的一列?

<!DOCTYPE HTML>
<?php

include "config_mysqli.php";
$con = mysqli_connect($host, $user, $password, $database);
if (mysqli_connect_errno())
die('Could not connect: ' . mysqli_connect_error());

if (!$con) {  die('Could not connect: ' . mysql_error());}

$sql = "SELECT COUNT(*) AS 'Beers', DAYNAME(date) as 'Day'
FROM beer
WHERE date IS NOT NULL
GROUP BY DAYNAME(date) , DAYOFWEEK(date)
ORDER BY DAYOFWEEK(date);";

$result = $con->query($sql);

$rows = array();
while($r = mysqli_fetch_array($result)) {
$row[0] = $r['Day'];
$row[1] = $r['Beers'];
array_push($rows,$row);}

$data=json_encode($rows, JSON_NUMERIC_CHECK);

$sql2 = " SELECT DAYNAME(date) as 'TopDay'
FROM beer
WHERE date IS NOT NULL
GROUP BY DAYNAME(date)
ORDER BY COUNT(*) DESC
LIMIT 1;";

$result2 = $con->query($sql2);

$rows2 = array();
while($r2 = mysqli_fetch_array($result2)) {
$row2[0] = $r2['TopDay'];
array_push($rows2,$row2);}

$theTitle =  implode($rows2[0]);

?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Beer Intelligence Project</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
$(function () {
var chart,
mydata = <?php echo $data ?>;
$(document).ready(function() {
chart = new Highcharts.Chart({
    chart: {
        renderTo: 'bar',
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    },
    xAxis: {
    categories: mydata
        },
    yAxis: {
        title: {
            text: 'Beers' 
       }
        },
    title: {
        text: 'Beers by day of the week'
    },
subtitle: {
        text: 'The most beers are drank on a  <?php echo $theTitle?> '
    },
    tooltip: {pointFormat: '<tr><td style="color:{series.color};padding:0">Number of beers: </td>' + '<td style="padding:0"><b>{point.y:,.0f}</b></td></tr>'
    },
series: [{
        type: 'column',
        name: 'Beers',
        data: mydata,
    color: 'black'
            }] 
    });
});
});
</script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
    <div id="bar"></div>
</body>
</html>

谢谢, 添

1 个答案:

答案 0 :(得分:0)

您使用的是二维数组mydata作为xAxis.categories属性。你只想用这一天:

xAxis: {
  categories: mydata.map(function(data){ return data[0] });
},

这假设mydata看起来像这样:

[["Wednesday",62],["Thursday", 47]]

如果你不喝那么多话,可能会更容易编码;)