簡體   English   中英

選擇查詢以堆積條形圖顯示

[英]Select Query for Stacked Bar Chart Display

我正在嘗試制作一個堆疊的條形圖,我有一個名稱為FeedbackPOS的表,列為Q1,Q2,Q3,Q4,Q5,... Q11,每個值的范圍在5到1之間,例如5或4或3..1 在此處輸入圖片說明 圖(JQuery的)

<script>
    $(document).ready(function () {

        $.ajax({
            type: "Post",
            url: "FeedBackGraph.aspx/getdata",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (Response) {

                var d = Response.d.toString();
                var final_string = d;
                var result = final_string.substring(1, final_string.length - 1);
                var res = final_string.split(",").map(Number);
                console.log(res);
                //final_string = [1,3,2,4];

    var options = {
        chart: {
            height: 500,
            type: 'bar',
            stacked: true,
            stackType: '100%'
        },
        plotOptions: {
            bar: {
                horizontal: true,
            },

        },
        stroke: {
            width: 1,
            colors: ['#fff']
        },
        series: [{
            name: '5 *****',
            data: [4, 2, 5, 3, 4, 1, 2,5, 3, 1, 4]
        }, {
            name: '4 ****',
            data: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
        }, {
            name: '3 ***',
            data: [4, 2, 5, 3, 4, 1, 2, 5, 3, 1, 4]
        }, {
            name: '2 **',
            data: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
        }, {
            name: '1 *',
            data: [4, 2, 5, 3, 4, 1, 2, 5, 3, 1, 4]
        }],
        title: {
            text: 'FeedBack'
        },
        xaxis: {
            categories: ['Ambience', 'EfficiencyofService', 'FriendlyandAttentive', 'VarietyinMenu', 'Presentation', 'BeveragesQuality', 'FoodQuality', 'Anticipated', 'Money', 'ValuedGuest', 'Recommend'],
        },

        tooltip: {
            y: {
                formatter: function (val) {
                    return val + "K"
                }
            }
        },
        fill: {
            opacity: 1

        },

        legend: {
            position: 'top',
            horizontalAlign: 'left',
            offsetX: 40
        }
    }

                var chart = new ApexCharts(
                     document.querySelector("#chart"),
                     options
                 );

                chart.render();

            },
            error: function (result) {
            }
        });
    });
                    </script>

在此處輸入圖片說明 的WebMethod

     public static string getdata()
    {
        DateTime CurrentDate = System.DateTime.Now;
        string finldata = "";
        string customers = "";
        clsDBOperation obj = new clsDBOperation();
        string Query = @"select '5star' as Star, COUNT(q1) as Q1 from CRM0001FeedbackPOS where q1=5
union all select '4star' as Star, COUNT(q1) from CRM0001FeedbackPOS where q1=4
union all select '3star' as Star, COUNT(q1)  from CRM0001FeedbackPOS where q1=3
union all select '2star' as Star, COUNT(q1)  from CRM0001FeedbackPOS where q1=2
union all select '1star' as Star, COUNT(q1) from CRM0001FeedbackPOS where q1=1
select COUNT(q2) as Q2 from CRM0001FeedbackPOS where q2=5 
union all select COUNT( q2) from CRM0001FeedbackPOS where q2=4
union all select COUNT( q2)  from CRM0001FeedbackPOS where q2=3
union all select COUNT(  q2)  from CRM0001FeedbackPOS where q2=2
union all select COUNT(q2) from CRM0001FeedbackPOS where q2=1";
        DataTable dtProductMaster = obj.GetDataTable(Query);
        if (dtProductMaster.Rows.Count > 0)
        { 
            finldata = GetJsonData(dtProductMaster);
        }
        return finldata;
    }

SQLQuery的輸出

Rating| Q1

5star | 2
4star | 0
3star | 1
2star | 0
1star | 0

       Q2
        3
        0
        0
        0
        0

ExpectedOutput

Rating| Q1  | Q2....Q11

5star | 2   | 3
4star | 0   | 0
3star | 1   | 0
2star | 0   | 0
1star | 0   | 0.....Q11

但是我不知道如何在單個選擇查詢中加入所有列(q1 ... q11)? 然后我將在系列(以json格式)系列中使用這些值:

series: [{
                name: '5 *****',
                data: [4, 2, 5, 3, 4, 1, 2,5, 3, 1, 4]     // 5STAR
            }, {
                name: '4 ****',
                data: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]    // 4STAR
            }, {
                name: '3 ***',
                data: [4, 2, 5, 3, 4, 1, 2, 5, 3, 1, 4]    // 3STAR
            }, {
                name: '2 **',
                data: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]     // 2STAR
            }, {
                name: '1 *',
                data: [4, 2, 5, 3, 4, 1, 2, 5, 3, 1, 4]     // 1STAR
            }],

抱歉,由於代表人數限制,無法發表評論。 正如我從您的查詢中看到的那樣,由於所有這些聯合,您將放置一些東西來標識數據來自JS內部的數據,所以它會很慢並且會給sql大量工作。 像在Q1列中為行數據'5'創建一個數據系列,並在Q1數據行值'4'中創建另一個系列? 所以你可以簡單地做一個

Select  Count(q1) from Table query 

還可以給您一個想法,您可以並且應該在演示中查看devexpress圖表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM