如何在 chart.js 条形图上更改条形颜色

huangapple go评论60阅读模式
英文:

How to change bar-color on a chart.js bar graph

问题

我有一个使用chart.js的条形图。我想要能够更改条形图的颜色(值部分)。

我尝试查阅chart.js文档,但未找到相关信息。

<main>
    <div class="content">
        <h1>Results</h1>
        <h2>Trial 1</h2>
        <div class="results" class="results" id="trial-1"> <canvas class="show-results"></canvas></div>
        <h2>Trial 2</h2>
        <div class="results" id="trial-1"> <canvas class="show-results"></canvas></div>
        <h2>Trial 3</h2>
        <div class="results" id="trial-1"> <canvas class="show-results"></canvas></div>
        <h2>Trial 4</h2>
        <div class="results" id="trial-1"> <canvas class="show-results"></canvas></div>
        <h2>Trial 5</h2>
        <div class="results" id="trial-1"> <canvas class="show-results"></canvas></div>
        <h2>Results Overall</h2>
        <div class="results" id="trial-1"> <canvas class="show-results"></canvas></div>
    </div>
</main>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    const ctx = document.getElementsByClassName("show-results");
    for (let i = 0; i < ctx.length; i++) {
        new Chart(ctx[i], {
            type: 'bar',
            data: {
                labels: ['Lua', 'Python', 'JavaScript', 'Java', 'C', 'C++', 'F#', 'Haskell', 'C#', 'Rust', 'Go'],
                datasets: [{
                    label: 'Time to compile and run',
                    data: [12, 19, 3, 5, 2, 3],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    }
</script>
英文:

I have a bar chart using chart.js. I want to be able to change the color of the bar-chart (the value part).

I've tried looking on the chart.js docs but I couldn't find anything.

    &lt;main&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;h1&gt;Results&lt;/h1&gt;
&lt;h2&gt;Trial 1&lt;/h2&gt;
&lt;div class=&quot;results&quot; class=&quot;results&quot; id=&quot;trial-1&quot;&gt; &lt;canvas class=&quot;show-results&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;
&lt;h2&gt;Trial 2&lt;/h2&gt;
&lt;div class=&quot;results&quot; id=&quot;trial-1&quot;&gt; &lt;canvas class=&quot;show-results&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;
&lt;h2&gt;Trial 3&lt;/h2&gt;
&lt;div class=&quot;results&quot; id=&quot;trial-1&quot;&gt; &lt;canvas class=&quot;show-results&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;
&lt;h2&gt;Trial 4&lt;/h2&gt;
&lt;div class=&quot;results&quot; id=&quot;trial-1&quot;&gt; &lt;canvas class=&quot;show-results&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;
&lt;h2&gt;Trial 5&lt;/h2&gt;
&lt;div class=&quot;results&quot; id=&quot;trial-1&quot;&gt; &lt;canvas class=&quot;show-results&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;
&lt;h2&gt;Results Overall&lt;/h2&gt;
&lt;div class=&quot;results&quot; id=&quot;trial-1&quot;&gt; &lt;canvas class=&quot;show-results&quot;&gt;&lt;/canvas&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
const ctx = document.getElementsByClassName(&quot;show-results&quot;);
for (let i = 0; i &lt; ctx.length; i++) {
new Chart(ctx[i], {
type: &#39;bar&#39;,
data: {
labels: [&#39;Lua&#39;, &#39;Python&#39;, &#39;JavaScript&#39;, &#39;Java&#39;, &#39;C&#39;, &#39;C++&#39;, &#39;F#&#39;, &#39;Haskell&#39;, &#39;C#&#39;, &#39;Rust&#39;, &#39;Go&#39;],
datasets: [{
label: &#39;Time to compile and run&#39;,
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
&lt;/script&gt;

答案1

得分: 0

仅翻译代码部分如下:

backgroundColor: [
    'rgba(239, 197, 45, 1)'
]

请注意,上述代码是 JavaScript 中的一部分,用于设置图表的背景颜色。

英文:

Simpy add

                &#39;rgba(239, 197, 45, 1)&#39;
]

To the datasets array in your code.

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
const ctx = document.getElementsByClassName(&quot;show-results&quot;);
for (let i = 0; i &lt; ctx.length; i++) {
new Chart(ctx[i], {
type: &#39;bar&#39;,
data: {
labels: [&#39;Lua&#39;, &#39;Python&#39;, &#39;JavaScript&#39;, &#39;Java&#39;, &#39;C&#39;, &#39;C++&#39;, &#39;F#&#39;, &#39;Haskell&#39;, &#39;C#&#39;, &#39;Rust&#39;, &#39;Go&#39;],
datasets: [{
label: &#39;Time to compile and run&#39;,
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
backgroundColor: [
&#39;rgba(239, 197, 45, 1)&#39;
]
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年2月14日 04:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定