繁体   English   中英

HighCharts上的X轴标签

[英]X axis labels on HighCharts

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

在此处输入图片说明

关于如何使xAxis:{categores}或series:[{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>

谢谢,蒂姆

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

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

假设mydata看起来像这样:

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

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

暂无
暂无

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

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