chart js标签和分组标签

时间:2016-10-27 14:21:11

标签: javascript jquery chart.js

请稍微上学。 我有一个工作图表,我需要稍微改变一下。 我的数据有四个字段omlPartgroup,xmonth,xyear和QTY 我希望我的图表显示每个omlpartgroup,然后显示该组中的月份和年份,因此每个partgroup将有12个条形,标签将是具有年和月子组的部分组。

我的代码

window.jQuery(document).ready(function(){
window.jQuery.ajax({
   url:"http://localhost/html5/data.php",
   method:"GET",
   success: function(data){
        console.log(data)
        var omlPartgroupid =[];
        var xyear =[];
        var xmonth =[];
        var QTY=[];

        for(var i in data){
            omlPartgroupid.push("PartGroup"+ data[i].omlPartgroupid);
            xyear.push(data[i].xyear);
            xmonth.push(data[i].xmonth);
            QTY.push(data[i].QTY);
            }
       var chartdata ={
           labels:xmonth,
           datasets :[
            {
           backgroundColor: 'rgba(200,200,200,0.75)',
           borderColor: 'rgba(200,200,200,0.75)',
           hoverBackground: 'rgba(200,200,200,1)',
           hoverBorderColor:  'rgba(200,200,200,1)',
           data:QTY
            }
               ]
       };
       var ctx=$("#mycanvas");
       var barGraph =new Chart(ctx,{
           type: 'bar',
           data: chartdata

       });
   },
   error: function(data){
       console.log(data);
   }

}); });

我的数据

