Giter VIP home page Giter VIP logo

Comments (3)

Kesslarr avatar Kesslarr commented on May 22, 2024 1

Hi @y-takey , thanks for your answering.

maybe, you can use a plugin: https://github.com/nagix/chartjs-plugin-colorschemes
I figured out other way to give backgroundColor automatically:

let i = 0;
                let color = [];
                for (let key in _this.spaceList) {
                    let x = Math.floor(Math.random() * 255);
                    let y = Math.floor(Math.random() * 255);
                    let z = Math.floor(Math.random() * 255);
                    color.push("rgba(" + x + "," + y + "," + z + ",0.6)");
                    let type = {
                        label: key,
                        data: [_this.spaceList[key]],
                        backgroundColor: color[i]
                    }
                    data.push(type);
                    i++;
                }

I know this isn't a great way, but it's simple and quick, still thanks for your helping!

from chartjs-plugin-stacked100.

Kesslarr avatar Kesslarr commented on May 22, 2024

Okay, I found this solution
found out there was something I missed in code

getSpace: function () {
            let _this = this;
            $.getJSON('/dataset/space', function (res) {
                _this.spaceList = res;
                let data = [];
                let color = ["rgba(244, 143, 177, 0.6)", "rgba(255, 235, 59, 0.6)", "rgba(100, 181, 246, 0.6)"];
                let i = 0;
                for (let key in _this.spaceList) {
                    let type = {
                        label: key,
                        data: _this.spaceList[key],     -->here has to be            data: [_this.spaceList[key]],
                        backgroundColor: color[i]
                    }
                    data.push(type);
                    i++;
                }

                new Chart(document.getElementById("storage"), {
                    type: "horizontalBar",
                    data: {
                        labels: ["空間"],
                        datasets: data
                    },
                    options: {
                        plugins: {
                            stacked100: {enable: true}
                        }
                    }
                });
}

after that, I can print dynamic chart successfully.

But I still have question, can chart.js give backgroundColor automatically? My datasets would be more and more, is there has any way to give random backgroundColor that same as the number of datasets?

Thanks
Kase

from chartjs-plugin-stacked100.

y-takey avatar y-takey commented on May 22, 2024

Hi @Kesslarr , I'm glad that the issue was solved.

can chart.js give backgroundColor automatically?

maybe, you can use a plugin: https://github.com/nagix/chartjs-plugin-colorschemes

from chartjs-plugin-stacked100.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.