jqPlot Stacked Bar Chart rendered off-chart
Asked Answered
O

1

8

I'm using jqPlot to generate a stacked bar chart based on data from a web method.

The chart renders successfully, but is blank. When I set the pointLabels to 'true', they appear in a jumble to the left of the chart. I'm guessing the stacked bars are also being rendered off-chart, but I don't understand why.

Could some one please explain how to fix this?

enter image description here

Here is the 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;
    }

Here is the dataPoint class used by the web method:

        public class dataPoint
    {
        public string x1Value { get; set; }
        public string y1Value { get; set; }
        public string x2Value { get; set; }
        public string y2Value { get; set; }
        public string x3Value { get; set; }
        public string y3Value { get; set; }
        public string x4Value { get; set; }
        public string y4Value { get; set; }
    }

Here an example of the data it pulls from the database:

enter image description here

Here is the 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!");
        }
    });
Ornithine answered 22/10, 2015 at 17:20 Comment(1)
Could you set up a jsfiddle with some of your sample data? It might help get to the bottom of it. Or even just amend the question with a JSON response from the WebMethod.Finish
F
3

I think there are two things that are combining to cause your problem:

First: not properly duplicating arrays. When splitting up your data into types, you're resetting your temporary array with arrType.length = 0. This resets the array length, but doesn't make a new array. That means that in fact all the array references you push to arr point to the same array - the last data for the last type processed. You need to replace arrType.length = 0; with:

arrType = [];

Alternatively, keep the .length = 0 and use the following when pushing the array to arr:

arr.push(arrType.slice());

Second: incorrect renderer being used. The renderer for the xaxis should be $.jqplot.DateAxisRenderer and not $.jqplot.CategoryAxisRenderer which is what you're currently using. The date renderer is a plugin too, so you need to make sure you include the following (obviously with path adjusted appropriately to your setup):

<script type="text/javascript" src="plugins/jqplot.dateAxisRenderer.js"></script>

You'll want tickOptions on the xaxis to be something like:

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

With these adjustments, and with some sample data derived from your C# code, the JS successfully produced the following:

sample graph

Hope that solves the issue!

Finish answered 31/10, 2015 at 12:2 Comment(1)
I managed to get it working before I saw your post. There were a number of issues. First, there was a problem with my javascript that prepared the incoming data for jqPlot. Also, I was under the impression the data supplied to jqPlot should be in the form of an array of x and y coordinates, it would be nice if that were the case, but it's not. Finally, I found jqPlot chokes when the series don't all have the same number of elements, so I fixed that data issue, too. After that it worked like a charm. Thank you for responding though.Ornithine

© 2022 - 2024 — McMap. All rights reserved.