Issue with Chart.js functionality when attempting to draw lines with only a single bar present

I am attempting to create this chart:

https://i.sstatic.net/o81AC.png

When I draw the lines (Meta and Rango de aceptación), they appear as points instead of lines. I suspect this is because I only have one bar, whereas in other charts with 2 bars, the lines are drawn correctly.

Below is the code I am using. Can you help me identify what I'm doing wrong?

<canvas id="myChart2016_5857751099b04" width="500" height="500" style="display: block; width: 500px; height: 500px;"></canvas>
<script style="text/javascript">
var ctx2016_5857751099b04 = document.getElementById("myChart2016_5857751099b04");
var myChart2016_5857751099b04 = new Chart(ctx2016_5857751099b04, {
    type: 'bar',
    data: {
        labels: ["Year 1"],
        datasets: [
            {backgroundColor:'rgba(36, 143, 36,0)',borderColor:'rgba(75, 172, 198,1)',
             type: 'line',
             label: 'Meta',
             data: [100],
             options: { tension:0.0, bezierCurve:false },
             borderWidth: 1,
             tension:0.25 },                         
            {backgroundColor:'rgba(51, 51, 26,0)',borderColor:'rgba(182, 87, 8,1)',
             type: 'line',
             label: 'Rango de aceptación ',
             data: [90],
             options: { tension:0.0, bezierCurve:false },
             borderWidth: 1,
             tension:0.25 },                         
            {backgroundColor:['rgba(99, 33, 36 ,0.1)'],borderColor:['rgba(99, 33, 36, 1)'],
             type: 'bar',
             label: 'Percentage of suppliers satisfactorily evaluated',
             data: [100],
             options: { tension:0.0, bezierCurve:false },
             borderWidth: 1,
             tension:0.25 }          
          ]
    },
    options: {
        tension:1,
        scaleBeginAtZero: true,
        scaleStartValue: 0,
        scales: {
            xAxes: [{
                categorySpacing: 20,
                gridLines: {
                    color: "rgba(0, 0, 0, 0)",
                },
            }],
            yAxes: [{
                categorySpacing: 20,
                ticks: {
                    beginAtZero:true
                }
            }]
        }

    }
});
</script>

I have tried setting the Meta and Rango de aceptación data values to:

data: [100,100,100] and data: [90,90,90]

However, the outcome is not what I expected:

