Constructing Json for JqPlot Bar Charts

狂风中的少年 提交于 2020-01-03 02:55:37

问题


I have managed to create something to populate the JqPlot pie chart (very hard coded – it will be automated). Here is how I’ve done it:

public ActionResult PieChart()
    {
        return View();
    }

    public ActionResult PieChartJSON()
    {
        List<PieChartData> sampleData = new List<PieChartData>();
        PieChartData test = new PieChartData();
        PieChartData test2 = new PieChartData();
        PieChartData test3 = new PieChartData();
        PieChartData test4 = new PieChartData();
        PieChartData test5 = new PieChartData();

        test.Label = "ta";
        test.Value = 3;
        sampleData.Add(test);

        test2.Label = "be";
        test2.Value = 5;
        sampleData.Add(test2);

        test3.Label = "ga";
        test3.Value = 3;
        sampleData.Add(test3);

        test4.Label = "ma";
        test4.Value = 8;
        sampleData.Add(test4);

        test5.Label = "ja";
        test5.Value = 8;
        sampleData.Add(test5);

        return Json(sampleData, JsonRequestBehavior.AllowGet);
    }

JQuery:

jQuery(document).ready(function () {
    urlDataJSON = '/Home/PieChartJSON';

    $.getJSON(urlDataJSON, "", function (data) {
        var dataSlices = [];
        var dataLabels = "";

        $.each(data, function (entryindex, entry) {
            dataSlices.push(entry['Value']);
            dataLabels = dataLabels + entry['Label'];
        });
        options = {
            legend: { show: true },
            title: 'Poll Results',
            seriesDefaults: {
                // Make this a pie chart.
                renderer: jQuery.jqplot.PieRenderer,
                rendererOptions: {
                    // Put data labels on the pie slices.
                    // By default, labels show the percentage of the slice.
                    showDataLabels: true
                }
            }
        }
        var plot = $.jqplot('pieChart', [dataSlices], options);
    });
}); 

http://i.stack.imgur.com/ohup4.png *Produced Graph

I’d like to be able to create something similar to the bar graph on the following page: http://www.jqplot.com/tests/bar-charts.php (second chart down). This bar graph is created using the following jQuery:

I’m quite new to C# and Json and I’m a little unsure how the Json data should be constructed to create this bar graph. Can anyone help me out?

  $(document).ready(function(){
    // For horizontal bar charts, x an y values must will be "flipped"
    // from their vertical bar counterpart.
    var plot2 = $.jqplot('chart2', [
        [[2,1], [4,2], [6,3], [3,4]], 
        [[5,1], [1,2], [3,3], [4,4]], 
        [[4,1], [7,2], [1,3], [2,4]]], {
        seriesDefaults: {
            renderer:$.jqplot.BarRenderer,
            // Show point labels to the right ('e'ast) of each bar.
            // edgeTolerance of -15 allows labels flow outside the grid
            // up to 15 pixels.  If they flow out more than that, they 
            // will be hidden.
            pointLabels: { show: true, location: 'e', edgeTolerance: -15 },
            // Rotate the bar shadow as if bar is lit from top right.
            shadowAngle: 135,
            // Here's where we tell the chart it is oriented horizontally.
            rendererOptions: {
                barDirection: 'horizontal'
            }
        },
        axes: {
            yaxis: {
                renderer: $.jqplot.CategoryAxisRenderer
            }
        }
    });
});

回答1:


Understanding that you want to build a bar chart for the given data you could have your dataSlices build in a following way, assuming your JSON data comes as an array:

var json = [
    {"Label": "be", "Value": 5}
    ,{"Label": "ga", "Value": 3}
    ,{"Label": "ma", "Value": 8}
    ,{"Label": "ja", "Value": 8}];
var dataSlices = [];
var ticks = [];
$.each(json, function (entryindex, entry) {
    dataSlices.push(entry['Value']);
    ticks.push(entry['Label']);
});

This is demonstrated in the following code which reuses a standard example from jqPlot website.




回答2:


Try this, Working fine,

 $.getJSON('/mservice/getvalues', function(data) {
        var items1 = new Array();
        var j=0;
        for ( var i in data ) {
            var items = new Array();
            items.push(i,Number(data[i]));
            items1[j++] = items;
        }

        var plot1 = jQuery.jqplot('chart1', eval([items1]), {

                    seriesDefaults:{
                        renderer:jQuery.jqplot.PieRenderer,
                        rendererOptions:{
                            dataLabels:'value',
                            showDataLabels:true
                        }
                    },
                    legend:{ show:true, location:'e' }
                }
        );

    });


来源:https://stackoverflow.com/questions/10514311/constructing-json-for-jqplot-bar-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!