Plotting live-updating data with JQplot

Utilizing JQPlot, JavaScript, Asp.net 4.5, C# and MS Visual Studio 2012.

Hello all! I'm facing an issue with the following code:

script>
$(document).ready(function () {
    var dataArray = [];

    <%foreach(Last12MonthsRegistered reg in dbregistered)
                  {%>
    dataArray.push(['<%=reg.MonthName.Trim()%>',<%= reg.TotalReg%>]);
        <%}%>

    var plot2 = $.jqplot('chart1', [dataArray], {
        // Give the plot a title.
        title: 'Users Registered Last 12 Months',
        axesDefaults: {
            labelRenderer: $.jqplot.CanvasAxisLabelRenderer
        },
        axes: {
            // options for each axis are specified in separate option objects.
            xaxis: {
                label: "Months"
            },
            yaxis: {
                label: "User Total"
            }
        }
    });
});
</script>

I am trying to render a jqplot graph using data obtained from my SQL Database. This data is bound to a list of objects. I access the values and loop through a Foreach loop in the script above to populate an array, which is then used for the JQplot.

The issue I am encountering is that nothing appears on the graph. When debugging the JS script, the results showed:

dataArray.push(['October',0]);

dataArray.push(['November',0]);

... (more months with corresponding user registrations)

dataArray.push(['September',1]);

which seems correct. However, when hovering over the array during debugging, it shows:

0: Array[2]
1: Array[2]
... (similar pattern for other months)
length: 12
__proto__: Array[0]

This does not seem correct at all! Continuing debugging results in a blank graph being displayed.

I am new to JavaScript and this is my first time using JQPlot. I am struggling to find the necessary information in their documentation. Can you help me understand why my array appears to be incorrect?

Thank you!

UPDATE: 24/10/2013 - 11:24 AM found some more info and changed my code to a bar chart.

    $(document).ready(function () {
    var dataArray = [];
    var ticks = [];

    <%foreach(Last12MonthsRegistered reg in dbregistered)
                  {%>
    dataArray.push(<%= reg.TotalReg%>);
    <%}%>

    <%foreach(Last12MonthsRegistered reg in dbregistered)
                  {%>
    ticks.push('<%= reg.MonthName.Trim()%>');
    <%}%>

    var plot1 = $.jqplot('chart1', [dataArray], {
        // Give the plot a title.

        seriesDefaults: {
            renderer: $.jqplot.BarRenderer,
            rendererOptions: { fillToZero: true }
        },
        series: [
            { label: 'Users Registered' },
        ],
        
        legend: {
            show: true,
            placement: 'outsideGrid'
        },
        axes: {
            xaxis: {
                renderer: $.jqplot.CategoryAxsisRenderer,
                ticks: ticks
            },
            yaxis: {
                pad: 1.05
            }
        }
    });
});

The arrays appear to be fine, and the month names are displayed on the x-axis, which is great. The only issue is that they are stacked on top of each other to the very left, resulting in nothing being displayed and the names overlapping. Any thoughts on this?

I am puzzled, any insights would be greatly appreciated.

Answer №1

