Can#39;t get bar chart colors in Chart js working in React Js(在 React Js 中工作的 Chart js 中无法获得条形图颜色)
问题描述
This is my first time working with Chart Js and I managed to get it displaying on my page but the keys 'label', 'backgroundColor', 'borderColor', and 'borderWidth' won't display. The keys 'labels' and 'data' work just fine as I can see the labels and the bars in the chart. I tried assigning the non-displaying keys to variables just like 'labels' and 'data' to see if it would work that way but no luck. I also tried passing hex colors and regular color names like red, blue, etc. but that didn't work either. If anyone could provide some assistance it would be greatly appreciated, thanks!
'use strict';
import React, { Component } from 'react';
var Chart = require("react-chartjs").Bar;
class BarChart extends Component {
capitalize(name) {
return name.charAt(0).toUpperCase() + name.slice(1);
}
render() {
var pokeLabels = this.props.stats.map((label) => {
return this.capitalize(label.stat.name)
})
var pokeDataSet = this.props.stats.map((set) => {
return set.base_stat
})
console.log(this.props.stats);
var data = {
labels: pokeLabels,
datasets: [
{
label: 'Pokemon Stats',
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
data: pokeDataSet
}
]
}
return (
<div className='row'>
<div className='col-xs-12 col-sm-offset-3 col-xs-offset-0'>
<Chart data={data} width="600" height="250" redraw />
</div>
</div>
);
}
}
export default BarChart;
So I was able to get the colors working by doing this:
datasets: [
{
label: "My First dataset",
fillColor: ["rgba(0,10,220,0.5)","rgba(220,0,10,0.5)","rgba(220,0,0,0.5)","rgba(120,250,120,0.5)" ],
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: pokeDataSet
}
Hope this can help someone who has a similar issue!
这篇关于在 React Js 中工作的 Chart js 中无法获得条形图颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 React Js 中工作的 Chart js 中无法获得条形图颜色
基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01