Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
chartjs display data in one bar line with 3 different data sets in 3 different colors

I currently display my data like this: Click here

but I would like to display the data like this: Click here

The idea is that at the bottom it would show the summary of all three colors numbers combined, but once you hover it would show how much each color has.

If all is one bar, if red is 7, blue is 3, the red but be a lot more in the bar.

This is the code for version 1, done with chart js

var ctx = document.getElementById("myChart").getContext("2d");

var data = {
    labels: ["Chocolate", "Vanilla", "Strawberry"],
    datasets: [
        {
            label: "Blue",
            backgroundColor: "blue",
            data: [3, 7, 4]
        },
        {
            label: "Red",
            backgroundColor: "red",
            data: [4, 3, 5]
        },
        {
            label: "Green",
            backgroundColor: "green",
            data: [7, 2, 6]
        }
    ]
};

var myBarChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        barValueSpacing: 20,
        scales: {
            yAxes: [{
                ticks: {
                    min: 0,
                }
            }]
        }
    }
});

const myChart = new Chart(
    document.getElementById('myChart'),
    config
);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

your problem is the next, you are using a wrong config you must something like this:

<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>

<canvas id="MyChart" width="400" height="200"></canvas>
<script>
new Chart(document.getElementById("MyChart"), {
  type: 'bar',
  data: {
    labels: [2017],
    datasets: [{
      label: "Chocolate",
      type: "bar",
      stack: "Base",
      backgroundColor: "#eece01",
      data: [30],
    }, {
      label: "Vanilla",
      type: "bar",
      stack: "Base",
      backgroundColor: "#87d84d",
      data: [-15],
    }, {
      label: "Strawberry",
      type: "bar",
      stack: "Base",
      backgroundColor: "#f8981f",      
      data: [20],
    }, {
      label: "candy",
      type: "bar",
      stack: "Base",
      backgroundColor: "#00b300",
      backgroundColorHover: "#3e95cd",
      data: [-10]
    }]
  },
  options: {
    plugins: {
      title: {
        display: true,
        text: 'Chart.js Bar Chart - Stacked'
      },
    },
    responsive: true,
    interaction: {
      intersect: false,
    },
    scales: {
      x: {
        stacked: true,
      },
      y: {
        stacked: true
      }
    }
  }
});
</script>

Now you just need to fix the styles and sizes of the graphic

about 4 years ago · Juan Pablo Isaza Report

0

If someone finds this, the solution is this:

  const config = {
      type: 'bar',
      data: data,
      options: {
        plugins: {
          title: {
            display: true,
            text: 'Chart.js Bar Chart - Stacked'
          },
        },
        responsive: true,
        scales: {
          x: {
            stacked: true,
          },
          y: {
            stacked: true
          }
        }
      }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!