Once I fixed the spelling of $.jqplot.CategoryAxisRenderer, your code started functioning perfectly. Check out the Jsfiddle here

    <%foreach(Last12MonthsRegistered reg in dbregistered)
    {%>
       dataArray.push(['<%=reg.MonthName.Trim()%>',<%= reg.TotalReg%>]);
    <%}%>

    <%foreach(Last12MonthsRegistered reg in dbregistered)
    {%>
       ticks.push('<%= reg.MonthName.Trim()%>');
    <%}%>

        var plot1 = $.jqplot('chart1', [dataArray], {
                // Adding a title to the plot.

                seriesDefaults: {
                    renderer: $.jqplot.BarRenderer,
                    rendererOptions: { fillToZero: true }
                },
                // Custom labels for each series specified using the "label"
                // option under the respective series option.
                series: [
                    { label: 'Users Registered' },
                ],
                // Displaying the legend, positioning it outside the grid within
                // the plot container while resizing the grid to accommodate it.
                // Using "outside" would allow the legend to overflow the container.
                legend: {
                    show: true,
                    placement: 'outsideGrid'
                },
                axes: {
                    // Axis specific options are provided in separate objeccts.
                    xaxis: {
                        renderer: $.jqplot.CategoryAxisRenderer
                        ,ticks: ticks
                    },
                    yaxis: {
                        pad: 1.05,
                        min: 0
                    }
                }
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

After switching pages, Selenium is unable to locate an element even though it is still visible on the page

When using Selenium and Java, I encountered an issue where after clicking on a button, I landed on another page but could not find the desired input tag in the viewport right away. To address this, I implemented a wait for the page to load using: driver. ...

undefined reference to $

I'm currently working on a JavaScript project that involves using key events to display alphabets on the screen. However, I've encountered an error and need some assistance. <!DOCTYPE html> <html lang="en"> <head> <met ...

jQuery .click() only triggering upon page load

I've been searching all over the place and I just can't seem to find a solution to my specific situation. But here's what I'm dealing with: Instead of using inline HTML onclick, I'm trying to use jQuery click() like this $(docume ...

Positioning the image to appear behind the Canvas

As I near the end of the game, I'm encountering an issue where the background image overlaps the Canvas. How can I resolve this problem? -Translated by Google Link to game Link to game with background ...

Why isn't the onChange function triggering in the input type text when the input is not manually typed in?

I am currently facing an issue with two text fields in my HTML form. Here is how they are set up: HTML : <input type="text" id="input1" onchange="doSomething();" disabled/> <input type="text" id="input2"/> JavaScript : function doSomething( ...

AngularJS: Implement ng-repeat directive to generate two separate HTML containers

I have a collection of items and I want to divide them into two separate containers. Is there a way to achieve this using ng-repeat? <div> <ul> <li>Some item</li> <li>Some item</li> <li&g ...

Displaying Props Information in a Modal Window using React

Currently venturing into the realm of React JS, where I am in the process of developing a sample eCommerce application for real-time use. However, I have hit a roadblock. In my Products List, there is a Buy button for each product. When clicking on this b ...

Gridview with bespoke edit and delete buttons

I am currently working with C#.net My goal is to include custom edit and delete buttons for each row in GridView1. Instead of inline editing, I want these buttons to direct users to separate views (editView/deleteView within the same form). The edit but ...

Having trouble establishing a connection with localhost at port 4445 using Nightwatch and Selenium

I am encountering an issue while trying to execute my Nightwatch script in Javascript. The error message I am receiving is: \ Connecting to localhost on port 4445... ‼ Error connecting to localhost on port 4445. × failed E ...

I am encountering difficulty in printing multiple documents from FireStore

I am facing an issue where I can successfully retrieve and print all documents from a Firestore collection one by one using console.log(). However, when attempting to display these documents on the screen, only the most recent document is showing up. Here ...

Explore an array library in JavaScript

How can I access elements in a list of arrays separated by commas? [ ["service_state", "service_description", "service_icons", "svc_plugin_output", "svc_state_age", "svc_check_age", "perfometer"], ["OK", "Check_MK", "", "OK- Agent version 1.2.4p4, ...

Steps to create a custom function that can manage numerous onclick actions to toggle the visibility of a specific field

I'm relatively new to coding and JavaScript. I'm working on a basic webpage that involves showing and hiding parts of sentences for language learning purposes. Is there a way to create a single function that can show and hide the sentence when a ...

The issue with loading scripts in a ReactJS NextJS app is related to the inline condition not working

I'm having trouble with an inline condition for loading scripts. The condition seems to be working because the tag is displaying text, but when it comes to scripts, it doesn't work. How can I resolve this issue? const cookie = new Cookies().get ...

Is there a way to print the entire Bootstrap modal even if the content in the modal body is scrolled beyond visibility?

An issue arises when a user needs to scroll down to see all of the content in the modal body. When printing the modal, only the visible part is printed rather than the entire content. Various code snippets from different sources have been tried with no suc ...

I am having trouble with my append function even though I checked the console log and did not see any errors (beginner's inquiry)

As I practice working with functions and dynamically creating input fields, I have encountered an issue where I am unable to append my input field to the form. Despite using console.log() and not seeing any errors, I can't figure out what mistake I ma ...

The operation cannot be found

I am completely new to javascript, so here is my situation: I'm attempting to integrate a reading-position-indicator into my WordPress theme. To achieve this, I placed a div right behind the footer, close to all the script tags, as the position bar s ...

Can a WCF service utilize both the SOAP and JSON methods simultaneously?

I recently started working with WCF, and I need to utilize the same WCF for bothSOAPandJSON` formats using the post method. Is this achievable? Any guidance would be greatly appreciated. ...

GraphQL and Relay.js issue: Error Message: Field "id" is expected in "Node"

It appears that the discrepancy lies in naming conventions between my schema.js file and the database field. The 'id' field in the schema is actually named differently in the database. var classroomType = new GraphQLObjectType({ name: 'Cl ...

Jest is simulating a third-party library, yet it is persistently attempting to reach

Here's a function I have: export type SendMessageParameters = { chatInstance?: ChatSession, // ... other parameters ... }; const sendMessageFunction = async ({ chatInstance, // ... other parameters ... }: SendMessageParameters): Promise<vo ...

Searching for a specific string within an array using JavaScript: tips and tricks!

I have a variable which looks like this: var d; d = [{'category': 'Hourly Report','from_start_datetime': '2013','format': 'yyyy-mm-dd hh:ii:ss', 'id': 'dateRangeTo'}] When I ...