Is it feasible to generate a vertical bar or horizontal graph using Chart.JS?

Is there a way to manipulate Chart.JS to create a horizontal bar chart with percentages displayed along the bottom, similar to this example created using xlsxwriter?

View Example

UPDATE

I marked a certain answer as correct based on the jsfiddle provided. However, I am still having trouble getting the data to stack properly. Here is the code I am using:

var ctxBarChart = $("#priceComplianceBarChart").get(0).getContext("2d");
var barChartData = {
    labels: ["Bix Produce", "Capitol City", "Charlies Portland", "Costa Fruit and Produce", "Get Fresh Sales", "Loffredo East", "Loffredo West", "Paragon", "Piazza Produce"],
    datasets: [
        {
            label: "Price Compliant",
            backgroundColor: "rgba(34,139,34,0.5)",
            hoverBackgroundColor: "rgba(34,139,34,1)",
            data: [17724, 5565, 3806, 5925, 5721, 6635, 14080, 9027, 25553]
        },
        {
            label: "Non-Compliant",
            backgroundColor: "rgba(255, 0, 0, 0.5)",
            hoverBackgroundColor: "rgba(255, 0, 0, 1)",
            data: [170, 10, 180, 140, 30, 10, 50, 100, 10]
        }
    ]
}

var optionsBar = {
    scales: {
        xAxes: [{
            stacked: true
        }],
        yAxes: [{
            stacked: true
        }]
    }
};

var priceBarChart = new Chart(ctxBarChart, {
    type: 'horizontalBar',
    data: barChartData,
    options: optionsBar
});

Please help me figure out what I'm doing wrong.

UPDATE 3

After some trial and error, I was able to get it working by adjusting the options like so:

var optionsBar = {
    scales: {
        xAxes: [{
            stacked: true
        }],
        yAxes: [{
            stacked: true
        }]
    }
};

Answer №1

  1. To display a horizontal bar chart instead of a regular chart, you can set the type to 'horizontalBar'.
  2. If you want to create a stacked bar chart, you need to configure the following options:


xAxes: [{
    stacked: true
}],
yAxes: [{
    stacked: true
}]

You can use this code snippet as a reference:

var config = {
  type: 'horizontalBar',
  data: {
    labels: ["A", "B", "C", "D", "E"],
    datasets: [{
      label: "Dataset 1",
      backgroundColor: "rgba(154,178,96,0.5)",
      hoverBackgroundColor: "rgba(154,178,96,1)",
      data: [10, 15, 5, 81, 55],
    }, {
      label: "Dataset 2",
      backgroundColor: "rgba(197,213,167,0.5)",
      hoverBackgroundColor: "rgba(197,213,167,1)",
      data: [90, 85, 95, 19, 45]
    }]
  },
  options: {
    scales: {
      xAxes: [{
        stacked: true
      }],
      yAxes: [{
        stacked: true
      }]
    }
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);

For a working example, check out:
https://jsfiddle.net/0xLxum4s/

(The visualization may not work in this particular preview, hence the link to jsfiddle).

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

"Retrieve the position of a contenteditable element while also considering HTML

I've been exploring a method to generate annotations within HTML text using JavaScript. The approach involves the user clicking on a contenteditable <div> and obtaining the cursor's position based on their click. Subsequently, when insertin ...

Using d3 to showcase pictures sourced from a csv file

Having recently embarked on a journey to learn javascript, d3, and the polymer project, I am facing a challenge that I hope to get some guidance on. After successfully parsing a csv file containing image information and creating an array specifically for ...

Submitting a form into a database with the help of AJAX within a looping structure

Trying to input values into the database using AJAX. The rows are generated in a loop from the database, with each row containing a button column. Clicking on the button submits the values to the database successfully. The issue is that it only inserts va ...

Error: The WebGL function enablevertexattribarray has an index that is outside the valid range

Take a look at the vertex and fragment shaders I've written: <script id="shader-fs" type="x-shader/x-fragment"> precision mediump float; uniform sampler2D uSampler; varying vec4 vColor; varying vec2 vTextur ...

Incorporating a Layered Div onto a Presentation Slider

Over at , we have a slider featured on the homepage. I am attempting to place a div ABOVE the image but BELOW the text. Essentially, my goal is to add a semi-transparent white overlay to enhance the image on the slider while keeping the text visible for b ...

Can someone explain why the min-width property is not being respected by the <input> element?

Visit this link for the code The text field in the provided link should only be 10px wide but it is currently displaying a width of 152px. Here is the code snippet: .input { width: 100%; box-sizing: border-box; } .cont { padding: 2px; } .main ...

Employing featherlightGallery.js without the need for anchor tags

I've been experimenting with the FeatherlightGallery plugin for a lightboxing feature on an image slider. The slider contains images directly, without anchor tags wrapping them. However, I'm facing an issue where clicking next/prev doesn't l ...

Comparison of element state prior to and post editing (with contentEditable)

Exploring how elements within a div can be monitored for changes made by the user (thanks to contentEditable), I created a sample page with the following setup: before_html = $("#example_div").children(); $("#differences_button").on("click", ...

Revamp the styling of various elements using CSS

Is it possible to combine multiple elements in jQuery to apply a single command rather than individually styling each element? Instead of: $(".element1").css('background','#000'); $(".element2").css('background','#000&ap ...

How do you determine the length of an array of arrays using jQuery if it is 'undefined'?

Looking at the array structure below: mdarray = { '0001':{address:'add1',title:'title1'}, '0002':{address:'add2',title:'title2'}, '0003':{address:'add3',title:'titl ...

Incorporate a division based on the selection made from a jQuery dropdown menu

Is there a way to dynamically display a div to the right of a drop-down menu based on the user's selection using DOM manipulation? For reference, you can view an example of my current progress here: http://jsbin.com/#/afojid/1/edit The initial drop ...

Alter the input title in Vue by clicking on a list item

I currently have an input field that is connected to an unordered list containing two list items. I want to be able to change the name/ID of the input field with a simple click on one of the list items. Since the list items are not links or buttons, my go ...

The object in question does not have the capability to support the "live" property or method

I've encountered an issue in IE related to a script error with the live method. The problem arises when testing on the website URL @ Despite trying multiple solutions and various fixes, I haven't been able to resolve the issue yet. Any assistanc ...

How can I find the last element that was selected using XPath in the browser console

Need help with XPath: $x(("//div[@class='ag-header-row']))[1] I'm working with an array of divs, but I want to select the last one. The [1] is necessary due to multiple rows with this class. I’ve heard about using [last()], but unsure w ...

Guide to highlighting rows in v-data-table with a click in Vuetify (version >= 2.0)

In my email management system, I utilize a v-data-table to organize emails. When a user clicks on a row, a popup displaying the email details appears. Desired Feature: I am looking to have the rows marked as "readed" (either bold or not bold) after th ...

How can I integrate checkboxes in a list view with jQuery Mobile?

How can I display the status of users with three different levels in a list view? I need to show whether they are attending or not, similar to the image provided. The issue is that currently the checkbox appears below the name, but I want it to be at the r ...

What is the best way to create a clickable color feature that triggers a function to change the fill of an SVG object when clicked by the user?

I am attempting to create a user-friendly color selection system where users can click on colors that will then be applied to fill an svg object, similar to a digital coloring book for children. I am struggling to figure out how to instruct JavaScript to s ...

Choosing Dropdown Options Using Query Strings

Currently, I am utilizing Bootstrap 4 in conjunction with CodeIgniter 3 for my project. The webpage features a simple dropdown group that offers various options to choose from. By default, the value displayed is 'Please Select'. However, I am in ...

Utilizing Compass SCSS with Blueprint for Optimal Overflow Settings

I've been working with blueprint through Compass, but I'm facing an issue where longer pages don't have scroll bars. It seems like the default setting for overflow is 'hidden'. Since this appears to be the standard, I assume there ...

What steps can I take to completely remove JavaScript from an HTML document?

To eliminate the <script> tags in the HTML, I can utilize regex just like this $html = preg_replace('#<script(.*?)>(.*?)</script>#is','', $html); While this approach works well, dealing with inline JavaScript require ...