代码之家  ›  专栏  ›  技术社区  ›  Dan Wier

画布未在图表中显示条形图

  •  0
  • Dan Wier  · 技术社区  · 7 年前

    我正在canvasjs中制作一个stackedColumn图表,从$ajax响应中输入数据,但没有显示这些列。我把一把小提琴放在一起,展示我的作品:

    https://jsfiddle.net/azjuzuu0/4/

    <input id="Button1" type="button" value="button" />
    <div id="chartContainer" style="height: 300px; width: 100%;"></div>
    
    
    
    $('input:button').on('click', function () {
    var response = <view fiddle for json>
    
    var JSONresponse = JSON.stringify(response).substring(6, JSON.stringify(response).length - 2);
    
                JSONresponse = JSONresponse.replace(/\\/g, '');
                var jsonObject = $.parseJSON(JSONresponse);
    
                // QuoteStatus, AssignedTo, Amount
    
                var currentAssignedTo; //this lets us keep track of who we're working on. 
                //    If the name of hte current obj is not hte currentassignedto, then we are on to a new person
    
                var assignedToCount = -1;
    
                var chart = new CanvasJS.Chart("chartContainer",
                    {
                        title: {
                            text: "Total of Jobs per Status",
                            fontFamily: "arial black",
                            fontColor: "#695A42"
                        },
                        animationEnabled: true,
                        toolTip: {
                            shared: true,
                            content: function (e) {
                                var str = '';
                                var total = 0;
                                var str3;
                                var str2;
                                for (var i = 0; i < e.entries.length; i++) {
                                    var str1 = "<span style= 'color:" + e.entries[i].dataSeries.color + "'> " + e.entries[i].dataSeries.name + "</span>: $<strong>" + e.entries[i].dataPoint.y + "</strong><br/>";
                                    total = e.entries[i].dataPoint.y + total;
                                    str = str.concat(str1);
                                };
                                str2 = "<span style = 'color:DodgerBlue; '><strong>" + (e.entries[0].dataPoint.x) + "</strong></span><br/>";
                                total = Math.round(total * 100) / 100;
                                str3 = "<span style = 'color:Tomato '>Total:</span><strong> $" + total + "</strong><br/>";
    
                                return (str2.concat(str)).concat(str3);
                            }
                        },
                        axisY: {
                            valueFormatString: "$#",
                            gridColor: "#B6B1A8",
                            tickColor: "#B6B1A8",
                            interlacedColor: "rgba(182,177,168,0.2)"
                        },
                        data: []
                    });
    
                $.each(jsonObject, function (i, obj) {
    
                    if (obj.AssignedTo !== currentAssignedTo) {
                        //we're either on the first row or are moving into a new person   color: colors[assignedToCount],
                        currentAssignedTo = obj.AssignedTo;
                        assignedToCount += 1;
                        chart.options.data.push({ type: 'stackedColumn', showInLegend: true, name: obj.AssignedTo, dataPoints: [] });
                    } //end if
    
                    chart.options.data[assignedToCount].dataPoints.push({ y: obj.Amount, label: obj.quoteType });
    
                }); // end $.each                       
    
                chart.render();
            })      
    

    我所依赖的一些源材料: https://canvasjs.com/editor/?id=https://canvasjs.com/example/gallery/column/coal_reserves/

    https://canvasjs.com/editor/?id=https://canvasjs.com/example/gallery/column/google_revenue/

    1 回复  |  直到 7 年前
        1
  •  4
  •   Vishwas R    7 年前

    数据点y值应该是数字,但在json中它存储为字符串。因此,在json本身或解析时将其更改为数字应该适用于您的情况。

    chart.options.data[assignedToCount].dataPoints.push({ y: Number(obj.Amount), label: obj.quoteType });
    

    this updated JSFiddle .