Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqPlot堆叠条形图在图表外渲染_Javascript_Charts_Jqplot_Webmethod - Fatal编程技术网

Javascript jqPlot堆叠条形图在图表外渲染

Javascript jqPlot堆叠条形图在图表外渲染,javascript,charts,jqplot,webmethod,Javascript,Charts,Jqplot,Webmethod,我正在使用jqPlot根据web方法中的数据生成一个堆叠条形图 图表成功呈现,但为空。当我将pointLabels设置为“true”时,它们会在图表的左侧以混乱的形式出现。我猜堆叠的条形图也被渲染出图表之外,但我不明白为什么 谁能解释一下怎么解决这个问题吗 以下是webmethod: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<dataPoint> getP

我正在使用jqPlot根据web方法中的数据生成一个堆叠条形图

图表成功呈现,但为空。当我将pointLabels设置为“true”时,它们会在图表的左侧以混乱的形式出现。我猜堆叠的条形图也被渲染出图表之外,但我不明白为什么

谁能解释一下怎么解决这个问题吗

以下是webmethod:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<dataPoint> getPartnerOrderVolumes()
    {
        List<dataPoint> p = new List<dataPoint>();
        DataTable dt = new DataTable();

        chart jep = new chart(5);
        foreach (chartData cd in jep.lstChartData)
        {
            dt = cd.GetData();
        }

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                dataPoint dp = new dataPoint();
                dp.x1Value = row[2].ToString();
                dp.y1Value = row[3].ToString();
                dp.y2Value = row[4].ToString();
                p.Add(dp);
            }
        }

        return p;
    }
下面是它从数据库中提取的数据示例:

下面是javascript:

            function OnSuccess_(response) {
            var aData = response.d;
            var types = [];
            var arrType = [];
            var arr = [];

            //  find distinct types (partners)
            for (i = 0; i < aData.length; i++) {
                if (types.indexOf(aData[i].y2Value) === -1) {
                    types.push(aData[i].y2Value);
                }
            }

            //  generate array containing arrays of each type
            for (i = 0; i < types.length; i++)
            {
                var filtered = aData.filter(function (el) {
                    return el.y2Value == types[i];
                });

                arrType.length = 0;

                $.map(filtered, function (item, index) {
                    var j = [item.x1Value, item.y1Value];
                    arrType.push(j);
                });

                arr.push(arrType);
            }

            $.jqplot.config.enablePlugins = true;

            plot1 = $.jqplot('chart5', arr, {
                title: 'Partner Order Volumes',
                // Only animate if we're not using excanvas (not in IE 7 or IE 8)..
                animate: !$.jqplot.use_excanvas,
                stackSeries: true,
                seriesColors: ['#F7911E', '#32AB52', '#FFE200', '#29303A'],
                seriesDefaults: {
                    shadow: true,
                    pointLabels: { show: true },
                    renderer: $.jqplot.BarRenderer,
                    rendererOptions: {
                        varyBarColor: true,
                        animation: { speed: 2000 },
                        barDirection: 'vertical'
                    }
                },
                legend: {
                    show: true,
                    location: 'e',
                    placement: 'outside',
                    labels: types
                },
                axesDefaults: {
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
                    tickRenderer: $.jqplot.CanvasAxisTickRenderer,
                    tickOptions: { fontSize: '10pt', textColor: '#000000' }
                },
                axes: {
                    xaxis: {
                        renderer: $.jqplot.CategoryAxisRenderer,
                        tickOptions: { angle: -30 }
                    },
                    yaxis: {
                        label: 'Count of New Orders',
                        min: 0,
                        max: 200
                    }
                },
                highlighter: { show: false }
            });
        }
        function OnErrorCall_(response) {
            alert("Whoops something went wrong!");
        }
    });
成功时的函数(响应){ var aData=响应d; var类型=[]; var-arrType=[]; var-arr=[]; //查找不同类型(合作伙伴) 对于(i=0;i我认为有两种因素共同导致您的问题:

首先:未正确复制阵列。将数据拆分为类型时,您正在使用
arrType.length=0重置临时数组。这会重置数组长度,但不会生成新数组。这意味着,实际上,您推送到
arr
的所有数组引用都指向同一个数组,即处理的最后一个类型的最后一个数据。您需要替换
arrType.length=0带有:

arrType=[]

或者,保持
.length=0
,并在将阵列推送到
arr
时使用以下命令:

arr.push(arrType.slice())

秒:使用的渲染器不正确
xaxis
的渲染器应该是
$.jqplot.DateAxisRenderer
,而不是当前使用的
$.jqplot.CategoryAxisRenderer
。日期渲染器也是一个插件,因此您需要确保包含以下内容(显然,路径根据您的设置进行了适当调整):

您希望
xaxis
上的
tickOptions
类似于:

tickOptions:{formatString:'%b%#d',角度:-30}

通过这些调整,再加上从C#代码中获得的一些示例数据,JS成功地生成了以下内容:


希望能解决问题

您能用一些示例数据设置一个JSFIDLE吗?这可能有助于弄清真相。或者干脆用WebMethod的JSON回复来修改这个问题。在我看到你的帖子之前,我已经设法让它工作了。有许多问题。首先,我的javascript为jqPlot准备传入数据时出现了一个问题。另外,我觉得提供给jqPlot的数据应该是x和y坐标数组的形式,如果是这样的话会更好,但事实并非如此。最后,我发现当系列中的元素数量不尽相同时,jqPlot会阻塞,所以我也解决了这个数据问题。在那之后,它就像一个符咒。谢谢你的回复。
            function OnSuccess_(response) {
            var aData = response.d;
            var types = [];
            var arrType = [];
            var arr = [];

            //  find distinct types (partners)
            for (i = 0; i < aData.length; i++) {
                if (types.indexOf(aData[i].y2Value) === -1) {
                    types.push(aData[i].y2Value);
                }
            }

            //  generate array containing arrays of each type
            for (i = 0; i < types.length; i++)
            {
                var filtered = aData.filter(function (el) {
                    return el.y2Value == types[i];
                });

                arrType.length = 0;

                $.map(filtered, function (item, index) {
                    var j = [item.x1Value, item.y1Value];
                    arrType.push(j);
                });

                arr.push(arrType);
            }

            $.jqplot.config.enablePlugins = true;

            plot1 = $.jqplot('chart5', arr, {
                title: 'Partner Order Volumes',
                // Only animate if we're not using excanvas (not in IE 7 or IE 8)..
                animate: !$.jqplot.use_excanvas,
                stackSeries: true,
                seriesColors: ['#F7911E', '#32AB52', '#FFE200', '#29303A'],
                seriesDefaults: {
                    shadow: true,
                    pointLabels: { show: true },
                    renderer: $.jqplot.BarRenderer,
                    rendererOptions: {
                        varyBarColor: true,
                        animation: { speed: 2000 },
                        barDirection: 'vertical'
                    }
                },
                legend: {
                    show: true,
                    location: 'e',
                    placement: 'outside',
                    labels: types
                },
                axesDefaults: {
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
                    tickRenderer: $.jqplot.CanvasAxisTickRenderer,
                    tickOptions: { fontSize: '10pt', textColor: '#000000' }
                },
                axes: {
                    xaxis: {
                        renderer: $.jqplot.CategoryAxisRenderer,
                        tickOptions: { angle: -30 }
                    },
                    yaxis: {
                        label: 'Count of New Orders',
                        min: 0,
                        max: 200
                    }
                },
                highlighter: { show: false }
            });
        }
        function OnErrorCall_(response) {
            alert("Whoops something went wrong!");
        }
    });