Chart.js:柱状图多颜色

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

Chart.js: Bar chart multiple colors

问题

有没有办法使用多种颜色?

示例:
Chart.js:柱状图多颜色

谢谢!

英文:

Is there a way to have multiple colors?

Example:
Chart.js:柱状图多颜色

Thank you!

答案1

得分: 2

以下是要翻译的内容:

New answer:

我的原始答案让我感到不满意,我认为一定有更好的方法来实现这种样式。所以这里有一个更好的解决方案,它使用了径向渐变

请注意,这个实现方式相当简单,只支持单个数据集!

const colours = [
  { primary: '#fec1c6', shadow: '#e8b0b5' },
  { primary: '#bdeeed', shadow: '#aad2d0' },
  { primary: '#e4da84', shadow: '#d3ca76' }
];

new Chart(document.getElementById('chart'), {
  type: 'doughnut',
  data: {
    datasets: [{
      data: [3, 2, 2]
    }]
  },
  options: {
    cutoutPercentage: 65
  },
  plugins: [{
    beforeDatasetsUpdate: c => {
      const x = (c.chartArea.right + c.chartArea.left) / 2,
        y = (c.chartArea.bottom + c.chartArea.top) / 2,
        bgc = [];

      for (let i = 0; i < colours.length; i++) {
        const gradient = c.ctx.createRadialGradient(x, y, c.innerRadius, x, y, c.outerRadius);

        gradient.addColorStop(0, colours[i].shadow);
        gradient.addColorStop(.4, colours[i].shadow);
        gradient.addColorStop(.45, colours[i].primary);
        gradient.addColorStop(1, colours[i].primary);

        bgc.push(gradient);
      }
      c.config.data.datasets[0].backgroundColor = bgc;
    }
  }]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="75"></canvas>

Original answer:

如果你不介意一个“不太干净”的解决方案,你可以通过复制你的数据集来实现类似的视觉效果,例如:

const values = [3, 2, 2],
  primaryColours = ['#fec1c6', '#bdeeed', '#e4da84'],
  secondaryColours = ['#e8b0b5', '#aad2d0', '#d3ca76'];

new Chart(document.getElementById('chart'), {
  type: 'doughnut',
  data: {
    datasets: [{
      data: values,
      weight: 2,
      backgroundColor: primaryColours,
      borderColor: primaryColours
    }, {
      data: values,
      weight: 1,
      backgroundColor: secondaryColours,
      borderColor: secondaryColours
    }]
  },
  options: {
    cutoutPercentage: 65
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="75"></canvas>
英文:

New answer:

My original answer bothered me and I thought there must be a better way to achieve this style. So here's a much better solution that uses a radial gradient.

Note that this implementation is quite naïve in that it only supports a single dataset!

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const colours = [
  { primary: &#39;#fec1c6&#39;, shadow: &#39;#e8b0b5&#39; },
  { primary: &#39;#bdeeed&#39;, shadow: &#39;#aad2d0&#39; },
  { primary: &#39;#e4da84&#39;, shadow: &#39;#d3ca76&#39; }
];

new Chart(document.getElementById(&#39;chart&#39;), {
  type: &#39;doughnut&#39;,
  data: {
    datasets: [{
      data: [3, 2, 2]
    }]
  },
  options: {
    cutoutPercentage: 65
  },
  plugins: [{
    beforeDatasetsUpdate: c =&gt; {
      const x = (c.chartArea.right + c.chartArea.left) / 2,
        y = (c.chartArea.bottom + c.chartArea.top) / 2,
        bgc = [];

      for (let i = 0; i &lt; colours.length; i++) {
        const gradient = c.ctx.createRadialGradient(x, y, c.innerRadius, x, y, c.outerRadius);

        gradient.addColorStop(0, colours[i].shadow);
        gradient.addColorStop(.4, colours[i].shadow);
        gradient.addColorStop(.45, colours[i].primary);
        gradient.addColorStop(1, colours[i].primary);

        bgc.push(gradient);
      }
      c.config.data.datasets[0].backgroundColor = bgc;
    }
  }]
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js&quot;&gt;&lt;/script&gt;
&lt;canvas id=&quot;chart&quot; height=&quot;75&quot;&gt;&lt;/canvas&gt;

<!-- end snippet -->


Original answer:

If you don't mind a 'dirty' solution you can achieve a similar visual result by duplicating your dataset, e.g.:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const values = [3, 2, 2],
  primaryColours = [&#39;#fec1c6&#39;, &#39;#bdeeed&#39;, &#39;#e4da84&#39;],
  secondaryColours = [&#39;#e8b0b5&#39;, &#39;#aad2d0&#39;, &#39;#d3ca76&#39;];

new Chart(document.getElementById(&#39;chart&#39;), {
  type: &#39;doughnut&#39;,
  data: {
    datasets: [{
      data: values,
      weight: 2,
      backgroundColor: primaryColours,
      borderColor: primaryColours
    }, {
      data: values,
      weight: 1,
      backgroundColor: secondaryColours,
      borderColor: secondaryColours
    }]
  },
  options: {
    cutoutPercentage: 65
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js&quot;&gt;&lt;/script&gt;
&lt;canvas id=&quot;chart&quot; height=&quot;75&quot;&gt;&lt;/canvas&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 23:07:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614448.html
匿名

发表评论

匿名网友

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

确定