简体   繁体   English

数据数组中的高图类别

[英]Highcharts categories from data array

I'm trying to pull data from a mysql table and set it as the x-axis categories but I don't quite understand how. 我正在尝试从mysql表中提取数据并将其设置为x轴类别,但我不太了解该怎么做。

Basically, I have Job numbers in a table, and I want to pull those and make them the categories on the x-axis, eventually when a column is clicked for a specific job, it'll drill down for more columns (which add up to the total percentage of the job number). 基本上,我在表格中有作业编号,我想将其编号并使其成为x轴上的类别,最终当单击某项特定作业的列时,它将向下钻取更多列(这些总和占职位总数的百分比)。

Here's an example of what I'm trying to do, it works with the column data when using an array there but not with the categories so it wouldn't work this way. 这是我要执行的操作的一个示例,当在其中使用数组时,它适用于列数据,但不适用于类别,因此无法以这种方式工作。

http://codepen.io/anon/pen/BoJqA http://codepen.io/anon/pen/BoJqA

How would I go about doing this? 我将如何去做呢?

I'm not sure if the question is "how can i do this with php/mysql" or "how can i define column names in highcharts". 我不确定问题是“如何使用php / mysql执行此操作”还是“如何在highcharts中定义列名称”。 i'll try to answer the latter. 我会尽力回答后者。 try this code to show custom column names instead of 1,2,3,...: 尝试使用以下代码显示自定义列名称,而不是1,2,3,...:

$(function () { 
    var data = {"name":"Percentage","data":[50,28,150,125,34,75]};
    var categories = [5600, 5700,5800,5900,6000,6100];
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column'
        },
        title: {
            text: 'Job Budget Report'
        },
        xAxis: {
            categories: categories
        },
        yAxis: {
            title: {
                text: '% Budget hrs'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
             }]
        },
        series: [data]
    });
});

I've never used Highcharts but if it does not have an interface to directly access a database I think all you would need to do is pull the data from the mySQL query and put it in an array that would be used by Highcharts. 我从未使用过Highcharts,但是如果它没有直接访问数据库的接口,我想您所要做的就是从mySQL查询中提取数据并将其放入Highcharts将使用的数组中。

Maybe something like: 也许像:

$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}

$countsPerJob = array();
$res = $mysqli->query( "SELECT jobNumber FROM JobTable" );
while( ($row = $res->fetch_assoc()) ) {
    if( isset( $countsPerJob[$row['jobNumber'] ) ) {
        ++$countsPerJob[$row['jobNumber']];
    } else {
        $countsPerJob[$row['jobNumber']] = 1;
    }
}

Now $countsPerJob has all the information from the database table. 现在, $countsPerJob具有数据库表中的所有信息。 You can then use the keys from $countsPerJob for the categories and the value for each entry in the array for the column height. 然后,您可以将$countsPerJob的键用于类别,并将数组中每个条目的值用于列高。

It looks like you have more data than categories ? 看起来您拥有的数据多于类别? (You have 6 data points and 3 categories). (您有6个数据点和3个类别)。

However, I think you need to supply an array to the categories, but you are supplying an object. 但是,我认为您需要为类别提供一个数组,但是您正在提供一个对象。 You can change your code to: 您可以将代码更改为:

$(function () { 
    var data = {"name":"Percentage","data":[50,28,150,125,34,75]};
    var categories = [5600, 5700,5800];
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column'
        },
        title: {
            text: 'Job Budget Report'
        },
        xAxis: {
            categories: categories
        },
        yAxis: {
            title: {
                text: '% Budget hrs'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
             }]
        },
        series: [data]
    });
});

'categories' is just a simple array. “类别”只是一个简单的数组。 You would specify it as 您可以将其指定为

var categories = [5600, 5700,5800];

And then in the chart: 然后在图表中:

xAxis: {
  categories: categories
}

(or, of course, you could just populate them directly in the chart) (或者,当然,您可以直接在图表中填充它们)

However, as noted elsewhere, you have 3 categories and 6 data points, so your last 3 data points will have categories of '3', '4', and '5' as is. 但是,如其他地方所述,您具有3个类别和6个数据点,因此,您的最后3个数据点将按原样具有类别“ 3”,“ 4”和“ 5”。

If the chart is supposed to be a grouped chart, you'll need to set your data up differently. 如果该图表应为分组图表,则需要以其他方式设置数据。 If not, you'll need to supply more category titles. 如果不是,则需要提供更多类别标题。

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

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