jQWidgets Forums

jQuery UI Widgets Forums Plugins Data Adapter Keys as Datafields

This topic contains 2 replies, has 2 voices, and was last updated by  sintakonte 11 years, 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • Keys as Datafields #52171

    sintakonte
    Participant

    Hey guys,

    i’m evaluating right now jqWidgets for our business use.

    i need some help because i don’t understand something with the dataadapter
    for example i’ve json data like

    {"May 2013":
       {"sales":"2,335.48",
        "sales average":"1,878.57"
       },
      "June 2013":
       {"sales":"397.20",
        "sales average":"1,749.20"
       }
    }

    My problem is how do i get the keys “may 2013”, “June 2013” and so on as my categoryAxis Datafield?
    I’d appreciate your help – thx in advance

    Keys as Datafields #52182

    Peter Stoev
    Keymaster

    Hi sintakonte,

    I have prepared a small sample which demonstrates how to prepare and bind your data for displaying in jqxChart.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title id='Description'>jqxChart Line Series Example</title>
        <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
        <script type="text/javascript" src="../../scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxchart.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                // prepare the data
                var data = {"May 2013":
                    {"sales":"2,335.48",
                     "sales average":"1,878.57"
                    },
                   "June 2013":
                    {"sales":"397.20",
                     "sales average":"1,749.20"
                    }
                }
                var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    
                var chartData = new Array();
                $.each(data, function (index, value) {
                    var obj = {};
                    obj.Date = index;
                    obj.Sales = this["sales"].replace(/,/, '');
                    obj.Avg = this["sales average"].replace(/,/, '');
                    chartData.push(obj);
                });
    
                var source =
                {
                    datatype: "array",
                    datafields: [
                        { name: 'Date', type: "date", format: "MMMM yyyy" },
                        { name: 'Sales'},
                        { name: 'Avg'}
                    ],
                    localData: chartData
                };
    
                var dataAdapter = new $.jqx.dataAdapter(source, { async: false, autoBind: true, loadError: function (xhr, status, error) { alert('Error loading "' + source.url + '" : ' + error); } });
           
                // prepare jqxChart settings
                var settings = {
                    enableAnimations: true,
                    showLegend: true,
                    padding: { left: 10, top: 5, right: 50, bottom: 5 },
                    titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
                    source: dataAdapter,
                    categoryAxis:
                        {
                            dataField: 'Date',
                            type: 'date',
                            baseUnit: 'month',
                            showTickMarks: true,
                            tickMarksInterval: 1,
                            tickMarksColor: '#888888',
                            unitInterval: 1,
                            showGridLines: true,
                            gridLinesInterval: 1,
                            gridLinesColor: '#888888',
                            valuesOnTicks: true,
                            formatFunction: function(value)
                            {
                                return dataAdapter.formatDate(value, "MMMM yyyy");
                            }
                        },
                    colorScheme: 'scheme04',
                    seriesGroups:
                        [
                            {
                                type: 'line',
                                valueAxis:
                                {
                                    unitInterval: 100,
                                    minValue: 0,
                                    maxValue: 3000,
                                    displayValueAxis: true,
                                    description: 'Daily Closing Price',
                                    axisSize: 'auto',
                                    tickMarksColor: '#888888'
                                },
                                series: [
                                        { dataField: 'Sales', displayText: 'Sales' },
                                        { dataField: 'Avg', displayText: 'Avg' }
                                    ]
                            }
                        ]
                };
    
                // setup the chart
                $('#chartContainer').jqxChart(settings);
    
            });
        </script>
    </head>
    <body class='default'>
        <div id='chartContainer' style="width:850px; height:500px">
        </div>
    </body>
    </html>
    

    Hope this helps.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    Keys as Datafields #52185

    sintakonte
    Participant

    yeah men thx
    i thought i’ve to solve this in this manner but wasn’t sure because i thought there is a “better” solution

    thx for your help

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.