[

Answer №1

Enter your desired label below to activate its functionality.

labels: ["Year 1","",""]

Here is a functional code example:

<script style="text/javascript">
                var ctx2016_5857751099b04 = document.getElementById("myChart2016_5857751099b04");
                var myChart2016_5857751099b04 = new Chart(ctx2016_5857751099b04, {
                    type: 'bar',
                    data: {

            labels: [ "Year 1", "", ""],
            datasets: [
                            {backgroundColor:'rgba(36, 143, 36,0)',borderColor:'rgba(75, 172, 198,1)',
                        type: 'line',
                        label: 'Goal',
                        data: [100,100,100],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 },                         {backgroundColor:'rgba(51, 51, 26,0)',borderColor:'rgba(182, 87, 8,1)',
                        type: 'line',
                        label: 'Acceptance Range ',
                        data: [90,90,90],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 },                         {backgroundColor:['rgba(99, 33, 36 ,0.1)'],borderColor:['rgba(99, 33, 36, 1)'],
                        type: 'bar',
                        label: 'Percentage of satisfactorily evaluated suppliers ',
                        data: [100],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 }          ]},
                options: {
                        tension:1,
                        scaleBeginAtZero: true,
                        scaleStartValue: 0,
                        scales: {
                            xAxes: [{
                                categorySpacing: 20,
                                gridLines: {
                                      color: "rgba(0, 0, 0, 0)",
                                  },
                            }],
                            yAxes: [{
                                categorySpacing: 20,
                                ticks: {
                                    beginAtZero:true
                                }
                            }]
                        }

                    }
                });
                </script>

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

Is there a way to determine if an <a> element contains a certain class while it is contained within an <li> element?

Here is an example of some HTML code: <ul id="myList"> <li> <a class="myListItem li-selected">One</a> </li> <li> <a class="myListItem">Two</a> < ...

Exploring JSON without taking into account letter case

Looking to conduct a case-insensitive search in a JSON file? Here's the JSON data you can work with: { "Data" : [ {"Name": "Widget", "Price": "25.00", "Quantity": "5" }, {"Name": "Thing", "Price": "15.00", "Quantity": "5" }, {"Nam ...

Updating the src attribute of a video element using jQuery

I've been attempting to use this jQuery solution to update the video source, but it doesn't seem to be working as expected. The video still loads the initial inline source. Here is the HTML: <video id="lanvid" autoplay class="lanvid" p ...

Problem with pairing table rows and cells, slight misalignment of 1 pixel

In my attempt to align an icon and text side by side, with the icon on the left and the text on the right, I encountered an issue where the element's box seems to be consistently 1 pixel off. This misalignment is causing the text to not line up proper ...

Leveraging class names to dynamically apply CSS styles to components based on their props value

In the process of developing a collection of reusable components, I have utilized material-ui and styled them with CSS. One specific requirement is to dynamically set the width of a component through a prop passed into a custom button. My goal is to lever ...

Creating a custom breakpoint in Bootstrap for adding unique CSS styles

Consider this scenario <div class="col-md-my"> </div> .xx { color: black; } .yy { color: red; } Similar to how Bootstrap adjusts width at different breakpoints. Create a new class called col-md-my When the width exceeds the ...

Learn how to effectively share an image using the Math.random function

Is there a way to display a random number of images on a webpage using JavaScript? Let's say we want to show X number of images, where X is a randomly generated number. For the sake of this example, let's set X to be 10. <input class="randomb ...

range input in firefox not functioning properly with focus method

One issue with using the input type range in HTML code is that when a focus event is triggered, it may not work properly in Firefox. Below is an example of the code: var p = document.getElementById("price"), res = document.getElementById("result"); t ...

The plugin's element is not compatible with jQuery methods

This particular plugin is designed to enhance the appearance of checkbox inputs. It creates a more visually appealing version of standard checkboxes. However, I have encountered an issue with one of the variables in the code. Let's discuss the theLab ...

Error: Vue displays a blank page after separating the list item component

I'm struggling with the code below: ReceiptList.vue <template> Hello <div class="list-container"> <ul> <ReceiptListItem/> </ul> </div> </template> <script setup> ...

Error Message: MPAndroidChart Line Chart Resource Not Found

I am currently working on a line chart example that I discovered here. Everything appears to be set up correctly, but when I execute the code, even though all necessary resources are present, I encounter an issue with calling the line. lineChart = (LineCh ...

Jumbotron featuring videos in a Bootstrap Carousel

I am attempting to create a bootstrap carousel featuring videos within a jumbotron. Despite my best efforts, I am struggling to make it work correctly. The videos appear too small within the jumbotron and seem to be overlaid by other elements. I have exper ...

Attempting to extract data from an HTML table and store it in an array using JavaScript with the help of jQuery

Currently, I am faced with a challenge involving extracting values from an HTML table and storing them in a 2-dimensional array using JavaScript. jQuery seems like the ideal solution for this task. While I have managed to get the indices working properly, ...

Tips for dynamically retrieving a CSS file for an HTML page

With multiple html pages containing the same navbar, I made the decision to place the navbar in its own file called navbar.html and use ajax with jquery.get() to dynamically load it onto each page. This prevents redundant code across all pages. Currently, ...

PHP: Using simple_html_dom to locate the content within a span element having a specific class

If we have an HTML structure similar to this: (...) <div class="UserLevel type_2"> <span class="LevelNum">23</span> </div> (...) What is a simple way to extract the value 23 and store it in a PHP variable using simple_html_dom ...

Hiding elements on mobile and tablet devices using display:none in html/css

I have a fiddle that is functioning perfectly in desktop view. When clicked, the description box of a product item is displayed at the bottom. See screenshot below: https://i.sstatic.net/yaZDC.png However, in mobile view, all description boxes are automa ...

The Boostrap-5 carousel seems to be malfunctioning and not functioning correctly

I've been teaching myself how to construct websites using tutorials and other resources. I'm currently diving into the world of Bootstrap, but I'm having trouble with the carousel feature. No matter how much I try, the slides get stuck on th ...

CSS: Ensure container stays at the top without setting a fixed height

Is it possible to keep a container fixed at the top without setting a specific height? I have a container positioned on the body with margins all around. The container has an overflow set to auto, causing it to extend beyond the screen's height (with ...

Avoiding clashes between CSS styles in web applications

As I work on developing a web application that can be embedded on external websites, one of the challenges I am facing involves constructing dialogues with their own unique stylesheets. This could potentially lead to conflicts if both my dialogue container ...

Having trouble interacting with element - Selenium WebDriver is not allowing the click

I'm attempting to select the Checkout Button. This is what it looks like : https://i.stack.imgur.com/TiMEO.png And here's the HTML snippet for it : <div id="buy-button-next"> <span data-reactroot=""> <div data-cid="buy- ...