[{"omlPartgroupid":"191  ","xmonth":"10","xyear":"2015","QTY":"52"},{"omlPartgroupid":"191  ","xmonth":"11","xyear":"2015","QTY":"145"},{"omlPartgroupid":"191  ","xmonth":"12","xyear":"2015","QTY":"122"},{"omlPartgroupid":"191  ","xmonth":"1","xyear":"2016","QTY":"348"},{"omlPartgroupid":"191  ","xmonth":"2","xyear":"2016","QTY":"460"},{"omlPartgroupid":"191  ","xmonth":"3","xyear":"2016","QTY":"187"},{"omlPartgroupid":"191  ","xmonth":"4","xyear":"2016","QTY":"109"},{"omlPartgroupid":"191  ","xmonth":"5","xyear":"2016","QTY":"234"},{"omlPartgroupid":"191  ","xmonth":"6","xyear":"2016","QTY":"166"},{"omlPartgroupid":"191  ","xmonth":"7","xyear":"2016","QTY":"186"},{"omlPartgroupid":"191  ","xmonth":"8","xyear":"2016","QTY":"250"},{"omlPartgroupid":"191  ","xmonth":"9","xyear":"2016","QTY":"1077"},{"omlPartgroupid":"191  ","xmonth":"10","xyear":"2016","QTY":"594"},{"omlPartgroupid":"193  ","xmonth":"10","xyear":"2015","QTY":"39"},{"omlPartgroupid":"193  ","xmonth":"11","xyear":"2015","QTY":"183"},{"omlPartgroupid":"193  ","xmonth":"12","xyear":"2015","QTY":"136"},{"omlPartgroupid":"193  ","xmonth":"1","xyear":"2016","QTY":"212"},{"omlPartgroupid":"193  ","xmonth":"2","xyear":"2016","QTY":"460"},{"omlPartgroupid":"193  ","xmonth":"3","xyear":"2016","QTY":"176"},{"omlPartgroupid":"193  ","xmonth":"4","xyear":"2016","QTY":"187"},{"omlPartgroupid":"193  ","xmonth":"5","xyear":"2016","QTY":"174"},{"omlPartgroupid":"193  ","xmonth":"6","xyear":"2016","QTY":"151"},{"omlPartgroupid":"193  ","xmonth":"7","xyear":"2016","QTY":"164"},{"omlPartgroupid":"193  ","xmonth":"8","xyear":"2016","QTY":"237"},{"omlPartgroupid":"193  ","xmonth":"9","xyear":"2016","QTY":"798"},{"omlPartgroupid":"193  ","xmonth":"10","xyear":"2016","QTY":"662"},{"omlPartgroupid":"195  ","xmonth":"10","xyear":"2015","QTY":"9"},{"omlPartgroupid":"195  ","xmonth":"11","xyear":"2015","QTY":"38"},{"omlPartgroupid":"195  ","xmonth":"12","xyear":"2015","QTY":"35"},{"omlPartgroupid":"195  ","xmonth":"1","xyear":"2016","QTY":"68"},{"omlPartgroupid":"195  ","xmonth":"2","xyear":"2016","QTY":"161"},{"omlPartgroupid":"195  ","xmonth":"3","xyear":"2016","QTY":"73"},{"omlPartgroupid":"195  ","xmonth":"4","xyear":"2016","QTY":"69"},{"omlPartgroupid":"195  ","xmonth":"5","xyear":"2016","QTY":"56"},{"omlPartgroupid":"195  ","xmonth":"6","xyear":"2016","QTY":"55"},{"omlPartgroupid":"195  ","xmonth":"7","xyear":"2016","QTY":"50"},{"omlPartgroupid":"195  ","xmonth":"8","xyear":"2016","QTY":"114"},{"omlPartgroupid":"195  ","xmonth":"9","xyear":"2016","QTY":"1046"},{"omlPartgroupid":"195  ","xmonth":"10","xyear":"2016","QTY":"883"},{"omlPartgroupid":"197  ","xmonth":"10","xyear":"2015","QTY":"34"},{"omlPartgroupid":"197  ","xmonth":"11","xyear":"2015","QTY":"76"},{"omlPartgroupid":"197  ","xmonth":"12","xyear":"2015","QTY":"114"},{"omlPartgroupid":"197  ","xmonth":"1","xyear":"2016","QTY":"173"},{"omlPartgroupid":"197  ","xmonth":"2","xyear":"2016","QTY":"327"},{"omlPartgroupid":"197  ","xmonth":"3","xyear":"2016","QTY":"134"},{"omlPartgroupid":"197  ","xmonth":"4","xyear":"2016","QTY":"125"},{"omlPartgroupid":"197  ","xmonth":"5","xyear":"2016","QTY":"200"},{"omlPartgroupid":"197  ","xmonth":"6","xyear":"2016","QTY":"104"},{"omlPartgroupid":"197  ","xmonth":"7","xyear":"2016","QTY":"99"},{"omlPartgroupid":"197  ","xmonth":"8","xyear":"2016","QTY":"191"},{"omlPartgroupid":"197  ","xmonth":"9","xyear":"2016","QTY":"845"},{"omlPartgroupid":"197  ","xmonth":"10","xyear":"2016","QTY":"578"},{"omlPartgroupid":"199  ","xmonth":"10","xyear":"2015","QTY":"35"},{"omlPartgroupid":"199  ","xmonth":"11","xyear":"2015","QTY":"75"},{"omlPartgroupid":"199  ","xmonth":"12","xyear":"2015","QTY":"76"},{"omlPartgroupid":"199  ","xmonth":"1","xyear":"2016","QTY":"105"},{"omlPartgroupid":"199  ","xmonth":"2","xyear":"2016","QTY":"229"},{"omlPartgroupid":"199  ","xmonth":"3","xyear":"2016","QTY":"147"},{"omlPartgroupid":"199  ","xmonth":"4","xyear":"2016","QTY":"73"},{"omlPartgroupid":"199  ","xmonth":"5","xyear":"2016","QTY":"50"},{"omlPartgroupid":"199  ","xmonth":"6","xyear":"2016","QTY":"58"},{"omlPartgroupid":"199  ","xmonth":"7","xyear":"2016","QTY":"103"},{"omlPartgroupid":"199  ","xmonth":"8","xyear":"2016","QTY":"4230"},{"omlPartgroupid":"199  ","xmonth":"9","xyear":"2016","QTY":"2570"},{"omlPartgroupid":"199  ","xmonth":"10","xyear":"2016","QTY":"730"}]

1 个答案:

答案 0 :(得分:4)

这里引用: https://github.com/chartjs/Chart.js/issues/2138

它的大部分是创建第二轴。我在这里省略了一些设置,但你可以在下面找到一个JS小提琴:

    options: {
        scales: {
            xAxes:[ {
                id: 'time',
                type: 'category',
                ticks: {
                    callback: function(label) {
                        var labelArray = label.split("|");
                        return  labelArray[0] + "/" + labelArray[1];
                    }
                }
            },
            {
                id: 'partGroup',
                type: 'category',
                gridLines: {
                    drawOnChartArea: false // only want the grid lines for one axis to show up
                },
                ticks: {
                    callback: function(label) {
                        var labelArray = label.split("|");
                        return labelArray[0] === "10" && labelArray[1] == "2015" ?      labelArray[2] : "";
                    }
                }

            }


             ]
        }
    }

这里是Fiddle with your data