【问题标题】:Highcharts bar chart with negative and positive data带有负数和正数数据的 Highcharts 条形图
【发布时间】:2015-09-04 20:22:03
【问题描述】:

我正在编写一个显示条形图的脚本。我有这个工作到一定程度。

我要做的是在图表中显示 MySQL 查询的结果,左侧为负值,右侧为正值。表中的结果值为“1”或“2”。

目前的代码是:

$(function () {

var data =[<?php


mysql_select_db($database_test, $con);
$query_result = sprintf("SELECT COUNT(Condition), ConditionValue AS RC1 FROM FeedBack WHERE ConditionValue = 1 AND FeedBackDate BETWEEN '" . date("Y-m-d", strtotime($_POST['FromDate'])) . "' AND '". date("Y-m-d", strtotime($_POST['ToDate'])) . "'");
$result = mysql_query($query_result, $con) or die(mysql_error());
$totalRows_result_rc = mysql_num_rows($result);



while ($row_result = mysql_fetch_assoc($result)){
?>


[<?php echo $row_result['RC1'];?>]

<?php
}
?>
]
$('#container1').highcharts({
    chart: {
    type: 'bar'
    },
title: {
    text: 'Condition'
},
subtitle: {
    text: ''
},

legend: {
    layout: 'vertical',
    align: 'right',
    verticalAlign: 'top',
    x: -20,
    y: 34,
    floating: false,
    borderWidth: 1,
    backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
    shadow: true
},

plotOptions: {
    series: {
        shadow:false,
        borderWidth:0,
        dataLabels:{
            enabled:true,
            formatter: function() {
                return this.y +'%';
            }
        }
    }
},

xAxis:{
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickLength:3,
    title:{
        text:'<?php print $totalRows_result_rc;?> records'
    },
},

yAxis:{
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickWidth:1,
tickLength:3,
gridLineColor:'#ddd',
    title:{
        text:'Between <?php print $_POST['FromDate'];?> and <?php print $_POST['ToDate'];?>',
        rotation:0,
        margin:50,
 },

labels: {
    formatter: function() {
        return (this.isLast ? this.value + '%' : this.value);
    }
 }
},
series: [{

color: '#CC0000',
name: 'Conditione',
data: data,
maxPointWidth: 10,
index:0,
legendIndex:1,
exporting: {
    filename: 'Ccondition'
}

}]
});
});

我已经用多种不同的方式编写了这个,但无法获得所需的结果。

谁能指出我哪里出错了。非常感谢您花时间提供帮助。

【问题讨论】:

  • 那么,什么不起作用?默认情况下,对于条形图,左侧为负值,右侧为正值。有什么更具体的,或者与您想要的默认行为不同的东西吗?
  • 另外,你说的值要么是1,要么是2。你的意思是它们也可以是-1和-2?

标签: highcharts


【解决方案1】:

这可以通过使用两个系列来完成:一个包含负值,一个包含正值。它们应该(但根据您的需要不是必需的)共享相同的 xAxis 值。看看 highcharts 网站上的这个example。注意使用两个系列:

    series: [{
        name: 'Male',
        data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2,
            -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4,
            -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0]
    }, {
        name: 'Female',
        data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9,
            3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9,
            1.8, 1.2, 0.6, 0.1, 0.0]
    }]

并且链接到相同的类别:

var categories = ['0-4', '5-9', '10-14', '15-19',
        '20-24', '25-29', '30-34', '35-39', '40-44',
        '45-49', '50-54', '55-59', '60-64', '65-69',
        '70-74', '75-79', '80-84', '85-89', '90-94',
        '95-99', '100 + '];

他们在这里进行一些格式化,以便将负值标记为正值,而其他一些 yAxis 标签发生变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2021-06-27
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多